Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - allow passing environment variables, user-name and password #166

Open
wants to merge 1 commit into
base: release-1.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ By default, GoTTY starts a web server at port 8080. Open the URL on your web bro
--max-connection "0" Set the maximum number of simultaneous connections (0 to disable)
--once Accept only one client and exit on disconnection [$GOTTY_ONCE]
--permit-arguments Permit clients to send command line arguments in URL (e.g. http://example.com:8080/?arg=AAA&arg=BBB) [$GOTTY_PERMIT_ARGUMENTS]
--permit-environment Permit clients to add (or replace) environment variables in URL (e.g. http://example.com:8080/?env=GOTTY=running&env=TMUXsession=name)
--enable-auth-args Reuse the http basic-auth user-name and password to replace all '%u' and '%p' in arguments
--close-signal "1" Signal sent to the command process when gotty close it (default: SIGHUP) [$GOTTY_CLOSE_SIGNAL]
--config "~/.gotty" Config file path [$GOTTY_CONFIG]
--version, -v print the version
Expand Down
38 changes: 38 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Options struct {
Port string `hcl:"port"`
PermitWrite bool `hcl:"permit_write"`
EnableBasicAuth bool `hcl:"enable_basic_auth"`
EnableAuthArgs bool `hcl:"enable_auth_args"`
Credential string `hcl:"credential"`
EnableRandomUrl bool `hcl:"enable_random_url"`
RandomUrlLength int `hcl:"random_url_length"`
Expand All @@ -73,6 +74,7 @@ type Options struct {
Once bool `hcl:"once"`
Timeout int `hcl:"timeout"`
PermitArguments bool `hcl:"permit_arguments"`
PermitEnvironment bool `hcl:"permit_environment"`
CloseSignal int `hcl:"close_signal"`
Preferences HtermPrefernces `hcl:"preferences"`
RawPreferences map[string]interface{} `hcl:"preferences"`
Expand All @@ -87,6 +89,8 @@ var DefaultOptions = Options{
Port: "8080",
PermitWrite: false,
EnableBasicAuth: false,
EnableAuthArgs: false,
PermitEnvironment: false,
Credential: "",
EnableRandomUrl: false,
RandomUrlLength: 8,
Expand Down Expand Up @@ -368,6 +372,39 @@ func (app *App) handleWS(w http.ResponseWriter, r *http.Request) {
argv = append(argv, params...)
}
}
env := os.Environ()
if app.options.PermitEnvironment {
if init.Arguments == "" {
init.Arguments = "?"
}
query, err := url.Parse(init.Arguments)
if err != nil {
log.Print("Failed to parse arguments for environment")
conn.Close()
return
}
envarg := query.Query()["env"]
if len(envarg) != 0 {
env = append(env, envarg...)
}
}
if app.options.EnableAuthArgs {
creds := strings.SplitN(init.AuthToken, ":", 2)
if len(creds) != 2 {
log.Print("Failed to parse authentication details")
conn.Close()
return
}
username, password := creds[0], creds[1]
for idx, arg := range argv {
if arg == "%u" {
argv[idx] = username
}
if arg == "%p" {
argv[idx] = password
}
}
}

app.server.StartRoutine()

Expand All @@ -383,6 +420,7 @@ func (app *App) handleWS(w http.ResponseWriter, r *http.Request) {
}

cmd := exec.Command(app.command[0], argv...)
cmd.Env = env
ptyIo, err := pty.Start(cmd)
if err != nil {
log.Print("Failed to execute command")
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func main() {
flag{"close-signal", "", "Signal sent to the command process when gotty close it (default: SIGHUP)"},
flag{"width", "", "Static width of the screen, 0(default) means dynamically resize"},
flag{"height", "", "Static height of the screen, 0(default) means dynamically resize"},
flag{"permit-environment", "", "Permit clients to send environment variables in URL (e.g. http://example.com:8080/?env=ZZZ&env=YYY)"},
flag{"enable-auth-args", "", "Use in combination with %u and %p as arguments that will be replaced with the username and password used with HTTP auth"},
}

mappingHint := map[string]string{
Expand Down