-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathsshtunnel.go
218 lines (190 loc) · 5.52 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
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
/* Copyright (C) 2016 Krešimir Nesek
*
* Based on blog post by Svett Ralchev: http://blog.ralch.com/tutorial/golang-ssh-tunneling/
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
package main
import (
"os"
"fmt"
"io"
"net"
"os/user"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"regexp"
"strconv"
"os/exec"
"strings"
"errors"
)
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
}
func GetUser() (string, error) {
//attempt fetching logged in user via os/user package. It may not work when cross-compiled due to CGO requirement.
//More info at: https://github.com/golang/go/issues/11797
osUser, err := user.Current()
Trace.Printf("os/user detected user as %v", osUser)
if err != nil || osUser == nil || osUser.Username == "" {
// os/user didn't work. Let's try using "whoami" command
path, err := exec.LookPath("whoami")
if err != nil {
//whoami not found... we're giving up
return "", errors.New("Could not detect current user")
}
out, err := exec.Command(path).Output()
if err != nil {
//something went wrong, giving up
return "", errors.New("Could not detect current user")
}
return strings.TrimSpace(string(out)), nil
}
return osUser.Username, 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]
sshHost := result[3]
sshPort := parsePort(result[5], 22)
if sshUser == "" {
osUser, err := GetUser()
if err != nil {
Error.Printf("Could not detect current username to use when connecting via SSH. Please specify a username " +
"when specifying SSH host (e.g. your_username@%s)\n", sshHost)
os.Exit(1)
}
sshUser = osUser
}
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,
}
}