forked from SpigotMC/BungeeCord
/
UpstreamBridge.java
373 lines (329 loc) · 13.2 KB
/
UpstreamBridge.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
package net.md_5.bungee.connection;
import com.google.common.base.Preconditions;
import com.mojang.brigadier.context.StringRange;
import com.mojang.brigadier.suggestion.Suggestion;
import com.mojang.brigadier.suggestion.Suggestions;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.ServerConnection.KeepAliveData;
import net.md_5.bungee.UserConnection;
import net.md_5.bungee.Util;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.ChatEvent;
import net.md_5.bungee.api.event.PlayerDisconnectEvent;
import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.api.event.SettingsChangedEvent;
import net.md_5.bungee.api.event.TabCompleteEvent;
import net.md_5.bungee.entitymap.EntityMap;
import net.md_5.bungee.forge.ForgeConstants;
import net.md_5.bungee.netty.ChannelWrapper;
import net.md_5.bungee.netty.PacketHandler;
import net.md_5.bungee.protocol.PacketWrapper;
import net.md_5.bungee.protocol.ProtocolConstants;
import net.md_5.bungee.protocol.packet.Chat;
import net.md_5.bungee.protocol.packet.ClientChat;
import net.md_5.bungee.protocol.packet.ClientCommand;
import net.md_5.bungee.protocol.packet.ClientSettings;
import net.md_5.bungee.protocol.packet.KeepAlive;
import net.md_5.bungee.protocol.packet.PlayerListItem;
import net.md_5.bungee.protocol.packet.PlayerListItemRemove;
import net.md_5.bungee.protocol.packet.PluginMessage;
import net.md_5.bungee.protocol.packet.TabCompleteRequest;
import net.md_5.bungee.protocol.packet.TabCompleteResponse;
import net.md_5.bungee.util.AllowedCharacters;
public class UpstreamBridge extends PacketHandler
{
private final ProxyServer bungee;
private final UserConnection con;
private long lastTabCompletion = -1; //BotFilter
public UpstreamBridge(ProxyServer bungee, UserConnection con)
{
this.bungee = bungee;
this.con = con;
BungeeCord.getInstance().addConnection( con );
con.getTabListHandler().onConnect();
con.unsafe().sendPacket( BungeeCord.getInstance().registerChannels( con.getPendingConnection().getVersion() ) );
//BotFilter start
if ( con.isCallSettingsEvent() )
{
SettingsChangedEvent settingsEvent = new SettingsChangedEvent( con );
bungee.getPluginManager().callEvent( settingsEvent );
con.setCallSettingsEvent( false );
}
if ( !con.getDelayedPluginMessages().isEmpty() )
{
con.setDelayedPluginMessages( clearPluginMessages( con.getDelayedPluginMessages() ) );
}
//BotFilter end
}
//BotFilter start
private List<PluginMessage> clearPluginMessages(List<PluginMessage> delayedPluginMessages)
{
List<PluginMessage> cleared = new ArrayList<>();
for ( PluginMessage message : delayedPluginMessages )
{
try
{
this.handle( message );
cleared.add( message );
} catch ( Throwable t )
{
if ( !( t instanceof CancelSendSignal ) )
{
throw new RuntimeException( t.getMessage(), t );
}
}
}
delayedPluginMessages.clear();
return cleared;
}
//BotFilter end
@Override
public void exception(Throwable t) throws Exception
{
con.disconnect( Util.exception( t ) );
}
@Override
public void disconnected(ChannelWrapper channel) throws Exception
{
// We lost connection to the client
PlayerDisconnectEvent event = new PlayerDisconnectEvent( con );
bungee.getPluginManager().callEvent( event );
con.getTabListHandler().onDisconnect();
BungeeCord.getInstance().removeConnection( con );
if ( con.getServer() != null )
{
// Manually remove from everyone's tab list
// since the packet from the server arrives
// too late
// TODO: This should only done with server_unique
// tab list (which is the only one supported
// currently)
PlayerListItem oldPacket = new PlayerListItem();
oldPacket.setAction( PlayerListItem.Action.REMOVE_PLAYER );
PlayerListItem.Item item = new PlayerListItem.Item();
item.setUuid( con.getUniqueId() );
oldPacket.setItems( new PlayerListItem.Item[]
{
item
} );
PlayerListItemRemove newPacket = new PlayerListItemRemove();
newPacket.setUuids( new UUID[]
{
con.getUniqueId()
} );
for ( ProxiedPlayer player : con.getServer().getInfo().getPlayers() )
{
if ( player.getPendingConnection().getVersion() >= ProtocolConstants.MINECRAFT_1_19_3 )
{
player.unsafe().sendPacket( newPacket );
} else
{
player.unsafe().sendPacket( oldPacket );
}
}
con.getServer().disconnect( "Quitting" );
}
}
@Override
public void writabilityChanged(ChannelWrapper channel) throws Exception
{
if ( con.getServer() != null )
{
con.getServer().getCh().getHandle().config().setAutoRead( channel.getHandle().isWritable() );
}
}
@Override
public boolean shouldHandle(PacketWrapper packet) throws Exception
{
return con.getServer() != null || packet.packet instanceof PluginMessage;
}
@Override
public void handle(PacketWrapper packet) throws Exception
{
if ( con.getServer() != null )
{
EntityMap rewrite = con.getEntityRewrite();
if ( rewrite != null )
{
rewrite.rewriteServerbound( packet.buf, con.getClientEntityId(), con.getServerEntityId(), con.getPendingConnection().getVersion() );
}
con.getServer().getCh().write( packet );
}
}
@Override
public void handle(KeepAlive alive) throws Exception
{
//BotFilter start - fix possibility race condition caused by botfilter keep alive packet
if ( con.getServer() == null )
{
throw CancelSendSignal.INSTANCE;
}
//BotFilter end
KeepAliveData keepAliveData = con.getServer().getKeepAlives().peek();
if ( keepAliveData != null && alive.getRandomId() == keepAliveData.getId() )
{
Preconditions.checkState( keepAliveData == con.getServer().getKeepAlives().poll(), "keepalive queue mismatch" );
int newPing = (int) ( System.currentTimeMillis() - keepAliveData.getTime() );
con.getTabListHandler().onPingChange( newPing );
con.setPing( newPing );
} else
{
throw CancelSendSignal.INSTANCE;
}
}
@Override
public void handle(Chat chat) throws Exception
{
String message = handleChat( chat.getMessage() );
if ( message != null )
{
chat.setMessage( message );
con.getServer().unsafe().sendPacket( chat );
}
throw CancelSendSignal.INSTANCE;
}
@Override
public void handle(ClientChat chat) throws Exception
{
handleChat( chat.getMessage() );
}
@Override
public void handle(ClientCommand command) throws Exception
{
handleChat( "/" + command.getCommand() );
}
private String handleChat(String message)
{
for ( int index = 0, length = message.length(); index < length; index++ )
{
char c = message.charAt( index );
if ( !AllowedCharacters.isChatAllowedCharacter( c ) )
{
con.disconnect( bungee.getTranslation( "illegal_chat_characters", Util.unicode( c ) ) );
throw CancelSendSignal.INSTANCE;
}
}
ChatEvent chatEvent = new ChatEvent( con, con.getServer(), message );
if ( !bungee.getPluginManager().callEvent( chatEvent ).isCancelled() )
{
message = chatEvent.getMessage();
if ( !chatEvent.isCommand() || !bungee.getPluginManager().dispatchCommand( con, message.substring( 1 ) ) )
{
return message;
}
}
throw CancelSendSignal.INSTANCE;
}
@Override
public void handle(TabCompleteRequest tabComplete) throws Exception
{
//BotFilter start
long now = System.currentTimeMillis();
if ( con.getPendingConnection().getVersion() <= ProtocolConstants.MINECRAFT_1_12_2 && lastTabCompletion > 0 && ( now - lastTabCompletion ) <= 500 )
{
throw CancelSendSignal.INSTANCE;
}
lastTabCompletion = now;
//BotFilter end
List<String> suggestions = new ArrayList<>();
boolean isRegisteredCommand = false;
boolean isCommand = tabComplete.getCursor().startsWith( "/" );
if ( isCommand )
{
isRegisteredCommand = bungee.getPluginManager().dispatchCommand( con, tabComplete.getCursor().substring( 1 ), suggestions );
}
TabCompleteEvent tabCompleteEvent = new TabCompleteEvent( con, con.getServer(), tabComplete.getCursor(), suggestions );
bungee.getPluginManager().callEvent( tabCompleteEvent );
if ( tabCompleteEvent.isCancelled() )
{
throw CancelSendSignal.INSTANCE;
}
List<String> results = tabCompleteEvent.getSuggestions();
if ( !results.isEmpty() )
{
// Unclear how to handle 1.13 commands at this point. Because we don't inject into the command packets we are unlikely to get this far unless
// Bungee plugins are adding results for commands they don't own anyway
if ( con.getPendingConnection().getVersion() < ProtocolConstants.MINECRAFT_1_13 )
{
con.unsafe().sendPacket( new TabCompleteResponse( results ) );
} else
{
int start = tabComplete.getCursor().lastIndexOf( ' ' ) + 1;
int end = tabComplete.getCursor().length();
StringRange range = StringRange.between( start, end );
List<Suggestion> brigadier = new LinkedList<>();
for ( String s : results )
{
brigadier.add( new Suggestion( range, s ) );
}
con.unsafe().sendPacket( new TabCompleteResponse( tabComplete.getTransactionId(), new Suggestions( range, brigadier ) ) );
}
throw CancelSendSignal.INSTANCE;
}
// Don't forward tab completions if the command is a registered bungee command
if ( isRegisteredCommand )
{
throw CancelSendSignal.INSTANCE;
}
if ( isCommand && con.getPendingConnection().getVersion() < ProtocolConstants.MINECRAFT_1_13 )
{
int lastSpace = tabComplete.getCursor().lastIndexOf( ' ' );
if ( lastSpace == -1 )
{
con.setLastCommandTabbed( tabComplete.getCursor().substring( 1 ) );
}
}
}
@Override
public void handle(ClientSettings settings) throws Exception
{
con.setSettings( settings );
SettingsChangedEvent settingsEvent = new SettingsChangedEvent( con );
bungee.getPluginManager().callEvent( settingsEvent );
}
@Override
public void handle(PluginMessage pluginMessage) throws Exception
{
if ( pluginMessage.getTag().equals( "BungeeCord" ) )
{
throw CancelSendSignal.INSTANCE;
}
if ( BungeeCord.getInstance().config.isForgeSupport() )
{
// Hack around Forge race conditions
if ( pluginMessage.getTag().equals( "FML" ) && pluginMessage.getStream().readUnsignedByte() == 1 )
{
throw CancelSendSignal.INSTANCE;
}
// We handle forge handshake messages if forge support is enabled.
if ( pluginMessage.getTag().equals( ForgeConstants.FML_HANDSHAKE_TAG ) )
{
// Let our forge client handler deal with this packet.
con.getForgeClientHandler().handle( pluginMessage );
throw CancelSendSignal.INSTANCE;
}
if ( con.getServer() != null && !con.getServer().isForgeServer() && pluginMessage.getData().length > Short.MAX_VALUE )
{
// Drop the packet if the server is not a Forge server and the message was > 32kiB (as suggested by @jk-5)
// Do this AFTER the mod list, so we get that even if the intial server isn't modded.
throw CancelSendSignal.INSTANCE;
}
}
PluginMessageEvent event = new PluginMessageEvent( con, con.getServer(), pluginMessage.getTag(), pluginMessage.getData().clone() );
if ( bungee.getPluginManager().callEvent( event ).isCancelled() )
{
throw CancelSendSignal.INSTANCE;
}
con.getPendingConnection().relayMessage( pluginMessage );
}
@Override
public String toString()
{
return "[" + con.getName() + "] -> UpstreamBridge";
}
}