-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
147 lines (125 loc) · 2.71 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
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"sync"
"golang.org/x/crypto/ssh"
"github.com/BurntSushi/toml"
)
var (
cfgpath = flag.String("config", "cfg.toml", "config path")
)
type sshServer struct {
Addr string
User string
Password string
KeyPath string
}
type config struct {
SSH sshServer
Ports map[string]string
}
type session struct {
client *ssh.Client
listenAddr string
remoteAddr string
}
func newSession(listen, remote string, client *ssh.Client) *session {
return &session{
client: client,
listenAddr: listen,
remoteAddr: remote,
}
}
func (s *session) handleConn(conn net.Conn) {
log.Printf("accept %s", conn.RemoteAddr())
remote, err := s.client.Dial("tcp", s.remoteAddr)
if err != nil {
log.Printf("dial %s error", s.remoteAddr)
return
}
log.Printf("%s -> %s connected.", conn.RemoteAddr(), s.remoteAddr)
wait := new(sync.WaitGroup)
wait.Add(2)
go func() {
io.Copy(remote, conn)
remote.Close()
wait.Done()
}()
go func() {
io.Copy(conn, remote)
conn.Close()
wait.Done()
}()
wait.Wait()
log.Printf("%s -> %s closed", conn.RemoteAddr(), s.remoteAddr)
}
func (s *session) Run() error {
l, err := net.Listen("tcp", s.listenAddr)
if err != nil {
return err
}
for {
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
go s.handleConn(conn)
}
}
func login(cfg *sshServer) (*ssh.Client, error) {
var methods []ssh.AuthMethod
if cfg.KeyPath == "" && cfg.Password == "" {
return nil, fmt.Errorf("empty private key and password")
}
if cfg.KeyPath != "" {
key, err := ioutil.ReadFile(cfg.KeyPath)
if err != nil {
return nil, fmt.Errorf("unable to read private key: %v", err)
}
// Create the Signer for this private key.
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
log.Fatalf("unable to parse private key: %v", err)
}
methods = append(methods, ssh.PublicKeys(signer))
}
if cfg.Password != "" {
methods = append(methods, ssh.Password(cfg.Password))
}
sshconfig := &ssh.ClientConfig{
User: cfg.User,
Auth: methods,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
return ssh.Dial("tcp", cfg.Addr, sshconfig)
}
func main() {
flag.Parse()
var cfg config
_, err := toml.DecodeFile(*cfgpath, &cfg)
if err != nil {
log.Fatal(err)
}
client, err := login(&cfg.SSH)
if err != nil {
log.Fatal(err)
}
log.Printf("dial %s success", cfg.SSH.Addr)
log.Printf("ports: %v", cfg.Ports)
for local, remote := range cfg.Ports {
sess := newSession(":"+local, remote, client)
go func(local, remote string) {
log.Printf("run session %s -> %s", local, remote)
err := sess.Run()
if err != nil {
log.Fatalf("run %s error:%s", local, err)
}
}(local, remote)
}
select {}
}