-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsshtunnel.go
181 lines (154 loc) · 4.24 KB
/
sshtunnel.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"os"
"fmt"
"io"
"net"
"os/user"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"regexp"
"strconv"
)
type Endpoint struct {
Host string
Port int
}
func (endpoint *Endpoint) String() string {
return fmt.Sprintf("%s:%d", endpoint.Host, endpoint.Port)
}
type SSHTunnel struct {
Local *Endpoint
Server *Endpoint
Remote *Endpoint
Config *ssh.ClientConfig
}
func (tunnel *SSHTunnel) Start() error {
listener, err := net.Listen("tcp", tunnel.Local.String())
if err != nil {
Error.Printf("SSH Tunnel: Failed to start server at %s. Error: %s", tunnel.Local.String(), err)
return err
}
defer listener.Close()
for {
serverConn, err := ssh.Dial("tcp", tunnel.Server.String(), tunnel.Config)
if err != nil {
Error.Fatalf("SSH Tunnel: %s\n", err)
return err
}
conn, err := listener.Accept()
if err != nil {
Error.Printf("SSH Tunnel: Failed to accept connection: %s", err)
return err
}
Info.Print("SSH Tunnel: Accepted connection to forward to the tunnel...")
go tunnel.forward(conn, serverConn)
}
}
func (tunnel *SSHTunnel) forward(localConn net.Conn, sshServerConn *ssh.Client) {
/*
serverConn, err := ssh.Dial("tcp", tunnel.Server.String(), tunnel.Config)
if err != nil {
Error.Fatalf("SSH Tunnel: Server dial error: %s\n", err)
return
}*/
remoteConn, err := sshServerConn.Dial("tcp", tunnel.Remote.String())
if err != nil {
Error.Fatalf("SSH Tunnel: Remote dial error: %s\n", err)
return
}
copyConn := func(writer, reader net.Conn) {
_, err:= io.Copy(writer, reader)
if err != nil {
Error.Fatalf("SSH Tunnel: Could not forward conenction: %s\n", err)
}
}
go copyConn(localConn, remoteConn)
go copyConn(remoteConn, localConn)
}
func SSHAgent() ssh.AuthMethod {
if sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
return ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers)
}
return nil
}
//
// sshHostDef [email protected]:port
// tunnelDef local_port:remote_host:remote_port
//
func NewSSHTunnelFromHostStrings(sshHostDef string, tunnelDef string) *SSHTunnel {
sshHostRegexp := regexp.MustCompile(`((\w*)@)?([^:@]+)(:(\d{2,5}))?`)
match := sshHostRegexp.FindAllStringSubmatch(sshHostDef, -1)
if len(match) == 0 {
Error.Fatalf("SSH Tunnel: Failed to parse ssh host %s\n", sshHostDef)
}
result := match[0]
sshUser := result[2]
if sshUser == "" {
osUser, _ := user.Current()
sshUser = osUser.Username
}
sshHost := result[3]
sshPort := parsePort(result[5], 22)
Trace.Printf("SSH Tunnel: Server - User: %s, Host: %s, Port: %d\n", sshUser, sshHost, sshPort)
//Setting up defaults
localPort := 9199
remotePort := 9200
remoteHost := "localhost"
tunnelRegexp := regexp.MustCompile(`((\d{2,5}):)?([^:@]+)(:(\d{2,5}))?`)
match = tunnelRegexp.FindAllStringSubmatch(tunnelDef, -1)
if len(match) == 0 {
Trace.Print("SSH Tunnel: Failed to parse remote tunnel host/port, using defaults\n")
} else {
result = match[0]
localPort = parsePort(result[2], 9199)
remotePort = parsePort(result[5], 9200)
remoteHost = result[3]
}
Trace.Printf("SSH Tunnel: Local port : %d, Remote Host: %s, Remote Port: %d\n", localPort, remoteHost, remotePort)
return NewSSHTunnel(sshUser, sshHost, sshPort, localPort, remoteHost, remotePort)
}
func parsePort(portStr string, defaultPort int) int {
if portStr != "" {
port, err := strconv.Atoi(portStr)
if (err != nil) {
Error.Printf("SSH Tunnel: Reverting to port %d because given port was not numeric: %s\n", defaultPort, err)
port = defaultPort
}
return port
}
return defaultPort
}
func passwordCallback() (string, error) {
fmt.Println("Enter ssh password:")
pwd := readPasswd();
return pwd, nil;
}
func NewSSHTunnel(sshUser string, sshHost string, sshPort int, localPort int,
remoteHost string, remotePort int) *SSHTunnel {
localEndpoint := &Endpoint{
Host: "localhost",
Port: localPort,
}
serverEndpoint := &Endpoint{
Host: sshHost,
Port: sshPort,
}
remoteEndpoint := &Endpoint{
Host: remoteHost,
Port: remotePort,
}
sshConfig := &ssh.ClientConfig{
User: sshUser,
Auth: []ssh.AuthMethod{
SSHAgent(),
ssh.PasswordCallback(passwordCallback),
},
}
return &SSHTunnel{
Config: sshConfig,
Local: localEndpoint,
Server: serverEndpoint,
Remote: remoteEndpoint,
}
}