From c312e0d264781817200e559bc7cd12498a6f88c0 Mon Sep 17 00:00:00 2001 From: Mihai Parparita Date: Mon, 12 Sep 2022 11:51:19 -0700 Subject: [PATCH] cmd/tsconnect: allow hostname to be specified The auto-generated hostname is nice as a default, but there are cases where the client has a more specific name that it can generate. Signed-off-by: Mihai Parparita --- cmd/tsconnect/src/types/wasm_js.d.ts | 1 + cmd/tsconnect/wasm/wasm_js.go | 29 ++++++++++++++-------------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/cmd/tsconnect/src/types/wasm_js.d.ts b/cmd/tsconnect/src/types/wasm_js.d.ts index b7ae9c793..c9dd84230 100644 --- a/cmd/tsconnect/src/types/wasm_js.d.ts +++ b/cmd/tsconnect/src/types/wasm_js.d.ts @@ -47,6 +47,7 @@ declare global { stateStorage?: IPNStateStorage authKey?: string controlURL?: string + hostname?: string } type IPNCallbacks = { diff --git a/cmd/tsconnect/wasm/wasm_js.go b/cmd/tsconnect/wasm/wasm_js.go index 0873b6c83..fd5a46da4 100644 --- a/cmd/tsconnect/wasm/wasm_js.go +++ b/cmd/tsconnect/wasm/wasm_js.go @@ -61,26 +61,30 @@ func main() { func newIPN(jsConfig js.Value) map[string]any { netns.SetEnabled(false) - jsStateStorage := jsConfig.Get("stateStorage") var store ipn.StateStore - if jsStateStorage.IsUndefined() { - store = new(mem.Store) - } else { + if jsStateStorage := jsConfig.Get("stateStorage"); !jsStateStorage.IsUndefined() { store = &jsStateStore{jsStateStorage} + } else { + store = new(mem.Store) } - jsControlURL := jsConfig.Get("controlURL") controlURL := ControlURL - if jsControlURL.Type() == js.TypeString { + if jsControlURL := jsConfig.Get("controlURL"); jsControlURL.Type() == js.TypeString { controlURL = jsControlURL.String() } - jsAuthKey := jsConfig.Get("authKey") var authKey string - if jsAuthKey.Type() == js.TypeString { + if jsAuthKey := jsConfig.Get("authKey"); jsAuthKey.Type() == js.TypeString { authKey = jsAuthKey.String() } + var hostname string + if jsHostname := jsConfig.Get("hostname"); jsHostname.Type() == js.TypeString { + hostname = jsHostname.String() + } else { + hostname = generateHostname() + } + lpc := getOrCreateLogPolicyConfig(store) c := logtail.Config{ Collection: lpc.Collection, @@ -136,6 +140,7 @@ func newIPN(jsConfig js.Value) map[string]any { lb: lb, controlURL: controlURL, authKey: authKey, + hostname: hostname, } return map[string]any{ @@ -196,6 +201,7 @@ type jsIPN struct { lb *ipnlocal.LocalBackend controlURL string authKey string + hostname string } var jsIPNState = map[ipn.State]string{ @@ -284,7 +290,7 @@ func (i *jsIPN) run(jsCallbacks js.Value) { RouteAll: false, AllowSingleHosts: true, WantRunning: true, - Hostname: generateHostname(), + Hostname: i.hostname, }, AuthKey: i.authKey, }) @@ -357,9 +363,6 @@ func (s *jsSSHSession) Run() { onDone := s.termConfig.Get("onDone") defer onDone.Invoke() - write := func(s string) { - writeFn.Invoke(s) - } writeError := func(label string, err error) { writeErrorFn.Invoke(fmt.Sprintf("%s Error: %v\r\n", label, err)) } @@ -384,7 +387,6 @@ func (s *jsSSHSession) Run() { return } defer sshConn.Close() - write("SSH Connected\r\n") sshClient := ssh.NewClient(sshConn, nil, nil) defer sshClient.Close() @@ -395,7 +397,6 @@ func (s *jsSSHSession) Run() { return } s.session = session - write("Session Established\r\n") defer session.Close() stdin, err := session.StdinPipe()