-
Notifications
You must be signed in to change notification settings - Fork 76
/
proxy.go
314 lines (251 loc) · 8.44 KB
/
proxy.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package proxy
import (
"fmt"
"math/rand"
"net"
"regexp"
"time"
"github.com/jhead/phantom/internal/clientmap"
"github.com/jhead/phantom/internal/proto"
"github.com/rs/zerolog/log"
"github.com/tevino/abool"
reuse "github.com/libp2p/go-reuseport"
)
const maxMTU = 1472
var idleCheckInterval = 5 * time.Second
type ProxyServer struct {
bindAddress *net.UDPAddr
boundPort uint16
remoteServerAddress *net.UDPAddr
pingServer net.PacketConn
pingServerV6 net.PacketConn
server *net.UDPConn
clientMap *clientmap.ClientMap
prefs ProxyPrefs
dead *abool.AtomicBool
serverOffline bool
}
type ProxyPrefs struct {
BindAddress string
BindPort uint16
RemoteServer string
IdleTimeout time.Duration
EnableIPv6 bool
RemovePorts bool
NumWorkers uint
}
var randSource = rand.NewSource(time.Now().UnixNano())
var serverID = randSource.Int63()
var offlineErrorRegex = regexp.MustCompile("(timeout)|(connection refused)")
func New(prefs ProxyPrefs) (*ProxyServer, error) {
bindPort := prefs.BindPort
// Randomize port if not provided
if bindPort == 0 {
bindPort = (uint16(randSource.Int63()) % 14000) + 50000
}
// Format full bind address with port
prefs.BindAddress = fmt.Sprintf("%s:%d", prefs.BindAddress, bindPort)
bindAddress, err := net.ResolveUDPAddr("udp", prefs.BindAddress)
if err != nil {
return nil, fmt.Errorf("Invalid bind address: %s", err)
}
remoteServerAddress, err := net.ResolveUDPAddr("udp", prefs.RemoteServer)
if err != nil {
return nil, fmt.Errorf("Invalid server address: %s", err)
}
return &ProxyServer{
bindAddress,
bindPort,
remoteServerAddress,
nil,
nil,
nil,
clientmap.New(prefs.IdleTimeout, idleCheckInterval),
prefs,
abool.New(),
false,
}, nil
}
func (proxy *ProxyServer) Start() error {
// Bind to 19132 on all addresses to receive broadcasted pings
// Sets SO_REUSEADDR et al to support multiple instances of phantom
log.Info().Msgf("Binding ping server to port 19132")
if pingServer, err := reuse.ListenPacket("udp4", ":19132"); err == nil {
proxy.pingServer = pingServer
// Start proxying ping packets from the broadcast listener
go proxy.readLoop(proxy.pingServer)
} else {
// Bind failed
return err
}
// Minecraft automatically broadcasts on port 19133 to the local IPv6 network
if proxy.prefs.EnableIPv6 {
log.Info().Msgf("Binding IPv6 ping server to port 19133")
if pingServerV6, err := reuse.ListenPacket("udp6", ":19133"); err == nil {
proxy.pingServerV6 = pingServerV6
// Start proxying ping packets from the broadcast listener
go proxy.readLoop(proxy.pingServerV6)
} else {
// IPv6 Bind failed
log.Warn().Msgf("Failed to bind IPv6 ping listener: %v", err)
}
}
network := "udp4"
if proxy.prefs.EnableIPv6 {
network = "udp"
}
// Bind to specified UDP addr and port to receive data from Minecraft clients
log.Info().Msgf("Binding proxy server to: %v", proxy.bindAddress)
if server, err := reuse.ListenPacket(network, proxy.bindAddress.String()); err == nil {
// a safe cast, I promise
proxy.server = server.(*net.UDPConn)
} else {
return err
}
log.Info().Msgf("Proxy server listening!")
log.Info().Msgf("Once your console pings phantom, you should see replies below.")
// Start processing everything else using the proxy listener
proxy.startWorkers(proxy.server)
return nil
}
func (proxy *ProxyServer) Close() {
log.Info().Msgf("Stopping proxy server")
// Stop UDP listeners
proxy.server.Close()
proxy.pingServer.Close()
if proxy.pingServerV6 != nil {
proxy.pingServerV6.Close()
}
// Close all connections
proxy.clientMap.Close()
// Stop loops
proxy.dead.Set()
}
func (proxy *ProxyServer) startWorkers(listener net.PacketConn) {
log.Info().Msgf("Starting %d workers", proxy.prefs.NumWorkers)
for i := uint(0); i < proxy.prefs.NumWorkers; i++ {
if i < proxy.prefs.NumWorkers-1 {
go proxy.readLoop(listener)
} else {
proxy.readLoop(listener)
}
}
}
// Continually reads data from the provided listener and passes it to
// processDataFromClients until the ProxyServer has been closed.
func (proxy *ProxyServer) readLoop(listener net.PacketConn) {
log.Info().Msgf("Listener starting up: %s", listener.LocalAddr())
packetBuffer := make([]byte, maxMTU)
for !proxy.dead.IsSet() {
err := proxy.processDataFromClients(listener, packetBuffer)
if err != nil {
log.Warn().Msgf("Error while processing client data: %s", err)
}
}
log.Info().Msgf("Listener shut down: %s", listener.LocalAddr())
}
// Inspects an incoming UDP packet, looking up the client in our connection
// map, lazily creating a new connection to the remote server when necessary,
// then forwarding the data to that remote connection.
//
// When a new client connects, an additional goroutine is created to read
// data from the server and send it back to the client.
func (proxy *ProxyServer) processDataFromClients(listener net.PacketConn, packetBuffer []byte) error {
// Read the next packet from the client
read, client, _ := listener.ReadFrom(packetBuffer)
if read <= 0 {
return nil
}
data := packetBuffer[:read]
log.Trace().Msgf("client recv: %v", data)
// Handler triggered when a new client connects and we create a new connetion to the remote server
onNewConnection := func(newServerConn *net.UDPConn) {
log.Info().Msgf("New connection from client %s -> %s", client.String(), listener.LocalAddr())
proxy.processDataFromServer(newServerConn, client)
}
serverConn, err := proxy.clientMap.Get(
client,
proxy.remoteServerAddress,
onNewConnection,
)
if err != nil {
return err
}
// Wait 5 seconds for the server to respond to whatever we sent, or else timeout
_ = serverConn.SetReadDeadline(time.Now().Add(time.Second * 5))
if packetID := data[0]; packetID == proto.UnconnectedPingID {
log.Info().Msgf("Received LAN ping from client: %s", client.String())
if proxy.serverOffline {
replyBuffer := proto.OfflinePong
replyBytes := proxy.rewriteUnconnectedPong(replyBuffer.Bytes())
proxy.server.WriteTo(replyBytes, client)
log.Info().Msgf("Sent server offline pong to client: %v", client.String())
}
// Pass ping through to server even if it's offline
}
// Write packet from client to server
_, err = serverConn.Write(data)
return err
}
// Proxies packets sent by the server to us for a specific Minecraft client back to
// that client's UDP connection.
func (proxy *ProxyServer) processDataFromServer(remoteConn *net.UDPConn, client net.Addr) {
buffer := make([]byte, maxMTU)
for !proxy.dead.IsSet() {
// Read the next packet from the server
read, _, err := remoteConn.ReadFrom(buffer)
// Remove read timeout, server responded
_ = remoteConn.SetReadDeadline(time.Time{})
// Read error
if err != nil {
log.Warn().Msgf("%v", err)
offlineError := offlineErrorRegex.MatchString(err.Error())
if offlineError && !proxy.serverOffline {
log.Warn().Msgf("Server seems to be offline :(")
log.Warn().Msgf("We'll keep trying to connect...")
proxy.serverOffline = true
}
break
}
// Empty read
if read < 1 {
continue
}
if proxy.serverOffline {
log.Info().Msgf("Server is back online!")
proxy.serverOffline = false
}
// Resize data to byte count from 'read'
data := buffer[:read]
log.Trace().Msgf("server recv: %v", data)
// Rewrite Unconnected Pong packets
if packetID := data[0]; packetID == proto.UnconnectedPongID {
data = proxy.rewriteUnconnectedPong(data)
log.Info().Msgf("Sent LAN pong to client: %v", client.String())
}
proxy.server.WriteTo(data, client)
}
proxy.clientMap.Delete(client)
}
func (proxy *ProxyServer) rewriteUnconnectedPong(data []byte) []byte {
log.Debug().Msgf("Received Unconnected Pong from server: %v", data)
if packet, err := proto.ReadUnconnectedPing(data); err == nil {
// Overwrite the server ID with one unique to this phantom instance.
// If we don't do this, the client will get confused if you restart phantom.
packet.Pong.ServerID = fmt.Sprintf("%d", serverID)
// Overwrite port numbers sent back from server (if any)
if packet.Pong.Port4 != "" && !proxy.prefs.RemovePorts {
packet.Pong.Port4 = fmt.Sprintf("%d", proxy.boundPort)
packet.Pong.Port6 = packet.Pong.Port4
} else if proxy.prefs.RemovePorts {
packet.Pong.Port4 = ""
packet.Pong.Port6 = ""
}
packetBuffer := packet.Build()
log.Debug().Msgf("Unconnected Pong: %v", packet)
return packetBuffer.Bytes()
} else {
log.Warn().Msgf("Failed to rewrite pong: %v", err)
}
return data
}