-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.go
179 lines (151 loc) · 4.67 KB
/
ssh.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
package main
import (
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"io"
"net"
"os"
"strings"
"go.uber.org/zap"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
func startSshTunnel(c *Config) {
if c.Tunnel == "" {
return
}
tunnelSplit := strings.Split(c.Tunnel, "@")
username, tunnel := tunnelSplit[0], tunnelSplit[1]
tunnelSplit = strings.Split(tunnel, ":")
tunnel, publishPort := strings.Join(tunnelSplit[:len(tunnelSplit)-1], ":"), tunnelSplit[len(tunnelSplit)-1]
sshConfig := &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
if c.PrivateKeyPath != "" {
if auth, err := withKey(c.PrivateKeyPath, c.PrivateKeyPass); err == nil {
sshConfig.Auth = append(sshConfig.Auth, auth)
} else {
logs.FatalIf(err, "loading private key failed")
}
} else if os.Getenv("SSH_AUTH_SOCK") != "" {
// ssh-agent(1) provides a UNIX socket at $SSH_AUTH_SOCK.
agentConnection, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
logs.FatalIf(err, "opening SSH_AUTH_SOCK")
sshConfig.Auth = append(sshConfig.Auth, ssh.PublicKeysCallback(agent.NewClient(agentConnection).Signers))
}
sshClient, err := ssh.Dial("tcp", tunnel, sshConfig)
logs.FatalIf(err, "dialing ssh server")
defer func() { _ = sshClient.Close() }()
// Listen on remote server port
listener, err := sshClient.Listen("tcp", fmt.Sprintf("0.0.0.0:%s", publishPort))
logs.FatalIf(err, "opening port on remote server")
defer func() { _ = listener.Close() }()
logs.Info("listening on remote server", zap.String("host", listener.Addr().String()))
for {
remote, err := listener.Accept()
logs.FatalIf(err, "error accepting connection")
local, err := net.Dial("tcp", c.ListenAddress)
logs.FatalIf(err, "dialing local service")
err = handleClient(local, remote)
logs.ErrorIf(err, "handling transport")
_ = remote.Close()
_ = local.Close()
}
}
func withKey(privateKeyPath, privateKeyPassword string) (ssh.AuthMethod, error) {
// read private key file
pemBytes, err := os.ReadFile(privateKeyPath)
if err != nil {
return nil, fmt.Errorf("Reading private key file failed %v", err)
}
// create signer
signer, err := signerFromPem(pemBytes, []byte(privateKeyPassword))
if err != nil {
return nil, err
}
return ssh.PublicKeys(signer), nil
}
// From https://sosedoff.com/2015/05/25/ssh-port-forwarding-with-go.html
// Handle local client connections and tunnel data to the remote server
// Will use io.Copy - http://golang.org/pkg/io/#Copy
func handleClient(local net.Conn, remote net.Conn) error {
chDone := make(chan error)
// Start remote -> local data transfer
go func() {
_, err := io.Copy(local, remote)
logs.ErrorIf(err, "error while copy remote->local")
chDone <- err
}()
// Start local -> remote data transfer
go func() {
_, err := io.Copy(remote, local)
logs.ErrorIf(err, "error while copy local->remote")
chDone <- err
}()
return <-chDone
}
func signerFromPem(pemBytes []byte, password []byte) (ssh.Signer, error) {
// read pem block
err := errors.New("Pem decode failed, no key found")
pemBlock, _ := pem.Decode(pemBytes)
if pemBlock == nil {
return nil, err
}
// handle encrypted key
if x509.IsEncryptedPEMBlock(pemBlock) {
// decrypt PEM
pemBlock.Bytes, err = x509.DecryptPEMBlock(pemBlock, []byte(password))
if err != nil {
return nil, fmt.Errorf("Decrypting PEM block failed %v", err)
}
// get RSA, EC or DSA key
key, err := parsePemBlock(pemBlock)
if err != nil {
return nil, err
}
// generate signer instance from key
signer, err := ssh.NewSignerFromKey(key)
if err != nil {
return nil, fmt.Errorf("Creating signer from encrypted key failed %v", err)
}
return signer, nil
} else {
// generate signer instance from plain key
signer, err := ssh.ParsePrivateKey(pemBytes)
if err != nil {
return nil, fmt.Errorf("Parsing plain private key failed %v", err)
}
return signer, nil
}
}
func parsePemBlock(block *pem.Block) (interface{}, error) {
switch block.Type {
case "RSA PRIVATE KEY":
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("Parsing PKCS private key failed %v", err)
} else {
return key, nil
}
case "EC PRIVATE KEY":
key, err := x509.ParseECPrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("Parsing EC private key failed %v", err)
} else {
return key, nil
}
case "DSA PRIVATE KEY":
key, err := ssh.ParseDSAPrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("Parsing DSA private key failed %v", err)
} else {
return key, nil
}
default:
return nil, fmt.Errorf("Parsing private key failed, unsupported key type %q", block.Type)
}
}