Compare commits

...

24 Commits

Author SHA1 Message Date
老J
e10230d94e
Merge 9813acaff3 into c69d11d17d 2024-03-25 12:09:23 -07:00
Soren L. Hansen
c69d11d17d Rebuild assets 2024-03-25 12:08:45 -07:00
Soren L. Hansen
e9bc3f0131 Merge remote-tracking branch 'origin/dependabot/npm_and_yarn/js/follow-redirects-1.15.6' 2024-03-25 11:32:33 -07:00
Soren L. Hansen
f719846bf3 Merge remote-tracking branch 'origin/dependabot/npm_and_yarn/js/webpack-dev-middleware-5.3.4' 2024-03-25 11:32:17 -07:00
Soren L. Hansen
adfc112d1a Merge branch 'upgrade-xterm' of https://github.com/josegonzalez/gotty 2024-03-25 11:30:46 -07:00
Jose Diaz-Gonzalez
ba9326e417 feat: add support for specifying custom querystring arguments
This allows folks to implement token-based authentication for websocket access.

Closes #82
2024-03-25 10:55:26 -07:00
dependabot[bot]
e9887233c0
Bump webpack-dev-middleware from 5.3.3 to 5.3.4 in /js
Bumps [webpack-dev-middleware](https://github.com/webpack/webpack-dev-middleware) from 5.3.3 to 5.3.4.
- [Release notes](https://github.com/webpack/webpack-dev-middleware/releases)
- [Changelog](https://github.com/webpack/webpack-dev-middleware/blob/v5.3.4/CHANGELOG.md)
- [Commits](https://github.com/webpack/webpack-dev-middleware/compare/v5.3.3...v5.3.4)

---
updated-dependencies:
- dependency-name: webpack-dev-middleware
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-03-23 11:27:03 +00:00
dependabot[bot]
09c61da056
Bump follow-redirects from 1.15.5 to 1.15.6 in /js
Bumps [follow-redirects](https://github.com/follow-redirects/follow-redirects) from 1.15.5 to 1.15.6.
- [Release notes](https://github.com/follow-redirects/follow-redirects/releases)
- [Commits](https://github.com/follow-redirects/follow-redirects/compare/v1.15.5...v1.15.6)

---
updated-dependencies:
- dependency-name: follow-redirects
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-03-17 00:13:12 +00:00
Jose Diaz-Gonzalez
721607826e feat: upgrade xterm the latest 5.3.0 release
There are some breaking api changes as documented here: https://github.com/xtermjs/xterm.js/releases/tag/5.0.0
2024-02-15 02:29:48 -05:00
llaoj
9813acaff3 Merge branch 'develop': control sequence 2022-12-13 13:53:30 +08:00
llaoj
8450aa5ee2 control sequenes 2022-12-13 13:52:15 +08:00
llaoj
9c203ef201 For the brevity of the code, I changed the print format of arguments. 2022-12-12 09:25:43 +08:00
llaoj
0620cb5c2c For the brevity of the code, I changed the print format of arguments. 2022-12-12 09:19:30 +08:00
老J
9224e98568
Merge branch 'sorenisanerd:master' into master 2022-12-11 18:10:11 +08:00
llaoj
f764187d57 1. Adjusted the log format
2. optimized code
Merge branch 'develop'
2022-12-11 18:01:49 +08:00
llaoj
24549dca01 1. Adjusted the log format
2. optimized code
2022-12-11 18:00:15 +08:00
llaoj
abddcbc68d 1. add writes logs in stdout
Merge branch 'develop'
2022-11-15 09:52:03 +08:00
llaoj
8a407192cc ascii to str 2022-11-15 09:34:38 +08:00
llaoj
8a7fa9ecb9 ascii to str 2022-11-14 12:15:16 +08:00
llaoj
70a264ed00 writes log 2022-11-14 09:36:47 +08:00
llaoj
1c4427af53 writes log 2022-11-14 09:32:24 +08:00
llaoj
73f0ca5a2f writes log 2022-11-14 08:53:57 +08:00
llaoj
a7d7ea6629 1. introduct operation log feature in README.md
2. add args(json) in operation logs
2022-11-13 11:14:11 +08:00
llaoj
3e9fbfc86e operation log 2022-11-11 17:06:22 +08:00
15 changed files with 261 additions and 47 deletions

3
.gotty
View File

@ -7,6 +7,9 @@
// [bool] Permit clients to write to the TTY // [bool] Permit clients to write to the TTY
// permit_write = false // permit_write = false
// [bool] Log user's writes in the TTY
// write_log = false
// [bool] Enable basic authentication // [bool] Enable basic authentication
// enable_basic_auth = false // enable_basic_auth = false

View File

@ -57,6 +57,7 @@ By default, GoTTY starts a web server at port 8080. Open the URL on your web bro
--port value, -p value Port number to liten (default: "8080") [$GOTTY_PORT] --port value, -p value Port number to liten (default: "8080") [$GOTTY_PORT]
--path value, -m value Base path (default: "/") [$GOTTY_PATH] --path value, -m value Base path (default: "/") [$GOTTY_PATH]
--permit-write, -w Permit clients to write to the TTY (BE CAREFUL) (default: false) [$GOTTY_PERMIT_WRITE] --permit-write, -w Permit clients to write to the TTY (BE CAREFUL) (default: false) [$GOTTY_PERMIT_WRITE]
--write-log Log user's writes in the TTY (default: false) [$GOTTY_WRITE_LOG]
--credential value, -c value Credential for Basic Authentication (ex: user:pass, default disabled) [$GOTTY_CREDENTIAL] --credential value, -c value Credential for Basic Authentication (ex: user:pass, default disabled) [$GOTTY_CREDENTIAL]
--random-url, -r Add a random string to the URL (default: false) [$GOTTY_RANDOM_URL] --random-url, -r Add a random string to the URL (default: false) [$GOTTY_RANDOM_URL]
--random-url-length value Random URL length (default: 8) [$GOTTY_RANDOM_URL_LENGTH] --random-url-length value Random URL length (default: 8) [$GOTTY_RANDOM_URL_LENGTH]
@ -75,6 +76,7 @@ By default, GoTTY starts a web server at port 8080. Open the URL on your web bro
--width value Static width of the screen, 0(default) means dynamically resize (default: 0) [$GOTTY_WIDTH] --width value Static width of the screen, 0(default) means dynamically resize (default: 0) [$GOTTY_WIDTH]
--height value Static height of the screen, 0(default) means dynamically resize (default: 0) [$GOTTY_HEIGHT] --height value Static height of the screen, 0(default) means dynamically resize (default: 0) [$GOTTY_HEIGHT]
--ws-origin value A regular expression that matches origin URLs to be accepted by WebSocket. No cross origin requests are acceptable by default [$GOTTY_WS_ORIGIN] --ws-origin value A regular expression that matches origin URLs to be accepted by WebSocket. No cross origin requests are acceptable by default [$GOTTY_WS_ORIGIN]
--ws-query-args value Querystring arguments to append to the websocket instantiation [$GOTTY_WS_QUERY_ARGS]
--enable-webgl Enable WebGL renderer (default: true) [$GOTTY_ENABLE_WEBGL] --enable-webgl Enable WebGL renderer (default: true) [$GOTTY_ENABLE_WEBGL]
--close-signal value Signal sent to the command process when gotty close it (default: SIGHUP) (default: 1) [$GOTTY_CLOSE_SIGNAL] --close-signal value Signal sent to the command process when gotty close it (default: SIGHUP) (default: 1) [$GOTTY_CLOSE_SIGNAL]
--close-timeout value Time in seconds to force kill process after client is disconnected (default: -1) (default: -1) [$GOTTY_CLOSE_TIMEOUT] --close-timeout value Time in seconds to force kill process after client is disconnected (default: -1) (default: -1) [$GOTTY_CLOSE_TIMEOUT]
@ -151,6 +153,37 @@ When you want to create a jailed environment for each client, you can use Docker
$ gotty -w docker run -it --rm busybox $ gotty -w docker run -it --rm busybox
``` ```
## Write log
If you set `--write-log` option, user's writes in the TTY can be Logged. for example:
if you run gotty like this:
```shell
./gotty -w --write-log --permit-arguments ./test.sh
```
this is `test.sh`:
```sh
#!/bin/bash
echo "Welcome: $4"
kubectl -n $1 exec -it $2 -c $3 -- sh
```
visit `http://127.0.0.1:8080/?arg=without-istio&arg=sleep-7b6d569576-57sjq&arg=sleep&arg=21001713` and input your commands in shell, and you will see user's writes in the log (operation logs):
```
...
2022/12/13 13:36:46 [write-log] map[arg:[without-istio sleep-7b6d569576-8nz7t sleep 21001713]] ls[CR]
2022/12/13 13:37:25 [write-log] map[arg:[without-istio sleep-7b6d569576-8nz7t sleep 21001713]] echo[SPACE]hello[SPACE]world;[CR]
2022/12/13 13:47:00 [write-log] map[arg:[without-istio sleep-7b6d569576-8nz7t sleep 21001713]] [CUU][CR]
...
```
Using the `[write-log]` flag, you can collect and store these logs persistently. All args are in the log, including the userID.
## Development ## Development
You can build a binary by simply running `make`. go1.16 is required. You can build a binary by simply running `make`. go1.16 is required.

View File

@ -149,6 +149,7 @@
right: 0; right: 0;
z-index: 10; z-index: 10;
color: transparent; color: transparent;
pointer-events: none;
} }
.xterm .live-region { .xterm .live-region {
@ -160,13 +161,27 @@
} }
.xterm-dim { .xterm-dim {
opacity: 0.5; /* Dim should not apply to background, so the opacity of the foreground color is applied
* explicitly in the generated class and reset to 1 here */
opacity: 1 !important;
} }
.xterm-underline { .xterm-underline-1 { text-decoration: underline; }
text-decoration: underline; .xterm-underline-2 { text-decoration: double underline; }
.xterm-underline-3 { text-decoration: wavy underline; }
.xterm-underline-4 { text-decoration: dotted underline; }
.xterm-underline-5 { text-decoration: dashed underline; }
.xterm-overline {
text-decoration: overline;
} }
.xterm-overline.xterm-underline-1 { text-decoration: overline underline; }
.xterm-overline.xterm-underline-2 { text-decoration: overline double underline; }
.xterm-overline.xterm-underline-3 { text-decoration: overline wavy underline; }
.xterm-overline.xterm-underline-4 { text-decoration: overline dotted underline; }
.xterm-overline.xterm-underline-5 { text-decoration: overline dashed underline; }
.xterm-strikethrough { .xterm-strikethrough {
text-decoration: line-through; text-decoration: line-through;
} }
@ -176,8 +191,12 @@
position: absolute; position: absolute;
} }
.xterm-decoration-overview-ruler { .xterm-screen .xterm-decoration-container .xterm-decoration.xterm-decoration-top-layer {
z-index: 7; z-index: 7;
}
.xterm-decoration-overview-ruler {
z-index: 8;
position: absolute; position: absolute;
top: 0; top: 0;
right: 0; right: 0;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

50
js/package-lock.json generated
View File

@ -15,10 +15,10 @@
"preact": "^10.19.4", "preact": "^10.19.4",
"react-bootstrap": "^2.10.1", "react-bootstrap": "^2.10.1",
"style-loader": "^2.0.0", "style-loader": "^2.0.0",
"xterm": "^4.19.0", "xterm": "^5.3.0",
"xterm-addon-fit": "^0.5.0", "xterm-addon-fit": "^0.8.0",
"xterm-addon-web-links": "^0.4.0", "xterm-addon-web-links": "^0.9.0",
"xterm-addon-webgl": "^0.10.0", "xterm-addon-webgl": "^0.16.0",
"zmodem.js": "^0.1.10" "zmodem.js": "^0.1.10"
}, },
"devDependencies": { "devDependencies": {
@ -1662,9 +1662,9 @@
} }
}, },
"node_modules/follow-redirects": { "node_modules/follow-redirects": {
"version": "1.15.5", "version": "1.15.6",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz",
"integrity": "sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==", "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==",
"dev": true, "dev": true,
"funding": [ "funding": [
{ {
@ -4239,9 +4239,9 @@
} }
}, },
"node_modules/webpack-dev-middleware": { "node_modules/webpack-dev-middleware": {
"version": "5.3.3", "version": "5.3.4",
"resolved": "https://registry.npmjs.org/webpack-dev-middleware/-/webpack-dev-middleware-5.3.3.tgz", "resolved": "https://registry.npmjs.org/webpack-dev-middleware/-/webpack-dev-middleware-5.3.4.tgz",
"integrity": "sha512-hj5CYrY0bZLB+eTO+x/j67Pkrquiy7kWepMHmUMoPsmcUaeEnQJqFzHJOyxgWlq746/wUuA64p9ta34Kyb01pA==", "integrity": "sha512-BVdTqhhs+0IfoeAf7EoH5WE+exCmqGerHfDM0IL096Px60Tq2Mn9MAbnaGUe6HiMa41KMCYF19gyzZmBcq/o4Q==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"colorette": "^2.0.10", "colorette": "^2.0.10",
@ -4479,32 +4479,32 @@
} }
}, },
"node_modules/xterm": { "node_modules/xterm": {
"version": "4.19.0", "version": "5.3.0",
"resolved": "https://registry.npmjs.org/xterm/-/xterm-4.19.0.tgz", "resolved": "https://registry.npmjs.org/xterm/-/xterm-5.3.0.tgz",
"integrity": "sha512-c3Cp4eOVsYY5Q839dR5IejghRPpxciGmLWWaP9g+ppfMeBChMeLa1DCA+pmX/jyDZ+zxFOmlJL/82qVdayVoGQ==" "integrity": "sha512-8QqjlekLUFTrU6x7xck1MsPzPA571K5zNqWm0M0oroYEWVOptZ0+ubQSkQ3uxIEhcIHRujJy6emDWX4A7qyFzg=="
}, },
"node_modules/xterm-addon-fit": { "node_modules/xterm-addon-fit": {
"version": "0.5.0", "version": "0.8.0",
"resolved": "https://registry.npmjs.org/xterm-addon-fit/-/xterm-addon-fit-0.5.0.tgz", "resolved": "https://registry.npmjs.org/xterm-addon-fit/-/xterm-addon-fit-0.8.0.tgz",
"integrity": "sha512-DsS9fqhXHacEmsPxBJZvfj2la30Iz9xk+UKjhQgnYNkrUIN5CYLbw7WEfz117c7+S86S/tpHPfvNxJsF5/G8wQ==", "integrity": "sha512-yj3Np7XlvxxhYF/EJ7p3KHaMt6OdwQ+HDu573Vx1lRXsVxOcnVJs51RgjZOouIZOczTsskaS+CpXspK81/DLqw==",
"peerDependencies": { "peerDependencies": {
"xterm": "^4.0.0" "xterm": "^5.0.0"
} }
}, },
"node_modules/xterm-addon-web-links": { "node_modules/xterm-addon-web-links": {
"version": "0.4.0", "version": "0.9.0",
"resolved": "https://registry.npmjs.org/xterm-addon-web-links/-/xterm-addon-web-links-0.4.0.tgz", "resolved": "https://registry.npmjs.org/xterm-addon-web-links/-/xterm-addon-web-links-0.9.0.tgz",
"integrity": "sha512-xv8GeiINmx0zENO9hf5k+5bnkaE8mRzF+OBAr9WeFq2eLaQSudioQSiT34M1ofKbzcdjSsKiZm19Rw3i4eXamg==", "integrity": "sha512-LIzi4jBbPlrKMZF3ihoyqayWyTXAwGfu4yprz1aK2p71e9UKXN6RRzVONR0L+Zd+Ik5tPVI9bwp9e8fDTQh49Q==",
"peerDependencies": { "peerDependencies": {
"xterm": "^4.0.0" "xterm": "^5.0.0"
} }
}, },
"node_modules/xterm-addon-webgl": { "node_modules/xterm-addon-webgl": {
"version": "0.10.0", "version": "0.16.0",
"resolved": "https://registry.npmjs.org/xterm-addon-webgl/-/xterm-addon-webgl-0.10.0.tgz", "resolved": "https://registry.npmjs.org/xterm-addon-webgl/-/xterm-addon-webgl-0.16.0.tgz",
"integrity": "sha512-MJzyWie5yw+PH6p//fXlXzmsULLtpBo992EWEKl2uv5M5Zj9etTwfuutCUK7o98mr6itDl+vS/CYIMP68jCf8w==", "integrity": "sha512-E8cq1AiqNOv0M/FghPT+zPAEnvIQRDbAbkb04rRYSxUym69elPWVJ4sv22FCLBqM/3LcrmBLl/pELnBebVFKgA==",
"peerDependencies": { "peerDependencies": {
"xterm": "^4.0.0" "xterm": "^5.0.0"
} }
}, },
"node_modules/yallist": { "node_modules/yallist": {

View File

@ -25,10 +25,10 @@
"preact": "^10.19.4", "preact": "^10.19.4",
"react-bootstrap": "^2.10.1", "react-bootstrap": "^2.10.1",
"style-loader": "^2.0.0", "style-loader": "^2.0.0",
"xterm": "^4.19.0", "xterm": "^5.3.0",
"xterm-addon-fit": "^0.5.0", "xterm-addon-fit": "^0.8.0",
"xterm-addon-web-links": "^0.4.0", "xterm-addon-web-links": "^0.9.0",
"xterm-addon-webgl": "^0.10.0", "xterm-addon-webgl": "^0.16.0",
"zmodem.js": "^0.1.10" "zmodem.js": "^0.1.10"
} }
} }

View File

@ -1,10 +1,11 @@
import { OurXterm } from "./xterm";
import { Terminal, WebTTY, protocols } from "./webtty";
import { ConnectionFactory } from "./websocket"; import { ConnectionFactory } from "./websocket";
import { Terminal, WebTTY, protocols } from "./webtty";
import { OurXterm } from "./xterm";
// @TODO remove these // @TODO remove these
declare var gotty_auth_token: string; declare var gotty_auth_token: string;
declare var gotty_term: string; declare var gotty_term: string;
declare var gotty_ws_query_args: string;
const elem = document.getElementById("terminal") const elem = document.getElementById("terminal")
@ -13,7 +14,8 @@ if (elem !== null) {
term = new OurXterm(elem); term = new OurXterm(elem);
const httpsEnabled = window.location.protocol == "https:"; const httpsEnabled = window.location.protocol == "https:";
const url = (httpsEnabled ? 'wss://' : 'ws://') + window.location.host + window.location.pathname + 'ws'; const queryArgs = (gotty_ws_query_args === "") ? "" : "?" + gotty_ws_query_args;
const url = (httpsEnabled ? 'wss://' : 'ws://') + window.location.host + window.location.pathname + 'ws' + queryArgs;
const args = window.location.search; const args = window.location.search;
const factory = new ConnectionFactory(url, protocols); const factory = new ConnectionFactory(url, protocols);
const wt = new WebTTY(term, factory, args, gotty_auth_token); const wt = new WebTTY(term, factory, args, gotty_auth_token);

View File

@ -1,4 +1,4 @@
import { Terminal, IDisposable } from "xterm"; import { IDisposable, Terminal } from "xterm";
import { FitAddon } from 'xterm-addon-fit'; import { FitAddon } from 'xterm-addon-fit';
import { WebLinksAddon } from 'xterm-addon-web-links'; import { WebLinksAddon } from 'xterm-addon-web-links';
import { WebglAddon } from 'xterm-addon-webgl'; import { WebglAddon } from 'xterm-addon-webgl';
@ -104,9 +104,9 @@ export class OurXterm {
if (key == "EnableWebGL" && key) { if (key == "EnableWebGL" && key) {
this.term.loadAddon(new WebglAddon()); this.term.loadAddon(new WebglAddon());
} else if (key == "font-size") { } else if (key == "font-size") {
this.term.setOption("fontSize", value[key]) this.term.options.fontSize = value[key]
} else if (key == "font-family") { } else if (key == "font-family") {
this.term.setOption("fontFamily", value[key]) this.term.options.fontFamily = value[key]
} }
}); });
}; };

View File

@ -1,7 +1,7 @@
import { Component, ComponentChildren, createRef, render } from "preact";
import { ITerminalAddon, Terminal } from "xterm"; import { ITerminalAddon, Terminal } from "xterm";
import { Browser, Detection, Offer, Sentry, Session } from 'zmodem.js/src/zmodem_browser'; import { Browser, Detection, Offer, Sentry, Session } from 'zmodem.js/src/zmodem_browser';
import { MyModal, Button } from "./MyModal"; import { Button, MyModal } from "./MyModal";
import { Component, ComponentChildren, createRef, render } from "preact";
export class ZModemAddon implements ITerminalAddon { export class ZModemAddon implements ITerminalAddon {
term: Terminal; term: Terminal;

View File

@ -8,6 +8,7 @@ import (
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"strings"
"sync/atomic" "sync/atomic"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
@ -145,10 +146,14 @@ func (server *Server) processWSConn(ctx context.Context, conn *websocket.Conn, h
opts := []webtty.Option{ opts := []webtty.Option{
webtty.WithWindowTitle(titleBuf.Bytes()), webtty.WithWindowTitle(titleBuf.Bytes()),
webtty.WithArguments(params),
} }
if server.options.PermitWrite { if server.options.PermitWrite {
opts = append(opts, webtty.WithPermitWrite()) opts = append(opts, webtty.WithPermitWrite())
} }
if server.options.WriteLog {
opts = append(opts, webtty.WithWriteLog())
}
if server.options.EnableReconnect { if server.options.EnableReconnect {
opts = append(opts, webtty.WithReconnect(server.options.ReconnectTime)) opts = append(opts, webtty.WithReconnect(server.options.ReconnectTime))
} }
@ -233,7 +238,12 @@ func (server *Server) handleAuthToken(w http.ResponseWriter, r *http.Request) {
func (server *Server) handleConfig(w http.ResponseWriter, r *http.Request) { func (server *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/javascript") w.Header().Set("Content-Type", "application/javascript")
w.Write([]byte("var gotty_term = 'xterm';")) lines := []string{
"var gotty_term = 'xterm';",
"var gotty_ws_query_args = '" + server.options.WSQueryArgs + "';",
}
w.Write([]byte(strings.Join(lines, "\n")))
} }
// titleVariables merges maps in a specified order. // titleVariables merges maps in a specified order.

View File

@ -9,6 +9,7 @@ type Options struct {
Port string `hcl:"port" flagName:"port" flagSName:"p" flagDescribe:"Port number to liten" default:"8080"` Port string `hcl:"port" flagName:"port" flagSName:"p" flagDescribe:"Port number to liten" default:"8080"`
Path string `hcl:"path" flagName:"path" flagSName:"m" flagDescribe:"Base path" default:"/"` Path string `hcl:"path" flagName:"path" flagSName:"m" flagDescribe:"Base path" default:"/"`
PermitWrite bool `hcl:"permit_write" flagName:"permit-write" flagSName:"w" flagDescribe:"Permit clients to write to the TTY (BE CAREFUL)" default:"false"` PermitWrite bool `hcl:"permit_write" flagName:"permit-write" flagSName:"w" flagDescribe:"Permit clients to write to the TTY (BE CAREFUL)" default:"false"`
WriteLog bool `hcl:"write_log" flagName:"write-log" flagDescribe:"Log user's writes in the TTY" default:"false"`
EnableBasicAuth bool `hcl:"enable_basic_auth" default:"false"` EnableBasicAuth bool `hcl:"enable_basic_auth" default:"false"`
Credential string `hcl:"credential" flagName:"credential" flagSName:"c" flagDescribe:"Credential for Basic Authentication (ex: user:pass, default disabled)" default:""` Credential string `hcl:"credential" flagName:"credential" flagSName:"c" flagDescribe:"Credential for Basic Authentication (ex: user:pass, default disabled)" default:""`
EnableRandomUrl bool `hcl:"enable_random_url" flagName:"random-url" flagSName:"r" flagDescribe:"Add a random string to the URL" default:"false"` EnableRandomUrl bool `hcl:"enable_random_url" flagName:"random-url" flagSName:"r" flagDescribe:"Add a random string to the URL" default:"false"`
@ -30,6 +31,7 @@ type Options struct {
Width int `hcl:"width" flagName:"width" flagDescribe:"Static width of the screen, 0(default) means dynamically resize" default:"0"` Width int `hcl:"width" flagName:"width" flagDescribe:"Static width of the screen, 0(default) means dynamically resize" default:"0"`
Height int `hcl:"height" flagName:"height" flagDescribe:"Static height of the screen, 0(default) means dynamically resize" default:"0"` Height int `hcl:"height" flagName:"height" flagDescribe:"Static height of the screen, 0(default) means dynamically resize" default:"0"`
WSOrigin string `hcl:"ws_origin" flagName:"ws-origin" flagDescribe:"A regular expression that matches origin URLs to be accepted by WebSocket. No cross origin requests are acceptable by default" default:""` WSOrigin string `hcl:"ws_origin" flagName:"ws-origin" flagDescribe:"A regular expression that matches origin URLs to be accepted by WebSocket. No cross origin requests are acceptable by default" default:""`
WSQueryArgs string `hcl:"ws_query_args" flagName:"ws-query-args" flagDescribe:"Querystring arguments to append to the websocket instantiation" default:""`
EnableWebGL bool `hcl:"enable_webgl" flagName:"enable-webgl" flagDescribe:"Enable WebGL renderer" default:"true"` EnableWebGL bool `hcl:"enable_webgl" flagName:"enable-webgl" flagDescribe:"Enable WebGL renderer" default:"true"`
Quiet bool `hcl:"quiet" flagName:"quiet" flagDescribe:"Don't log" default:"false"` Quiet bool `hcl:"quiet" flagName:"quiet" flagDescribe:"Don't log" default:"false"`

114
utils/log.go Normal file
View File

@ -0,0 +1,114 @@
package utils
import (
"fmt"
"regexp"
)
var ControlCodes = map[byte]string{
0: "NUL",
1: "SOH",
2: "STX",
3: "ETX",
4: "EOT",
5: "ENQ",
6: "ACK",
7: "BEL",
8: "BS",
9: "HT",
10: "LF",
11: "VT",
12: "FF",
13: "CR",
14: "SO",
15: "SI",
16: "DLE",
17: "DCI",
18: "DC2",
19: "DC3",
20: "DC4",
21: "NAK",
22: "SYN",
23: "TB",
24: "CAN",
25: "EM",
26: "SUB",
27: "ESC",
28: "FS",
29: "GS",
30: "RS",
31: "US",
32: "SPACE",
127: "DEL",
}
// https://en.wikipedia.org/wiki/ANSI_escape_code
// https://xtermjs.org/docs/api/vtfeatures/
var ControlSequences = map[string]string{
// Cursor Up
"ESC[A": "CUU",
// Cursor Down
"ESC[B": "CUD",
// Cursor Forward
"ESC[C": "CUF",
// Cursor Back
"ESC[D": "CUB",
}
var ControlSequencePatterns = map[string]string{
// Device Status Report
// Reports the cursor position (CPR) by transmitting `ESC[n;mR`, where n is the row and m is the column.
"^ESC\\[\\d+;\\d+R$": "",
}
func ControlCodesToStr(codes []byte) (str string) {
for _, code := range codes {
if value, ok := ControlCodes[code]; ok {
str += value
} else {
str += string(code)
}
}
return
}
func ControlCodesToEscapedStr(codes []byte) (str string) {
for _, code := range codes {
if value, ok := ControlCodes[code]; ok {
str += fmt.Sprintf("[%s]", value)
} else if code == 91 || code == 92 || code == 93 {
// escaping [ ] \
str += fmt.Sprintf("\\%s", string(code))
} else {
str += string(code)
}
}
return
}
func ControlSequenceToStr(codes []byte) (string, bool) {
sequence := ControlCodesToStr(codes)
for key, value := range ControlSequences {
if key == sequence {
return fmt.Sprintf("[%s]", value), true
}
}
for key, value := range ControlSequencePatterns {
if regexp.MustCompile(key).Match([]byte(sequence)) {
return value, true
}
}
return sequence, false
}
func FormatWriteLog(codes []byte, line *string) {
n := len(codes)
if n >= 3 {
if str, exists := ControlSequenceToStr(codes); exists {
*line += str
return
}
}
*line += ControlCodesToEscapedStr(codes)
}

View File

@ -17,6 +17,14 @@ func WithPermitWrite() Option {
} }
} }
// WithWriteLog sets a WebTTY to log user's writes in the TTY.
func WithWriteLog() Option {
return func(wt *WebTTY) error {
wt.writeLog = true
return nil
}
}
// WithFixedColumns sets a fixed width to TTY master. // WithFixedColumns sets a fixed width to TTY master.
func WithFixedColumns(columns int) Option { func WithFixedColumns(columns int) Option {
return func(wt *WebTTY) error { return func(wt *WebTTY) error {
@ -41,6 +49,14 @@ func WithWindowTitle(windowTitle []byte) Option {
} }
} }
// WithArguments sets the command line arguments that clients send
func WithArguments(arguments map[string][]string) Option {
return func(wt *WebTTY) error {
wt.arguments = arguments
return nil
}
}
// WithReconnect enables reconnection on the master side. // WithReconnect enables reconnection on the master side.
func WithReconnect(timeInSeconds int) Option { func WithReconnect(timeInSeconds int) Option {
return func(wt *WebTTY) error { return func(wt *WebTTY) error {

View File

@ -4,6 +4,8 @@ import (
"context" "context"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"github.com/sorenisanerd/gotty/utils"
"log"
"sync" "sync"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -19,7 +21,9 @@ type WebTTY struct {
slave Slave slave Slave
windowTitle []byte windowTitle []byte
arguments map[string][]string
permitWrite bool permitWrite bool
writeLog bool
columns int columns int
rows int rows int
reconnect int // in seconds reconnect int // in seconds
@ -93,13 +97,14 @@ func (wt *WebTTY) Run(ctx context.Context) error {
go func() { go func() {
errs <- func() error { errs <- func() error {
buffer := make([]byte, wt.bufferSize) buffer := make([]byte, wt.bufferSize)
var line string
for { for {
n, err := wt.masterConn.Read(buffer) n, err := wt.masterConn.Read(buffer)
if err != nil { if err != nil {
return ErrMasterClosed return ErrMasterClosed
} }
err = wt.handleMasterReadEvent(buffer[:n]) err = wt.handleMasterReadEvent(buffer[:n], &line)
if err != nil { if err != nil {
return err return err
} }
@ -168,7 +173,7 @@ func (wt *WebTTY) masterWrite(data []byte) error {
return nil return nil
} }
func (wt *WebTTY) handleMasterReadEvent(data []byte) error { func (wt *WebTTY) handleMasterReadEvent(data []byte, line *string) error {
if len(data) == 0 { if len(data) == 0 {
return errors.New("unexpected zero length read from master") return errors.New("unexpected zero length read from master")
} }
@ -189,6 +194,16 @@ func (wt *WebTTY) handleMasterReadEvent(data []byte) error {
return errors.Wrapf(err, "failed to decode received data") return errors.Wrapf(err, "failed to decode received data")
} }
if wt.writeLog {
utils.FormatWriteLog(decodedBuffer[:n], line)
// 13(ASCII) means carriage return(CR)
// it is the end of a line
if decodedBuffer[n-1] == 13 {
log.Printf("[write-log] %v %s\n", wt.arguments, *line)
*line = ""
}
}
_, err = wt.slave.Write(decodedBuffer[:n]) _, err = wt.slave.Write(decodedBuffer[:n])
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to write received data to slave") return errors.Wrapf(err, "failed to write received data to slave")