1
0
mirror of https://github.com/SuperBFG7/ympd synced 2024-06-24 05:33:17 +00:00
ympd/src/mympd.c

296 lines
10 KiB
C
Raw Normal View History

2018-05-24 18:17:54 +00:00
/* myMPD
(c) 2018 Juergen Mang <mail@jcgames.de>
This project's homepage is: https://github.com/jcorporation/mympd
2018-05-24 18:17:54 +00:00
myMPD ist fork of:
ympd
(c) 2013-2014 Andrew Karpow <andy@ndyk.de>
This project's homepage is: http://www.ympd.org
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/time.h>
#include <pwd.h>
2018-05-24 18:17:54 +00:00
#include "../dist/src/mongoose/mongoose.h"
2018-05-24 18:17:54 +00:00
#include "mpd_client.h"
#include "config.h"
extern char *optarg;
2018-06-12 00:07:03 +00:00
static sig_atomic_t s_signal_received = 0;
static struct mg_serve_http_opts s_http_server_opts;
2018-07-09 21:58:25 +00:00
char s_redirect[250];
2018-05-24 18:17:54 +00:00
2018-06-12 00:07:03 +00:00
static void signal_handler(int sig_num) {
signal(sig_num, signal_handler); // Reinstantiate signal handler
s_signal_received = sig_num;
2018-05-24 18:17:54 +00:00
}
static void handle_api(struct mg_connection *nc, struct http_message *hm) {
2018-07-09 21:58:25 +00:00
if (!is_websocket(nc)) {
2018-06-18 19:55:54 +00:00
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);
2018-07-09 21:58:25 +00:00
if (!is_websocket(nc)) {
2018-06-18 19:55:54 +00:00
mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */
}
2018-06-14 22:57:57 +00:00
}
2018-06-12 00:07:03 +00:00
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
2018-05-24 18:17:54 +00:00
switch(ev) {
case MG_EV_WEBSOCKET_HANDSHAKE_DONE: {
2018-06-14 15:28:10 +00:00
#ifdef DEBUG
fprintf(stdout,"New Websocket connection\n");
2018-06-14 15:28:10 +00:00
#endif
2018-06-18 19:55:54 +00:00
struct mg_str d = {(char *) "{\"cmd\":\"MPD_API_WELCOME\"}", 25 };
2018-06-14 22:00:54 +00:00
callback_mympd(nc, d);
break;
2018-06-12 00:07:03 +00:00
}
case MG_EV_HTTP_REQUEST: {
struct http_message *hm = (struct http_message *) ev_data;
2018-06-14 15:28:10 +00:00
#ifdef DEBUG
printf("HTTP request: %.*s\n",hm->uri.len,hm->uri.p);
2018-06-14 15:28:10 +00:00
#endif
if (mg_vcmp(&hm->uri, "/api") == 0) {
handle_api(nc, hm);
}
else {
mg_serve_http(nc, hm, s_http_server_opts);
}
2018-06-11 20:35:11 +00:00
break;
2018-06-12 00:07:03 +00:00
}
case MG_EV_CLOSE: {
if (is_websocket(nc)) {
2018-06-14 15:28:10 +00:00
#ifdef DEBUG
2018-07-09 21:58:25 +00:00
printf("Websocket connection closed\n");
2018-06-14 15:28:10 +00:00
#endif
2018-06-14 22:00:54 +00:00
mympd_close_handler(nc);
}
else {
2018-06-14 15:28:10 +00:00
#ifdef DEBUG
fprintf(stdout,"HTTP Close\n");
2018-06-14 15:28:10 +00:00
#endif
}
break;
}
2018-05-24 18:17:54 +00:00
}
}
2018-07-09 21:58:25 +00:00
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;
}
}
}
2018-07-10 23:06:31 +00:00
int main(int argc, char **argv) {
2018-05-24 18:17:54 +00:00
int n, option_index = 0;
2018-06-12 00:07:03 +00:00
struct mg_mgr mgr;
struct mg_connection *nc;
2018-07-09 21:58:25 +00:00
struct mg_connection *nc_http;
2018-05-24 18:17:54 +00:00
unsigned int current_timer = 0, last_timer = 0;
char *run_as_user = NULL;
char *webport = "80";
2018-07-09 21:58:25 +00:00
char *sslport = "443";
2018-05-24 18:17:54 +00:00
mpd.port = 6600;
strcpy(mpd.host, "127.0.0.1");
streamport = 8000;
2018-05-27 14:13:42 +00:00
strcpy(coverimage, "folder.jpg");
2018-07-15 22:51:36 +00:00
mpd.statefile = "/var/lib/mympd/mympd.state";
2018-07-09 21:58:25 +00:00
struct mg_bind_opts bind_opts;
const char *err;
bool ssl = false;
char *s_ssl_cert = "/etc/mympd/ssl/server.pem";
char *s_ssl_key = "/etc/mympd/ssl/server.key";
2018-07-09 21:58:25 +00:00
char hostname[1024];
hostname[1023] = '\0';
gethostname(hostname, 1023);
2018-05-24 18:17:54 +00:00
static struct option long_options[] = {
{"mpdhost", required_argument, 0, 'h'},
{"mpdport", required_argument, 0, 'p'},
{"mpdpass", required_argument, 0, 'm'},
2018-05-24 18:17:54 +00:00
{"webport", required_argument, 0, 'w'},
2018-07-09 21:58:25 +00:00
{"ssl", no_argument, 0, 'S'},
{"sslport", required_argument, 0, 'W'},
{"sslcert", required_argument, 0, 'C'},
{"sslkey", required_argument, 0, 'K'},
2018-05-24 18:17:54 +00:00
{"user", required_argument, 0, 'u'},
{"streamport", required_argument, 0, 's'},
2018-05-27 14:13:42 +00:00
{"coverimage", required_argument, 0, 'i'},
2018-06-19 22:43:36 +00:00
{"statefile", required_argument, 0, 't'},
{"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 0 },
2018-05-24 18:17:54 +00:00
{0, 0, 0, 0 }
};
2018-07-09 21:58:25 +00:00
while((n = getopt_long(argc, argv, "h:p:w:SW:C:K:u:vm:s:i:t:",
2018-05-24 18:17:54 +00:00
long_options, &option_index)) != -1) {
switch (n) {
2018-06-19 22:43:36 +00:00
case 't':
mpd.statefile = strdup(optarg);
2018-05-24 18:17:54 +00:00
break;
case 'h':
strncpy(mpd.host, optarg, sizeof(mpd.host));
break;
case 'p':
mpd.port = atoi(optarg);
break;
case 'w':
webport = strdup(optarg);
break;
2018-07-09 21:58:25 +00:00
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;
2018-05-24 18:17:54 +00:00
case 'u':
run_as_user = strdup(optarg);
break;
case 'm':
if (strlen(optarg) > 0)
mpd.password = strdup(optarg);
break;
case 's':
streamport = atoi(optarg);
break;
2018-05-27 14:13:42 +00:00
case 'i':
strncpy(coverimage, optarg, sizeof(coverimage));
break;
2018-05-24 18:17:54 +00:00
case 'v':
fprintf(stdout, "myMPD %d.%d.%d\n"
"Copyright (C) 2018 Juergen Mang <mail@jcgames.de>\n"
2018-07-15 22:51:36 +00:00
"Built " __DATE__ " "__TIME__"\n",
MYMPD_VERSION_MAJOR, MYMPD_VERSION_MINOR, MYMPD_VERSION_PATCH);
2018-05-24 18:17:54 +00:00
return EXIT_SUCCESS;
break;
default:
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"
2018-07-09 21:58:25 +00:00
" -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/ssl/server.pem]\n"
" -K, --sslkey <filename>\tfilename for ssl key [/etc/mympd/ssl/server.key]\n"
2018-05-24 18:17:54 +00:00
" -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"
" -s, --streamport <port>\tconnect to mpd http stream at port [8000]\n"
2018-05-27 14:13:42 +00:00
" -i, --coverimage <filename>\tfilename for coverimage [folder.jpg]\n"
" -t, --statefile <filename>\tfilename for mympd state [/var/lib/mympd/mympd.state]\n"
2018-05-24 18:17:54 +00:00
" --help\t\t\t\tthis help\n"
, argv[0]);
return EXIT_FAILURE;
}
}
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
setvbuf(stdout, NULL, _IOLBF, 0);
setvbuf(stderr, NULL, _IOLBF, 0);
mg_mgr_init(&mgr, NULL);
2018-07-09 21:58:25 +00:00
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) {
2018-07-15 22:51:36 +00:00
fprintf(stderr, "Error starting server on port %s\n", webport );
2018-07-09 21:58:25 +00:00
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) {
2018-07-15 22:51:36 +00:00
fprintf(stderr, "Error starting server on port %s\n", webport );
2018-07-09 21:58:25 +00:00
return EXIT_FAILURE;
}
}
2018-07-24 22:58:29 +00:00
if (run_as_user != NULL) {
printf("Droping privileges\n");
struct passwd *pw;
if ((pw = getpwnam(run_as_user)) == NULL) {
printf("Unknown user\n");
return EXIT_FAILURE;
} else if (setgid(pw->pw_gid) != 0) {
2018-07-15 22:51:36 +00:00
printf("setgid() failed\n");
return EXIT_FAILURE;
} else if (setuid(pw->pw_uid) != 0) {
printf("setuid() failed\n");
return EXIT_FAILURE;
}
}
if (getuid() == 0) {
printf("myMPD should not be run with root privileges\n");
2018-06-21 22:26:20 +00:00
mg_mgr_free(&mgr);
return EXIT_FAILURE;
}
2018-07-09 21:58:25 +00:00
if (ssl == true)
mg_set_protocol_http_websocket(nc_http);
2018-06-12 00:07:03 +00:00
mg_set_protocol_http_websocket(nc);
s_http_server_opts.document_root = SRC_PATH;
2018-07-09 21:58:25 +00:00
s_http_server_opts.enable_directory_listing = "no";
2018-05-24 18:17:54 +00:00
2018-07-09 21:58:25 +00:00
printf("myMPD started on http port %s\n", webport);
if (ssl == true)
printf("myMPD started on ssl port %s\n", sslport);
2018-06-12 00:07:03 +00:00
while (s_signal_received == 0) {
mg_mgr_poll(&mgr, 200);
2018-05-24 18:17:54 +00:00
current_timer = time(NULL);
2018-07-15 22:51:36 +00:00
if (current_timer - last_timer) {
2018-05-24 18:17:54 +00:00
last_timer = current_timer;
2018-06-14 22:00:54 +00:00
mympd_poll(&mgr);
2018-05-24 18:17:54 +00:00
}
}
2018-06-12 00:07:03 +00:00
mg_mgr_free(&mgr);
2018-06-14 22:00:54 +00:00
mympd_disconnect();
2018-05-24 18:17:54 +00:00
return EXIT_SUCCESS;
}