-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathsshtunnel.go
More file actions
190 lines (161 loc) · 4.31 KB
/
sshtunnel.go
File metadata and controls
190 lines (161 loc) · 4.31 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
// copied from https://gist.github.com/svett/5d695dcc4cc6ad5dd275
package ssh_ca_util
import (
// "log"
// "bufio"
// "time"
"os"
"fmt"
"io"
"strings"
"strconv"
"net"
"sync"
"net/url"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
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(out_port chan int) error {
listener, err := net.Listen("tcp", tunnel.Local.String())
if err != nil {
return err
}
//fmt.Println("Using local port:", listener.Addr().(*net.TCPAddr).Port)
//tunnel.LocalPort = listener.Addr().(*net.TCPAddr).Port
out_port <- listener.Addr().(*net.TCPAddr).Port
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
return err
}
go tunnel.forward(conn)
}
}
func (tunnel *SSHtunnel) forward(localConn net.Conn) {
serverConn, err := ssh.Dial("tcp", tunnel.Server.String(), tunnel.Config)
if err != nil {
fmt.Printf("Server dial error: %s\n", err)
return
}
remoteConn, err := serverConn.Dial("tcp", tunnel.Remote.String())
if err != nil {
fmt.Printf("Remote dial error: %s\n", err)
return
}
copyConn:=func(writer, reader net.Conn) {
_, err:= io.Copy(writer, reader)
if err != nil {
fmt.Printf("io.Copy error: %s", 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
}
var (
jobIsRunning bool
JobIsrunningMu sync.Mutex
)
func StartTunnelIfNeeded(config *RequesterConfig) {
if len(config.SshBastion) > 0 {
JobIsrunningMu.Lock()
start := !jobIsRunning
jobIsRunning = true
JobIsrunningMu.Unlock()
if start {
if !strings.HasPrefix(config.SshBastion, "ssh://") {
fmt.Printf("Bastion host must start with ssh://. Exiting\n")
os.Exit(1)
}
bastion_parsed, err := url.Parse(config.SshBastion)
if err != nil {
fmt.Printf("url.Parse error for SshBastion: %s", err)
}
// Check to see if it's a nonstardard port
host_parts := strings.Split(bastion_parsed.Host, ":")
var ssh_port int
ssh_port = 22
if len(host_parts) == 2 {
var err error
ssh_port, err = strconv.Atoi(host_parts[1])
if err != nil {
fmt.Printf("strconv.Atoi error: %s", err)
}
}
// Get remote end information
remote_parsed, err := url.Parse(config.SignerUrl)
if err != nil {
fmt.Printf("url.Parse error on SignerUrl: %s", err)
}
remote_parts := strings.Split(remote_parsed.Host, ":")
if len(remote_parts) != 2 {
fmt.Printf("Missing port for SignerUrl. Exiting")
os.Exit(1)
}
remote_port, err := strconv.Atoi(remote_parts[1])
if err != nil {
fmt.Printf("strconv.Atoi error: %s", err)
}
//fmt.Printf("config stuff: %s, %d\n", host_parts[0], ssh_port)
//fmt.Printf("starting tunnel config...\n")
localEndpoint := &Endpoint{
Host: "localhost",
Port: 0,
}
serverEndpoint := &Endpoint{
Host: host_parts[0],
Port: ssh_port,
}
remoteEndpoint := &Endpoint{
Host: remote_parts[0],
Port: remote_port,
}
sshConfig := &ssh.ClientConfig{
User: bastion_parsed.User.Username(),
Auth: []ssh.AuthMethod{
SSHAgent(),
},
// TODO: fix this to actually check the trusted hosts
// https://utcc.utoronto.ca/~cks/space/blog/programming/GoSSHHostKeyCheckingNotes
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
}
tunnel := &SSHtunnel{
Config: sshConfig,
Local: localEndpoint,
Server: serverEndpoint,
Remote: remoteEndpoint,
}
//fmt.Printf("starting tunnel...\n")
out_port_chan := make(chan int)
go tunnel.Start(out_port_chan)
var local_port int
local_port = <- out_port_chan
//fmt.Printf("Using local port: %d\n", local_port)
//fmt.Printf("doing normal stuff...\n")
config.SignerUrl = fmt.Sprintf("%s://localhost:%d/", remote_parsed.Scheme, local_port)
//fmt.Printf("sshtunnel using signer url: %s", config.SignerUrl)
// end new stuff
}
}
}