/
server_open_ssh.go
74 lines (61 loc) · 1.58 KB
/
server_open_ssh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package sshserver
import (
"fmt"
"io"
"net"
"github.com/gliderlabs/ssh"
gossh "golang.org/x/crypto/ssh"
)
// directStreamLocalOpenSSHMsg is a struct used for SSH_MSG_CHANNEL_OPEN message
// with "direct-streamlocal@openssh.com" string.
//
// See openssh-portable/PROTOCOL, section 2.4. connection: Unix domain socket forwarding
// https://github.com/openssh/openssh-portable/blob/master/PROTOCOL#L235
type directStreamLocalOpenSSHMsg struct {
SocketPath string
Reserved0 string
Reserved1 uint32
}
// handleDirectStreamLocalOpenSSH is used to forward local conn to a remote unix socket.
// Corresponds to the "direct-streamlocal@openssh.com" channel type.
// Used by the Recode CLI to reach the GRPC server unix socket.
func handleDirectStreamLocalOpenSSH(
srv *ssh.Server,
conn *gossh.ServerConn,
newChan gossh.NewChannel,
ctx ssh.Context,
) {
msg := directStreamLocalOpenSSHMsg{}
err := gossh.Unmarshal(newChan.ExtraData(), &msg)
if err != nil {
newChan.Reject(
gossh.ConnectionFailed,
fmt.Sprintf(
"error parsing direct stream local openssh data: %s",
err.Error(),
),
)
return
}
socketConn, err := net.Dial("unix", msg.SocketPath)
if err != nil {
newChan.Reject(gossh.ConnectionFailed, err.Error())
return
}
requestChan, reqs, err := newChan.Accept()
if err != nil {
socketConn.Close()
return
}
go gossh.DiscardRequests(reqs)
go func() {
defer requestChan.Close()
defer socketConn.Close()
io.Copy(requestChan, socketConn)
}()
go func() {
defer requestChan.Close()
defer socketConn.Close()
io.Copy(socketConn, requestChan)
}()
}