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/ympd
|
|
|
|
|
|
|
|
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>
|
2018-06-14 19:56:12 +00:00
|
|
|
#include <pwd.h>
|
2018-05-24 18:17:54 +00:00
|
|
|
|
2018-06-11 20:35:11 +00:00
|
|
|
#include "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-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
|
|
|
}
|
|
|
|
|
2018-06-14 19:56:12 +00:00
|
|
|
static void handle_api(struct mg_connection *nc, struct http_message *hm) {
|
2018-06-18 19:55:54 +00:00
|
|
|
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)) {
|
|
|
|
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) {
|
2018-06-12 23:29:58 +00:00
|
|
|
case MG_EV_WEBSOCKET_HANDSHAKE_DONE: {
|
2018-06-14 15:28:10 +00:00
|
|
|
#ifdef DEBUG
|
2018-06-12 23:29:58 +00:00
|
|
|
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);
|
2018-06-12 23:29:58 +00:00
|
|
|
break;
|
2018-06-12 00:07:03 +00:00
|
|
|
}
|
|
|
|
case MG_EV_HTTP_REQUEST: {
|
2018-06-12 23:29:58 +00:00
|
|
|
struct http_message *hm = (struct http_message *) ev_data;
|
2018-06-14 15:28:10 +00:00
|
|
|
#ifdef DEBUG
|
2018-06-12 23:29:58 +00:00
|
|
|
printf("HTTP request: %.*s\n",hm->uri.len,hm->uri.p);
|
2018-06-14 15:28:10 +00:00
|
|
|
#endif
|
2018-06-14 19:56:12 +00:00
|
|
|
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
|
|
|
}
|
2018-06-12 23:29:58 +00:00
|
|
|
case MG_EV_CLOSE: {
|
|
|
|
if (is_websocket(nc)) {
|
2018-06-14 15:28:10 +00:00
|
|
|
#ifdef DEBUG
|
2018-06-12 23:29:58 +00:00
|
|
|
fprintf(stdout,"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);
|
2018-06-12 23:29:58 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-06-14 15:28:10 +00:00
|
|
|
#ifdef DEBUG
|
2018-06-12 23:29:58 +00:00
|
|
|
fprintf(stdout,"HTTP Close\n");
|
2018-06-14 15:28:10 +00:00
|
|
|
#endif
|
2018-06-12 23:29:58 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-05-24 18:17:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int n, option_index = 0;
|
2018-06-12 00:07:03 +00:00
|
|
|
struct mg_mgr mgr;
|
|
|
|
struct mg_connection *nc;
|
2018-05-24 18:17:54 +00:00
|
|
|
unsigned int current_timer = 0, last_timer = 0;
|
|
|
|
char *run_as_user = NULL;
|
|
|
|
char *webport = "8080";
|
|
|
|
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-06-19 22:43:36 +00:00
|
|
|
mpd.statefile="/var/lib/mympd/mympd.state";
|
2018-05-24 18:17:54 +00:00
|
|
|
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"host", required_argument, 0, 'h'},
|
|
|
|
{"port", required_argument, 0, 'p'},
|
|
|
|
{"webport", required_argument, 0, 'w'},
|
|
|
|
{"user", required_argument, 0, 'u'},
|
|
|
|
{"version", no_argument, 0, 'v'},
|
|
|
|
{"help", no_argument, 0, 0 },
|
|
|
|
{"mpdpass", required_argument, 0, 'm'},
|
|
|
|
{"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'},
|
2018-05-24 18:17:54 +00:00
|
|
|
{0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
2018-06-19 22:43:36 +00:00
|
|
|
while((n = getopt_long(argc, argv, "D:h:p:w:u:vm:s:i:c: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;
|
|
|
|
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-05-29 21:28:11 +00:00
|
|
|
"Built " __DATE__ " "__TIME__ "\n",
|
2018-05-29 21:05:34 +00:00
|
|
|
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"
|
|
|
|
" -w, --webport [ip:]<port>\tlisten interface/port for webserver [8080]\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"
|
|
|
|
" -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"
|
2018-06-19 22:43:36 +00:00
|
|
|
" -t, --statefile <filename>\tstatefile [/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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-06-14 19:56:12 +00:00
|
|
|
signal(SIGTERM, signal_handler);
|
|
|
|
signal(SIGINT, signal_handler);
|
|
|
|
setvbuf(stdout, NULL, _IOLBF, 0);
|
|
|
|
setvbuf(stderr, NULL, _IOLBF, 0);
|
|
|
|
|
|
|
|
mg_mgr_init(&mgr, NULL);
|
|
|
|
|
2018-06-12 00:07:03 +00:00
|
|
|
nc = mg_bind(&mgr, webport, ev_handler);
|
2018-06-14 19:56:12 +00:00
|
|
|
if (nc == NULL) {
|
2018-06-17 22:42:22 +00:00
|
|
|
fprintf(stderr, "Error starting server on port %s\n", webport);
|
2018-06-14 19:56:12 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
printf("setgid() failed");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
} else if (setuid(pw->pw_uid) != 0) {
|
|
|
|
printf("setuid() failed\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
free(run_as_user);
|
|
|
|
}
|
|
|
|
|
2018-06-12 00:07:03 +00:00
|
|
|
mg_set_protocol_http_websocket(nc);
|
|
|
|
s_http_server_opts.document_root = SRC_PATH;
|
2018-05-24 18:17:54 +00:00
|
|
|
|
2018-06-14 15:28:10 +00:00
|
|
|
printf("myMPD started on port %s\n", webport);
|
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);
|
|
|
|
if(current_timer - last_timer)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|