-
-
Notifications
You must be signed in to change notification settings - Fork 307
/
state.go
177 lines (147 loc) · 4.13 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
package utils
import (
"encoding/base64"
"fmt"
"io"
"log"
"net"
"sync"
"time"
"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
}
// HTTPHolder holds proxy and connection info.
// SSHConnections is a map[string]*SSHConnection.
type HTTPHolder struct {
HTTPHost string
Scheme string
SSHConnections *sync.Map
Forward *forward.Forwarder
Balancer *roundrobin.RoundRobin
}
// AliasHolder holds alias and connection info.
// SSHConnections is a map[string]*SSHConnection.
type AliasHolder struct {
AliasHost string
SSHConnections *sync.Map
Balancer *roundrobin.RoundRobin
}
// TCPHolder holds proxy and connection info.
// SSHConnections is a map[string]*SSHConnection.
type TCPHolder struct {
TCPHost string
Listener net.Listener
SSHConnections *sync.Map
Balancer *roundrobin.RoundRobin
}
// 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
}
connectionLocation, err := tH.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, val interface{}) bool {
sshConn := val.(*SSHConnection)
sshConn.Listeners.Range(func(key, val interface{}) bool {
listenerAddr := key.(string)
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
}
go CopyBoth(conn, cl)
}
}
// State handles overall state. It retains mutexed maps for various
// datastructures and shared objects.
// SSHConnections is a map[string]*SSHConnection.
// Listeners is a map[string]net.Listener.
// HTTPListeners is a map[string]HTTPHolder.
// AliasListeners is a map[string]AliasHolder.
// TCPListeners is a map[string]TCPHolder.
type State struct {
Console *WebConsole
SSHConnections *sync.Map
Listeners *sync.Map
HTTPListeners *sync.Map
AliasListeners *sync.Map
TCPListeners *sync.Map
IPFilter *ipfilter.IPFilter
LogWriter io.Writer
}
// NewState returns a new State struct.
func NewState() *State {
return &State{
SSHConnections: &sync.Map{},
Listeners: &sync.Map{},
HTTPListeners: &sync.Map{},
AliasListeners: &sync.Map{},
TCPListeners: &sync.Map{},
IPFilter: Filter,
Console: NewWebConsole(),
LogWriter: multiWriter,
}
}