-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathssh.go
201 lines (172 loc) · 4.78 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
// SshReverseProxy - This tag line may change
//
// Copyright 2016 Dolf Schimmel, Freeaqingme.
//
// This Source Code Form is subject to the terms of the two-clause BSD license.
// For its contents, please refer to the LICENSE file.
//
package main
import (
"fmt"
"io"
"io/ioutil"
"net"
"strconv"
"time"
"golang.org/x/crypto/ssh"
backend "sshReverseProxy/UserBackend"
)
func handleLocalSshConn(lnConn net.Conn) {
defer func() {
if Config.Ssh_Reverse_Proxy.Exit_On_Panic {
return
}
if r := recover(); r != nil {
Log.Error("Recovered from panic in connection from "+
lnConn.RemoteAddr().String()+":", r)
}
}()
Log.Info("Received connection from", lnConn.RemoteAddr())
var sClient *ssh.Client
psConfig := getProxyServerSshConfig(&sClient)
psConn, psChans, psReqs, err := ssh.NewServerConn(lnConn, psConfig)
if err != nil {
Log.Info("Could not establish connection with " + lnConn.RemoteAddr().String() + ": " + err.Error())
return
}
defer psConn.Close()
defer sClient.Close()
go ssh.DiscardRequests(psReqs)
for newChannel := range psChans {
handleChannel(newChannel, sClient)
}
Log.Info("Lost connection with", lnConn.RemoteAddr())
}
func handleChannel(newChannel ssh.NewChannel, rClient *ssh.Client) {
if newChannel.ChannelType() != "session" {
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type: "+newChannel.ChannelType())
return
}
psChannel, psRequests, err := newChannel.Accept()
if err != nil {
panic("could not accept channel.")
}
sChannel, sRequests, err := rClient.OpenChannel(newChannel.ChannelType(), nil)
if err != nil {
panic("Failed to create session: " + err.Error())
}
go pipeRequests(psChannel, sChannel, psRequests, sRequests)
time.Sleep(50 * time.Millisecond)
go pipe(sChannel, psChannel)
go pipe(psChannel, sChannel)
}
func pipe(dst, src ssh.Channel) {
_, err := io.Copy(dst, src)
if err != nil {
fmt.Println(err.Error())
}
dst.CloseWrite()
}
func getProxyServerSshConfig(rClient **ssh.Client) *ssh.ServerConfig {
holdOff := func() {
duration, _ := time.ParseDuration(Config.Ssh_Reverse_Proxy.Auth_Error_Delay)
time.Sleep(duration)
}
callback := func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
host, port := backend.GetServerForUser(c.User())
if host != "" {
var err error
*rClient, err = getServerSshClient(host, port, c.User(), string(pass))
if err != nil {
Log.Info(fmt.Sprintf("Could not authorize %q on %s: %s",
c.User(), c.RemoteAddr().String(), err))
holdOff()
return nil, fmt.Errorf("Could not authorize %q on %s: %s",
c.User(), c.RemoteAddr().String(), err)
}
return nil, nil
}
Log.Info(fmt.Sprintf("Unknown user %q on %s", c.User(), c.RemoteAddr().String()))
holdOff()
return nil, fmt.Errorf("Unknown user %q on %s", c.User(), c.RemoteAddr().String())
}
config := &ssh.ServerConfig{
ServerVersion: "SSH-2.0-SshReverseProxy",
PasswordCallback: callback,
}
privateBytes, err := ioutil.ReadFile(Config.Ssh_Reverse_Proxy.Key_File)
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)
return config
}
func getServerSshClient(host, portStr, user, password string) (*ssh.Client, error) {
config := &ssh.ClientConfig{
User: user,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
}
port, err := strconv.ParseInt(portStr, 10, 64)
if err != nil {
panic("Port must be an integer")
}
conn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", host, port), config)
if err != nil {
return nil, fmt.Errorf("unable to connect: " + err.Error())
}
return conn, nil
}
func pipeRequests(psChannel, sChannel ssh.Channel, psRequests, sRequests <-chan *ssh.Request) {
defer func() {
return
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
}
}()
defer sChannel.Close()
defer psChannel.Close()
for {
select {
case lRequest, ok := <-psRequests:
if !ok {
return
}
if err := forwardRequest(lRequest, sChannel); err != nil {
fmt.Println("Error: " + err.Error())
continue
}
case rRequest, ok := <-sRequests:
if !ok {
return
}
if err := forwardRequest(rRequest, psChannel); err != nil {
fmt.Println("Error: " + err.Error())
continue
}
}
}
}
func forwardRequest(req *ssh.Request, channel ssh.Channel) error {
if string(req.Type) != "subsystem" && string(req.Type) != "exit-status" {
req.Reply(false, nil)
if req.Type == "env" {
return nil
}
return fmt.Errorf("Ignoring unsupported request type: %s", string(req.Type))
}
reply, err := channel.SendRequest(req.Type, req.WantReply, req.Payload)
if err != nil {
return err
}
if req.WantReply {
req.Reply(reply, nil)
}
return nil
}