-
Notifications
You must be signed in to change notification settings - Fork 4
/
DepenizenConnection.java
164 lines (138 loc) · 5.63 KB
/
DepenizenConnection.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
package com.denizenscript.depenizen.bungee;
import com.denizenscript.depenizen.bungee.packets.in.MyInfoPacketIn;
import com.denizenscript.depenizen.bungee.packets.out.AddServerPacketOut;
import com.denizenscript.depenizen.bungee.packets.out.RemoveServerPacketOut;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.event.ProxyPingEvent;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
public class DepenizenConnection extends ChannelInboundHandlerAdapter {
public void build(Channel channel, InetAddress address) {
this.serverAddress = address;
this.channel = channel;
connectionName = channel.remoteAddress().toString();
while (channel.pipeline().firstContext() != null) {
channel.pipeline().removeFirst();
}
channel.pipeline().addLast(this).addLast(new NettyExceptionHandler());
for (DepenizenConnection server : DepenizenBungee.instance.getConnections()) {
if (server.thisServer != null && server.thisServer.getName() != null) {
sendPacket(new AddServerPacketOut(server.thisServer.getName()));
}
}
DepenizenBungee.instance.addConnection(this);
DepenizenBungee.instance.getLogger().info("Connection '" + connectionName + "' now under control of Depenizen.");
isValid = true;
}
public void fail(String reason) {
DepenizenBungee.instance.getLogger().info("Connection '" + connectionName + (thisServer == null ? "" : (" / " + thisServer.getName())) + "' failed: " + reason);
channel.close();
}
public static enum Stage {
AWAIT_HEADER,
AWAIT_DATA
}
public void broadcastIdentity() {
ProxyServer.getInstance().getScheduler().schedule(DepenizenBungee.instance, new Runnable() {
@Override
public void run() {
if (thisServer.getName() != null) {
DepenizenBungee.instance.broadcastPacket(new AddServerPacketOut(thisServer.getName()));
}
}
}, 0, TimeUnit.MILLISECONDS);
}
public void broadcastRemoval() {
ProxyServer.getInstance().getScheduler().schedule(DepenizenBungee.instance, new Runnable() {
@Override
public void run() {
if (thisServer.getName() != null) {
DepenizenBungee.instance.broadcastPacket(new RemoveServerPacketOut(thisServer.getName()));
}
}
}, 0, TimeUnit.MILLISECONDS);
}
public boolean isValid = false;
public String connectionName;
public Stage currentStage = Stage.AWAIT_HEADER;
public Channel channel;
public ByteBuf tmp;
public int waitingLength;
public int packetId;
public int serverPort;
public InetAddress serverAddress;
public ServerInfo thisServer;
public boolean controlsProxyPing = false;
public long proxyPingId = 1;
public long lastPacketReceived = 0;
public HashMap<Long, ProxyPingEvent> proxyEventMap = new HashMap<>();
public void sendPacket(PacketOut packet) {
ByteBuf buf = channel.alloc().buffer();
packet.writeTo(buf);
ByteBuf header = channel.alloc().buffer();
header.writeInt(buf.writerIndex());
header.writeInt(packet.getPacketId());
channel.writeAndFlush(header);
channel.writeAndFlush(buf);
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
tmp = ctx.alloc().buffer(4);
lastPacketReceived = System.currentTimeMillis();
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) {
DepenizenBungee.instance.getLogger().info("Connection '" + connectionName + "' ended.");
tmp.release();
tmp = null;
isValid = false;
DepenizenBungee.instance.removeConnection(this);
broadcastRemoval();
for (ProxyPingEvent event : proxyEventMap.values()) {
event.completeIntent(DepenizenBungee.instance);
}
proxyEventMap.clear();
}
@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 (thisServer == null && packetId != MyInfoPacketIn.PACKET_ID) {
fail("Invalid FIRST packet id (must be MyInfoPacket): " + packetId);
return;
}
if (!DepenizenBungee.instance.packets.containsKey(packetId)) {
fail("Invalid packet id: " + packetId);
return;
}
}
}
if (currentStage == Stage.AWAIT_DATA) {
if (tmp.readableBytes() >= waitingLength) {
try {
lastPacketReceived = System.currentTimeMillis();
PacketIn packet = DepenizenBungee.instance.packets.get(packetId);
packet.process(this, tmp);
currentStage = Stage.AWAIT_HEADER;
}
catch (Throwable ex) {
ex.printStackTrace();
fail("Internal exception.");
return;
}
}
}
}
}