From: Shuanglei Tao Date: Sat, 24 Sep 2016 16:56:53 +0000 (+0800) Subject: Implemented authentication for websocket connection X-Git-Url: http://git.prime8.dev/?a=commitdiff_plain;h=9174d6116cd6448cda0ae237fc124473c9add12d;p=ttyd.git Implemented authentication for websocket connection --- diff --git a/src/http.c b/src/http.c index 7147ec5..52e207f 100644 --- a/src/http.c +++ b/src/http.c @@ -18,11 +18,11 @@ check_auth(struct lws *wsi) { if (strlen(token) == 0) continue; if (i++ == 2) { - b64_text = strdup(token); + b64_text = token; break; } } - if (b64_text != NULL && strcmp(b64_text, server->credential) == 0) + if (b64_text != NULL && !strcmp(b64_text, server->credential)) return 0; } @@ -51,18 +51,13 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { unsigned char buffer[4096 + LWS_PRE], *p, *end; char buf[256]; - int n; switch (reason) { case LWS_CALLBACK_HTTP: - lwsl_notice("lws_http_serve: %s\n", in); - { char name[100], rip[50]; - lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name, - sizeof(name), rip, sizeof(rip)); - sprintf(buf, "%s (%s)", name, rip); - lwsl_notice("HTTP connect from %s\n", buf); + lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name, sizeof(name), rip, sizeof(rip)); + lwsl_notice("HTTP connect from %s (%s), path: %s\n", name, rip, in); } if (len < 1) { @@ -70,7 +65,6 @@ callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, voi goto try_to_reuse; } - // TODO: this doesn't work for websocket switch (check_auth(wsi)) { case 0: break; @@ -85,37 +79,54 @@ callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, voi if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI)) return 0; - if (strcmp((const char *) in, "/")) { - lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, NULL); + p = buffer + LWS_PRE; + end = p + sizeof(buffer) - LWS_PRE; + + if (!strncmp((const char *)in, "/auth_token.js", 14)) { + size_t n = server->credential != NULL ? sprintf(buf, "var tty_auth_token = '%s';", server->credential) : 0; + + 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/javascript", + 22, &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) + return 1; + if (n > 0 && lws_write_http(wsi, buf, n) < 0) { + return 1; + } goto try_to_reuse; } - p = buffer + LWS_PRE; - end = p + sizeof(buffer) - LWS_PRE; + if (strncmp((const char *) in, "/", 1)) { + lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, NULL); + goto try_to_reuse; + } - if (lws_add_http_header_status(wsi, 200, &p, end)) + 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 *) "text/html", 9, &p, end)) return 1; - if (lws_add_http_header_content_length(wsi, index_html_len, &p, end)) + if (lws_add_http_header_content_length(wsi, (unsigned long) index_html_len, &p, end)) return 1; if (lws_finalize_http_header(wsi, &p, end)) return 1; - n = lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE), LWS_WRITE_HTTP_HEADERS); - if (n < 0) { + if (lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE), LWS_WRITE_HTTP_HEADERS) < 0) { return 1; } - n = lws_write_http(wsi, index_html, index_html_len); - if (n < 0) + if (lws_write_http(wsi, index_html, index_html_len) < 0) return 1; goto try_to_reuse; - case LWS_CALLBACK_HTTP_WRITEABLE: - lwsl_info("LWS_CALLBACK_HTTP_WRITEABLE\n"); - break; default: break; } diff --git a/src/index.html b/src/index.html index 37d54d6..8a15414 100644 --- a/src/index.html +++ b/src/index.html @@ -571,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v/s412fybe/nONdncoCHnBVliu0CQGBWlPY/5Kwo"+ "m2L/kruPM6Q7oz4tvDQy+bZ3HzOi+gNHA4DZEgA="+"");lib.resource.add("hterm/concat/date","text/plain","Sat, 10 Sep 2016 08:51:57 +0000"+"");lib.resource.add("hterm/changelog/version","text/plain","1.58"+"");lib.resource.add("hterm/changelog/date","text/plain","2016-07-12"+"");lib.resource.add("hterm/git/HEAD","text/plain","49f8641dd055afaad9eadcd8553804eff0dd2637"+""); +