BasedOnStyle: Google
Language: Cpp
+ColumnLimit: 100
break;
}
}
- if (b64_text != NULL && !strcmp(b64_text, server->credential))
- return AUTH_OK;
+ if (b64_text != NULL && !strcmp(b64_text, server->credential)) return AUTH_OK;
}
unsigned char buffer[1024 + LWS_PRE], *p, *end;
char *body = strdup("401 Unauthorized\n");
size_t n = strlen(body);
- if (lws_add_http_header_status(wsi, HTTP_STATUS_UNAUTHORIZED, &p, end))
- return AUTH_ERROR;
- if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_WWW_AUTHENTICATE,
- (unsigned char *)"Basic realm=\"ttyd\"", 18,
- &p, end))
- return AUTH_ERROR;
- if (lws_add_http_header_content_length(wsi, n, &p, end)) return AUTH_ERROR;
- if (lws_finalize_http_header(wsi, &p, end)) return AUTH_ERROR;
- if (lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE),
- LWS_WRITE_HTTP_HEADERS) < 0)
+ if (lws_add_http_header_status(wsi, HTTP_STATUS_UNAUTHORIZED, &p, end) ||
+ lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_WWW_AUTHENTICATE,
+ (unsigned char *)"Basic realm=\"ttyd\"", 18, &p, end) ||
+ lws_add_http_header_content_length(wsi, n, &p, end) ||
+ lws_finalize_http_header(wsi, &p, end) ||
+ lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE), LWS_WRITE_HTTP_HEADERS) < 0)
return AUTH_ERROR;
pss->buffer = pss->ptr = body;
}
static void pss_buffer_free(struct pss_http *pss) {
- if (pss->buffer != (char *)index_html && pss->buffer != html_cache)
- free(pss->buffer);
+ if (pss->buffer != (char *)index_html && pss->buffer != html_cache) free(pss->buffer);
}
static void access_log(struct lws *wsi, const char *path) {
lws_get_peer_simple(lws_get_network_wsi(wsi), rip, sizeof(rip));
#else
char name[100];
- lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name, sizeof(name), rip,
- sizeof(rip));
+ lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name, sizeof(name), rip, sizeof(rip));
#endif
lwsl_notice("HTTP %s - %s\n", path, rip);
}
-int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
- void *in, size_t len) {
+int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in,
+ size_t len) {
struct pss_http *pss = (struct pss_http *)user;
unsigned char buffer[4096 + LWS_PRE], *p, *end;
char buf[256];
end = p + sizeof(buffer) - LWS_PRE;
if (strcmp(pss->path, endpoints.token) == 0) {
- const char *credential =
- server->credential != NULL ? server->credential : "";
+ 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)) return 1;
- if (lws_add_http_header_by_token(
- wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
- (unsigned char *)"application/json;charset=utf-8", 30, &p, end))
- return 1;
- if (lws_add_http_header_content_length(wsi, (unsigned long)n, &p, end))
- return 1;
- if (lws_finalize_http_header(wsi, &p, end)) return 1;
- if (lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE),
- LWS_WRITE_HTTP_HEADERS) < 0)
+ if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end) ||
+ lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
+ (unsigned char *)"application/json;charset=utf-8", 30, &p,
+ end) ||
+ lws_add_http_header_content_length(wsi, (unsigned long)n, &p, end) ||
+ lws_finalize_http_header(wsi, &p, end) ||
+ lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE), LWS_WRITE_HTTP_HEADERS) < 0)
return 1;
+
pss->buffer = pss->ptr = strdup(buf);
pss->len = n;
lws_callback_on_writable(wsi);
break;
}
- // accessing `/base-path` redirects to `/base-path/`
+ // redirects `/base-path` to `/base-path/`
if (strcmp(pss->path, endpoints.parent) == 0) {
- if (lws_add_http_header_status(wsi, HTTP_STATUS_FOUND, &p, end)
- || lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_LOCATION,
- endpoints.index,
- strlen(endpoints.index), &p, end)
- || lws_add_http_header_content_length(wsi, 0, &p, end)
- || lws_finalize_http_header(wsi, &p, end)
- || lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE),
- LWS_WRITE_HTTP_HEADERS) < 0
- )
+ if (lws_add_http_header_status(wsi, HTTP_STATUS_FOUND, &p, end) ||
+ lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_LOCATION,
+ (unsigned char *)endpoints.index,
+ (int)strlen(endpoints.index), &p, end) ||
+ lws_add_http_header_content_length(wsi, 0, &p, end) ||
+ lws_finalize_http_header(wsi, &p, end) ||
+ lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE), LWS_WRITE_HTTP_HEADERS) < 0)
return 1;
goto try_to_reuse;
}
} else {
char *output = (char *)index_html;
size_t output_len = index_html_len;
- if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end)) return 1;
- if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
- (const unsigned char *)content_type, 9,
- &p, end))
+ if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end) ||
+ lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
+ (const unsigned char *)content_type, 9, &p, end))
return 1;
#ifdef LWS_WITH_HTTP_STREAM_COMPRESSION
if (!uncompress_html(&output, &output_len)) return 1;
}
#endif
- if (lws_add_http_header_content_length(wsi, (unsigned long)output_len,
- &p, end))
+ if (lws_add_http_header_content_length(wsi, (unsigned long)output_len, &p, end) ||
+ lws_finalize_http_header(wsi, &p, end) ||
+ lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE), LWS_WRITE_HTTP_HEADERS) < 0)
return 1;
- if (lws_finalize_http_header(wsi, &p, end)) return 1;
- if (lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE),
- LWS_WRITE_HTTP_HEADERS) < 0)
- return 1;
#if LWS_LIBRARY_VERSION_MAJOR < 2
if (lws_write_http(wsi, output, output_len) < 0) return 1;
goto try_to_reuse;
int err = X509_STORE_CTX_get_error((X509_STORE_CTX *)user);
int depth = X509_STORE_CTX_get_error_depth((X509_STORE_CTX *)user);
const char *msg = X509_verify_cert_error_string(err);
- lwsl_err("client certificate verification error: %s (%d), depth: %d\n",
- msg, err, depth);
+ lwsl_err("client certificate verification error: %s (%d), depth: %d\n", msg, err, depth);
return 1;
}
break;
free(proc);
}
-static void alloc_cb(uv_handle_t *handle, size_t suggested_size,
- uv_buf_t *buf) {
+static void alloc_cb(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
buf->base = xmalloc(suggested_size);
buf->len = suggested_size;
}
if (WIFEXITED(stat)) {
proc->status = WEXITSTATUS(stat);
- lwsl_notice("process exited with code %d, pid: %d\n", proc->status,
- proc->pid);
+ lwsl_notice("process exited with code %d, pid: %d\n", proc->status, proc->pid);
} else if (WIFSIGNALED(stat)) {
int sig = WTERMSIG(stat);
char sig_name[20];
proc->status = 128 + sig;
get_sig_name(sig, sig_name, sizeof(sig_name));
- lwsl_notice("process killed with signal %d (%s), pid: %d\n", sig,
- sig_name, proc->pid);
+ lwsl_notice("process killed with signal %d (%s), pid: %d\n", sig, sig_name, proc->pid);
}
LIST_REMOVE(proc, entry);
free(req);
}
-int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
- void *in, size_t len) {
+int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in,
+ size_t len) {
struct pss_tty *pss = (struct pss_tty *)user;
struct pty_proc *proc;
char buf[256];
lwsl_warn("refuse to serve WS client due to the --once option.\n");
return 1;
}
- if (server->max_clients > 0 &&
- server->client_count == server->max_clients) {
- lwsl_warn(
- "refuse to serve WS client due to the --max-clients option.\n");
+ if (server->max_clients > 0 && server->client_count == server->max_clients) {
+ 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 ||
uv_pipe_init(server->loop, &proc->pipe, 0);
if (server->url_arg) {
- while (lws_hdr_copy_fragment(wsi, buf, sizeof(buf),
- WSI_TOKEN_HTTP_URI_ARGS, n++) > 0) {
+ while (lws_hdr_copy_fragment(wsi, buf, sizeof(buf), WSI_TOKEN_HTTP_URI_ARGS, n++) > 0) {
if (strncmp(buf, "arg=", 4) == 0) {
- proc->args =
- xrealloc(proc->args, (proc->argc + 1) * sizeof(char *));
+ proc->args = xrealloc(proc->args, (proc->argc + 1) * sizeof(char *));
proc->args[proc->argc] = strdup(&buf[4]);
proc->argc++;
}
lws_hdr_copy(wsi, buf, sizeof(buf), WSI_TOKEN_GET_URI);
#if LWS_LIBRARY_VERSION_NUMBER >= 2004000
- lws_get_peer_simple(lws_get_network_wsi(wsi), pss->address,
- sizeof(pss->address));
+ lws_get_peer_simple(lws_get_network_wsi(wsi), pss->address, sizeof(pss->address));
#else
char name[100];
- lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name, sizeof(name),
- pss->address, sizeof(pss->address));
+ lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name, sizeof(name), pss->address,
+ sizeof(pss->address));
#endif
- lwsl_notice("WS %s - %s, clients: %d\n", buf, pss->address,
- server->client_count);
+ lwsl_notice("WS %s - %s, clients: %d\n", buf, pss->address, server->client_count);
break;
case LWS_CALLBACK_SERVER_WRITEABLE:
break;
}
if (send_initial_message(wsi, pss->initial_cmd_index) < 0) {
- lwsl_err("failed to send initial message, index: %d\n",
- pss->initial_cmd_index);
+ lwsl_err("failed to send initial message, index: %d\n", pss->initial_cmd_index);
lws_close_reason(wsi, LWS_CLOSE_STATUS_UNEXPECTED_CONDITION, NULL, 0);
return -1;
}
proc->pty_buffer[LWS_PRE] = OUTPUT;
n = (size_t)(proc->pty_len + 1);
- if (lws_write(wsi, (unsigned char *)proc->pty_buffer + LWS_PRE, n,
- LWS_WRITE_BINARY) < n) {
+ if (lws_write(wsi, (unsigned char *)proc->pty_buffer + LWS_PRE, n, LWS_WRITE_BINARY) < n) {
lwsl_err("write OUTPUT to WS\n");
}
free(proc->pty_buffer);
const char command = pss->buffer[0];
// check auth
- if (server->credential != NULL && !pss->authenticated &&
- command != JSON_DATA) {
+ if (server->credential != NULL && !pss->authenticated && command != JSON_DATA) {
lwsl_warn("WS client not authenticated\n");
return 1;
}
// check if there are more fragmented messages
- if (lws_remaining_packet_payload(wsi) > 0 ||
- !lws_is_final_fragment(wsi)) {
+ if (lws_remaining_packet_payload(wsi) > 0 || !lws_is_final_fragment(wsi)) {
return 0;
}
if (pss->wsi == NULL) break;
server->client_count--;
- lwsl_notice("WS closed from %s, clients: %d\n", pss->address,
- server->client_count);
+ lwsl_notice("WS closed from %s, clients: %d\n", pss->address, server->client_count);
if (pss->buffer != NULL) {
free(pss->buffer);
}
static void print_help() {
// clang-format off
fprintf(stderr, "ttyd is a tool for sharing terminal over the web\n\n"
- "USAGE:\n"
- " ttyd [options] <command> [<arguments...>]\n\n"
- "VERSION:\n"
- " %s\n\n"
- "OPTIONS:\n"
- " -p, --port Port to listen (default: 7681, use `0` for random port)\n"
- " -i, --interface Network interface to bind (eg: eth0), or UNIX domain socket path (eg: /var/run/ttyd.sock)\n"
- " -c, --credential Credential for Basic Authentication (format: username:password)\n"
- " -u, --uid User id to run with\n"
- " -g, --gid Group id to run with\n"
- " -s, --signal Signal to send to the command when exit it (default: 1, SIGHUP)\n"
- " -a, --url-arg Allow client to send command line arguments in URL (eg: http://localhost:7681?arg=foo&arg=bar)\n"
- " -R, --readonly Do not allow clients to write to the TTY\n"
- " -t, --client-option Send option to client (format: key=value), repeat to add more options\n"
- " -T, --terminal-type Terminal type to report, default: xterm-256color\n"
- " -O, --check-origin Do not allow websocket connection from different origin\n"
- " -m, --max-clients Maximum clients to support (default: 0, no limit)\n"
- " -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"
+ "USAGE:\n"
+ " ttyd [options] <command> [<arguments...>]\n\n"
+ "VERSION:\n"
+ " %s\n\n"
+ "OPTIONS:\n"
+ " -p, --port Port to listen (default: 7681, use `0` for random port)\n"
+ " -i, --interface Network interface to bind (eg: eth0), or UNIX domain socket path (eg: /var/run/ttyd.sock)\n"
+ " -c, --credential Credential for Basic Authentication (format: username:password)\n"
+ " -u, --uid User id to run with\n"
+ " -g, --gid Group id to run with\n"
+ " -s, --signal Signal to send to the command when exit it (default: 1, SIGHUP)\n"
+ " -a, --url-arg Allow client to send command line arguments in URL (eg: http://localhost:7681?arg=foo&arg=bar)\n"
+ " -R, --readonly Do not allow clients to write to the TTY\n"
+ " -t, --client-option Send option to client (format: key=value), repeat to add more options\n"
+ " -T, --terminal-type Terminal type to report, default: xterm-256color\n"
+ " -O, --check-origin Do not allow websocket connection from different origin\n"
+ " -m, --max-clients Maximum clients to support (default: 0, no limit)\n"
+ " -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"
+ " -6, --ipv6 Enable IPv6 support\n"
#endif
- " -S, --ssl Enable SSL\n"
- " -C, --ssl-cert SSL certificate file path\n"
- " -K, --ssl-key SSL key file path\n"
- " -A, --ssl-ca SSL CA file path for client certificate verification\n"
- " -d, --debug Set log level (default: 7)\n"
- " -v, --version Print the version and exit\n"
- " -h, --help Print this text and exit\n\n"
- "Visit https://github.com/tsl0922/ttyd to get more information and report bugs.\n",
+ " -S, --ssl Enable SSL\n"
+ " -C, --ssl-cert SSL certificate file path\n"
+ " -K, --ssl-key SSL key file path\n"
+ " -A, --ssl-ca SSL CA file path for client certificate verification\n"
+ " -d, --debug Set log level (default: 7)\n"
+ " -v, --version Print the version and exit\n"
+ " -h, --help Print this text and exit\n\n"
+ "Visit https://github.com/tsl0922/ttyd to get more information and report bugs.\n",
TTYD_VERSION
);
// clang-format on
char path[128];
strncpy(path, optarg, 128);
size_t len = strlen(path);
- while (len && path[len - 1] == '/') path[--len] = 0; // trim trailing /
+ while (len && path[len - 1] == '/') path[--len] = 0; // trim trailing /
if (!len) break;
#define sc(f) \
strncpy(path + len, endpoints.f, 128 - len); \
};
struct server {
- int client_count; // client count
- char *prefs_json; // client preferences
- char *credential; // encoded basic auth credential
- char *index; // custom index.html
- char *command; // full command line
- char **argv; // command with arguments
- int argc; // command + arguments count
- int sig_code; // close signal
- char sig_name[20]; // human readable signal string
- bool url_arg; // allow client to send cli arguments in URL
- bool readonly; // whether not allow clients to write to the TTY
- bool
- check_origin; // whether allow websocket connection from different origin
- int max_clients; // maximum clients to support
- bool once; // whether accept only one client and exit on disconnection
+ int client_count; // client count
+ char *prefs_json; // client preferences
+ char *credential; // encoded basic auth credential
+ char *index; // custom index.html
+ char *command; // full command line
+ char **argv; // command with arguments
+ int argc; // command + arguments count
+ int sig_code; // close signal
+ char sig_name[20]; // human readable signal string
+ bool url_arg; // allow client to send cli arguments in URL
+ bool readonly; // whether not allow clients to write to the TTY
+ bool check_origin; // whether allow websocket connection from different origin
+ int max_clients; // maximum clients to support
+ bool once; // whether accept only one client and exit on disconnection
char socket_path[255]; // UNIX domain socket path
char terminal_type[30]; // terminal type to report
#include "utils.h"
-pid_t pty_fork(int *pty, const char *file, char *const argv[],
- const char *term) {
+pid_t pty_fork(int *pty, const char *file, char *const argv[], const char *term) {
pid_t pid = forkpty(pty, NULL, NULL, NULL);
if (pid < 0) {