@@ -35,21 +35,23 @@ func configSSHCmd() *cobra.Command {
35
35
var (
36
36
configpath string
37
37
remove = false
38
+ p2p = false
38
39
)
39
40
40
41
cmd := & cobra.Command {
41
42
Use : "config-ssh" ,
42
43
Short : "Configure SSH to access Coder environments" ,
43
44
Long : "Inject the proper OpenSSH configuration into your local SSH config file." ,
44
- RunE : configSSH (& configpath , & remove ),
45
+ RunE : configSSH (& configpath , & remove , & p2p ),
45
46
}
46
47
cmd .Flags ().StringVar (& configpath , "filepath" , filepath .Join ("~" , ".ssh" , "config" ), "override the default path of your ssh config file" )
47
48
cmd .Flags ().BoolVar (& remove , "remove" , false , "remove the auto-generated Coder ssh config" )
49
+ cmd .Flags ().BoolVar (& p2p , "p2p" , false , "(experimental) uses coder tunnel to proxy ssh connection" )
48
50
49
51
return cmd
50
52
}
51
53
52
- func configSSH (configpath * string , remove * bool ) func (cmd * cobra.Command , _ []string ) error {
54
+ func configSSH (configpath * string , remove * bool , p2p * bool ) func (cmd * cobra.Command , _ []string ) error {
53
55
return func (cmd * cobra.Command , _ []string ) error {
54
56
ctx := cmd .Context ()
55
57
usr , err := user .Current ()
@@ -113,7 +115,7 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st
113
115
return xerrors .New ("SSH is disabled or not available for any environments in your Coder deployment." )
114
116
}
115
117
116
- newConfig := makeNewConfigs (user .Username , envsWithProviders , privateKeyFilepath )
118
+ newConfig := makeNewConfigs (user .Username , envsWithProviders , privateKeyFilepath , * p2p )
117
119
118
120
err = os .MkdirAll (filepath .Dir (* configpath ), os .ModePerm )
119
121
if err != nil {
@@ -174,7 +176,7 @@ func writeSSHKey(ctx context.Context, client coder.Client, privateKeyPath string
174
176
return ioutil .WriteFile (privateKeyPath , []byte (key .PrivateKey ), 0600 )
175
177
}
176
178
177
- func makeNewConfigs (userName string , envs []coderutil.EnvWithWorkspaceProvider , privateKeyFilepath string ) string {
179
+ func makeNewConfigs (userName string , envs []coderutil.EnvWithWorkspaceProvider , privateKeyFilepath string , p2p bool ) string {
178
180
newConfig := fmt .Sprintf ("\n %s\n %s\n \n " , sshStartToken , sshStartMessage )
179
181
180
182
sort .Slice (envs , func (i , j int ) bool { return envs [i ].Env .Name < envs [j ].Env .Name })
@@ -192,14 +194,27 @@ func makeNewConfigs(userName string, envs []coderutil.EnvWithWorkspaceProvider,
192
194
clog .LogWarn ("invalid access url" , clog .Causef ("malformed url: %q" , env .WorkspaceProvider .EnvproxyAccessURL ))
193
195
continue
194
196
}
195
- newConfig += makeSSHConfig (u .Host , userName , env .Env .Name , privateKeyFilepath )
197
+ newConfig += makeSSHConfig (u .Host , userName , env .Env .Name , privateKeyFilepath , p2p )
196
198
}
197
199
newConfig += fmt .Sprintf ("\n %s\n " , sshEndToken )
198
200
199
201
return newConfig
200
202
}
201
203
202
- func makeSSHConfig (host , userName , envName , privateKeyFilepath string ) string {
204
+ func makeSSHConfig (host , userName , envName , privateKeyFilepath string , p2p bool ) string {
205
+ if p2p {
206
+ return fmt .Sprintf (
207
+ `Host coder.%s
208
+ HostName localhost
209
+ ProxyCommand coder tunnel %s 22 stdio
210
+ StrictHostKeyChecking no
211
+ ConnectTimeout=0
212
+ IdentityFile="%s"
213
+ ServerAliveInterval 60
214
+ ServerAliveCountMax 3
215
+ ` , envName , envName , privateKeyFilepath )
216
+ }
217
+
203
218
return fmt .Sprintf (
204
219
`Host coder.%s
205
220
HostName %s
0 commit comments