1
0
mirror of https://github.com/SuperBFG7/ympd synced 2025-01-27 01:14:53 +00:00

Enable ssl options

This commit is contained in:
jcorporation 2018-07-09 22:58:25 +01:00
parent 020d63dfaf
commit 8d94cd9cab
6 changed files with 106 additions and 19 deletions

View File

@ -3,8 +3,8 @@ cmake_minimum_required(VERSION 2.6)
project (mympd C)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake/")
set(CPACK_PACKAGE_VERSION_MAJOR "3")
set(CPACK_PACKAGE_VERSION_MINOR "1")
set(CPACK_PACKAGE_VERSION_PATCH "1")
set(CPACK_PACKAGE_VERSION_MINOR "2")
set(CPACK_PACKAGE_VERSION_PATCH "0")
if(CMAKE_BUILD_TYPE MATCHES RELEASE)
set(ASSETS_PATH "${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME}/htdocs")
@ -12,7 +12,6 @@ if(CMAKE_BUILD_TYPE MATCHES RELEASE)
else()
set(ASSETS_PATH "${PROJECT_SOURCE_DIR}/htdocs")
set(DEBUG "ON")
set(CS_NDEBUG "ON")
endif()
find_package(LibMPDClient REQUIRED)
@ -23,9 +22,13 @@ include_directories(${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR} ${LIBMPDCLIENT_I
include(CheckCSourceCompiles)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Wall -Wextra -pedantic -D MG_DISABLE_SSL -D MG_ENABLE_IPV6 -D MG_DISABLE_MQTT -D MG_DISABLE_MQTT_BROKER -D MG_DISABLE_DNS_SERVER -D MG_DISABLE_COAP -D MG_DISABLE_HTTP_CGI -D MG_DISABLE_HTTP_SSI -D MG_DISABLE_HTTP_WEBDAV")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Wall -Wextra -pedantic -D MG_ENABLE_SSL -D MG_ENABLE_IPV6 -D MG_DISABLE_MQTT -D MG_DISABLE_MQTT_BROKER -D MG_DISABLE_DNS_SERVER -D MG_DISABLE_COAP -D MG_DISABLE_HTTP_CGI -D MG_DISABLE_HTTP_SSI -D MG_DISABLE_HTTP_WEBDAV")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -ggdb -D_FORTIFY_SOURCE=2 -fstack-protector -fsanitize=address -fno-omit-frame-pointer -fsanitize=undefined -fsanitize=shift -fsanitize=integer-divide-by-zero -fsanitize=unreachable -fsanitize=vla-bound -fsanitize=null -fsanitize=return -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=bounds-strict -fsanitize=alignment -fsanitize=object-size -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=nonnull-attribute -fsanitize=returns-nonnull-attribute -fsanitize=bool -fsanitize=enum -fsanitize=vptr -static-libasan")
find_package(OpenSSL REQUIRED)
include_directories(${OPENSSL_INCLUDE_DIR})
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS NS_ENABLE_SSL)
set(SOURCES
src/mympd.c
src/mpd_client.c
@ -34,7 +37,7 @@ set(SOURCES
)
add_executable(mympd ${SOURCES})
target_link_libraries(mympd ${LIBMPDCLIENT_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(mympd ${LIBMPDCLIENT_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES})
install(TARGETS mympd DESTINATION bin)
install(FILES mympd.1 DESTINATION ${CMAKE_INSTALL_PREFIX}/share/man/man1)
@ -47,3 +50,4 @@ install(FILES htdocs/css/bootstrap.min.css DESTINATION share/${PROJECT_NAME}/htd
install(FILES htdocs/css/mpd.min.css DESTINATION share/${PROJECT_NAME}/htdocs/css/)
install(DIRECTORY htdocs/assets DESTINATION share/${PROJECT_NAME}/htdocs)
install(DIRECTORY DESTINATION /var/lib/${PROJECT_NAME}/)
install(DIRECTORY DESTINATION /etc/${PROJECT_NAME}/)

View File

@ -41,6 +41,10 @@ Usage: ./mympd [OPTION]...
-h, --host <host> connect to mpd at host [localhost]
-p, --port <port> connect to mpd at port [6600]
-w, --webport <port> listen port for webserver [80]
-S, --ssl enable ssl
-W, --sslport listen port for ssl webserver [443]
-C, --sslcert filename for ssl certificate [/etc/mympd/server.pem]
-K, --sslkey filename for ssl key [/etc/mympd/server.key]
-s, --streamport <port> connect to mpd http stream at port [8000]
-u, --user <username> drop priviliges to user after socket bind
-m, --mpdpass <password> specifies the password to use when connecting to mpd

14
mympd.1
View File

@ -19,7 +19,19 @@ connect to mpd at host, defaults to localhost
connect to mpd at port, defaults to 6600
.TP
\fB\-w\fR, \fB\-\-webport PORT\fR
specifies the port for the webserver to listen to, defaults to 8080
listen interface/port for webserver [80]
.TP
\fB\-S\fR, \fB\-\-ssl\fR
enable ssl
.TP
\fB\-W\fR, \fB\-\-sslport PORT\fR
listen interface/port for ssl webserver [443]
.TP
\fB\-C\fR, \fB\-\-sslcert FILENAME\fR
filename for ssl certificate [/etc/mympd/server.pem]
.TP
\fB\-K\fR, \fB\-\-sslkey FILENAME\fR
filename for ssl key [/etc/mympd/server.key]
.TP
\fB-s\fR, \fB\-\-streamport PORT
connect to mpd http stream at port [8000]

View File

@ -9479,7 +9479,6 @@ static struct mg_ws_proto_data *mg_ws_get_proto_data(struct mg_connection *nc) {
*/
static void mg_ws_close(struct mg_connection *nc, const void *data,
size_t len) {
printf("SEND: %.*s\n",len,data);
if ((int) len == ~0) {
len = strlen((const char *) data);
}

View File

@ -4682,7 +4682,7 @@ int mg_http_parse_header(struct mg_str *hdr, const char *var_name, char *buf,
#ifdef __GNUC__
__attribute__((deprecated));
#endif
;
/*
* Gets and parses the Authorization: Basic header

View File

@ -37,6 +37,7 @@
extern char *optarg;
static sig_atomic_t s_signal_received = 0;
static struct mg_serve_http_opts s_http_server_opts;
char s_redirect[250];
static void signal_handler(int sig_num) {
signal(sig_num, signal_handler); // Reinstantiate signal handler
@ -44,14 +45,14 @@ static void signal_handler(int sig_num) {
}
static void handle_api(struct mg_connection *nc, struct http_message *hm) {
if(!is_websocket(nc)) {
if (!is_websocket(nc)) {
mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nContent-Type: application/json\r\n\r\n");
}
char buf[1000] = {0};
memcpy(buf, hm->body.p,sizeof(buf) - 1 < hm->body.len ? sizeof(buf) - 1 : hm->body.len);
struct mg_str d = {buf, strlen(buf)};
callback_mympd(nc, d);
if(!is_websocket(nc)) {
if (!is_websocket(nc)) {
mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */
}
}
@ -82,7 +83,7 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
case MG_EV_CLOSE: {
if (is_websocket(nc)) {
#ifdef DEBUG
fprintf(stdout,"Websocket connection closed\n");
printf("Websocket connection closed\n");
#endif
mympd_close_handler(nc);
}
@ -96,24 +97,49 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
}
}
static void ev_handler_http(struct mg_connection *nc_http, int ev, void *ev_data) {
switch(ev) {
case MG_EV_HTTP_REQUEST: {
printf("Redirecting to %s\n", s_redirect);
mg_http_send_redirect(nc_http, 301, mg_mk_str(s_redirect), mg_mk_str(NULL));
break;
}
}
}
int main(int argc, char **argv)
{
int n, option_index = 0;
struct mg_mgr mgr;
struct mg_connection *nc;
struct mg_connection *nc_http;
unsigned int current_timer = 0, last_timer = 0;
char *run_as_user = NULL;
char *webport = "80";
char *sslport = "443";
mpd.port = 6600;
strcpy(mpd.host, "127.0.0.1");
streamport = 8000;
strcpy(coverimage, "folder.jpg");
mpd.statefile="/var/lib/mympd/mympd.state";
struct mg_bind_opts bind_opts;
const char *err;
bool ssl = false;
char *s_ssl_cert = "/etc/mympd/server.pem";
char *s_ssl_key = "/etc/mympd/server.key";
char hostname[1024];
hostname[1023] = '\0';
gethostname(hostname, 1023);
static struct option long_options[] = {
{"host", required_argument, 0, 'h'},
{"port", required_argument, 0, 'p'},
{"webport", required_argument, 0, 'w'},
{"ssl", no_argument, 0, 'S'},
{"sslport", required_argument, 0, 'W'},
{"sslcert", required_argument, 0, 'C'},
{"sslkey", required_argument, 0, 'K'},
{"user", required_argument, 0, 'u'},
{"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 0 },
@ -124,7 +150,7 @@ int main(int argc, char **argv)
{0, 0, 0, 0 }
};
while((n = getopt_long(argc, argv, "D:h:p:w:u:vm:s:i:c:t:",
while((n = getopt_long(argc, argv, "h:p:w:SW:C:K:u:vm:s:i:t:",
long_options, &option_index)) != -1) {
switch (n) {
case 't':
@ -139,6 +165,18 @@ int main(int argc, char **argv)
case 'w':
webport = strdup(optarg);
break;
case 'S':
ssl = true;
break;
case 'W':
sslport = strdup(optarg);
break;
case 'C':
s_ssl_cert = strdup(optarg);
break;
case 'K':
s_ssl_key = strdup(optarg);
break;
case 'u':
run_as_user = strdup(optarg);
break;
@ -163,7 +201,11 @@ int main(int argc, char **argv)
fprintf(stderr, "Usage: %s [OPTION]...\n\n"
" -h, --host <host>\t\tconnect to mpd at host [localhost]\n"
" -p, --port <port>\t\tconnect to mpd at port [6600]\n"
" -w, --webport [ip:]<port>\tlisten interface/port for webserver [8080]\n"
" -w, --webport [ip:]<port>\tlisten interface/port for webserver [80]\n"
" -S, --ssl\tenable ssl\n"
" -W, --sslport [ip:]<port>\tlisten interface/port for ssl webserver [443]\n"
" -C, --sslcert <filename>\tfilename for ssl certificate [/etc/mympd/server.pem]\n"
" -K, --sslkey <filename>\tfilename for ssl key [/etc/mympd/server.key]\n"
" -u, --user <username>\t\tdrop priviliges to user after socket bind\n"
" -v, --version\t\t\tget version\n"
" -m, --mpdpass <password>\tspecifies the password to use when connecting to mpd\n"
@ -184,10 +226,29 @@ int main(int argc, char **argv)
mg_mgr_init(&mgr, NULL);
nc = mg_bind(&mgr, webport, ev_handler);
if (nc == NULL) {
fprintf(stderr, "Error starting server on port %s\n", webport);
return EXIT_FAILURE;
if (ssl == true) {
snprintf(s_redirect, 200, "https://%s:%s/", hostname, sslport);
nc_http = mg_bind(&mgr, webport, ev_handler_http);
if (nc_http == NULL) {
fprintf(stderr, "Error starting server on port %s", webport );
return EXIT_FAILURE;
}
memset(&bind_opts, 0, sizeof(bind_opts));
bind_opts.ssl_cert = s_ssl_cert;
bind_opts.ssl_key = s_ssl_key;
bind_opts.error_string = &err;
nc = mg_bind_opt(&mgr, sslport, ev_handler, bind_opts);
if (nc == NULL) {
fprintf(stderr, "Error starting server on port %s: %s\n", sslport, err);
return EXIT_FAILURE;
}
}
else {
nc = mg_bind(&mgr, webport, ev_handler);
if (nc == NULL) {
fprintf(stderr, "Error starting server on port %s", webport );
return EXIT_FAILURE;
}
}
if(run_as_user != NULL) {
@ -210,11 +271,18 @@ int main(int argc, char **argv)
mg_mgr_free(&mgr);
return EXIT_FAILURE;
}
if (ssl == true)
mg_set_protocol_http_websocket(nc_http);
mg_set_protocol_http_websocket(nc);
s_http_server_opts.document_root = SRC_PATH;
s_http_server_opts.enable_directory_listing = "no";
printf("myMPD started on port %s\n", webport);
printf("myMPD started on http port %s\n", webport);
if (ssl == true)
printf("myMPD started on ssl port %s\n", sslport);
while (s_signal_received == 0) {
mg_mgr_poll(&mgr, 200);
current_timer = time(NULL);