/
SubDataClient.java
486 lines (436 loc) · 20.6 KB
/
SubDataClient.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package net.ME1312.SubData.Client;
import net.ME1312.Galaxi.Library.Callback.Callback;
import net.ME1312.Galaxi.Library.Callback.ReturnCallback;
import net.ME1312.Galaxi.Library.Container;
import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.Galaxi.Library.NamedContainer;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.SubData.Client.Encryption.NEH;
import net.ME1312.SubData.Client.Library.ConnectionState;
import net.ME1312.SubData.Client.Library.DebugUtil;
import net.ME1312.SubData.Client.Library.DisconnectReason;
import net.ME1312.SubData.Client.Library.Exception.EncryptionException;
import net.ME1312.SubData.Client.Library.Exception.EndOfStreamException;
import net.ME1312.SubData.Client.Library.Exception.IllegalMessageException;
import net.ME1312.SubData.Client.Library.Exception.IllegalPacketException;
import net.ME1312.SubData.Client.Protocol.*;
import net.ME1312.SubData.Client.Protocol.Initial.InitPacketDeclaration;
import net.ME1312.SubData.Client.Protocol.Initial.InitialPacket;
import net.ME1312.SubData.Client.Protocol.Initial.InitialProtocol;
import net.ME1312.SubData.Client.Protocol.Internal.*;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.*;
import java.util.logging.Logger;
import static net.ME1312.SubData.Client.Library.ConnectionState.*;
import static net.ME1312.SubData.Client.Library.DisconnectReason.*;
/**
* SubData Client Class
*/
public class SubDataClient extends DataClient {
private Socket socket;
private LinkedList<PacketOut> queue;
private LinkedList<PacketOut> prequeue;
private OutputStream out;
private SubDataProtocol protocol;
private Cipher cipher = NEH.get();
private int cipherlevel = 0;
private ConnectionState state;
private Callback<Runnable> scheduler;
private Logger log;
SubDataClient(SubDataProtocol protocol, Callback<Runnable> scheduler, Logger log, InetAddress address, int port) throws IOException {
if (Util.isNull(address, port)) throw new NullPointerException();
this.protocol = protocol;
this.scheduler = scheduler;
this.log = log;
this.state = PRE_INITIALIZATION;
this.socket = new Socket(address, port);
this.out = socket.getOutputStream();
this.queue = null;
this.prequeue = new LinkedList<>();
log.info("Connected to " + socket.getRemoteSocketAddress());
read();
}
private void read(Container<Boolean> reset, InputStream data) {
try {
ByteArrayOutputStream pending = new ByteArrayOutputStream();
int id = -1, version = -1;
int b, position = 0;
while (position < 4 && (b = data.read()) != -1) {
position++;
pending.write(b);
switch (position) {
case 2:
id = ((int) ByteBuffer.wrap(pending.toByteArray()).order(ByteOrder.LITTLE_ENDIAN).getShort()) + -Short.MIN_VALUE;
pending.reset();
break;
case 4:
version = ((int) ByteBuffer.wrap(pending.toByteArray()).order(ByteOrder.LITTLE_ENDIAN).getShort()) + -Short.MIN_VALUE;
pending.reset();
break;
}
}
// Step 4 // Create a detached data forwarding InputStream
if (state != CLOSED && id >= 0 && version >= 0) {
InputStream forward = new InputStream() {
boolean open = true;
@Override
public int read() throws IOException {
if (open) {
int b = data.read();
if (b < 0) close();
return b;
} else return -1;
}
@Override
public void close() throws IOException {
open = false;
while (data.read() != -1);
}
};
HashMap<Integer, PacketIn> pIn = (state.asInt() >= READY.asInt())?protocol.pIn:Util.reflect(InitialProtocol.class.getDeclaredField("pIn"), null);
if (!pIn.keySet().contains(id)) throw new IllegalPacketException(getAddress().toString() + ": Could not find handler for packet: [" + DebugUtil.toHex(0xFFFF, id) + ", " + DebugUtil.toHex(0xFFFF, version) + "]");
PacketIn packet = pIn.get(id);
if (!packet.isCompatable(version)) throw new IllegalPacketException(getAddress().toString() + ": The handler does not support this packet version (" + DebugUtil.toHex(0xFFFF, packet.version()) + "): [" + DebugUtil.toHex(0xFFFF, id) + ", " + DebugUtil.toHex(0xFFFF, version) + "]");
// Step 5 // Invoke the Packet
if (state == PRE_INITIALIZATION && !(packet instanceof InitPacketDeclaration)) {
DebugUtil.logException(new IllegalStateException("Only InitPacketDeclaration may be received during the PRE_INITIALIZATION stage"), log);
close(PROTOCOL_MISMATCH);
} else if (state == CLOSING && !(packet instanceof PacketDisconnectUnderstood)) {
forward.close();
} else {
scheduler.run(() -> {
try {
packet.receive(this);
if (packet instanceof PacketStreamIn) {
((PacketStreamIn) packet).receive(this, forward);
} else forward.close();
} catch (Throwable e) {
DebugUtil.logException(new InvocationTargetException(e, getAddress().toString() + ": Exception while running packet handler"), log);
if (state.asInt() <= INITIALIZATION.asInt())
Util.isException(() -> close(PROTOCOL_MISMATCH)); // Issues during the init stages are signs of a PROTOCOL_MISMATCH
}
});
}
}
} catch (Exception e) {
if (!reset.get()) try {
if (!(e instanceof SocketException && !Boolean.getBoolean("subdata.debug"))) {
DebugUtil.logException(e, log);
} if (!(e instanceof SocketException)) {
close(UNHANDLED_EXCEPTION);
} else close(CONNECTION_INTERRUPTED);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
void read() {
if (!socket.isClosed()) new Thread(() -> {
Container<Boolean> reset = new Container<>(false);
try {
// Step 1 // Parse Escapes in the Encrypted Data
InputStream in = socket.getInputStream();
InputStream raw = new InputStream() {
boolean open = true;
boolean finished = false;
private int next() throws IOException {
int b = in.read();
switch (b) {
case -1:
throw new EndOfStreamException();
case '\u0010': // [DLE] (Escape character)
b = in.read();
break;
case '\u0018': // [CAN] (Read Reset character)
if (state != PRE_INITIALIZATION)
reset.set(true);
case '\u0017': // [ETB] (End of Packet character)
finished = true;
b = -1;
break;
}
return b;
}
@Override
public int read() throws IOException {
if (open) {
int b = next();
if (b <= -1) close();
return b;
} else return -1;
}
@Override
public void close() throws IOException {
if (open) {
open = false;
if (!socket.isClosed()) {
if (!finished) {
while (next() != -1);
}
}
SubDataClient.this.read();
}
}
};
// Step 3 // Parse the SubData Packet Formatting
PipedInputStream data = new PipedInputStream(1024);
new Thread(() -> read(reset, data), "SubDataClient::Packet_Listener(" + socket.getLocalAddress().toString() + ')').start();
// Step 2 // Decrypt the Data
PipedOutputStream forward = new PipedOutputStream(data);
cipher.decrypt(raw, forward);
forward.close();
} catch (Exception e) {
if (!reset.get()) try {
if (!(e instanceof SocketException && !Boolean.getBoolean("subdata.debug"))) {
DebugUtil.logException(e, log);
} if (!(e instanceof SocketException)) {
if (e instanceof EncryptionException)
close(ENCRYPTION_MISMATCH); // Classes that extend EncryptionException being thrown signify an ENCRYPTION_MISMATCH
else close(UNHANDLED_EXCEPTION);
} else close(CONNECTION_INTERRUPTED);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}, "SubDataClient::Data_Listener(" + socket.getLocalAddress().toString() + ')').start();
}
private void write(PacketOut next, OutputStream data) {
// Step 1 // Create a detached data forwarding OutputStream
try {
OutputStream forward = new OutputStream() {
boolean open = true;
@Override
public void write(int b) throws IOException {
if (open) data.write(b);
}
@Override
public void close() throws IOException {
open = false;
Util.isException(data::close);
}
};
// Step 2 // Write the Packet Metadata
HashMap<Class<? extends PacketOut>, Integer> pOut = (state.asInt() >= READY.asInt())?protocol.pOut:Util.reflect(InitialProtocol.class.getDeclaredField("pOut"), null);
if (!pOut.keySet().contains(next.getClass())) throw new IllegalMessageException("Could not find ID for packet: " + next.getClass().getCanonicalName());
if (next.version() > 65535 || next.version() < 0) throw new IllegalMessageException("Packet version is not in range (0x0000 to 0xFFFF): " + next.getClass().getCanonicalName());
data.write(ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort((short) (pOut.get(next.getClass()) + Short.MIN_VALUE)).array());
data.write(ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort((short) (next.version() + Short.MIN_VALUE)).array());
data.flush();
// Step 3 // Invoke the Packet
scheduler.run(() -> {
try {
if (next instanceof PacketStreamOut) {
((PacketStreamOut) next).send(this, forward);
} else forward.close();
} catch (Throwable e) {
DebugUtil.logException(e, log);
Util.isException(forward::close);
}
});
} catch (Throwable e) {
DebugUtil.logException(e, log);
Util.isException(data::close);
}
}
void write() {
if (queue != null && !socket.isClosed()) new Thread(() -> {
if (queue.size() > 0) {
try {
PacketOut next = Util.getDespiteException(() -> queue.get(0), null);
Util.isException(() -> queue.remove(0));
if (next != null) {
PipedOutputStream data = new PipedOutputStream();
PipedInputStream raw = new PipedInputStream(data, 1024);
new Thread(() -> write(next, data), "SubDataClient::Packet_Writer(" + socket.getLocalAddress().toString() + ')').start();
// Step 5 // Add Escapes to the Encrypted Data
OutputStream forward = new OutputStream() {
boolean open = true;
@Override
public void write(int b) throws IOException {
if (open) {
switch (b) {
case '\u0010': // [DLE] (Escape character)
case '\u0018': // [CAN] (Reader Reset character)
case '\u0017': // [ETB] (End of Packet character)
out.write('\u0010');
break;
}
out.write(b);
out.flush();
}
}
@Override
public void close() throws IOException {
if (open) {
open = false;
out.write('\u0017');
out.flush();
if (queue.size() > 0) SubDataClient.this.write();
else queue = null;
}
}
};
// Step 4 // Encrypt the Data
cipher.encrypt(raw, forward);
forward.close();
raw.close();
}
} catch (Throwable e) {
Util.isException(() -> queue.remove(0));
DebugUtil.logException(e, log);
if (queue.size() > 0) SubDataClient.this.write();
else queue = null;
}
} else queue = null;
}, "SubDataClient::Data_Writer(" + socket.getLocalAddress().toString() + ')').start();
}
/**
* Send a packet to Client
*
* @param packet Packet to send
*/
public void sendPacket(PacketOut packet) {
if (Util.isNull(packet)) throw new NullPointerException();
if (!isClosed()) {
if (state.asInt() < READY.asInt() && !(packet instanceof InitialPacket)) {
prequeue.add(packet);
} else if (state == CLOSING && !(packet instanceof PacketDisconnect || packet instanceof PacketDisconnectUnderstood)) {
// do nothing
} else {
boolean init = false;
if (queue == null) {
queue = new LinkedList<>();
init = true;
}
queue.add(packet);
if (init) write();
}
}
}
/**
* Forward a packet to another Client
*
* @param id Client ID
* @param packet Packet to send
*/
public void forwardPacket(UUID id, PacketOut packet) {
if (Util.isNull(id, packet)) throw new NullPointerException();
sendPacket(new PacketForwardPacket(id, packet));
}
public void sendMessage(MessageOut message) {
if (Util.isNull(message)) throw new NullPointerException();
sendPacket(new PacketSendMessage(message));
}
public void forwardMessage(UUID id, MessageOut message) {
if (Util.isNull(id, message)) throw new NullPointerException();
forwardPacket(id, new PacketSendMessage(message));
}
@Override
public void getClient(UUID id, Callback<ObjectMap<String>> callback) {
if (Util.isNull(id, callback)) throw new NullPointerException();
StackTraceElement[] origin = new Exception().getStackTrace();
sendPacket(new PacketDownloadClientList(id, data -> {
ObjectMap<String> serialized = null;
if (data.contains(id.toString())) {
serialized = data.getMap(id.toString());
}
try {
callback.run(serialized);
} catch (Throwable e) {
Throwable ew = new InvocationTargetException(e);
ew.setStackTrace(origin);
ew.printStackTrace();
}
}));
}
@Override
public void getClients(Callback<Map<UUID, ? extends ObjectMap<String>>> callback) {
if (Util.isNull(callback)) throw new NullPointerException();
StackTraceElement[] origin = new Exception().getStackTrace();
sendPacket(new PacketDownloadClientList(data -> {
HashMap<UUID, ObjectMap<String>> serialized = new HashMap<UUID, ObjectMap<String>>();
for (String id : data.getKeys()) {
serialized.put(UUID.fromString(id), data.getMap(id));
}
try {
callback.run(serialized);
} catch (Throwable e) {
Throwable ew = new InvocationTargetException(e);
ew.setStackTrace(origin);
ew.printStackTrace();
}
}));
}
/**
* Get the underlying Client Socket
*
* @return Client Socket
*/
public Socket getSocket() {
return socket;
}
public SubDataProtocol getProtocol() {
return protocol;
}
public InetSocketAddress getAddress() {
return new InetSocketAddress(socket.getInetAddress(), socket.getPort());
}
public void close() throws IOException {
if (state.asInt() < CLOSING.asInt() && !socket.isClosed()) {
boolean result = true;
LinkedList<ReturnCallback<DataClient, Boolean>> events = on.close;
on.close = new LinkedList<>();
for (ReturnCallback<DataClient, Boolean> next : events) try {
if (next != null) result = next.run(this) != Boolean.FALSE && result;
} catch (Throwable e) {
DebugUtil.logException(new InvocationTargetException(e, "Unhandled exception while running SubData Event"), log);
}
if (result) {
state = CLOSING;
if (!isClosed()) sendPacket(new PacketDisconnect());
Timer timeout = new Timer("SubDataClient::Disconnect_Timeout(" + socket.getLocalAddress().toString() + ')');
timeout.schedule(new TimerTask() {
@Override
public void run() {
if (!socket.isClosed()) try {
close(CLOSE_REQUESTED);
} catch (IOException e) {
DebugUtil.logException(e, log);
}
}
}, 5000);
}
}
}
void close(DisconnectReason reason) throws IOException {
if (state != CLOSED && !socket.isClosed()) {
if (state == CLOSING && reason == CONNECTION_INTERRUPTED) reason = CLOSE_REQUESTED;
state = CLOSED;
if (reason != CLOSE_REQUESTED) {
DebugUtil.logException(new SocketException("Connection closed: " + reason), log);
}
socket.close();
log.info("Disconnected from " + socket.getRemoteSocketAddress());
final DisconnectReason freason = reason;
scheduler.run(() -> {
LinkedList<Callback<NamedContainer<DisconnectReason, DataClient>>> events = on.closed;
on.closed = new LinkedList<>();
for (Callback<NamedContainer<DisconnectReason, DataClient>> next : events) try {
if (next != null) next.run(new NamedContainer<>(freason, this));
} catch (Throwable e) {
DebugUtil.logException(new InvocationTargetException(e, "Unhandled exception while running SubData Event"), log);
}
});
}
}
public boolean isClosed() {
return socket.isClosed();
}
}