-
-
Notifications
You must be signed in to change notification settings - Fork 315
/
Copy pathstate.go
233 lines (193 loc) · 5.49 KB
/
state.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package utils
import (
"encoding/base64"
"fmt"
"io"
"log"
"net"
"net/url"
"time"
"github.com/antoniomika/syncmap"
"github.com/jpillora/ipfilter"
"github.com/spf13/viper"
"github.com/vulcand/oxy/forward"
"github.com/vulcand/oxy/roundrobin"
)
// ListenerType represents any listener sish supports.
type ListenerType int
const (
// AliasListener represents a tcp alias.
AliasListener ListenerType = iota
// HTTPListener represents a HTTP proxy.
HTTPListener
// TCPListener represents a generic tcp listener.
TCPListener
// ProcessListener represents a process specific listener.
ProcessListener
)
// LogWriter represents a writer that is used for writing logs in multiple locations.
type LogWriter struct {
TimeFmt string
MultiWriter io.Writer
}
// Write implements the write function for the LogWriter. It will add a time in a
// specific format to logs.
func (w LogWriter) Write(bytes []byte) (int, error) {
return fmt.Fprintf(w.MultiWriter, "%v | %s", time.Now().Format(w.TimeFmt), string(bytes))
}
// ListenerHolder represents a generic listener.
type ListenerHolder struct {
net.Listener
ListenAddr string
Type ListenerType
SSHConn *SSHConnection
OriginalAddr string
OriginalPort uint32
}
// HTTPHolder holds proxy and connection info.
type HTTPHolder struct {
HTTPUrl *url.URL
SSHConnections *syncmap.Map[string, *SSHConnection]
Forward *forward.Forwarder
Balancer *roundrobin.RoundRobin
}
// AliasHolder holds alias and connection info.
type AliasHolder struct {
AliasHost string
SSHConnections *syncmap.Map[string, *SSHConnection]
Balancer *roundrobin.RoundRobin
}
// TCPHolder holds proxy and connection info.
type TCPHolder struct {
TCPHost string
Listener net.Listener
SSHConnections *syncmap.Map[string, *SSHConnection]
SNIProxy bool
Balancers *syncmap.Map[string, *roundrobin.RoundRobin]
NoHandle bool
}
// Handle will copy connections from one handler to a roundrobin server.
func (tH *TCPHolder) Handle(state *State) {
for {
cl, err := tH.Listener.Accept()
if err != nil {
break
}
clientRemote, _, err := net.SplitHostPort(cl.RemoteAddr().String())
if err != nil || state.IPFilter.Blocked(clientRemote) {
cl.Close()
continue
}
var bufBytes []byte
balancerName := ""
if tH.SNIProxy {
tlsHello, teeConn, err := PeekTLSHello(cl)
if tlsHello == nil {
log.Printf("Unable to read TLS hello: %s", err)
cl.Close()
continue
}
bufBytes = make([]byte, teeConn.Buffer.Buffered())
_, err = io.ReadFull(teeConn, bufBytes)
if err != nil {
log.Printf("Unable to read buffered data: %s", err)
cl.Close()
continue
}
balancerName = tlsHello.ServerName
}
pB, ok := tH.Balancers.Load(balancerName)
if !ok {
tH.Balancers.Range(func(n string, b *roundrobin.RoundRobin) bool {
if MatchesWildcardHost(balancerName, n) {
pB = b
return false
}
return true
})
if pB == nil {
log.Printf("Unable to load connection location: %s not found on TCP listener %s", balancerName, tH.TCPHost)
cl.Close()
continue
}
}
balancer := pB
connectionLocation, err := balancer.NextServer()
if err != nil {
log.Println("Unable to load connection location:", err)
cl.Close()
continue
}
host, err := base64.StdEncoding.DecodeString(connectionLocation.Host)
if err != nil {
log.Println("Unable to decode connection location:", err)
cl.Close()
continue
}
hostAddr := string(host)
logLine := fmt.Sprintf("Accepted connection from %s -> %s", cl.RemoteAddr().String(), cl.LocalAddr().String())
log.Println(logLine)
if viper.GetBool("log-to-client") {
tH.SSHConnections.Range(func(key string, sshConn *SSHConnection) bool {
sshConn.Listeners.Range(func(listenerAddr string, val net.Listener) bool {
if listenerAddr == hostAddr {
sshConn.SendMessage(logLine, true)
return false
}
return true
})
return true
})
}
conn, err := net.Dial("unix", hostAddr)
if err != nil {
log.Println("Error connecting to tcp balancer:", err)
cl.Close()
continue
}
if bufBytes != nil {
_, err := conn.Write(bufBytes)
if err != nil {
log.Println("Unable to write to conn:", err)
cl.Close()
continue
}
}
go CopyBoth(conn, cl)
}
}
type Ports struct {
// HTTPPort is used as a string override for the used HTTP port.
HTTPPort int
// HTTPSPort is used as a string override for the used HTTPS port.
HTTPSPort int
// SSHPort is used as a string override for the used SSH port.
SSHPort int
}
// State handles overall state. It retains mutexed maps for various
// datastructures and shared objects.
type State struct {
Console *WebConsole
SSHConnections *syncmap.Map[string, *SSHConnection]
Listeners *syncmap.Map[string, net.Listener]
HTTPListeners *syncmap.Map[string, *HTTPHolder]
AliasListeners *syncmap.Map[string, *AliasHolder]
TCPListeners *syncmap.Map[string, *TCPHolder]
IPFilter *ipfilter.IPFilter
LogWriter io.Writer
Ports *Ports
}
// NewState returns a new State struct.
func NewState() *State {
return &State{
SSHConnections: syncmap.New[string, *SSHConnection](),
Listeners: syncmap.New[string, net.Listener](),
HTTPListeners: syncmap.New[string, *HTTPHolder](),
AliasListeners: syncmap.New[string, *AliasHolder](),
TCPListeners: syncmap.New[string, *TCPHolder](),
IPFilter: Filter,
Console: NewWebConsole(),
LogWriter: multiWriter,
Ports: &Ports{},
}
}