diff --git a/README.md b/README.md index 69eb12f..9700adf 100644 --- a/README.md +++ b/README.md @@ -82,9 +82,11 @@ OPTIONS: --ssl-cert, -C SSL certificate file path --ssl-key, -K SSL key file path --ssl-ca, -A SSL CA file path for client certificate verification - --debug, -d Set log level (0-9, default: 7) + --debug, -d Set log level (default: 7) --version, -v Print the version and exit --help, -h Print this text and exit + +Visit https://github.com/tsl0922/ttyd to get more information and report bugs. ``` ## Example Usage diff --git a/html/js/app.js b/html/js/app.js index c9127e6..da8ddad 100644 --- a/html/js/app.js +++ b/html/js/app.js @@ -2,6 +2,7 @@ var terminalContainer = document.getElementById('terminal-container'), httpsEnabled = window.location.protocol == "https:", url = (httpsEnabled ? 'wss://' : 'ws://') + window.location.host + window.location.pathname + 'ws', + authToken = (typeof tty_auth_token !== 'undefined') ? tty_auth_token : null, protocols = ["tty"], autoReconnect = -1, term, pingTimer, wsError; @@ -10,10 +11,9 @@ var ws = new WebSocket(url, protocols); ws.onopen = function(event) { + console.log("Websocket connection opened"); wsError = false; - if (typeof tty_auth_token !== 'undefined') { - ws.send(JSON.stringify({AuthToken: tty_auth_token})); - } + ws.send(JSON.stringify({AuthToken: authToken})); pingTimer = setInterval(sendPing, 30 * 1000, ws); if (typeof term !== 'undefined') { @@ -22,7 +22,7 @@ term = new Terminal(); - term.on('resize', function (size) { + term.on('resize', function(size) { if (ws.readyState === WebSocket.OPEN) { ws.send("2" + JSON.stringify({columns: size.cols, rows: size.rows})); } @@ -30,22 +30,26 @@ term.showOverlay(size.cols + 'x' + size.rows); }, 500); }); + term.on("data", function(data) { if (ws.readyState === WebSocket.OPEN) { ws.send("0" + data); } }); - window.onresize = function(event) { + + term.on('open', function() { + window.onresize = function(event) { + term.fit(); + }; term.fit(); - }; + term.focus(); + }); while (terminalContainer.firstChild) { terminalContainer.removeChild(terminalContainer.firstChild); } term.open(terminalContainer); - term.fit(); - term.focus(); }; ws.onmessage = function(event) { @@ -68,12 +72,13 @@ break; case '4': autoReconnect = JSON.parse(data); - console.log("Enabling reconnect: " + autoReconnect + " seconds") + console.log("Enabling reconnect: " + autoReconnect + " seconds"); break; } }; ws.onclose = function(event) { + console.log("Websocket connection closed with code: " + event.code); if (term) { term.off('data'); term.off('resize'); @@ -86,11 +91,6 @@ setTimeout(openWs, autoReconnect * 1000); } }; - - ws.onerror = function(event) { - wsError = true; - term.showOverlay("Websocket handshake failed", null); - }; }; var sendPing = function(ws) { diff --git a/src/http.c b/src/http.c index 8b63e85..e704bed 100644 --- a/src/http.c +++ b/src/http.c @@ -50,21 +50,19 @@ check_auth(struct lws *wsi) { 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]; + char buf[256], name[100], rip[50]; switch (reason) { case LWS_CALLBACK_HTTP: - { - char name[100], rip[50]; - 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) { + // only GET method is allowed + if (!lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI) || len < 1) { lws_return_http_status(wsi, HTTP_STATUS_BAD_REQUEST, NULL); goto try_to_reuse; } + lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name, sizeof(name), rip, sizeof(rip)); + lwsl_notice("HTTP %s - %s (%s)\n", in, rip, name); + switch (check_auth(wsi)) { case 0: break; @@ -75,10 +73,6 @@ callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, voi return 1; } - // if a legal POST URL, let it continue and accept data - if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI)) - return 0; - p = buffer + LWS_PRE; end = p + sizeof(buffer) - LWS_PRE; @@ -130,7 +124,6 @@ callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, voi return 1; } goto try_to_reuse; - case LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION: if (!len || (SSL_get_verify_result((SSL *) in) != X509_V_OK)) { int err = X509_STORE_CTX_get_error((X509_STORE_CTX *) user); @@ -140,7 +133,6 @@ callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, voi return 1; } break; - default: break; } diff --git a/src/index.html b/src/index.html index fb3ab57..a0c65aa 100644 --- a/src/index.html +++ b/src/index.html @@ -16,6 +16,6 @@ break;case 33:t.shiftKey?e.scrollDisp=-(this.rows-1):e.key="[5~";break;case 34:
- +