-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathdomain.go
More file actions
238 lines (215 loc) · 6.17 KB
/
Copy pathdomain.go
File metadata and controls
238 lines (215 loc) · 6.17 KB
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
package gateway
import (
"errors"
"io"
"net"
"strconv"
"sync"
"time"
gossh "golang.org/x/crypto/ssh"
"lion/pkg/logger"
"github.com/jumpserver-dev/sdk-go/common"
"github.com/jumpserver-dev/sdk-go/model"
)
var ErrNoAvailable = errors.New("no available domain")
const (
miniTimeout = 15 * time.Second
)
type DomainGateway struct {
DstAddr string // 10.0.0.1:3389
sshClient *gossh.Client
jumpClient *gossh.Client
SelectedGateway *model.Gateway
Destination *model.Gateway
ln net.Listener
once sync.Once
}
func (d *DomainGateway) run() {
defer d.closeOnce()
for {
con, err := d.ln.Accept()
if err != nil {
break
}
logger.Infof("Accept new conn by SSH forwarder %s ", d.Name())
go d.handlerConn(con)
}
logger.Infof("Stop proxy by SSH forwarder %s", d.Name())
}
func (d *DomainGateway) handlerConn(srcCon net.Conn) {
defer srcCon.Close()
dstCon, err := d.sshClient.Dial("tcp", d.DstAddr)
if err != nil {
logger.Errorf("Failed gateway dial %s: %s ",
d.DstAddr, err.Error())
return
}
defer dstCon.Close()
go func() {
_, _ = io.Copy(dstCon, srcCon)
_ = dstCon.Close()
}()
_, _ = io.Copy(srcCon, dstCon)
logger.Infof("Gateway end proxy %s", d.DstAddr)
}
func (d *DomainGateway) Start() (err error) {
if !d.getAvailableGateway() {
return ErrNoAvailable
}
localIP := common.CurrentLocalIP()
d.ln, err = net.Listen("tcp", net.JoinHostPort(localIP, "0"))
if err != nil {
_ = d.sshClient.Close()
return err
}
go d.run()
return nil
}
func (d *DomainGateway) GetListenAddr() *net.TCPAddr {
return d.ln.Addr().(*net.TCPAddr)
}
func (d *DomainGateway) getAvailableGateway() bool {
if d.Destination != nil {
sshClient, jumpClient, err := d.createDestinationSSHClient()
if err != nil {
logger.Errorf("Dial SSH destination %s err: %s", d.Destination.Name, err)
return false
}
d.sshClient = sshClient
d.jumpClient = jumpClient
return true
}
if d.SelectedGateway != nil {
sshClient, err := d.createGatewaySSHClient(d.SelectedGateway)
if err != nil {
logger.Errorf("Dial select gateway %s err: %s ", d.SelectedGateway.Name, err)
return false
}
logger.Debugf("Dial select gateway %s success", d.SelectedGateway.Name)
d.sshClient = sshClient
return true
}
return false
}
func (d *DomainGateway) createDestinationSSHClient() (*gossh.Client, *gossh.Client, error) {
if d.SelectedGateway == nil {
client, err := d.createGatewaySSHClient(d.Destination)
return client, nil, err
}
jumpClient, err := d.createGatewaySSHClient(d.SelectedGateway)
if err != nil {
return nil, nil, err
}
addr := gatewaySSHAddress(d.Destination)
conn, err := jumpClient.Dial("tcp", addr)
if err != nil {
_ = jumpClient.Close()
return nil, nil, err
}
_ = conn.SetDeadline(time.Now().Add(miniTimeout))
clientConn, chans, reqs, err := gossh.NewClientConn(
conn, addr, gatewaySSHConfig(d.Destination),
)
if err != nil {
_ = conn.Close()
_ = jumpClient.Close()
return nil, nil, err
}
_ = conn.SetDeadline(time.Time{})
return gossh.NewClient(clientConn, chans, reqs), jumpClient, nil
}
func (d *DomainGateway) createGatewaySSHClient(gateway *model.Gateway) (*gossh.Client, error) {
return gossh.Dial("tcp", gatewaySSHAddress(gateway), gatewaySSHConfig(gateway))
}
func gatewaySSHConfig(gateway *model.Gateway) *gossh.ClientConfig {
auths := make([]gossh.AuthMethod, 0, 3)
loginAccount := gateway.Account
if loginAccount.IsSSHKey() {
if signer, err1 := gossh.ParsePrivateKey([]byte(loginAccount.Secret)); err1 == nil {
auths = append(auths, gossh.PublicKeys(signer))
} else {
logger.Errorf("Domain gateway Parse private key error: %s", err1)
}
} else {
auths = append(auths, gossh.Password(loginAccount.Secret))
auths = append(auths, gossh.KeyboardInteractive(func(user, instruction string,
questions []string, echos []bool) (answers []string, err error) {
return []string{loginAccount.Secret}, nil
}))
}
return &gossh.ClientConfig{
User: loginAccount.Username,
Auth: auths,
HostKeyCallback: NewTrustHostKeyCallback(),
Config: createSSHConfig(),
Timeout: miniTimeout,
HostKeyAlgorithms: allHostKeyAlgorithms(),
}
}
func gatewaySSHAddress(gateway *model.Gateway) string {
port := gateway.Protocols.GetProtocolPort("ssh")
return net.JoinHostPort(gateway.Address, strconv.Itoa(port))
}
func (d *DomainGateway) Name() string {
if d.Destination != nil {
return d.Destination.Name
}
if d.SelectedGateway != nil {
return d.SelectedGateway.Name
}
return "unknown"
}
func (d *DomainGateway) Stop() {
d.closeOnce()
}
func (d *DomainGateway) closeOnce() {
d.once.Do(func() {
if d.ln != nil {
_ = d.ln.Close()
}
if d.sshClient != nil {
_ = d.sshClient.Close()
}
if d.jumpClient != nil {
_ = d.jumpClient.Close()
}
})
}
func NewTrustHostKeyCallback() gossh.HostKeyCallback {
return func(hostname string, remote net.Addr, key gossh.PublicKey) error {
return nil
}
}
func createSSHConfig() gossh.Config {
var cfg gossh.Config
cfg.SetDefaults()
algos := gossh.SupportedAlgorithms()
insecureAlgos := gossh.InsecureAlgorithms()
ciphers := make([]string, 0, len(algos.Ciphers)+len(insecureAlgos.Ciphers))
/*
Change the ciphers order, placing aes128-ctr first.
Compatible with old ssh servers.
*/
ciphers = append(ciphers, gossh.CipherAES128CTR)
ciphers = append(ciphers, insecureAlgos.Ciphers...)
ciphers = append(ciphers, algos.Ciphers...)
keyExchanges := make([]string, 0, len(algos.KeyExchanges)+len(insecureAlgos.KeyExchanges))
keyExchanges = append(keyExchanges, insecureAlgos.KeyExchanges...)
keyExchanges = append(keyExchanges, algos.KeyExchanges...)
cfg.Ciphers = ciphers
cfg.KeyExchanges = keyExchanges
return cfg
}
func allHostKeyAlgorithms() []string {
supportedAlgos := gossh.SupportedAlgorithms()
insecureAlgos := gossh.InsecureAlgorithms()
hostKeyAlgos := make([]string, 0, len(supportedAlgos.HostKeys)+len(insecureAlgos.HostKeys)+1)
/*
Change the algorithm order, placing KeyAlgoED25519 first.
Compatible with certain SSH servers.
*/
hostKeyAlgos = append(hostKeyAlgos, gossh.KeyAlgoED25519)
hostKeyAlgos = append(hostKeyAlgos, supportedAlgos.HostKeys...)
hostKeyAlgos = append(hostKeyAlgos, insecureAlgos.HostKeys...)
return hostKeyAlgos
}