-
Notifications
You must be signed in to change notification settings - Fork 3
/
ssh.go
215 lines (188 loc) · 4.68 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
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
package main
import (
"code.google.com/p/go.crypto/ssh"
"code.google.com/p/go.crypto/ssh/terminal"
"fmt"
"github.com/kelseyhightower/envconfig"
"io"
"io/ioutil"
"log"
"net"
"os"
)
type configuration struct {
PrivateKeyPath string
LocalSSHAddr string
RemoteSSHAddr string
RemoteForwardAddress string
RemoteSSHUser string
RemotePrivateKeyPath string
}
var appConfig configuration
func main() {
println("starting ssh server...")
err := envconfig.Process("wormhole", &appConfig)
if err != nil {
log.Fatal(err.Error())
}
if appConfig.LocalSSHAddr == "" || appConfig.PrivateKeyPath == "" ||
appConfig.RemoteSSHAddr == "" || appConfig.RemoteForwardAddress == "" ||
appConfig.RemoteSSHUser == "" || appConfig.RemotePrivateKeyPath == "" {
fmt.Println("Missing config")
os.Exit(-1)
}
// An SSH server is represented by a ServerConfig, which holds
// certificate details and handles authentication of ServerConns.
config := &ssh.ServerConfig{
PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
// Should use constant-time compare (or better, salt+hash) in
// a production setting.
if c.User() == "testuser" && string(pass) == "" {
return nil, nil
}
return nil, fmt.Errorf("password rejected for %q", c.User())
},
}
privateBytes, err := ioutil.ReadFile(appConfig.PrivateKeyPath)
if err != nil {
panic("Failed to load private key")
}
private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
panic("Failed to parse private key")
}
config.AddHostKey(private)
// Once a ServerConfig has been configured, connections can be
// accepted.
listener, err := net.Listen("tcp", appConfig.LocalSSHAddr)
if err != nil {
panic("failed to listen for connection")
}
for {
nConn, err := listener.Accept()
if err != nil {
panic("failed to accept incoming connection")
}
conn, chans, reqs, err := ssh.NewServerConn(nConn, config)
if err != nil {
panic("failed to handshake")
}
go processRequests(conn, reqs)
for newChannel := range chans {
if newChannel.ChannelType() != "session" {
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
continue
}
channel, requests, err := newChannel.Accept()
if err != nil {
panic("could not accept channel.")
}
go func(in <-chan *ssh.Request) {
for req := range in {
ok := false
switch req.Type {
case "shell":
ok = true
if len(req.Payload) > 0 {
ok = false
}
}
req.Reply(ok, nil)
}
}(requests)
term := terminal.NewTerminal(channel, "> ")
go func() {
defer channel.Close()
for {
_, err := term.ReadLine()
if err != nil {
break
}
}
}()
}
}
}
func processRequests(conn *ssh.ServerConn, reqs <-chan *ssh.Request) {
for req := range reqs {
if req.Type != "tcpip-forward" {
// accept only tcpip-forward requests
if req.WantReply {
req.Reply(false, nil)
}
continue
}
type channelForwardMsg struct {
Laddr string
Lport uint32
}
m := &channelForwardMsg{}
ssh.Unmarshal(req.Payload, m)
privateBytes, err := ioutil.ReadFile(appConfig.RemotePrivateKeyPath)
if err != nil {
log.Fatal(err.Error())
}
signer, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
log.Fatal(err.Error())
}
config := &ssh.ClientConfig{
User: appConfig.RemoteSSHUser,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
}
sshClientConn, err := ssh.Dial("tcp", appConfig.RemoteSSHAddr, config)
if err != nil {
log.Fatal(err.Error())
}
type channelOpenForwardMsg struct {
raddr string
rport uint32
laddr string
lport uint32
}
fm := &channelOpenForwardMsg{
raddr: "localhost",
rport: m.Lport,
laddr: "localhost",
lport: m.Lport,
}
channel, reqs, err := conn.Conn.OpenChannel("forwarded-tcpip", ssh.Marshal(fm))
if err != nil {
log.Fatal(err.Error())
}
go ssh.DiscardRequests(reqs)
portListener, err := sshClientConn.Listen("tcp", appConfig.RemoteForwardAddress)
if err != nil {
log.Fatal(err.Error())
}
go func() {
for {
sshConn, err := portListener.Accept()
if err != nil {
log.Fatal(err.Error())
}
// Copy localConn.Reader to sshConn.Writer
go func(sshConn net.Conn) {
_, err := io.Copy(sshConn, channel)
if err != nil {
log.Println("io.Copy failed: %v", err)
sshConn.Close()
return
}
}(sshConn)
// Copy sshConn.Reader to localConn.Writer
go func(sshConn net.Conn) {
_, err := io.Copy(channel, sshConn)
if err != nil {
log.Println("io.Copy failed: %v", err)
sshConn.Close()
return
}
}(sshConn)
}
}()
req.Reply(true, nil)
}
}