MCPcopy Index your code
hub / github.com/cli/cli / StartSSHServerWithOptions

Method StartSSHServerWithOptions

internal/codespaces/rpc/invoker.go:216–249  ·  view source on GitHub ↗

Starts a remote SSH server to allow the user to connect to the codespace via SSH

(ctx context.Context, options StartSSHServerOptions)

Source from the content-addressed store, hash-verified

214
215// Starts a remote SSH server to allow the user to connect to the codespace via SSH
216func (i *invoker) StartSSHServerWithOptions(ctx context.Context, options StartSSHServerOptions) (int, string, error) {
217 ctx = i.appendMetadata(ctx)
218 ctx, cancel := context.WithTimeout(ctx, requestTimeout)
219 defer cancel()
220
221 userPublicKey := ""
222 if options.UserPublicKeyFile != "" {
223 publicKeyBytes, err := os.ReadFile(options.UserPublicKeyFile)
224 if err != nil {
225 return 0, "", fmt.Errorf("failed to read public key file: %w", err)
226 }
227
228 userPublicKey = strings.TrimSpace(string(publicKeyBytes))
229 }
230
231 response, err := i.sshClient.StartRemoteServerAsync(ctx, &ssh.StartRemoteServerRequest{UserPublicKey: userPublicKey})
232 if err != nil {
233 return 0, "", fmt.Errorf("failed to invoke SSH RPC: %w", err)
234 }
235
236 if !response.Result {
237 return 0, "", fmt.Errorf("failed to start SSH server: %s", response.Message)
238 }
239
240 port, err := strconv.Atoi(response.ServerPort)
241 if err != nil {
242 return 0, "", fmt.Errorf("failed to parse SSH server port: %w", err)
243 }
244
245 if !isUsernameValid(response.User) {
246 return 0, "", fmt.Errorf("invalid username: %s", response.User)
247 }
248 return port, response.User, nil
249}
250
251func listenTCP() (*net.TCPListener, error) {
252 // We will end up using this same address to connect, so specify the IP also or the connect will fail

Callers 1

StartSSHServerMethod · 0.95

Calls 4

appendMetadataMethod · 0.95
isUsernameValidFunction · 0.85
ErrorfMethod · 0.65

Tested by

no test coverage detected