From c38cdc3fd74a31d0a5dc70785c51e1d452b44c50 Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Thu, 26 Mar 2020 01:25:05 +0800 Subject: [PATCH] clang-format: udpate ColumnLimit to 100 and reformat code --- .clang-format | 1 + src/http.c | 87 ++++++++++++++++++++------------------------------ src/protocol.c | 50 +++++++++++------------------ src/server.c | 62 +++++++++++++++++------------------ src/server.h | 29 ++++++++--------- src/terminal.c | 3 +- 6 files changed, 100 insertions(+), 132 deletions(-) diff --git a/.clang-format b/.clang-format index a327123..f23b895 100644 --- a/.clang-format +++ b/.clang-format @@ -1,2 +1,3 @@ BasedOnStyle: Google Language: Cpp +ColumnLimit: 100 diff --git a/src/http.c b/src/http.c index 06734ee..ae62668 100644 --- a/src/http.c +++ b/src/http.c @@ -30,8 +30,7 @@ static int check_auth(struct lws *wsi, struct pss_http *pss) { 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; @@ -41,16 +40,12 @@ static int check_auth(struct lws *wsi, struct pss_http *pss) { 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; @@ -98,8 +93,7 @@ static bool uncompress_html(char **output, size_t *output_len) { } 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) { @@ -109,14 +103,13 @@ 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]; @@ -140,37 +133,32 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, 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; } @@ -187,10 +175,9 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, } 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; @@ -204,14 +191,11 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, } #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; @@ -265,8 +249,7 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, 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; diff --git a/src/protocol.c b/src/protocol.c index 4a1fc0d..9586075 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -105,8 +105,7 @@ static void pty_proc_free(struct pty_proc *proc) { 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; } @@ -149,16 +148,14 @@ static void child_cb(uv_signal_t *handle, int signum) { 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); @@ -225,8 +222,8 @@ static void write_cb(uv_write_t *req, int status) { 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]; @@ -238,10 +235,8 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user, 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 || @@ -272,11 +267,9 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user, 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++; } @@ -289,15 +282,13 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user, 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: @@ -309,8 +300,7 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user, 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; } @@ -332,8 +322,7 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user, 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); @@ -355,15 +344,13 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user, 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; } @@ -428,8 +415,7 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user, 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); } diff --git a/src/server.c b/src/server.c index b243b31..c7ee1ef 100644 --- a/src/server.c +++ b/src/server.c @@ -74,38 +74,38 @@ static const char *opt_string = "p:i:c:u:g:s:I:b:6aSC:K:A:Rt:T:Om:oBd:vh"; 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] []\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] []\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 @@ -370,7 +370,7 @@ int main(int argc, char **argv) { 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); \ diff --git a/src/server.h b/src/server.h index 03318ff..116d9b9 100644 --- a/src/server.h +++ b/src/server.h @@ -66,21 +66,20 @@ struct pss_tty { }; 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 diff --git a/src/terminal.c b/src/terminal.c index 140573f..cdf9fe3 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -16,8 +16,7 @@ #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) { -- 2.43.4