-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
BungeeClientHandler.java
109 lines (95 loc) · 3.85 KB
/
BungeeClientHandler.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
package com.denizenscript.depenizen.bukkit.bungee;
import com.denizenscript.depenizen.bukkit.Depenizen;
import com.denizenscript.depenizen.bukkit.bungee.packets.out.ControlsProxyPingPacketOut;
import com.denizenscript.depenizen.bukkit.bungee.packets.out.MyInfoPacketOut;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.Bukkit;
public class BungeeClientHandler extends ChannelInboundHandlerAdapter {
public static final byte[] FAKE_HANDSHAKE = new byte[] {
5 + 5 + 3, // Packet length prefix
0, // Packet ID 0
20, // "Protocol 20"
1 + 1 + 5, // 1-byte host name, 1 byte null, 5 bytes 'depen'
68, // Host name is the letter 'D'
0, // null byte to sneak in our identifier
(byte) 'd', (byte) 'e', (byte) 'p', (byte) 'e', (byte) 'n', // Special identifier 'depen'
80, 0, // "We're connecting to port 80"
1 // We request protocol 1: ping status (to avoid uneeded proxy functionality)
};
public ByteBuf tmp;
public void fail(String reason) {
dB.echoError("Depenizen-Bungee connection failed: " + reason);
channel.close();
}
public static enum Stage {
AWAIT_HEADER,
AWAIT_DATA
}
public Channel channel;
public int waitingLength;
public int packetId;
public Stage currentStage = Stage.AWAIT_HEADER;
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
tmp = ctx.alloc().buffer(4);
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) {
dB.log("Depenizen-Bungee connection ended.");
tmp.release();
tmp = null;
BungeeBridge.instance.connected = false;
// TODO: trigger reconnect (after a delay)
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
dB.log("Depenizen-Bungee bridge sending handshake...");
// Send a fake minecraft handshake to get us in
ByteBuf handshake = ctx.alloc().buffer(FAKE_HANDSHAKE.length);
handshake.writeBytes(FAKE_HANDSHAKE);
ctx.writeAndFlush(handshake); // Will release handshake
Bukkit.getScheduler().scheduleSyncDelayedTask(Depenizen.instance, new Runnable() {
@Override
public void run() {
BungeeBridge.instance.sendPacket(new MyInfoPacketOut(Bukkit.getPort()));
BungeeBridge.instance.sendPacket(new ControlsProxyPingPacketOut(BungeeBridge.instance.controlsProxyPing));
BungeeBridge.instance.connected = true;
}
}, 30);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf m = (ByteBuf) msg;
tmp.writeBytes(m);
m.release();
if (currentStage == Stage.AWAIT_HEADER) {
if (tmp.readableBytes() >= 8) {
waitingLength = tmp.readInt();
packetId = tmp.readInt();
currentStage = Stage.AWAIT_DATA;
if (!BungeeBridge.instance.packets.containsKey(packetId)) {
fail("Invalid packet id: " + packetId);
return;
}
}
}
if (currentStage == Stage.AWAIT_DATA) {
if (tmp.readableBytes() >= waitingLength) {
try {
PacketIn packet = BungeeBridge.instance.packets.get(packetId);
packet.process(tmp);
currentStage = Stage.AWAIT_HEADER;
}
catch (Throwable ex) {
ex.printStackTrace();
fail("Internal exception.");
return;
}
}
}
}
}