forked from haveachin/infrared
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.go
219 lines (183 loc) · 4.8 KB
/
gateway.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
package infrared
import (
"errors"
"log"
"net/http"
"sync"
"github.com/JohnEridium/infrared/callback"
"github.com/JohnEridium/infrared/protocol/handshaking"
"github.com/pires/go-proxyproto"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
proxiesActive = promauto.NewGauge(prometheus.GaugeOpts{
Name: "infrared_proxies",
Help: "The total number of proxies running",
})
)
type Gateway struct {
listeners sync.Map
Proxies sync.Map
closed chan bool
wg sync.WaitGroup
receiveProxyProtocol bool
}
func (gateway *Gateway) ListenAndServe(proxies []*Proxy) error {
if len(proxies) <= 0 {
return errors.New("no proxies in gateway")
}
gateway.closed = make(chan bool, len(proxies))
for _, proxy := range proxies {
if err := gateway.RegisterProxy(proxy); err != nil {
gateway.Close()
return err
}
}
log.Println("All proxies are online")
return nil
}
func (gateway *Gateway) EnablePrometheus(bind string) error {
gateway.wg.Add(1)
go func() {
defer gateway.wg.Done()
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(bind, nil)
}()
log.Println("Enabling Prometheus metrics endpoint on", bind)
return nil
}
func (gateway *Gateway) KeepProcessActive() {
gateway.wg.Wait()
}
// Close closes all listeners
func (gateway *Gateway) Close() {
gateway.listeners.Range(func(k, v interface{}) bool {
gateway.closed <- true
_ = v.(Listener).Close()
return false
})
}
func (gateway *Gateway) CloseProxy(proxyUID string) {
log.Println("Closing proxy with UID", proxyUID)
v, ok := gateway.Proxies.LoadAndDelete(proxyUID)
if !ok {
return
}
proxiesActive.Dec()
proxy := v.(*Proxy)
closeListener := true
gateway.Proxies.Range(func(k, v interface{}) bool {
otherProxy := v.(*Proxy)
if proxy.ListenTo() == otherProxy.ListenTo() {
closeListener = false
return false
}
return true
})
if !closeListener {
return
}
v, ok = gateway.listeners.Load(proxy.ListenTo())
if !ok {
return
}
v.(Listener).Close()
}
func (gateway *Gateway) RegisterProxy(proxy *Proxy) error {
// Register new Proxy
proxyUID := proxy.UID()
log.Println("Registering proxy with UID", proxyUID)
gateway.Proxies.Store(proxyUID, proxy)
proxiesActive.Inc()
proxy.Config.removeCallback = func() {
gateway.CloseProxy(proxyUID)
}
proxy.Config.changeCallback = func() {
if proxyUID == proxy.UID() {
return
}
gateway.CloseProxy(proxyUID)
if err := gateway.RegisterProxy(proxy); err != nil {
log.Println(err)
}
}
playersConnected.WithLabelValues(proxy.DomainName())
// Check if a gate is already listening to the Proxy address
addr := proxy.ListenTo()
if _, ok := gateway.listeners.Load(addr); ok {
return nil
}
log.Println("Creating listener on", addr)
listener, err := Listen(addr)
if err != nil {
return err
}
gateway.listeners.Store(addr, listener)
gateway.wg.Add(1)
go func() {
if err := gateway.listenAndServe(listener, addr); err != nil {
log.Printf("Failed to listen on %s; error: %s", proxy.ListenTo(), err)
}
}()
return nil
}
func (gateway *Gateway) listenAndServe(listener Listener, addr string) error {
defer gateway.wg.Done()
for {
conn, err := listener.Accept()
if err != nil {
// TODO: Refactor this; it feels hacky
if err.Error() == "use of closed network connection" {
log.Println("Closing listener on", addr)
gateway.listeners.Delete(addr)
return nil
}
continue
}
go func() {
log.Printf("[>] Incoming %s on listener %s", conn.RemoteAddr(), addr)
defer conn.Close()
if err := gateway.serve(conn, addr); err != nil {
log.Printf("[x] %s closed connection with %s; error: %s", conn.RemoteAddr(), addr, err)
return
}
log.Printf("[x] %s closed connection with %s", conn.RemoteAddr(), addr)
}()
}
}
func (gateway *Gateway) serve(conn Conn, addr string) error {
connRemoteAddr := conn.RemoteAddr()
if gateway.receiveProxyProtocol {
header, err := proxyproto.Read(conn.Reader())
if err != nil {
return err
}
connRemoteAddr = header.SourceAddr
}
pk, err := conn.PeekPacket()
if err != nil {
return err
}
hs, err := handshaking.UnmarshalServerBoundHandshake(pk)
if err != nil {
return err
}
proxyUID := proxyUID(hs.ParseServerAddress(), addr)
log.Printf("[i] %s requests proxy with UID %s", connRemoteAddr, proxyUID)
v, ok := gateway.Proxies.Load(proxyUID)
if !ok {
// Client send an invalid address/port; we don't have a v for that address
return errors.New("no proxy with uid " + proxyUID)
}
proxy := v.(*Proxy)
if err := proxy.handleConn(conn, connRemoteAddr); err != nil {
proxy.CallbackLogger().LogEvent(callback.ErrorEvent{
Error: err.Error(),
ProxyUID: proxyUID,
})
return err
}
return nil
}