From 77ac0b8e5860b69a32655ed4ef60c96d0c1cf933 Mon Sep 17 00:00:00 2001 From: George Grozdev Date: Thu, 14 Oct 2021 09:48:46 +0100 Subject: [PATCH 01/10] Allow processing of HTTP headers at WS creation --- backend/localcommand/factory.go | 4 ++-- backend/localcommand/local_command.go | 14 +++++++++++++- backend/localcommand/local_command_test.go | 4 ++-- server/handlers.go | 6 +++--- server/slave.go | 2 +- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/backend/localcommand/factory.go b/backend/localcommand/factory.go index 9d0c916..be6da37 100644 --- a/backend/localcommand/factory.go +++ b/backend/localcommand/factory.go @@ -37,12 +37,12 @@ func (factory *Factory) Name() string { return "local command" } -func (factory *Factory) New(params map[string][]string) (server.Slave, error) { +func (factory *Factory) New(params map[string][]string, headers map[string][]string) (server.Slave, error) { argv := make([]string, len(factory.argv)) copy(argv, factory.argv) if params["arg"] != nil && len(params["arg"]) > 0 { argv = append(argv, params["arg"]...) } - return New(factory.command, argv, factory.opts...) + return New(factory.command, argv, headers, factory.opts...) } diff --git a/backend/localcommand/local_command.go b/backend/localcommand/local_command.go index 71b6c18..d0e0753 100644 --- a/backend/localcommand/local_command.go +++ b/backend/localcommand/local_command.go @@ -3,6 +3,7 @@ package localcommand import ( "os" "os/exec" + "strings" "syscall" "time" @@ -27,11 +28,22 @@ type LocalCommand struct { ptyClosed chan struct{} } -func New(command string, argv []string, options ...Option) (*LocalCommand, error) { +func New(command string, argv []string, headers map[string][]string, options ...Option) (*LocalCommand, error) { cmd := exec.Command(command, argv...) cmd.Env = append(os.Environ(), "TERM=xterm-256color") + // Combine headers into key=value pairs to set as env vars + // Prefix the headers with "http_" so we don't overwrite any other env vars + // which potentially has the same name and to bring these closer to what + // a (F)CGI server would proxy to a backend service + // Replace hyphen with underscore and make them all upper case + for key, values := range headers { + h := "HTTP_" + strings.Replace(strings.ToUpper(key), "-", "_", -1) + "=" + strings.Join(values, ",") + // log.Printf("Adding header: %s", h) + cmd.Env = append(cmd.Env, h) + } + pty, err := pty.Start(cmd) if err != nil { // todo close cmd? diff --git a/backend/localcommand/local_command_test.go b/backend/localcommand/local_command_test.go index ddda029..220cb73 100644 --- a/backend/localcommand/local_command_test.go +++ b/backend/localcommand/local_command_test.go @@ -23,7 +23,7 @@ func TestNewFactory(t *testing.T) { t.Errorf("factory.options = %v, expected %v", factory.options, &Options{}) } - slave, _ := factory.New(nil) + slave, _ := factory.New(nil, nil) lcmd := slave.(*LocalCommand) if lcmd.closeSignal != 123 { t.Errorf("lcmd.closeSignal = %v, expected %v", lcmd.closeSignal, 123) @@ -40,7 +40,7 @@ func TestFactoryNew(t *testing.T) { return } - slave, err := factory.New(nil) + slave, err := factory.New(nil, nil) if err != nil { t.Errorf("factory.New() returned error") return diff --git a/server/handlers.go b/server/handlers.go index 5347ab6..a0acec1 100644 --- a/server/handlers.go +++ b/server/handlers.go @@ -72,7 +72,7 @@ func (server *Server) generateHandleWS(ctx context.Context, cancel context.Cance } defer conn.Close() - err = server.processWSConn(ctx, conn) + err = server.processWSConn(ctx, conn, r.Header) switch err { case ctx.Err(): @@ -87,7 +87,7 @@ func (server *Server) generateHandleWS(ctx context.Context, cancel context.Cance } } -func (server *Server) processWSConn(ctx context.Context, conn *websocket.Conn) error { +func (server *Server) processWSConn(ctx context.Context, conn *websocket.Conn, headers map[string][]string) error { typ, initLine, err := conn.ReadMessage() if err != nil { return errors.Wrapf(err, "failed to authenticate websocket connection") @@ -116,7 +116,7 @@ func (server *Server) processWSConn(ctx context.Context, conn *websocket.Conn) e } params := query.Query() var slave Slave - slave, err = server.factory.New(params) + slave, err = server.factory.New(params, headers) if err != nil { return errors.Wrapf(err, "failed to create backend") } diff --git a/server/slave.go b/server/slave.go index 52cd9fe..db9d731 100644 --- a/server/slave.go +++ b/server/slave.go @@ -13,5 +13,5 @@ type Slave interface { type Factory interface { Name() string - New(params map[string][]string) (Slave, error) + New(params map[string][]string, headers map[string][]string) (Slave, error) } From 3913c1c91e37989fe32ac6d37737c334cd32fdfa Mon Sep 17 00:00:00 2001 From: Callum Gare Date: Wed, 18 May 2022 22:35:51 +1000 Subject: [PATCH 02/10] Add gitlab action steps to build and push docker --- .github/workflows/pre-release.yaml | 21 +++++++++++++++++++-- Dockerfile | 15 ++++++++++----- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/.github/workflows/pre-release.yaml b/.github/workflows/pre-release.yaml index 8e2748f..0fa849f 100644 --- a/.github/workflows/pre-release.yaml +++ b/.github/workflows/pre-release.yaml @@ -22,7 +22,6 @@ jobs: with: go-version: 1.16 - - name: "Build & test" run: "make tools release-artifacts" @@ -34,4 +33,22 @@ jobs: title: "Development Build" files: | LICENSE - builds/dist/* \ No newline at end of file + builds/dist/* + + - uses: docker/setup-qemu-action@v1 + + - uses: docker/setup-buildx-action@v1 + + - uses: docker/login-action@v1 + with: + username: "${{ secrets.DOCKER_HUB_USER }}" + password: "${{ secrets.DOCKER_HUB_TOKEN }}" + + - name: "Build and push docker image" + uses: docker/build-push-action@v2 + with: + context: . + file: ./Dockerfile + platforms: linux/amd64,linux/arm/v7,linux/arm64 + push: true + tags: "${{ secrets.DOCKER_REPO }}:latest" \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 565e142..bd4b7dc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,15 +1,20 @@ -FROM golang:1.16 +FROM node:16 as js-build +WORKDIR /gotty +COPY js /gotty/js +COPY Makefile /gotty/ +RUN make bindata/static/js/gotty.js.map +FROM golang:1.16 as go-build WORKDIR /gotty COPY . /gotty +COPY --from=js-build /gotty/js/node_modules /gotty/js/node_modules +COPY --from=js-build /gotty/bindata/static/js /gotty/bindata/static/js RUN CGO_ENABLED=0 make FROM alpine:latest - RUN apk update && \ apk upgrade && \ - apk --no-cache add ca-certificates && \ - apk add bash + apk --no-cache add ca-certificates bash WORKDIR /root -COPY --from=0 /gotty/gotty /usr/bin/ +COPY --from=go-build /gotty/gotty /usr/bin/ CMD ["gotty", "-w", "bash"] From e8f5de568d20ad817cb00dcd83fd9b431af7dbee Mon Sep 17 00:00:00 2001 From: llach Date: Thu, 15 Sep 2022 16:15:54 +0000 Subject: [PATCH 03/10] task: pass headers --- server/handlers.go | 6 +++++- server/options.go | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/server/handlers.go b/server/handlers.go index 1f985fd..3393b22 100644 --- a/server/handlers.go +++ b/server/handlers.go @@ -72,7 +72,11 @@ func (server *Server) generateHandleWS(ctx context.Context, cancel context.Cance } defer conn.Close() - err = server.processWSConn(ctx, conn, r.Header) + if server.options.PassHeaders { + err = server.processWSConn(ctx, conn, r.Header) + } else { + err = server.processWSConn(ctx, conn, nil) + } switch err { case ctx.Err(): diff --git a/server/options.go b/server/options.go index b7dc3d3..b51b03f 100644 --- a/server/options.go +++ b/server/options.go @@ -26,6 +26,7 @@ type Options struct { Once bool `hcl:"once" flagName:"once" flagDescribe:"Accept only one client and exit on disconnection" default:"false"` Timeout int `hcl:"timeout" flagName:"timeout" flagDescribe:"Timeout seconds for waiting a client(0 to disable)" default:"0"` PermitArguments bool `hcl:"permit_arguments" flagName:"permit-arguments" flagDescribe:"Permit clients to send command line arguments in URL (e.g. http://example.com:8080/?arg=AAA&arg=BBB)" default:"false"` + PassHeaders bool `hcl:"pass_headers" flagName:"pass-headers" flagDescribe:"Pass HTTP request headers as environment variables (e.g. Host becomes HTTP_HOST)" default:"false"` 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"` 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:""` From d92d17c15d2743cf80bd9182a6df0b4ea4ed4649 Mon Sep 17 00:00:00 2001 From: llach Date: Thu, 15 Sep 2022 16:46:39 +0000 Subject: [PATCH 04/10] task: change exmaple to use Cookie --- server/options.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/options.go b/server/options.go index b51b03f..b03d78d 100644 --- a/server/options.go +++ b/server/options.go @@ -26,7 +26,7 @@ type Options struct { Once bool `hcl:"once" flagName:"once" flagDescribe:"Accept only one client and exit on disconnection" default:"false"` Timeout int `hcl:"timeout" flagName:"timeout" flagDescribe:"Timeout seconds for waiting a client(0 to disable)" default:"0"` PermitArguments bool `hcl:"permit_arguments" flagName:"permit-arguments" flagDescribe:"Permit clients to send command line arguments in URL (e.g. http://example.com:8080/?arg=AAA&arg=BBB)" default:"false"` - PassHeaders bool `hcl:"pass_headers" flagName:"pass-headers" flagDescribe:"Pass HTTP request headers as environment variables (e.g. Host becomes HTTP_HOST)" default:"false"` + PassHeaders bool `hcl:"pass_headers" flagName:"pass-headers" flagDescribe:"Pass HTTP request headers as environment variables (e.g. Cookie becomes HTTP_COOKIE)" default:"false"` 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"` 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:""` From 87c10e562e213e96c869664ccab8c88c5f51472d Mon Sep 17 00:00:00 2001 From: "Soren L. Hansen" Date: Thu, 20 Oct 2022 18:38:41 -0700 Subject: [PATCH 05/10] Add Twitch badge (#53) --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index e4a2f36..39404e8 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,11 @@ [![GitHub release](http://img.shields.io/github/release/sorenisanerd/gotty.svg?style=flat-square)][release] [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)][license] +[![Maintainer streaming](https://twitch-status.soren.tools/sorencodes)][twitch] [release]: https://github.com/sorenisanerd/gotty/releases [license]: https://github.com/sorenisanerd/gotty/blob/master/LICENSE +[twitch]: https://twitch.tv/sorencodes GoTTY is a simple command line tool that turns your CLI tools into web applications. From e51731714fd11c6d68df65575a1341fd66a494fc Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 9 Nov 2022 05:12:24 +0000 Subject: [PATCH 06/10] Bump loader-utils from 2.0.2 to 2.0.3 in /js Bumps [loader-utils](https://github.com/webpack/loader-utils) from 2.0.2 to 2.0.3. - [Release notes](https://github.com/webpack/loader-utils/releases) - [Changelog](https://github.com/webpack/loader-utils/blob/v2.0.3/CHANGELOG.md) - [Commits](https://github.com/webpack/loader-utils/compare/v2.0.2...v2.0.3) --- updated-dependencies: - dependency-name: loader-utils dependency-type: indirect ... Signed-off-by: dependabot[bot] --- js/package-lock.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/js/package-lock.json b/js/package-lock.json index 97d3210..459a606 100644 --- a/js/package-lock.json +++ b/js/package-lock.json @@ -1384,9 +1384,9 @@ } }, "node_modules/loader-utils": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-2.0.2.tgz", - "integrity": "sha512-TM57VeHptv569d/GKh6TAYdzKblwDNiumOdkFnejjD0XwTH87K90w3O7AiJRqdQoXygvi1VQTJTLGhJl7WqA7A==", + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-2.0.3.tgz", + "integrity": "sha512-THWqIsn8QRnvLl0shHYVBN9syumU8pYWEHPTmkiVGd+7K5eFNVSY6AJhRvgGF70gg1Dz+l/k8WicvFCxdEs60A==", "dependencies": { "big.js": "^5.2.2", "emojis-list": "^3.0.0", @@ -3770,9 +3770,9 @@ "integrity": "sha512-3R/1M+yS3j5ou80Me59j7F9IMs4PXs3VqRrm0TU3AbKPxlmpoY1TNscJV/oGJXo8qCatFGTfDbY6W6ipGOYXfg==" }, "loader-utils": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-2.0.2.tgz", - "integrity": "sha512-TM57VeHptv569d/GKh6TAYdzKblwDNiumOdkFnejjD0XwTH87K90w3O7AiJRqdQoXygvi1VQTJTLGhJl7WqA7A==", + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-2.0.3.tgz", + "integrity": "sha512-THWqIsn8QRnvLl0shHYVBN9syumU8pYWEHPTmkiVGd+7K5eFNVSY6AJhRvgGF70gg1Dz+l/k8WicvFCxdEs60A==", "requires": { "big.js": "^5.2.2", "emojis-list": "^3.0.0", From df86da49649ad8f9fcb13d1d998bbaa98e973111 Mon Sep 17 00:00:00 2001 From: "Soren L. Hansen" Date: Fri, 11 Nov 2022 22:51:03 +0000 Subject: [PATCH 07/10] Accidentally deleted the checkout step --- .github/workflows/pre-release.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/pre-release.yaml b/.github/workflows/pre-release.yaml index 8bd2a69..d893411 100644 --- a/.github/workflows/pre-release.yaml +++ b/.github/workflows/pre-release.yaml @@ -11,7 +11,12 @@ jobs: name: "Pre Release Docker" runs-on: "ubuntu-latest" + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + - uses: docker/setup-qemu-action@v1 - uses: docker/setup-buildx-action@v1 From 31530f8a435603ff22d460c865f95c308ff3ba76 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 18 Nov 2022 00:42:09 +0000 Subject: [PATCH 08/10] Bump loader-utils from 2.0.3 to 2.0.4 in /js Bumps [loader-utils](https://github.com/webpack/loader-utils) from 2.0.3 to 2.0.4. - [Release notes](https://github.com/webpack/loader-utils/releases) - [Changelog](https://github.com/webpack/loader-utils/blob/v2.0.4/CHANGELOG.md) - [Commits](https://github.com/webpack/loader-utils/compare/v2.0.3...v2.0.4) --- updated-dependencies: - dependency-name: loader-utils dependency-type: indirect ... Signed-off-by: dependabot[bot] --- js/package-lock.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/js/package-lock.json b/js/package-lock.json index 459a606..4c2069c 100644 --- a/js/package-lock.json +++ b/js/package-lock.json @@ -1384,9 +1384,9 @@ } }, "node_modules/loader-utils": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-2.0.3.tgz", - "integrity": "sha512-THWqIsn8QRnvLl0shHYVBN9syumU8pYWEHPTmkiVGd+7K5eFNVSY6AJhRvgGF70gg1Dz+l/k8WicvFCxdEs60A==", + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-2.0.4.tgz", + "integrity": "sha512-xXqpXoINfFhgua9xiqD8fPFHgkoq1mmmpE92WlDbm9rNRd/EbRb+Gqf908T2DMfuHjjJlksiK2RbHVOdD/MqSw==", "dependencies": { "big.js": "^5.2.2", "emojis-list": "^3.0.0", @@ -3770,9 +3770,9 @@ "integrity": "sha512-3R/1M+yS3j5ou80Me59j7F9IMs4PXs3VqRrm0TU3AbKPxlmpoY1TNscJV/oGJXo8qCatFGTfDbY6W6ipGOYXfg==" }, "loader-utils": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-2.0.3.tgz", - "integrity": "sha512-THWqIsn8QRnvLl0shHYVBN9syumU8pYWEHPTmkiVGd+7K5eFNVSY6AJhRvgGF70gg1Dz+l/k8WicvFCxdEs60A==", + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-2.0.4.tgz", + "integrity": "sha512-xXqpXoINfFhgua9xiqD8fPFHgkoq1mmmpE92WlDbm9rNRd/EbRb+Gqf908T2DMfuHjjJlksiK2RbHVOdD/MqSw==", "requires": { "big.js": "^5.2.2", "emojis-list": "^3.0.0", From c7263e39bc1b1523016654c34ef4e984129570f5 Mon Sep 17 00:00:00 2001 From: "Soren L. Hansen" Date: Fri, 25 Nov 2022 14:01:34 -0800 Subject: [PATCH 09/10] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 39404e8..3951c7d 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ [![GitHub release](http://img.shields.io/github/release/sorenisanerd/gotty.svg?style=flat-square)][release] [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)][license] + [![Maintainer streaming](https://twitch-status.soren.tools/sorencodes)][twitch] [release]: https://github.com/sorenisanerd/gotty/releases From a7160c58c812ded0cac6c361ea7d0133bdce128a Mon Sep 17 00:00:00 2001 From: "Soren L. Hansen" Date: Fri, 25 Nov 2022 14:02:39 -0800 Subject: [PATCH 10/10] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 3951c7d..a154bc8 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,7 @@ [![GitHub release](http://img.shields.io/github/release/sorenisanerd/gotty.svg?style=flat-square)][release] [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)][license] - -[![Maintainer streaming](https://twitch-status.soren.tools/sorencodes)][twitch] +[![Maintainer streaming](https://twitch-status.soren.tools/sorencodes/)][twitch] [release]: https://github.com/sorenisanerd/gotty/releases [license]: https://github.com/sorenisanerd/gotty/blob/master/LICENSE