/
server_direct_tcpip.go
78 lines (64 loc) · 1.68 KB
/
server_direct_tcpip.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
75
76
77
78
package sshserver
import (
"github.com/gliderlabs/ssh"
"github.com/yolo-sh/agent-container/constants"
gossh "golang.org/x/crypto/ssh"
)
// proxyDirectTCPIPChannel is used during port forwarding
// to overwrite the destination IP with the env container one.
// "direct-tcpip" is for "client-to-server forwarded connections".
type proxyDirectTCPIPChannel struct {
originalChannel gossh.NewChannel
}
func (ch *proxyDirectTCPIPChannel) Accept() (
gossh.Channel,
<-chan *gossh.Request,
error,
) {
return ch.originalChannel.Accept()
}
func (ch *proxyDirectTCPIPChannel) Reject(
reason gossh.RejectionReason,
message string,
) error {
return ch.originalChannel.Reject(reason, message)
}
func (ch *proxyDirectTCPIPChannel) ChannelType() string {
return ch.originalChannel.ChannelType()
}
func (ch *proxyDirectTCPIPChannel) ExtraData() []byte {
msg := directTCPIPMsg{}
err := gossh.Unmarshal(ch.originalChannel.ExtraData(), &msg)
if err != nil {
return ch.originalChannel.ExtraData()
}
msg.DestAddr = constants.DockerContainerIPAddress
return gossh.Marshal(msg)
}
// directTCPIPMsg represents the message
// sent during the opening of "direct-tcpip" channels
type directTCPIPMsg struct {
DestAddr string
DestPort uint32
OriginAddr string
OriginPort uint32
}
// handleDirectTCPIP is used to forward local conn to a remote port.
// Corresponds to the "direct-tcpip" channel type.
// Used by the local VSCode instance to reach code-server.
func handleDirectTCPIP(
srv *ssh.Server,
conn *gossh.ServerConn,
newChan gossh.NewChannel,
ctx ssh.Context,
) {
proxyChannel := &proxyDirectTCPIPChannel{
originalChannel: newChan,
}
ssh.DirectTCPIPHandler(
srv,
conn,
proxyChannel,
ctx,
)
}