From 49221c3248e71e554c7a392b854bea9dec8fccb9 Mon Sep 17 00:00:00 2001 From: yzhang3 Date: Wed, 10 Aug 2016 13:58:39 +0800 Subject: [PATCH 1/4] add option for max connection --- .gotty | 3 +++ app/app.go | 15 +++++++++++++++ app/client_context.go | 2 ++ 3 files changed, 20 insertions(+) diff --git a/.gotty b/.gotty index 51d6e22d..a3c89a28 100644 --- a/.gotty +++ b/.gotty @@ -54,6 +54,9 @@ // To enable reconnection, set `true` to `enable_reconnect` // reconnect_time = false +// [int] Maximum connection to gotty, 0(default) means no limit. +// max_connection = 0 + // [bool] Accept only one client and exit gotty once the client exits // once = false diff --git a/app/app.go b/app/app.go index 60722cc6..c579e3e3 100644 --- a/app/app.go +++ b/app/app.go @@ -28,6 +28,10 @@ import ( "github.com/yudai/umutex" ) +var ( + connection int = 0 +) + type InitMessage struct { Arguments string `json:"Arguments,omitempty"` AuthToken string `json:"AuthToken,omitempty"` @@ -62,6 +66,7 @@ type Options struct { TitleFormat string `hcl:"title_format"` EnableReconnect bool `hcl:"enable_reconnect"` ReconnectTime int `hcl:"reconnect_time"` + MaxConnection int `hcl:"max_connection"` Once bool `hcl:"once"` PermitArguments bool `hcl:"permit_arguments"` CloseSignal int `hcl:"close_signal"` @@ -88,6 +93,7 @@ var DefaultOptions = Options{ TitleFormat: "GoTTY - {{ .Command }} ({{ .Hostname }})", EnableReconnect: false, ReconnectTime: 10, + MaxConnection: 0, Once: false, CloseSignal: 1, // syscall.SIGHUP Preferences: HtermPrefernces{}, @@ -274,6 +280,12 @@ func (app *App) makeServer(addr string, handler *http.Handler) (*http.Server, er } func (app *App) handleWS(w http.ResponseWriter, r *http.Request) { + if app.options.MaxConnection != 0 { + if connection >= app.options.MaxConnection { + log.Printf("reached max connection: %d", app.options.MaxConnection) + return + } + } log.Printf("New client connected: %s", r.RemoteAddr) if r.Method != "GET" { @@ -344,6 +356,9 @@ func (app *App) handleWS(w http.ResponseWriter, r *http.Request) { } log.Printf("Command is running for client %s with PID %d (args=%q)", r.RemoteAddr, cmd.Process.Pid, strings.Join(argv, " ")) + connection++ + log.Printf("Connection: %d/%d\n", connection, app.options.MaxConnection) + context := &clientContext{ app: app, request: r, diff --git a/app/client_context.go b/app/client_context.go index d4c2f29d..fc37aad1 100644 --- a/app/client_context.go +++ b/app/client_context.go @@ -79,7 +79,9 @@ func (context *clientContext) goHandleClient() { context.command.Wait() context.connection.Close() + connection-- log.Printf("Connection closed: %s", context.request.RemoteAddr) + log.Printf("Connection: %d/%d\n", connection, context.app.options.MaxConnection) }() } From da63580a3090f3176165675c1c05e5f6d083722b Mon Sep 17 00:00:00 2001 From: yzhang3 Date: Wed, 10 Aug 2016 14:21:58 +0800 Subject: [PATCH 2/4] add option in command --- main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/main.go b/main.go index 20f3600d..7d141982 100644 --- a/main.go +++ b/main.go @@ -33,6 +33,7 @@ func main() { flag{"title-format", "", "Title format of browser window"}, flag{"reconnect", "", "Enable reconnection"}, flag{"reconnect-time", "", "Time to reconnect"}, + flag{"max-connection", "", "Maximum connection to gotty, 0(default) means no limit"}, flag{"once", "", "Accept only one client and exit on disconnection"}, flag{"permit-arguments", "", "Permit clients to send command line arguments in URL (e.g. http://example.com:8080/?arg=AAA&arg=BBB)"}, flag{"close-signal", "", "Signal sent to the command process when gotty close it (default: SIGHUP)"}, From bbd1453ca929ff11cb10612b6c9adbeea58dd655 Mon Sep 17 00:00:00 2001 From: yzhang3 Date: Fri, 12 Aug 2016 13:54:29 +0800 Subject: [PATCH 3/4] format log; move global var into App --- app/app.go | 19 +++++++++++-------- app/client_context.go | 8 ++++++-- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/app/app.go b/app/app.go index c579e3e3..9efc850d 100644 --- a/app/app.go +++ b/app/app.go @@ -28,10 +28,6 @@ import ( "github.com/yudai/umutex" ) -var ( - connection int = 0 -) - type InitMessage struct { Arguments string `json:"Arguments,omitempty"` AuthToken string `json:"AuthToken,omitempty"` @@ -47,6 +43,8 @@ type App struct { titleTemplate *template.Template onceMutex *umutex.UnblockingMutex + + connections int } type Options struct { @@ -281,8 +279,8 @@ func (app *App) makeServer(addr string, handler *http.Handler) (*http.Server, er func (app *App) handleWS(w http.ResponseWriter, r *http.Request) { if app.options.MaxConnection != 0 { - if connection >= app.options.MaxConnection { - log.Printf("reached max connection: %d", app.options.MaxConnection) + if app.connections >= app.options.MaxConnection { + log.Printf("Reached max connection: %d", app.options.MaxConnection) return } } @@ -354,10 +352,15 @@ func (app *App) handleWS(w http.ResponseWriter, r *http.Request) { log.Print("Failed to execute command") return } + log.Printf("Command is running for client %s with PID %d (args=%q)", r.RemoteAddr, cmd.Process.Pid, strings.Join(argv, " ")) - connection++ - log.Printf("Connection: %d/%d\n", connection, app.options.MaxConnection) + app.connections++ + if app.options.MaxConnection != 0 { + log.Printf("Connections: %d/%d", app.connections, app.options.MaxConnection) + } else { + log.Printf("Connections: %d", app.connections) + } context := &clientContext{ app: app, diff --git a/app/client_context.go b/app/client_context.go index fc37aad1..ab147f7d 100644 --- a/app/client_context.go +++ b/app/client_context.go @@ -79,9 +79,13 @@ func (context *clientContext) goHandleClient() { context.command.Wait() context.connection.Close() - connection-- + context.app.connections-- log.Printf("Connection closed: %s", context.request.RemoteAddr) - log.Printf("Connection: %d/%d\n", connection, context.app.options.MaxConnection) + if context.app.options.MaxConnection != 0 { + log.Printf("Connections: %d/%d", context.app.connections, context.app.options.MaxConnection) + } else { + log.Printf("Connections: %d", context.app.connections) + } }() } From 527938050bec08999a9eb2d96c10dd4cd56bf367 Mon Sep 17 00:00:00 2001 From: yzhang3 Date: Fri, 12 Aug 2016 15:46:45 +0800 Subject: [PATCH 4/4] merge log --- app/app.go | 8 ++++---- app/client_context.go | 7 ++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/app.go b/app/app.go index 9efc850d..335c19bb 100644 --- a/app/app.go +++ b/app/app.go @@ -353,13 +353,13 @@ func (app *App) handleWS(w http.ResponseWriter, r *http.Request) { return } - log.Printf("Command is running for client %s with PID %d (args=%q)", r.RemoteAddr, cmd.Process.Pid, strings.Join(argv, " ")) - app.connections++ if app.options.MaxConnection != 0 { - log.Printf("Connections: %d/%d", app.connections, app.options.MaxConnection) + log.Printf("Command is running for client %s with PID %d (args=%q), connections: %d/%d", + r.RemoteAddr, cmd.Process.Pid, strings.Join(argv, " "), app.connections, app.options.MaxConnection) } else { - log.Printf("Connections: %d", app.connections) + log.Printf("Command is running for client %s with PID %d (args=%q), connections: %d", + r.RemoteAddr, cmd.Process.Pid, strings.Join(argv, " "), app.connections) } context := &clientContext{ diff --git a/app/client_context.go b/app/client_context.go index ab147f7d..3a3b27ae 100644 --- a/app/client_context.go +++ b/app/client_context.go @@ -80,11 +80,12 @@ func (context *clientContext) goHandleClient() { context.command.Wait() context.connection.Close() context.app.connections-- - log.Printf("Connection closed: %s", context.request.RemoteAddr) if context.app.options.MaxConnection != 0 { - log.Printf("Connections: %d/%d", context.app.connections, context.app.options.MaxConnection) + log.Printf("Connection closed: %s, connections: %d/%d", + context.request.RemoteAddr, context.app.connections, context.app.options.MaxConnection) } else { - log.Printf("Connections: %d", context.app.connections) + log.Printf("Connection closed: %s, connections: %d", + context.request.RemoteAddr, context.app.connections) } }() }