/
MCBridge.java
executable file
·299 lines (262 loc) · 8.34 KB
/
MCBridge.java
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
package com.raphfrk.craftproxylib;
import java.io.EOFException;
import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.atomic.AtomicBoolean;
import com.raphfrk.craftproxylib.handler.ConnectionHandler;
import com.raphfrk.craftproxylib.handler.PacketHandler;
import com.raphfrk.craftproxylib.handler.PacketHandlerRegistry;
import com.raphfrk.craftproxylib.packet.Packet;
/**
* Represents a bridge for two MCSockets
*/
public class MCBridge {
private final MCPassthroughThread serverToClientThread;
private final MCPassthroughThread clientToServerThread;
private final PacketHandler[] upstream;
private final PacketHandler[] downstream;
private final ConnectionHandler connectionHandler;
public MCBridge(Socket server, Socket client) throws IOException {
this(new MCSocket(server, true), new MCSocket(client, false));
}
public MCBridge(Socket server, Socket client, PacketHandlerRegistry registry) throws IOException {
this(new MCSocket(server, true), new MCSocket(client, false), registry);
}
public MCBridge(MCSocket server, MCSocket client) {
this(server, client, PacketHandlerRegistry.nullRegistry);
}
public MCBridge(MCSocket server, MCSocket client, PacketHandlerRegistry registry) {
this(server, client, registry, null);
}
public MCBridge(MCSocket server, MCSocket client, PacketHandlerRegistry registry, ConnectionHandler connectionHandler) {
this(server, client, registry.getUpstreamHandlers(), registry.getDownstreamHandlers(), connectionHandler);
}
public MCBridge(MCSocket server, MCSocket client, MCBridge bridge) {
this(server, client, bridge, null);
}
public MCBridge(MCSocket server, MCSocket client, MCBridge bridge, ConnectionHandler connectionHandler) {
this(server, client, bridge.getUpstreamHandlers(), bridge.getDownstreamHandlers(), connectionHandler);
}
public MCBridge(MCSocket server, MCSocket client, PacketHandler[] upstream, PacketHandler[] downstream, ConnectionHandler connectionHandler) {
AtomicBoolean closeSync = new AtomicBoolean(false);
this.upstream = createNewInstances(upstream);
this.downstream = createNewInstances(downstream);
this.serverToClientThread = new MCPassthroughThread(server, client, closeSync, this.downstream);
this.clientToServerThread = new MCPassthroughThread(client, server, closeSync, this.upstream);
this.serverToClientThread.setReturnThread(clientToServerThread);
this.clientToServerThread.setReturnThread(serverToClientThread);
this.connectionHandler = connectionHandler;
}
/**
* Starts the threads for the bridge
*/
public void start() {
serverToClientThread.start();
clientToServerThread.start();
}
/**
* Joins against the threads for the bridge. Both threads are killed if either socket closes.
*
* @throws InterruptedException
*/
public void join() throws InterruptedException {
join(0);
}
/**
* Interrupts the bridge
*/
public void interrupt() {
serverToClientThread.interrupt();
clientToServerThread.interrupt();
}
/**
* Joins against the threads for the bridge. Both threads are killed if either socket closes.
*
* @param millis the time to wait in milliseconds, or 0 for infinite time.
* @throws InterruptedException
*/
public void join(long millis) throws InterruptedException {
long startTime = System.currentTimeMillis();
serverToClientThread.join(millis);
if (millis != 0) {
millis -= System.currentTimeMillis() - startTime;
if (millis == 0) {
return;
}
}
clientToServerThread.join(millis);
}
/**
* Gets the upstream handler instance array.
*
* @return the handlers
*/
public PacketHandler[] getUpstreamHandlers() {
return upstream;
}
/**
* Gets the upstream handler instance array.
*
* @return the handlers
*/
public PacketHandler[] getDownstreamHandlers() {
return downstream;
}
public void log(String message) {
if (connectionHandler != null) {
connectionHandler.log(message);
} else {
CraftProxyLib.log(message);
}
}
private PacketHandler[] createNewInstances(PacketHandler[] handlers) {
PacketHandler[] newHandlers = new PacketHandler[256];
if (handlers != null) {
for (int i = 0; i < 256; i++) {
if (handlers[i] != null) {
newHandlers[i] = handlers[i].newInstance(this);
}
}
}
return newHandlers;
}
private static class MCPassthroughThread extends Thread {
private static final PacketHandler[] nullList = new PacketHandler[256];
private final PacketHandler[] handlers;
private final AtomicBoolean closeSync;
private MCPassthroughThread counterFlowThread = null;
private final MCSocket from;
private final MCSocket to;
private volatile boolean running = true;
private volatile boolean readError = false;
private volatile boolean writeError = false;
private volatile boolean safeDeath = false;
public MCPassthroughThread(MCSocket from, MCSocket to, AtomicBoolean closeSync, PacketHandler[] handlers) {
this.from = from;
this.to = to;
if (handlers == null) {
this.handlers = nullList;
} else {
this.handlers = handlers;
}
this.closeSync = closeSync;
}
public void setReturnThread(MCPassthroughThread t) {
this.counterFlowThread = t;
}
public boolean isReadError() {
if (isAlive()) {
throw new UnsupportedOperationException("This method should not be called while the thread is running");
}
return !safeDeath || readError;
}
public boolean isWriteError() {
if (isAlive()) {
throw new UnsupportedOperationException("This method should not be called while the thread is running");
}
return !safeDeath || writeError;
}
public void interrupt() {
running = false;
super.interrupt();
}
public void run() {
Packet p = null;
try {
while (running) {
try {
p = from.readPacket(p);
} catch (EOFException eof) {
running = false;
readError = true;
continue;
} catch (IOException ioe) {
running = false;
readError = true;
ioe.printStackTrace();
continue;
} catch (Exception e) {
running = false;
readError = true;
e.printStackTrace();
continue;
}
int id = p.getId();
if (id == -1) {
readError = true;
running = false;
throw new IllegalStateException("Incomplete packet read");
}
PacketHandler handler = handlers[id];
if (handler != null) {
try {
p = handler.handle(p, to.getOutputStream(), from.getOutputStream());
} catch (IOException ioe) {
readError = true;
writeError = true;
running = false;
throw new RuntimeException("Handler " + handler.getClass().getSimpleName() + " threw an IOException", ioe);
} catch (Exception e) {
readError = true;
writeError = true;
running = false;
throw new RuntimeException("Handler " + handler.getClass().getSimpleName() + " threw an exception", e);
}
}
if (p == PacketHandler.INTERRUPT) {
running = false;
} else if (p != null) {
try {
to.writePacket(p);
} catch (IOException ioe) {
ioe.printStackTrace();
running = false;
writeError = true;
} catch (Exception e) {
e.printStackTrace();
running = false;
writeError = true;
}
}
}
safeDeath = true;
} finally {
if (counterFlowThread == null) {
if (isReadError()) {
try {
from.close();
} catch (IOException ioe) {
}
}
if (isWriteError()) {
try {
to.close();
} catch (IOException ioe) {
}
}
} else {
counterFlowThread.interrupt();
boolean counterFlowThreadDead = !closeSync.compareAndSet(false, true);
if (counterFlowThreadDead) {
try {
counterFlowThread.join();
} catch (InterruptedException e) {
}
if (readError || counterFlowThread.isWriteError()) {
try {
from.close();
} catch (IOException ioe) {
}
}
if (writeError || counterFlowThread.isReadError()) {
try {
to.close();
} catch (IOException ioe) {
}
}
}
}
}
}
}
}