-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.go
150 lines (122 loc) · 2.73 KB
/
proxy.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 zcmd
import (
"context"
"fmt"
"io"
"io/ioutil"
"net"
"time"
"github.com/cybozu-go/well"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
)
// Proxy is client for proxy
type Proxy struct {
sshCfg []*sshClientConfig
}
type sshClientConfig struct {
forwardType ProxyForwardType
user string
key ssh.Signer
sshAddr string
localAddr string
remoteAddr string
}
// NewProxy returns Proxy
func NewProxy(cfgs []ProxyConfig) (*Proxy, error) {
proxy := new(Proxy)
for _, cfg := range cfgs {
key, err := parsePrivateKey(cfg.PrivateKey)
if err != nil {
return nil, err
}
for _, fwdCfg := range cfg.Forward {
sshCfg := &sshClientConfig{
forwardType: fwdCfg.Type,
user: cfg.User,
key: key,
sshAddr: fmt.Sprintf("%s:%d", cfg.Address, cfg.Port),
localAddr: fmt.Sprintf("%s:%d", fwdCfg.BindAddress, fwdCfg.BindPort),
remoteAddr: fmt.Sprintf("%s:%d", fwdCfg.RemoteAddress, fwdCfg.RemotePort),
}
proxy.sshCfg = append(proxy.sshCfg, sshCfg)
}
}
return proxy, nil
}
func parsePrivateKey(keyPath string) (ssh.Signer, error) {
buff, err := ioutil.ReadFile(keyPath)
if err != nil {
return nil, err
}
signer, err := ssh.ParsePrivateKey(buff)
if err != nil {
var passphrase string
err := Ask(&passphrase, "ssh passphrase for "+keyPath, true)
if err != nil {
return nil, err
}
return ssh.ParsePrivateKeyWithPassphrase(buff, []byte(passphrase))
}
return signer, nil
}
func makeSSHConfig(cfg sshClientConfig) *ssh.ClientConfig {
defaultSSHTimeout := 10 * time.Second
return &ssh.ClientConfig{
User: cfg.user,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(cfg.key),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: defaultSSHTimeout,
}
}
func handleClient(client net.Conn, remote io.ReadWriter) {
defer client.Close()
chDone := make(chan bool)
go func() {
_, err := io.Copy(client, remote)
if err != nil {
log.WithError(err)
}
chDone <- true
}()
go func() {
_, err := io.Copy(remote, client)
if err != nil {
log.WithError(err)
}
chDone <- true
}()
<-chDone
}
func (p *Proxy) Run(ctx context.Context) error {
env := well.NewEnvironment(ctx)
for _, cfg := range p.sshCfg {
conn, err := ssh.Dial("tcp", cfg.sshAddr, makeSSHConfig(*cfg))
if err != nil {
return err
}
defer conn.Close()
remote, err := conn.Dial("tcp", cfg.remoteAddr)
if err != nil {
return err
}
local, err := net.Listen("tcp", cfg.localAddr)
if err != nil {
return err
}
defer local.Close()
env.Go(func(ctx context.Context) error {
for {
client, err := local.Accept()
if err != nil {
return err
}
handleClient(client, remote)
}
})
}
env.Stop()
return env.Wait()
}