| 1 | package sshclient |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "net" |
| 8 | "net/url" |
| 9 | "os" |
| 10 | "runtime" |
| 11 | "strings" |
| 12 | "sync" |
| 13 | "time" |
| 14 | |
| 15 | "github.com/containers/gvisor-tap-vsock/pkg/fs" |
| 16 | "github.com/containers/gvisor-tap-vsock/pkg/utils" |
| 17 | "github.com/sirupsen/logrus" |
| 18 | ) |
| 19 | |
| 20 | type CloseWriteStream interface { |
| 21 | io.Reader |
| 22 | io.WriteCloser |
| 23 | CloseWrite() error |
| 24 | } |
| 25 | |
| 26 | type CloseWriteConn interface { |
| 27 | net.Conn |
| 28 | CloseWriteStream |
| 29 | } |
| 30 | |
| 31 | type SSHForward struct { |
| 32 | listener net.Listener |
| 33 | bastion *Bastion |
| 34 | sock *url.URL |
| 35 | } |
| 36 | |
| 37 | type SSHDialer interface { |
| 38 | DialContextTCP(ctx context.Context, addr string) (net.Conn, error) |
| 39 | } |
| 40 | |
| 41 | type genericTCPDialer struct { |
| 42 | } |
| 43 | |
| 44 | var defaultTCPDialer genericTCPDialer |
| 45 | |
| 46 | func (dialer *genericTCPDialer) DialContextTCP(ctx context.Context, addr string) (net.Conn, error) { |
| 47 | var d net.Dialer |
| 48 | return d.DialContext(ctx, "tcp", addr) |
| 49 | } |
| 50 | |
| 51 | func CreateSSHForward(ctx context.Context, src *url.URL, dest *url.URL, identity string, dialer SSHDialer) (*SSHForward, error) { |
| 52 | if dialer == nil { |
| 53 | dialer = &defaultTCPDialer |
| 54 | } |
| 55 | |
| 56 | return setupProxy(ctx, src, dest, identity, "", dialer) |
| 57 | } |
| 58 | |
| 59 | func CreateSSHForwardPassphrase(ctx context.Context, src *url.URL, dest *url.URL, identity string, passphrase string, dialer SSHDialer) (*SSHForward, error) { |
| 60 | if dialer == nil { |
| 61 | dialer = &defaultTCPDialer |
| 62 | } |
| 63 | |
| 64 | return setupProxy(ctx, src, dest, identity, passphrase, dialer) |
| 65 | } |
| 66 | |
| 67 | func (forward *SSHForward) AcceptAndTunnel(ctx context.Context) error { |
| 68 | return acceptConnection(ctx, forward.listener, forward.bastion, forward.sock) |
| 69 | } |
| 70 | |
| 71 | func (forward *SSHForward) Tunnel(ctx context.Context) (CloseWriteConn, error) { |
| 72 | return connectForward(ctx, forward.bastion) |
| 73 | } |
| 74 | |
| 75 | func (forward *SSHForward) Close() { |
| 76 | if forward.listener != nil { |
| 77 | forward.listener.Close() |
| 78 | } |
| 79 | if forward.bastion != nil { |
| 80 | forward.bastion.Close() |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func connectForward(ctx context.Context, bastion *Bastion) (CloseWriteConn, error) { |
| 85 | for retries := 1; ; retries++ { |
| 86 | forward, err := bastion.Client.Dial("unix", bastion.Path) |
| 87 | if err == nil { |
| 88 | return forward.(CloseWriteConn), nil |
| 89 | } |
| 90 | if retries > 2 { |
| 91 | return nil, fmt.Errorf("couldn't reestablish ssh tunnel on path: %s: %w", bastion.Path, err) |
| 92 | } |
| 93 | |
| 94 | _, _, err = bastion.Client.SendRequest("alive@gvproxy", true, nil) |
| 95 | if err != nil { |
| 96 | for bastionRetries := 1; ; bastionRetries++ { |
| 97 | err = bastion.Reconnect(ctx) |
| 98 | if err == nil { |
| 99 | break |
| 100 | } |
| 101 | if bastionRetries > 2 || !utils.Sleep(ctx, 200*time.Millisecond) { |
| 102 | return nil, fmt.Errorf("couldn't reestablish ssh connection: %s: %w", bastion.Host, err) |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if !utils.Sleep(ctx, 200*time.Millisecond) { |
| 108 | retries = 3 |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func listenUnix(socketURI *url.URL) (net.Listener, error) { |
| 114 | path := socketURI.Path |
| 115 | if runtime.GOOS == "windows" { |
| 116 | path = strings.TrimPrefix(path, "/") |
| 117 | } |
| 118 | |
| 119 | if err := os.Remove(path); err != nil && !os.IsNotExist(err) { |
| 120 | return nil, err |
| 121 | } |
| 122 | |
| 123 | oldmask := fs.Umask(0177) |
| 124 | defer fs.Umask(oldmask) |
| 125 | listener, err := net.Listen("unix", path) |
| 126 | if err != nil { |
| 127 | return listener, fmt.Errorf("error listening on socket: %s: %w", socketURI.Path, err) |
| 128 | } |
| 129 | |
| 130 | return listener, nil |
| 131 | } |
| 132 | |
| 133 | func setupProxy(ctx context.Context, socketURI *url.URL, dest *url.URL, identity string, passphrase string, dialer SSHDialer) (*SSHForward, error) { |
| 134 | var ( |
| 135 | listener net.Listener |
| 136 | err error |
| 137 | ) |
| 138 | switch socketURI.Scheme { |
| 139 | case "unix": |
| 140 | listener, err = listenUnix(socketURI) |
| 141 | if err != nil { |
| 142 | return &SSHForward{}, err |
| 143 | } |
| 144 | case "npipe": |
| 145 | listener, err = ListenNpipe(socketURI) |
| 146 | if err != nil { |
| 147 | return &SSHForward{}, err |
| 148 | } |
| 149 | case "": |
| 150 | |
| 151 | default: |
| 152 | return &SSHForward{}, fmt.Errorf("URI scheme not supported: %s", socketURI.Scheme) |
| 153 | } |
| 154 | |
| 155 | connectFunc := func(ctx context.Context, bastion *Bastion) (net.Conn, error) { |
| 156 | timeout := 5 * time.Second |
| 157 | if bastion != nil { |
| 158 | timeout = bastion.Config.Timeout |
| 159 | } |
| 160 | ctx, cancel := context.WithTimeout(ctx, timeout) |
| 161 | conn, err := dialer.DialContextTCP(ctx, dest.Host) |
| 162 | if cancel != nil { |
| 163 | cancel() |
| 164 | } |
| 165 | |
| 166 | return conn, err |
| 167 | } |
| 168 | |
| 169 | createBastion := func() (*Bastion, error) { |
| 170 | conn, err := connectFunc(ctx, nil) |
| 171 | if err != nil { |
| 172 | return nil, err |
| 173 | } |
| 174 | return CreateBastion(dest, passphrase, identity, conn, connectFunc) |
| 175 | } |
| 176 | bastion, err := utils.Retry(ctx, createBastion, "Waiting for sshd") |
| 177 | if err != nil { |
| 178 | return &SSHForward{}, fmt.Errorf("setupProxy failed: %w", err) |
| 179 | } |
| 180 | |
| 181 | logrus.Debugf("Socket forward established: %s -> %s\n", socketURI.Path, dest.Path) |
| 182 | |
| 183 | return &SSHForward{listener, bastion, socketURI}, nil |
| 184 | } |
| 185 | |
| 186 | func acceptConnection(ctx context.Context, listener net.Listener, bastion *Bastion, socketURI *url.URL) error { |
| 187 | con, err := listener.Accept() |
| 188 | if err != nil { |
| 189 | return fmt.Errorf("error accepting on socket: %s: %w", socketURI.Path, err) |
| 190 | } |
| 191 | |
| 192 | src, ok := con.(CloseWriteStream) |
| 193 | if !ok { |
| 194 | con.Close() |
| 195 | return fmt.Errorf("underlying socket does not support half-close %s: %w", socketURI.Path, err) |
| 196 | } |
| 197 | |
| 198 | var dest CloseWriteStream |
| 199 | |
| 200 | dest, err = connectForward(ctx, bastion) |
| 201 | if err != nil { |
| 202 | con.Close() |
| 203 | logrus.Error(err) |
| 204 | return nil |
| 205 | } |
| 206 | |
| 207 | complete := new(sync.WaitGroup) |
| 208 | complete.Add(2) |
| 209 | go forward(src, dest, complete) |
| 210 | go forward(dest, src, complete) |
| 211 | |
| 212 | go func() { |
| 213 | complete.Wait() |
| 214 | src.Close() |
| 215 | dest.Close() |
| 216 | }() |
| 217 | |
| 218 | return nil |
| 219 | } |
| 220 | |
| 221 | func forward(src io.ReadCloser, dest CloseWriteStream, complete *sync.WaitGroup) { |
| 222 | defer complete.Done() |
| 223 | _, _ = io.Copy(dest, src) |
| 224 | |
| 225 | |
| 226 | _ = dest.CloseWrite() |
| 227 | } |