|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | + |
| 10 | + "cdr.dev/slog" |
| 11 | + "github.com/coder/coder/peer" |
| 12 | + "github.com/coder/coder/peerbroker" |
| 13 | + "github.com/coder/retry" |
| 14 | + |
| 15 | + "github.com/gliderlabs/ssh" |
| 16 | + gossh "golang.org/x/crypto/ssh" |
| 17 | +) |
| 18 | + |
| 19 | +type Options struct { |
| 20 | + Logger slog.Logger |
| 21 | +} |
| 22 | + |
| 23 | +type Dialer func(ctx context.Context) (*peerbroker.Listener, error) |
| 24 | + |
| 25 | +func Server(dialer Dialer, options *Options) io.Closer { |
| 26 | + ctx, cancelFunc := context.WithCancel(context.Background()) |
| 27 | + s := &server{ |
| 28 | + clientDialer: dialer, |
| 29 | + options: options, |
| 30 | + closeCancel: cancelFunc, |
| 31 | + } |
| 32 | + s.init(ctx) |
| 33 | + return s |
| 34 | +} |
| 35 | + |
| 36 | +type server struct { |
| 37 | + clientDialer Dialer |
| 38 | + options *Options |
| 39 | + |
| 40 | + closeCancel context.CancelFunc |
| 41 | + closeMutex sync.Mutex |
| 42 | + closed chan struct{} |
| 43 | + closeError error |
| 44 | + |
| 45 | + sshServer *ssh.Server |
| 46 | +} |
| 47 | + |
| 48 | +func (s *server) init(ctx context.Context) { |
| 49 | + forwardHandler := &ssh.ForwardedTCPHandler{} |
| 50 | + s.sshServer = &ssh.Server{ |
| 51 | + LocalPortForwardingCallback: func(ctx ssh.Context, destinationHost string, destinationPort uint32) bool { |
| 52 | + return false |
| 53 | + }, |
| 54 | + ReversePortForwardingCallback: func(ctx ssh.Context, bindHost string, bindPort uint32) bool { |
| 55 | + return false |
| 56 | + }, |
| 57 | + PtyCallback: func(ctx ssh.Context, pty ssh.Pty) bool { |
| 58 | + return false |
| 59 | + }, |
| 60 | + ServerConfigCallback: func(ctx ssh.Context) *gossh.ServerConfig { |
| 61 | + return &gossh.ServerConfig{ |
| 62 | + Config: gossh.Config{ |
| 63 | + // "arcfour" is the fastest SSH cipher. We prioritize throughput |
| 64 | + // over encryption here, because the WebRTC connection is already |
| 65 | + // encrypted. If possible, we'd disable encryption entirely here. |
| 66 | + Ciphers: []string{"arcfour"}, |
| 67 | + }, |
| 68 | + NoClientAuth: true, |
| 69 | + } |
| 70 | + }, |
| 71 | + RequestHandlers: map[string]ssh.RequestHandler{ |
| 72 | + "tcpip-forward": forwardHandler.HandleSSHRequest, |
| 73 | + "cancel-tcpip-forward": forwardHandler.HandleSSHRequest, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + go s.run(ctx) |
| 78 | +} |
| 79 | + |
| 80 | +func (s *server) run(ctx context.Context) { |
| 81 | + var peerListener *peerbroker.Listener |
| 82 | + var err error |
| 83 | + // An exponential back-off occurs when the connection is failing to dial. |
| 84 | + // This is to prevent server spam in case of a coderd outage. |
| 85 | + for retrier := retry.New(50*time.Millisecond, 10*time.Second); retrier.Wait(ctx); { |
| 86 | + peerListener, err = s.clientDialer(ctx) |
| 87 | + if err != nil { |
| 88 | + if errors.Is(err, context.Canceled) { |
| 89 | + return |
| 90 | + } |
| 91 | + if s.isClosed() { |
| 92 | + return |
| 93 | + } |
| 94 | + s.options.Logger.Warn(context.Background(), "failed to dial", slog.Error(err)) |
| 95 | + continue |
| 96 | + } |
| 97 | + s.options.Logger.Debug(context.Background(), "connected") |
| 98 | + break |
| 99 | + } |
| 100 | + |
| 101 | + for { |
| 102 | + conn, err := peerListener.Accept() |
| 103 | + if err != nil { |
| 104 | + // This is closed! |
| 105 | + return |
| 106 | + } |
| 107 | + go s.handle(ctx, conn) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func (s *server) handle(ctx context.Context, conn *peer.Conn) { |
| 112 | + for { |
| 113 | + channel, err := conn.Accept(ctx) |
| 114 | + if err != nil { |
| 115 | + // TODO: Log here! |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + switch channel.Protocol() { |
| 120 | + case "ssh": |
| 121 | + s.sshServer.HandleConn(channel.NetConn()) |
| 122 | + case "proxy": |
| 123 | + // Proxy the port provided. |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// isClosed returns whether the API is closed or not. |
| 129 | +func (s *server) isClosed() bool { |
| 130 | + select { |
| 131 | + case <-s.closed: |
| 132 | + return true |
| 133 | + default: |
| 134 | + return false |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func (s *server) Close() error { |
| 139 | + return nil |
| 140 | +} |
0 commit comments