From: Shuanglei Tao Date: Mon, 30 Jan 2017 13:46:53 +0000 (+0800) Subject: Add support for listening on UNIX domain socket X-Git-Url: http://git.prime8.dev/?a=commitdiff_plain;h=de75490d3ec594b42478d92d135ef12cb1fdb735;p=ttyd.git Add support for listening on UNIX domain socket --- diff --git a/README.md b/README.md index 01e15f9..69eb12f 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ VERSION: OPTIONS: --port, -p Port to listen (default: 7681, use `0` for random port) - --interface, -i Network interface to bind + --interface, -i Network interface to bind (eg: eth0), or UNIX domain socket path (eg: /var/run/ttyd.sock) --credential, -c Credential for Basic Authentication (format: username:password) --uid, -u User id to run with --gid, -g Group id to run with diff --git a/src/server.c b/src/server.c index cfe3923..cf5e94d 100644 --- a/src/server.c +++ b/src/server.c @@ -52,7 +52,7 @@ void print_help() { " %s\n\n" "OPTIONS:\n" " --port, -p Port to listen (default: 7681, use `0` for random port)\n" - " --interface, -i Network interface to bind\n" + " --interface, -i Network interface to bind (eg: eth0), or UNIX domain socket path (eg: /var/run/ttyd.sock)\n" " --credential, -c Credential for Basic Authentication (format: username:password)\n" " --uid, -u User id to run with\n" " --gid, -g Group id to run with\n" @@ -130,6 +130,13 @@ tty_server_free(struct tty_server *ts) { } while (ts->argv[i] != NULL); free(ts->argv); free(ts->sig_name); + if (ts->socket_path != NULL) { + struct stat st; + if (!stat(ts->socket_path, &st)) { + unlink(ts->socket_path); + } + free(ts->socket_path); + } free(ts); } @@ -346,14 +353,24 @@ main(int argc, char **argv) { lws_set_log_level(debug_level, NULL); -#if LWS_LIBRARY_VERSION_MAJOR == 2 +#if LWS_LIBRARY_VERSION_MAJOR >= 2 char server_hdr[128] = ""; sprintf(server_hdr, "ttyd/%s (libwebsockets/%s)", TTYD_VERSION, LWS_LIBRARY_VERSION); info.server_string = server_hdr; #endif - if (strlen(iface) > 0) + if (strlen(iface) > 0) { info.iface = iface; + if (endswith(info.iface, ".sock") || endswith(info.iface, ".socket")) { +#ifdef LWS_USE_UNIX_SOCK + info.options |= LWS_SERVER_OPTION_UNIX_SOCK; + server->socket_path = strdup(info.iface); +#else + fprintf(stderr, "libwebsockets is not compiled with UNIX domain socket support"); + return -1; +#endif + } + } if (ssl) { info.ssl_cert_filepath = cert_path; info.ssl_private_key_filepath = key_path; @@ -373,7 +390,7 @@ main(int argc, char **argv) { "!AES256-SHA256"; if (strlen(info.ssl_ca_filepath) > 0) info.options |= LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT; -#if LWS_LIBRARY_VERSION_MAJOR == 2 +#if LWS_LIBRARY_VERSION_MAJOR >= 2 info.options |= LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS; #endif } diff --git a/src/server.h b/src/server.h index 2c971d9..6951b70 100644 --- a/src/server.h +++ b/src/server.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -78,6 +79,7 @@ struct tty_server { bool readonly; // whether not allow clients to write to the TTY bool check_origin; // whether allow websocket connection from different origin bool once; // whether accept only one client and exit on disconnection + char *socket_path; // UNIX domain socket path pthread_mutex_t lock; }; diff --git a/src/utils.c b/src/utils.c index 4b2a4a0..be5cd16 100644 --- a/src/utils.c +++ b/src/utils.c @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -35,6 +36,13 @@ uppercase(char *str) { return str; } +bool +endswith(const char * str, const char * suffix) { + size_t str_len = strlen(str); + size_t suffix_len = strlen(suffix); + return str_len > suffix_len && !strcmp(str + (str_len - suffix_len), suffix); +} + int get_sig_name(int sig, char *buf) { int n = sprintf(buf, "SIG%s", sig < NSIG ? strsignal(sig) : "unknown"); diff --git a/src/utils.h b/src/utils.h index 5df992c..92b639a 100644 --- a/src/utils.h +++ b/src/utils.h @@ -13,6 +13,10 @@ xrealloc(void *p, size_t size); char * uppercase(char *str); +// Check whether str ends with suffix +bool +endswith(const char * str, const char * suffix); + // Get human readable signal string int get_sig_name(int sig, char *buf);