-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
channels.go
208 lines (174 loc) · 4.89 KB
/
channels.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
package sshmuxer
import (
"encoding/base64"
"fmt"
"io"
"log"
"net"
"strings"
"github.com/antoniomika/sish/utils"
"github.com/logrusorgru/aurora"
"github.com/spf13/viper"
"golang.org/x/crypto/ssh"
)
// proxyProtoPrefix is used when deciding what proxy protocol
// version to use.
var proxyProtoPrefix = "proxyproto:"
// handleSession handles the channel when a user requests a session.
// This is how we send console messages.
func handleSession(newChannel ssh.NewChannel, sshConn *utils.SSHConnection, state *utils.State) {
connection, requests, err := newChannel.Accept()
if err != nil {
sshConn.CleanUp(state)
return
}
if viper.GetBool("debug") {
log.Println("Handling session for connection:", connection)
}
writeToSession(connection, aurora.BgRed("Press Ctrl-C to close the session.").String()+"\r\n")
go func() {
for {
select {
case c := <-sshConn.Messages:
writeToSession(connection, c)
case <-sshConn.Close:
return
}
}
}()
go func() {
for {
data := make([]byte, 4096)
dataRead, err := connection.Read(data)
if err != nil && err == io.EOF {
break
} else if err != nil {
select {
case <-sshConn.Close:
break
default:
sshConn.CleanUp(state)
}
break
}
if dataRead != 0 {
if data[0] == 3 {
sshConn.CleanUp(state)
}
}
}
}()
go func() {
for req := range requests {
switch req.Type {
case "shell":
err := req.Reply(true, nil)
if err != nil {
log.Println("Error replying to socket request:", err)
}
case "exec":
payloadString := string(req.Payload[4:])
if strings.HasPrefix(payloadString, proxyProtoPrefix) && viper.GetBool("proxy-protocol") {
sshConn.ProxyProto = getProxyProtoVersion(strings.TrimPrefix(payloadString, proxyProtoPrefix))
if sshConn.ProxyProto != 0 {
sshConn.SendMessage(fmt.Sprintf("Proxy protocol enabled for TCP connections. Using protocol version %d", int(sshConn.ProxyProto)), true)
}
}
default:
if viper.GetBool("debug") {
log.Println("Sub Channel Type", req.Type, req.WantReply, string(req.Payload))
}
}
}
}()
}
// handleAlias is used when handling a SSH connection to attach to an alias listener.
func handleAlias(newChannel ssh.NewChannel, sshConn *utils.SSHConnection, state *utils.State) {
connection, requests, err := newChannel.Accept()
if err != nil {
sshConn.CleanUp(state)
return
}
go ssh.DiscardRequests(requests)
if viper.GetBool("debug") {
log.Println("Handling alias connection for:", connection)
}
check := &forwardedTCPPayload{}
err = ssh.Unmarshal(newChannel.ExtraData(), check)
if err != nil {
log.Println("Error unmarshaling information:", err)
sshConn.CleanUp(state)
return
}
tcpAliasToConnect := fmt.Sprintf("%s:%d", check.Addr, check.Port)
loc, ok := state.AliasListeners.Load(tcpAliasToConnect)
if !ok {
log.Println("Unable to load tcp alias:", tcpAliasToConnect)
sshConn.CleanUp(state)
return
}
aH := loc.(*utils.AliasHolder)
connectionLocation, err := aH.Balancer.NextServer()
if err != nil {
log.Println("Unable to load connection location:", err)
sshConn.CleanUp(state)
return
}
host, err := base64.StdEncoding.DecodeString(connectionLocation.Host)
if err != nil {
log.Println("Unable to decode connection location:", err)
sshConn.CleanUp(state)
return
}
aliasAddr := string(host)
logLine := fmt.Sprintf("Accepted connection from %s -> %s", sshConn.SSHConn.RemoteAddr().String(), tcpAliasToConnect)
log.Println(logLine)
if viper.GetBool("log-to-client") {
aH.SSHConnections.Range(func(key, val interface{}) bool {
sshConn := val.(*utils.SSHConnection)
sshConn.Listeners.Range(func(key, val interface{}) bool {
listenerAddr := key.(string)
if listenerAddr == aliasAddr {
sshConn.SendMessage(logLine, true)
return false
}
return true
})
return true
})
}
conn, err := net.Dial("unix", aliasAddr)
if err != nil {
log.Println("Error connecting to alias:", err)
sshConn.CleanUp(state)
return
}
sshConn.Listeners.Store(aliasAddr, conn)
utils.CopyBoth(conn, connection)
select {
case <-sshConn.Close:
break
default:
sshConn.CleanUp(state)
}
}
// writeToSession is where we write to the underlying session channel.
func writeToSession(connection ssh.Channel, c string) {
_, err := connection.Write(append([]byte(c), []byte{'\r', '\n'}...))
if err != nil && viper.GetBool("debug") {
log.Println("Error trying to write message to socket:", err)
}
}
// getProxyProtoVersion returns the proxy proto version selected by the client.
func getProxyProtoVersion(proxyProtoUserVersion string) byte {
if viper.GetString("proxy-protocol-version") != "userdefined" {
proxyProtoUserVersion = viper.GetString("proxy-protocol-version")
}
realProtoVersion := 0
if proxyProtoUserVersion == "1" {
realProtoVersion = 1
} else if proxyProtoUserVersion == "2" {
realProtoVersion = 2
}
return byte(realProtoVersion)
}