mirror of
https://github.com/tsl0922/ttyd.git
synced 2026-01-09 12:24:21 +01:00
Add support for listening on UNIX domain socket
This commit is contained in:
25
src/server.c
25
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
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
@@ -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");
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user