-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
45 lines (41 loc) · 968 Bytes
/
server.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
package main
import (
"log"
"net"
"os"
)
func startServer(cfg Config) {
ln, err := net.Listen(cfg.flgNetwork, cfg.flgListenAddr)
if err != nil {
log.Fatalln(err)
}
defer ln.Close()
for {
conn, err := ln.Accept()
if err != nil {
logf("accept error: %v", err)
continue
}
cconn := NewCryptConn(conn, cfg.flgCryptoMethod, []byte(cfg.flgSecretKey))
if err := cconn.ServerHandshake(); err != nil {
logf("server handshake error: %v", err)
cconn.Close()
continue
}
go func() {
if err := runServer(cfg, cconn); err != nil {
logf("server run error: %v", err)
}
}()
}
}
func runServer(cfg Config, cconn *CryptConn) (err error) {
if cfg.flgFwdAddr != "" {
return serverForwardPort(cfg.flgReverse, cfg.flgNetwork, cfg.flgFwdAddr, cconn)
}
//server non reverse: exec cmd mode
if cfg.flgCommand != "" && !cfg.flgReverse {
return pipeCmd2Conn(cfg.flgCommand, cconn)
}
return pipeStdInOut(os.Stdin, os.Stdout, cconn)
}