MCPcopy
hub / github.com/ContainerSSH/ContainerSSH / shellRequestReply

Function shellRequestReply

internal/sshserver/Server_test.go:410–466  ·  view source on GitHub ↗

endregion region Helper

(
	host string,
	user string,
	authMethod ssh.AuthMethod,
	hostKey []byte,
	request []byte,
	onShell chan struct{},
	canSendResponse chan struct{},
)

Source from the content-addressed store, hash-verified

408//region Helper
409
410func shellRequestReply(
411 host string,
412 user string,
413 authMethod ssh.AuthMethod,
414 hostKey []byte,
415 request []byte,
416 onShell chan struct{},
417 canSendResponse chan struct{},
418) (reply []byte, exitStatus int, err error) {
419 sshConfig := &ssh.ClientConfig{
420 User: user,
421 Auth: []ssh.AuthMethod{authMethod},
422 }
423 sshConfig.HostKeyCallback = func(hostname string, remote net.Addr, key ssh.PublicKey) error {
424 if bytes.Equal(key.Marshal(), hostKey) {
425 return nil
426 }
427 return fmt.Errorf("invalid host")
428 }
429 sshConnection, err := ssh.Dial("tcp", host, sshConfig)
430 if err != nil {
431 return nil, -1, fmt.Errorf("handshake failed (%w)", err)
432 }
433 defer func() {
434 if sshConnection != nil {
435 _ = sshConnection.Close()
436 }
437 }()
438
439 session, err := sshConnection.NewSession()
440 if err != nil {
441 return nil, -1, fmt.Errorf("new session failed (%w)", err)
442 }
443
444 stdin, stdout, err := createPipe(session)
445 if err != nil {
446 return nil, -1, err
447 }
448
449 if err := session.Setenv("TERM", "xterm"); err != nil {
450 return nil, -1, err
451 }
452
453 if err := session.Shell(); err != nil {
454 return nil, -1, fmt.Errorf("failed to request shell (%w)", err)
455 }
456 if onShell != nil {
457 onShell <- struct{}{}
458 }
459 if canSendResponse != nil {
460 <-canSendResponse
461 }
462 if _, err := stdin.Write(request); err != nil {
463 return nil, -1, fmt.Errorf("failed to write to shell (%w)", err)
464 }
465 return read(stdout, stdin, session)
466}
467

Callers 3

TestSessionSuccessFunction · 0.85
TestSessionErrorFunction · 0.85
TestPubKeyFunction · 0.85

Calls 7

createPipeFunction · 0.85
readFunction · 0.85
MarshalMethod · 0.65
ErrorfMethod · 0.65
CloseMethod · 0.65
ShellMethod · 0.65
WriteMethod · 0.65

Tested by

no test coverage detected