-
Notifications
You must be signed in to change notification settings - Fork 5
/
DepenizenBungee.java
287 lines (263 loc) · 11.4 KB
/
DepenizenBungee.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
package com.denizenscript.depenizen.bungee;
import com.denizenscript.depenizen.bungee.packets.in.*;
import com.denizenscript.depenizen.bungee.packets.out.*;
import io.netty.channel.Channel;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.*;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.connection.InitialHandler;
import net.md_5.bungee.event.EventHandler;
import net.md_5.bungee.event.EventPriority;
import net.md_5.bungee.netty.ChannelWrapper;
import java.lang.invoke.MethodHandle;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class DepenizenBungee extends Plugin implements Listener {
public static DepenizenBungee instance;
public HashMap<Integer, PacketIn> packets = new HashMap<>();
public final List<DepenizenConnection> connections = new ArrayList<>();
public void addConnection(DepenizenConnection connection) {
synchronized (connections) {
connections.add(connection);
}
}
public void removeConnection(DepenizenConnection connection) {
synchronized (connections) {
connections.remove(connection);
}
}
public List<DepenizenConnection> getConnections() {
synchronized (connections) {
return new ArrayList<>(connections);
}
}
public DepenizenConnection getConnectionByName(String name) {
name = name.toLowerCase(Locale.ENGLISH);
for (DepenizenConnection connection : getConnections()) {
if (connection.thisServer != null && name.equals(connection.thisServer.getName().toLowerCase(Locale.ENGLISH))) {
return connection;
}
}
return null;
}
public void registerPackets() {
packets.put(1, new KeepAlivePacketIn());
packets.put(MyInfoPacketIn.PACKET_ID, new MyInfoPacketIn());
packets.put(12, new ControlProxyPingPacketIn());
packets.put(13, new ProxyPingResultPacketIn());
packets.put(14, new RedirectPacketIn());
packets.put(15, new ExecuteCommandPacketIn());
packets.put(16, new ControlProxyCommandPacketIn());
packets.put(17, new ProxyCommandResultPacketIn());
}
@Override
public void onEnable() {
instance = this;
getLogger().info("DepenizenBungee loading...");
getProxy().getPluginManager().registerListener(this, this);
registerPackets();
ProxyServer.getInstance().getScheduler().schedule(this, new Runnable() {
@Override
public void run() {
long curTime = System.currentTimeMillis();
KeepAlivePacketOut packet = new KeepAlivePacketOut();
for (DepenizenConnection connection : getConnections()) {
if (connection.thisServer == null) {
continue;
}
if (curTime > connection.lastPacketReceived + 20 * 1000) {
// 20 seconds without a packet = connection lost!
connection.fail("Connection time out.");
}
else {
connection.sendPacket(packet);
}
}
}
}, 1, 1, TimeUnit.SECONDS);
}
public void broadcastPacket(PacketOut packet) {
for (DepenizenConnection connection : getConnections()) {
connection.sendPacket(packet);
}
}
@EventHandler
public void onProxyPing(ProxyPingEvent event) {
for (DepenizenConnection connection : getConnections()) {
if (connection.controlsProxyPing && connection.isValid) {
event.registerIntent(this);
long id = connection.proxyPingId++;
connection.proxyEventMap.put(id, event);
ProxyPingPacketOut packet = new ProxyPingPacketOut();
packet.id = id;
packet.address = event.getConnection().getAddress().toString();
packet.currentPlayers = event.getResponse().getPlayers().getOnline();
packet.maxPlayers = event.getResponse().getPlayers().getMax();
packet.motd = event.getResponse().getDescriptionComponent().toLegacyText();
packet.protocol = event.getResponse().getVersion().getProtocol();
packet.version = event.getResponse().getVersion().getName();
connection.sendPacket(packet);
}
}
}
@EventHandler
public void onServerSwitch(ServerConnectEvent event) {
PlayerSwitchServerPacketOut packet = new PlayerSwitchServerPacketOut();
packet.name = event.getPlayer().getName();
packet.uuid = event.getPlayer().getUniqueId();
packet.newServer = event.getTarget().getName();
broadcastPacket(packet);
}
@EventHandler
public void onPlayerJoin(PostLoginEvent event) {
PlayerJoinPacketOut packet = new PlayerJoinPacketOut();
packet.name = event.getPlayer().getName();
packet.uuid = event.getPlayer().getUniqueId();
broadcastPacket(packet);
}
@EventHandler
public void onPlayerQuit(PlayerDisconnectEvent event) {
PlayerQuitPacketOut packet = new PlayerQuitPacketOut();
packet.name = event.getPlayer().getName();
packet.uuid = event.getPlayer().getUniqueId();
broadcastPacket(packet);
}
public static boolean proxyCommandNoDup = false;
public static long proxyCommandId = 1;
public HashMap<Long, CompletableFuture<String>> proxyCommandWaiters = new HashMap<>();
@EventHandler(priority = EventPriority.HIGHEST)
public void onProxyCommand(ChatEvent event) {
if (!event.isProxyCommand()) {
return;
}
if (event.isCancelled()) {
return;
}
if (proxyCommandNoDup) {
return;
}
List<CompletableFuture<String>> futures = new ArrayList<>();
for (DepenizenConnection connection : getConnections()) {
if (connection.controlsProxyCommand && connection.isValid) {
long newId = proxyCommandId++;
CompletableFuture<String> future = new CompletableFuture<>();
futures.add(future);
proxyCommandWaiters.put(newId, future);
connection.sendPacket(new ProxyCommandPacketOut(newId, ((CommandSender) event.getSender()).getName(),
event.getSender() instanceof ProxiedPlayer ? ((ProxiedPlayer) event.getSender()).getUniqueId() : null, event.getMessage()));
}
}
if (futures.isEmpty()) {
return;
}
event.setCancelled(true);
String[] command = new String[1];
command[0] = event.getMessage();
Runnable finished = new Runnable() {
@Override
public void run() {
proxyCommandNoDup = true;
try {
ProxyServer.getInstance().getPluginManager().dispatchCommand((CommandSender) event.getSender(), command[0].substring(1));
}
finally {
proxyCommandNoDup = false;
}
}
};
ProxyServer.getInstance().getScheduler().runAsync(this, new Runnable() {
@Override
public void run() {
for (CompletableFuture<String> future : futures) {
try {
String result = future.get(5, TimeUnit.SECONDS);
if (result != null) {
if (result.equalsIgnoreCase("cancelled")) {
return;
}
else if (result.startsWith("/")) {
command[0] = result;
}
}
}
catch (TimeoutException ex) {
continue;
}
catch (ExecutionException ex) {
ex.printStackTrace();
return;
}
catch (InterruptedException ex) {
ex.printStackTrace();
return;
}
}
ProxyServer.getInstance().getScheduler().schedule(DepenizenBungee.this, finished, 0, TimeUnit.SECONDS);
}
});
}
@EventHandler
public void onPlayerHandshake(PlayerHandshakeEvent event) {
InitialHandler handler = (InitialHandler) event.getConnection();
// Only operate on connections started by Depenizen
if (!handler.getExtraDataInHandshake().equals("\0depen")) {
return;
}
getLogger().info("Depenizen handshake seen from: " + handler.getAddress());
final InetAddress address = handler.getAddress().getAddress();
if (!address.isLoopbackAddress()) { // Localhost is always allowed
boolean isValid = false;
for (ServerInfo info : getProxy ().getServers().values()) {
if (info.getAddress().getAddress().equals(address)) {
isValid = true;
break;
}
}
if (!isValid) {
getLogger().warning("INVALID/FAKE Depenizen connection denied from: " + handler.getAddress() + "... if this was meant to be a real connection, make sure the server address in bungee.yml matches the IP shown here.");
return;
}
}
final Channel channel;
try {
// Set 'closed' to true, so Bungee ignores the connection
ChannelWrapper wrapper = (ChannelWrapper) INITIALHANDLER_GET_CH.invoke(handler);
CHANNELWRAPPER_SET_CLOSED.invoke(wrapper, true);
// Get the underlying netty channel to do with as we please
channel = (Channel) CHANNELWRAPPER_GET_CH.invoke(wrapper);
// Add it to the list, for the network thread to handle
final DepenizenConnection connection = new DepenizenConnection();
getProxy().getScheduler().schedule(this, new Runnable() {
@Override
public void run() {
try {
connection.build(channel, address);
}
catch (Throwable ex) {
getLogger().severe("Exception while handling Depenizen connection build...");
ex.printStackTrace();
}
}
}, 1, TimeUnit.MILLISECONDS);
}
catch (Throwable ex) {
getLogger().severe("Exception while handling Depenizen handshake...");
ex.printStackTrace();
return;
}
}
public static MethodHandle INITIALHANDLER_GET_CH = ReflectionHelper.getGetter(InitialHandler.class, "ch");
public static MethodHandle CHANNELWRAPPER_SET_CLOSED = ReflectionHelper.getSetter(ChannelWrapper.class, "closed");
public static MethodHandle CHANNELWRAPPER_GET_CH = ReflectionHelper.getGetter(ChannelWrapper.class, "ch");
}