mirror of
https://github.com/tsl0922/ttyd.git
synced 2026-02-23 10:24:23 +01:00
* Add -b, --base-path option for reverse proxies (#151)
This commit is contained in:
committed by
GitHub
parent
50a310b644
commit
97260858bf
@@ -80,6 +80,7 @@ OPTIONS:
|
||||
-o, --once Accept only one client and exit on disconnection
|
||||
-B, --browser Open terminal with the default system browser
|
||||
-I, --index Custom index.html path
|
||||
-b, --base-path Expected base path for requests coming from a reverse proxy (eg: /mounted/here)
|
||||
-6, --ipv6 Enable IPv6 support
|
||||
-S, --ssl Enable SSL
|
||||
-C, --ssl-cert SSL certificate file path
|
||||
|
||||
@@ -149,7 +149,7 @@ callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, voi
|
||||
p = buffer + LWS_PRE;
|
||||
end = p + sizeof(buffer) - LWS_PRE;
|
||||
|
||||
if (strncmp(pss->path, "/token", 6) == 0) {
|
||||
if (strcmp(pss->path, endpoints.token) == 0) {
|
||||
const char *credential = server->credential != NULL ? server->credential : "";
|
||||
size_t n = sprintf(buf, "{\"token\": \"%s\"}", credential);
|
||||
if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end))
|
||||
@@ -171,7 +171,7 @@ callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, voi
|
||||
break;
|
||||
}
|
||||
|
||||
if (strcmp(pss->path, "/") != 0) {
|
||||
if (strcmp(pss->path, endpoints.index) != 0) {
|
||||
lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, NULL);
|
||||
goto try_to_reuse;
|
||||
}
|
||||
|
||||
@@ -251,7 +251,7 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
|
||||
lwsl_warn("refuse to serve WS client due to the --max-clients option.\n");
|
||||
return 1;
|
||||
}
|
||||
if (lws_hdr_copy(wsi, buf, sizeof(buf), WSI_TOKEN_GET_URI) <= 0 || strcmp(buf, WS_PATH) != 0) {
|
||||
if (lws_hdr_copy(wsi, buf, sizeof(buf), WSI_TOKEN_GET_URI) <= 0 || strcmp(buf, endpoints.ws) != 0) {
|
||||
lwsl_warn("refuse to serve WS client for illegal ws path: %s\n", buf);
|
||||
return 1;
|
||||
}
|
||||
|
||||
16
src/server.c
16
src/server.c
@@ -20,6 +20,7 @@
|
||||
volatile bool force_exit = false;
|
||||
struct lws_context *context;
|
||||
struct server *server;
|
||||
struct endpoints endpoints = {"/ws", "/", "/token"};
|
||||
|
||||
// websocket protocols
|
||||
static const struct lws_protocols protocols[] = {
|
||||
@@ -46,6 +47,7 @@ static const struct option options[] = {
|
||||
{"gid", required_argument, NULL, 'g'},
|
||||
{"signal", required_argument, NULL, 's'},
|
||||
{"index", required_argument, NULL, 'I'},
|
||||
{"base-path", required_argument, NULL, 'b'},
|
||||
{"ipv6", no_argument, NULL, '6'},
|
||||
{"ssl", no_argument, NULL, 'S'},
|
||||
{"ssl-cert", required_argument, NULL, 'C'},
|
||||
@@ -64,7 +66,7 @@ static const struct option options[] = {
|
||||
{"help", no_argument, NULL, 'h'},
|
||||
{NULL, 0, 0, 0}
|
||||
};
|
||||
static const char *opt_string = "p:i:c:u:g:s:I:6aSC:K:A:Rt:T:Om:oBd:vh";
|
||||
static const char *opt_string = "p:i:c:u:g:s:I:b:6aSC:K:A:Rt:T:Om:oBd:vh";
|
||||
|
||||
void print_help() {
|
||||
fprintf(stderr, "ttyd is a tool for sharing terminal over the web\n\n"
|
||||
@@ -88,6 +90,7 @@ void print_help() {
|
||||
" -o, --once Accept only one client and exit on disconnection\n"
|
||||
" -B, --browser Open terminal with the default system browser\n"
|
||||
" -I, --index Custom index.html path\n"
|
||||
" -b, --base-path Expected base path for requests coming from a reverse proxy (eg: /mounted/here)\n"
|
||||
#ifdef LWS_WITH_IPV6
|
||||
" -6, --ipv6 Enable IPv6 support\n"
|
||||
#endif
|
||||
@@ -363,6 +366,17 @@ main(int argc, char **argv) {
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case 'b': {
|
||||
char path[128];
|
||||
strncpy(path, optarg, 128);
|
||||
size_t len = strlen(path);
|
||||
#define sc(f) \
|
||||
strncpy(path + len, endpoints.f, 128 - len); \
|
||||
endpoints.f = strdup(path);
|
||||
sc(ws) sc(index) sc(token)
|
||||
#undef sc
|
||||
}
|
||||
break;
|
||||
case '6':
|
||||
info.options &= ~(LWS_SERVER_OPTION_DISABLE_IPV6);
|
||||
break;
|
||||
|
||||
@@ -13,12 +13,17 @@
|
||||
#define SET_WINDOW_TITLE '1'
|
||||
#define SET_PREFERENCES '2'
|
||||
|
||||
// websocket url path
|
||||
#define WS_PATH "/ws"
|
||||
// url paths
|
||||
struct endpoints {
|
||||
char *ws;
|
||||
char *index;
|
||||
char *token;
|
||||
};
|
||||
|
||||
extern volatile bool force_exit;
|
||||
extern struct lws_context *context;
|
||||
extern struct server *server;
|
||||
extern struct endpoints endpoints;
|
||||
|
||||
typedef enum {
|
||||
STATE_INIT, STATE_KILL, STATE_EXIT
|
||||
|
||||
Reference in New Issue
Block a user