-
Notifications
You must be signed in to change notification settings - Fork 0
/
ship.go
162 lines (159 loc) · 3.58 KB
/
ship.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
package main
import (
"io"
"io/ioutil"
"log"
"math/rand"
"net"
"strings"
"time"
"github.com/samuelventura/go-tools"
"github.com/samuelventura/go-tree"
"golang.org/x/crypto/ssh"
)
func run(node tree.Node) {
name := node.GetValue("name").(string)
pool := node.GetValue("pool").(string)
record := node.GetValue("record").(string)
count := node.GetValue("count").(*countDso)
keypath := node.GetValue("keypath").(string)
id := node.GetValue("id").(*idDso)
key, err := ioutil.ReadFile(keypath)
if err != nil {
log.Panicln(err)
}
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
log.Panicln(err)
}
hkcb := func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }
config := &ssh.ClientConfig{
User: name,
Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
HostKeyCallback: ssh.HostKeyCallback(hkcb),
}
var txts []string
if len(record) > 0 {
txts, err = net.LookupTXT(record)
if err != nil {
log.Panicln(err)
}
} else {
txts = []string{pool}
}
var eps []string
for _, txt := range txts {
addrs := strings.Split(txt, ",")
eps = append(eps, addrs...)
}
log.Println("eps", eps)
rand.Seed(time.Now().UnixNano())
l := len(eps)
//random start
n := rand.Intn(l)
for i := 0; i < l; i++ {
ii := (n + i) % l
addr := eps[ii]
log.Println("ep", ii, addr, name)
conn, err := net.DialTimeout("tcp", addr, 10*time.Second)
if err != nil {
log.Println(err)
continue
}
tools.KeepAlive(conn, 5)
sshCon, sshch, reqch, err := ssh.NewClientConn(conn, addr, config)
if err != nil {
log.Println(err)
conn.Close()
continue
}
node.AddCloser("conn", conn.Close)
node.AddCloser("sshCon", sshCon.Close)
node.AddProcess("ping", func() {
defer log.Println("request handler exited")
for {
timer := time.NewTimer(10 * time.Second)
select {
case req := <-reqch:
if req == nil {
return
}
if req.Type == "ping" {
err := req.Reply(true, nil)
switch err {
case nil:
timer.Stop()
default:
return
}
}
case <-timer.C:
log.Println("idle timeout")
return
case <-node.Closed():
return
}
}
})
handleForward := func(ch ssh.NewChannel) {
addr := string(ch.ExtraData())
conn, err := net.DialTimeout("tcp", addr, 5*time.Second)
if err != nil {
log.Println(err)
return
}
defer conn.Close()
tools.KeepAlive(conn, 5)
sshChan, reqChan, err := ch.Accept()
if err != nil {
log.Println(err)
return
}
defer sshChan.Close()
done := make(chan interface{}, 2)
go func() {
for range reqChan {
}
}()
go func() {
io.Copy(sshChan, conn)
done <- true
}()
go func() {
io.Copy(conn, sshChan)
done <- true
}()
select {
case <-done: //close on first error
case <-node.Closed():
}
}
setupForward := func(ch ssh.NewChannel, cid string) {
defer node.IfRecoverAction(func() {
ch.Reject(ssh.ConnectionFailed, "closing")
})
child := node.AddChild(cid)
child.AddProcess("handler", func() {
defer child.IfRecoverAction(func() {
ch.Reject(ssh.ConnectionFailed, "panic")
})
log.Println("open", count.increment(), cid)
defer func() { log.Println("close", count.decrement(), cid) }()
handleForward(ch)
})
}
node.AddProcess("sshch", func() {
for ch := range sshch {
if ch.ChannelType() != "forward" {
ch.Reject(ssh.Prohibited, "unsupported")
return
}
addr := string(ch.ExtraData())
cid := id.next(addr)
setupForward(ch, cid)
}
})
return
}
log.Panicln("connection failed", txts)
}