forked from leffss/sshproxyserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
328 lines (281 loc) · 8.07 KB
/
main.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package main
import (
"encoding/binary"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"strings"
"time"
"golang.org/x/crypto/ssh"
"github.com/leffss/sshproxyserver/asciinema"
"github.com/leffss/sshproxyserver/sshupstream"
uuid "github.com/satori/go.uuid"
)
var (
// sz 下载文件
ZModemSzStart = fmt.Sprintf("%+q", "rz\r**\x18B00000000000000\r\x8a\x11")
ZModemSzEnd = fmt.Sprintf("%+q", "\r**\x18B0800000000022d\r\x8a")
// 经过测试发现不一定会出现,就是两个大写的字母 o, 建议不过滤
//ZModemSzEnd2 = fmt.Sprintf("%+q", "OO")
// rz 上传文件
ZModemRzStart = fmt.Sprintf("%+q", "rz waiting to receive.**\x18B0100000023be50\r\x8a\x11")
ZModemRzEnd = fmt.Sprintf("%+q", "**\x18B0800000000022d\r\x8a")
// zmodem 取消 \x18\x18\x18\x18\x18\x08\x08\x08\x08\x08,使用 %+q 的形式无法正确使用 strings.Index 处理
ZModemCancel = string([]byte{24, 24, 24, 24, 24, 8, 8, 8, 8, 8})
hostPrivateKeySigner ssh.Signer
TerminalModes = ssh.TerminalModes{
ssh.ECHO: 1,
ssh.TTY_OP_ISPEED: 14400,
ssh.TTY_OP_OSPEED: 14400,
}
)
type Std struct {
channel ssh.Channel
id string
shell string
term string
width int
height int
startTime time.Time
ZModem bool
file *os.File
castV2 *asciinema.CastV2Header
}
func NewStd(channel ssh.Channel, shell, term string, width, height int) *Std {
return &Std{
channel: channel,
id: uuid.NewV4().String(),
shell: shell,
term: term,
width: width,
height: height,
startTime: time.Now(),
ZModem: false,
}
}
func (s *Std) Write(p []byte) (n int, err error) {
res := fmt.Sprintf("%+q", string(p))
// 使用 zModem 传输的文件内容不记录
if s.ZModem {
if res == ZModemSzEnd || res == ZModemRzEnd {
//log.Println("zModem end")
s.ZModem = false
now := time.Now()
if err := s.castV2.PushData(s.startTime, now, asciinema.V2OutputEvent, []byte("")); err != nil {
log.Println(err)
}
}
if index := strings.Index(string(p), ZModemCancel); index != -1 {
//log.Println("zModem cancel")
s.ZModem = false
}
} else {
if res == ZModemSzStart || res == ZModemRzStart {
//log.Println("zModem start")
s.ZModem = true
} else {
// 保存结果为 asciinema v2 格式,方便回放
now := time.Now()
if err := s.castV2.PushData(s.startTime, now, asciinema.V2OutputEvent, p); err != nil {
log.Println(err)
}
}
}
return s.channel.Write(p)
}
func (s *Std) Read(p []byte) (n int, err error) {
return s.channel.Read(p)
}
func (s *Std) SetTerm(term string) {
s.term = term
}
func (s *Std) InitAsciinema() {
f, _ := os.Create(s.id)
s.file = f
castMetadata := &asciinema.CastMetadata{
Version: asciinema.V2Version,
Width: s.width,
Height: s.height,
//Title: "",
Timestamp: s.startTime,
//Duration: 0,
//Command: "",
Env: map[string]string{"SHELL": s.shell, "TERM": s.term},
//IdleTimeLimit: 0,
}
castV2, _ := asciinema.NewCastV2(castMetadata, s.file)
if err := castV2.PushHeader(); err != nil {
log.Println(err)
}
s.castV2 = castV2
}
func (s *Std) CloseFile() {
if err := s.file.Close(); err != nil {
log.Println(err)
}
}
func keyAuth(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
log.Println(conn.RemoteAddr(), "auth with", key.Type())
return nil, nil
}
func passwordAuth(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
_ = password
log.Println(conn.RemoteAddr(), "auth with password")
return nil, nil
}
// parseDims extracts two uint32s from the provided buffer.
func parseDims(b []byte) (uint32, uint32) {
w := binary.BigEndian.Uint32(b)
h := binary.BigEndian.Uint32(b[4:])
return w, h
}
func handleChannels(host, username, password string, timeout time.Duration, sshConn *ssh.ServerConn, chans <-chan ssh.NewChannel, sessionNum uint8) {
defer func() {
log.Printf("ssh connection close %s (%s)", sshConn.RemoteAddr(), sshConn.ClientVersion())
}()
notAllowCloneSession := false
numSession := sessionNum
for newChannel := range chans {
if notAllowCloneSession { // 仅允许创建一个 session
_ = newChannel.Reject(ssh.UnknownChannelType, fmt.Sprintf("not allow clone session more"))
continue
}
if t := newChannel.ChannelType(); t != "session" {
_ = newChannel.Reject(ssh.UnknownChannelType, fmt.Sprintf("unknown channel type: %s", t))
_ = sshConn.Close()
return
}
channel, requests, err := newChannel.Accept()
if err != nil {
log.Printf("could not accept channel (%s)", err)
_ = sshConn.Close()
return
}
client, session, err := sshupstream.NewSshUpstream(host, username, password, timeout)
if err != nil {
log.Printf("could not connect ssh upstream server")
_, _ = channel.Write([]byte("could not connect ssh upstream server"))
_ = channel.Close()
_ = sshConn.Close()
return
}
defer func() {
err = client.Close()
if err != nil {
log.Printf("client.close err: %s", err)
}
}()
// 控制 clone session 数量
if sessionNum != 0 {
numSession--
if numSession == 0 {
notAllowCloneSession = true
}
}
// channel 实现了 io.Reader 与 io.Writer 接口
//session.Stdin = channel
//session.Stdout = channel
//session.Stderr = channel
// Std 通过继承 channel 也实现了 io.Reader 与 io.Writer 接口
std := NewStd(channel, "/bin/sh", "linux", 250, 40)
session.Stdin = std
session.Stdout = std
session.Stderr = std
//_, _ = io.WriteString(std, fmt.Sprintf("connect ssh upstream %s\n\r", host))
// Sessions have out-of-band requests such as "shell", "pty-req", "env" and so on.
go func(in <-chan *ssh.Request) {
for req := range in {
ok := false
switch req.Type {
case "pty-req":
ok = true
termLen := req.Payload[3]
termType := string(req.Payload[4 : termLen+4])
w, h := parseDims(req.Payload[termLen+4:])
//_ = session.WindowChange(int(h), int(w))
_ = session.RequestPty(termType, int(h), int(w), TerminalModes)
_ = session.Shell()
std.SetTerm(termType)
std.InitAsciinema()
defer std.CloseFile()
case "window-change":
w, h := parseDims(req.Payload)
_ = session.WindowChange(int(h), int(w))
continue
case "shell":
ok = true
case "exec":
//ok = false
case "subsystem":
//ok = false
case "x11-req":
//ok = false
}
if !ok {
log.Printf("unsupport %s request...", req.Type)
}
_ = req.Reply(ok, nil)
}
}(requests)
// 解决 ctrl + d 退出后,连接不关闭的情况
go func() {
time.Sleep(time.Second * 3)
if err := session.Wait(); err != nil {
return
}
_ = channel.Close()
}()
}
}
func init() {
keyPath := "./ssh_proxy_rsa.key"
hostPrivateKey, err := ioutil.ReadFile(keyPath)
if err != nil {
panic(err)
}
hostPrivateKeySigner, err = ssh.ParsePrivateKey(hostPrivateKey)
if err != nil {
panic(err)
}
}
func main() {
host := "192.168.223.111:22"
username := "root"
password := "123456"
timeout := 5 * time.Second
config := &ssh.ServerConfig{
MaxAuthTries: 3,
PasswordCallback: passwordAuth,
PublicKeyCallback: keyAuth,
ServerVersion: "SSH-2.0-go-ssh-proxy-server", // 必须以 `SSH-2.0-` 开头
BannerCallback: func(conn ssh.ConnMetadata) string {
return fmt.Sprintf("Welcome to go ssh proxy server, your address is %s", strings.Split(conn.RemoteAddr().String(), ":")[0])
},
}
config.AddHostKey(hostPrivateKeySigner)
port := "2222"
socket, err := net.Listen("tcp", ":" + port)
if err != nil {
panic(err)
}
var sessionNum uint8 = 1 //控制一个 ssh 连接上能够打开的 session 数, 0 无限制,不推荐
for {
tcpConn, err := socket.Accept()
if err != nil {
log.Printf("failed to accept incoming connection (%s)", err)
continue
}
// From a standard TCP connection to an encrypted SSH connection
sshConn, chans, reqs, err := ssh.NewServerConn(tcpConn, config)
if err != nil {
log.Printf("failed to handshake (%s)", err)
continue
}
log.Printf("ssh connection from %s (%s)", sshConn.RemoteAddr(), sshConn.ClientVersion())
go ssh.DiscardRequests(reqs)
// Accept all channels
go handleChannels(host, username, password, timeout, sshConn, chans, sessionNum)
}
}