forked from antoniomika/sish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channels.go
157 lines (134 loc) · 3.33 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
package main
import (
"fmt"
"io"
"log"
"net"
"strings"
"github.com/logrusorgru/aurora"
"golang.org/x/crypto/ssh"
)
var proxyProtoPrefix = "proxyproto:"
func handleSession(newChannel ssh.NewChannel, sshConn *SSHConnection, state *State) {
connection, requests, err := newChannel.Accept()
if err != nil {
sshConn.CleanUp(state)
return
}
if *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) && *proxyProtoEnabled {
sshConn.ProxyProto = getProxyProtoVersion(strings.TrimPrefix(payloadString, proxyProtoPrefix))
if sshConn.ProxyProto != 0 {
sendMessage(sshConn, fmt.Sprintf("Proxy protocol enabled for TCP connections. Using protocol version %d", int(sshConn.ProxyProto)), true)
}
}
default:
if *debug {
log.Println("Sub Channel Type", req.Type, req.WantReply, string(req.Payload))
}
}
}
}()
}
func handleAlias(newChannel ssh.NewChannel, sshConn *SSHConnection, state *State) {
connection, requests, err := newChannel.Accept()
if err != nil {
sshConn.CleanUp(state)
return
}
go ssh.DiscardRequests(requests)
if *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.TCPListeners.Load(tcpAliasToConnect)
if !ok {
log.Println("Unable to load tcp alias:", tcpAliasToConnect)
sshConn.CleanUp(state)
return
}
conn, err := net.Dial("unix", loc.(string))
if err != nil {
log.Println("Error connecting to alias:", err)
sshConn.CleanUp(state)
return
}
sshConn.Listeners.Store(conn.RemoteAddr(), nil)
copyBoth(conn, connection)
select {
case <-sshConn.Close:
break
default:
sshConn.CleanUp(state)
}
}
func writeToSession(connection ssh.Channel, c string) {
_, err := connection.Write(append([]byte(c), []byte{'\r', '\n'}...))
if err != nil && *debug {
log.Println("Error trying to write message to socket:", err)
}
}
func getProxyProtoVersion(proxyProtoUserVersion string) byte {
if *proxyProtoVersion != "userdefined" {
proxyProtoUserVersion = *proxyProtoVersion
}
realProtoVersion := 0
if proxyProtoUserVersion == "1" {
realProtoVersion = 1
} else if proxyProtoUserVersion == "2" {
realProtoVersion = 2
}
return byte(realProtoVersion)
}