Skip to content

Commit

Permalink
made connection error limit into a configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
zephinzer committed Jun 25, 2021
1 parent b1bd5af commit 8334f9e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Configurations can be set via flags or environment variables. To view available
| Allowed hostnames | `--allowed-hostnames` | `ALLOWED_HOSTNAMES` | `"localhost"` | Comma delimited list of hostnames that are allowed to connect to the websocket |
| Arguments | `--arguments` | `ARGUMENTS` | `"-l"` | Comma delimited list of arguments that should be passed to the target binary |
| Command | `--command` | `COMMAND` | `"/bin/bash"` | Absolute path to the binary to run |
| Connection error limit | `--connection-error-limit` | `CONNECTION_ERROR_LIMIT` | `10` | Number of times a connection should be re-attempted by the server to the XTerm.js frontend before the connection is considered dead and shut down |
| Maximum buffer size in bytes | `--max-buffer-size-bytes` | `MAX_BUFFER_SIZE_BYTES` | `512` | Maximum length of input from the browser terminal |
| Log format | `--log-format` | `LOG_FORMAT` | `"text"` | Format with which to output logs, one of `"json"` or `"text"` |
| Log level | `--log-level` | `LOG_LEVEL` | `"debug"` | Minimum level of logs to output, one of `"trace"`, `"debug"`, `"info"`, `"warn"`, `"error"` |
Expand Down
5 changes: 5 additions & 0 deletions cmd/cloudshell/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ var conf = config.Map{
Usage: "absolute path to command to run",
Shorthand: "t",
},
"connection-error-limit": &config.Int{
Default: 10,
Usage: "number of times a connection should be re-attempted before it's considered dead",
Shorthand: "l",
},
"max-buffer-size-bytes": &config.Int{
Default: 512,
Usage: "maximum length of input from terminal",
Expand Down
9 changes: 6 additions & 3 deletions cmd/cloudshell/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func runE(_ *cobra.Command, _ []string) error {

// debug stuff
command := conf.GetString("command")
connectionErrorLimit := conf.GetInt("connection-error-limit")
arguments := conf.GetStringSlice("arguments")
allowedHostnames := conf.GetStringSlice("allowed-hostnames")
maxBufferSizeBytes := conf.GetInt("max-buffer-size-bytes")
Expand All @@ -62,6 +63,7 @@ func runE(_ *cobra.Command, _ []string) error {
log.Infof("arguments : ['%s']", strings.Join(arguments, "', '"))

log.Infof("allowed hosts : ['%s']", strings.Join(allowedHostnames, "', '"))
log.Infof("connection error limit: %v", connectionErrorLimit)
log.Infof("max buffer size : %v bytes", maxBufferSizeBytes)
log.Infof("server address : '%s' ", serverAddress)
log.Infof("server port : %v", serverPort)
Expand All @@ -76,9 +78,10 @@ func runE(_ *cobra.Command, _ []string) error {

// this is the endpoint for xterm.js to connect to
xtermjsHandlerOptions := xtermjs.HandlerOpts{
AllowedHostnames: allowedHostnames,
Arguments: arguments,
Command: command,
AllowedHostnames: allowedHostnames,
Arguments: arguments,
Command: command,
ConnectionErrorLimit: connectionErrorLimit,
CreateLogger: func(connectionUUID string, r *http.Request) xtermjs.Logger {
createRequestLog(r, map[string]interface{}{"connection_uuid": connectionUUID}).Infof("created logger for connection '%s'", connectionUUID)
return createRequestLog(nil, map[string]interface{}{"connection_uuid": connectionUUID})
Expand Down
13 changes: 9 additions & 4 deletions pkg/xtermjs/handler_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ import (
"github.com/gorilla/websocket"
)

// ConnectionErrorLimit defines the number of consecutive errors that can happen
// before a connection is considered unusable
const ConnectionErrorLimit = 10
const DefaultConnectionErrorLimit = 10

type HandlerOpts struct {
// AllowedHostnames is a list of strings which will be matched to the client
Expand All @@ -28,6 +26,9 @@ type HandlerOpts struct {
Arguments []string
// Command is the path to the binary we should create a TTY for
Command string
// ConnectionErrorLimit defines the number of consecutive errors that can happen
// before a connection is considered unusable
ConnectionErrorLimit int
// CreateLogger when specified should return a logger that the handler will use.
// The string argument being passed in will be a unique identifier for the
// current connection. When not specified, logs will be sent to stdout
Expand All @@ -37,6 +38,10 @@ type HandlerOpts struct {

func GetHandler(opts HandlerOpts) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
connectionErrorLimit := opts.ConnectionErrorLimit
if connectionErrorLimit < 0 {
connectionErrorLimit = DefaultConnectionErrorLimit
}
maxBufferSizeBytes := opts.MaxBufferSizeBytes

connectionUUID, err := uuid.NewUUID()
Expand Down Expand Up @@ -98,7 +103,7 @@ func GetHandler(opts HandlerOpts) func(http.ResponseWriter, *http.Request) {
errorCounter := 0
for {
// consider the connection closed/errored out
if errorCounter > ConnectionErrorLimit {
if errorCounter > connectionErrorLimit {
waiter.Done()
break
}
Expand Down

0 comments on commit 8334f9e

Please sign in to comment.