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 <mihai@tailscale.com>
This commit is contained in:
Mihai Parparita 2022-09-12 11:51:19 -07:00 committed by Mihai Parparita
parent 11fcc3a7b0
commit c312e0d264
2 changed files with 16 additions and 14 deletions

View File

@ -47,6 +47,7 @@ declare global {
stateStorage?: IPNStateStorage stateStorage?: IPNStateStorage
authKey?: string authKey?: string
controlURL?: string controlURL?: string
hostname?: string
} }
type IPNCallbacks = { type IPNCallbacks = {

View File

@ -61,26 +61,30 @@ func main() {
func newIPN(jsConfig js.Value) map[string]any { func newIPN(jsConfig js.Value) map[string]any {
netns.SetEnabled(false) netns.SetEnabled(false)
jsStateStorage := jsConfig.Get("stateStorage")
var store ipn.StateStore var store ipn.StateStore
if jsStateStorage.IsUndefined() { if jsStateStorage := jsConfig.Get("stateStorage"); !jsStateStorage.IsUndefined() {
store = new(mem.Store)
} else {
store = &jsStateStore{jsStateStorage} store = &jsStateStore{jsStateStorage}
} else {
store = new(mem.Store)
} }
jsControlURL := jsConfig.Get("controlURL")
controlURL := ControlURL controlURL := ControlURL
if jsControlURL.Type() == js.TypeString { if jsControlURL := jsConfig.Get("controlURL"); jsControlURL.Type() == js.TypeString {
controlURL = jsControlURL.String() controlURL = jsControlURL.String()
} }
jsAuthKey := jsConfig.Get("authKey")
var authKey string var authKey string
if jsAuthKey.Type() == js.TypeString { if jsAuthKey := jsConfig.Get("authKey"); jsAuthKey.Type() == js.TypeString {
authKey = jsAuthKey.String() authKey = jsAuthKey.String()
} }
var hostname string
if jsHostname := jsConfig.Get("hostname"); jsHostname.Type() == js.TypeString {
hostname = jsHostname.String()
} else {
hostname = generateHostname()
}
lpc := getOrCreateLogPolicyConfig(store) lpc := getOrCreateLogPolicyConfig(store)
c := logtail.Config{ c := logtail.Config{
Collection: lpc.Collection, Collection: lpc.Collection,
@ -136,6 +140,7 @@ func newIPN(jsConfig js.Value) map[string]any {
lb: lb, lb: lb,
controlURL: controlURL, controlURL: controlURL,
authKey: authKey, authKey: authKey,
hostname: hostname,
} }
return map[string]any{ return map[string]any{
@ -196,6 +201,7 @@ type jsIPN struct {
lb *ipnlocal.LocalBackend lb *ipnlocal.LocalBackend
controlURL string controlURL string
authKey string authKey string
hostname string
} }
var jsIPNState = map[ipn.State]string{ var jsIPNState = map[ipn.State]string{
@ -284,7 +290,7 @@ func (i *jsIPN) run(jsCallbacks js.Value) {
RouteAll: false, RouteAll: false,
AllowSingleHosts: true, AllowSingleHosts: true,
WantRunning: true, WantRunning: true,
Hostname: generateHostname(), Hostname: i.hostname,
}, },
AuthKey: i.authKey, AuthKey: i.authKey,
}) })
@ -357,9 +363,6 @@ func (s *jsSSHSession) Run() {
onDone := s.termConfig.Get("onDone") onDone := s.termConfig.Get("onDone")
defer onDone.Invoke() defer onDone.Invoke()
write := func(s string) {
writeFn.Invoke(s)
}
writeError := func(label string, err error) { writeError := func(label string, err error) {
writeErrorFn.Invoke(fmt.Sprintf("%s Error: %v\r\n", label, err)) writeErrorFn.Invoke(fmt.Sprintf("%s Error: %v\r\n", label, err))
} }
@ -384,7 +387,6 @@ func (s *jsSSHSession) Run() {
return return
} }
defer sshConn.Close() defer sshConn.Close()
write("SSH Connected\r\n")
sshClient := ssh.NewClient(sshConn, nil, nil) sshClient := ssh.NewClient(sshConn, nil, nil)
defer sshClient.Close() defer sshClient.Close()
@ -395,7 +397,6 @@ func (s *jsSSHSession) Run() {
return return
} }
s.session = session s.session = session
write("Session Established\r\n")
defer session.Close() defer session.Close()
stdin, err := session.StdinPipe() stdin, err := session.StdinPipe()