-
Notifications
You must be signed in to change notification settings - Fork 26
/
sshproxy.go
150 lines (117 loc) · 3.03 KB
/
sshproxy.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package sshproxy
import (
"io"
"log"
"net"
"golang.org/x/crypto/ssh"
)
type SshConn struct {
net.Conn
config *ssh.ServerConfig
callbackFn func(c ssh.ConnMetadata) (*ssh.Client, error)
wrapFn func(c ssh.ConnMetadata, r io.ReadCloser) (io.ReadCloser, error)
closeFn func(c ssh.ConnMetadata) error
}
func (p *SshConn) serve() error {
serverConn, chans, reqs, err := ssh.NewServerConn(p, p.config)
if err != nil {
log.Println("failed to handshake")
return (err)
}
defer serverConn.Close()
clientConn, err := p.callbackFn(serverConn)
if err != nil {
log.Printf("%s", err.Error())
return (err)
}
defer clientConn.Close()
go ssh.DiscardRequests(reqs)
for newChannel := range chans {
channel2, requests2, err2 := clientConn.OpenChannel(newChannel.ChannelType(), newChannel.ExtraData())
if err2 != nil {
log.Printf("Could not accept client channel: %s", err.Error())
return err
}
channel, requests, err := newChannel.Accept()
if err != nil {
log.Printf("Could not accept server channel: %s", err.Error())
return err
}
// connect requests
go func() {
log.Printf("Waiting for request")
r:
for {
var req *ssh.Request
var dst ssh.Channel
select {
case req = <-requests:
dst = channel2
case req = <-requests2:
dst = channel
}
log.Printf("Request: %s %s %s %s\n", dst, req.Type, req.WantReply, req.Payload)
b, err := dst.SendRequest(req.Type, req.WantReply, req.Payload)
if err != nil {
log.Printf("%s", err)
}
if req.WantReply {
req.Reply(b, nil)
}
switch req.Type {
case "exit-status":
break r
case "exec":
// not supported (yet)
default:
log.Println(req.Type)
}
}
channel.Close()
channel2.Close()
}()
// connect channels
log.Printf("Connecting channels.")
var wrappedChannel io.ReadCloser = channel
var wrappedChannel2 io.ReadCloser = channel2
if p.wrapFn != nil {
// wrappedChannel, err = p.wrapFn(channel)
wrappedChannel2, err = p.wrapFn(serverConn, channel2)
}
go io.Copy(channel2, wrappedChannel)
go io.Copy(channel, wrappedChannel2)
defer wrappedChannel.Close()
defer wrappedChannel2.Close()
}
if p.closeFn != nil {
p.closeFn(serverConn)
}
return nil
}
func ListenAndServe(addr string, serverConfig *ssh.ServerConfig,
callbackFn func(c ssh.ConnMetadata) (*ssh.Client, error),
wrapFn func(c ssh.ConnMetadata, r io.ReadCloser) (io.ReadCloser, error),
closeFn func(c ssh.ConnMetadata) error,
) error {
listener, err := net.Listen("tcp", addr)
if err != nil {
log.Printf("net.Listen failed: %v", err)
return err
}
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
log.Printf("listen.Accept failed: %v", err)
return err
}
sshconn := &SshConn{Conn: conn, config: serverConfig, callbackFn: callbackFn, wrapFn: wrapFn, closeFn: closeFn}
go func() {
if err := sshconn.serve(); err != nil {
log.Printf("Error occured while serving %s\n", err)
return
}
log.Println("Connection closed.")
}()
}
}