-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
InitialHandler.java
778 lines (690 loc) · 27.8 KB
/
InitialHandler.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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
package net.md_5.bungee.connection;
import com.google.common.base.Preconditions;
import com.google.gson.Gson;
import java.math.BigInteger;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.time.Instant;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Level;
import javax.crypto.SecretKey;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.BungeeServerInfo;
import net.md_5.bungee.EncryptionUtil;
import net.md_5.bungee.UserConnection;
import net.md_5.bungee.Util;
import net.md_5.bungee.api.AbstractReconnectHandler;
import net.md_5.bungee.api.Callback;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.ServerPing;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.config.ListenerInfo;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.PendingConnection;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.LoginEvent;
import net.md_5.bungee.api.event.PlayerHandshakeEvent;
import net.md_5.bungee.api.event.PostLoginEvent;
import net.md_5.bungee.api.event.PreLoginEvent;
import net.md_5.bungee.api.event.ProxyPingEvent;
import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.http.HttpClient;
import net.md_5.bungee.jni.cipher.BungeeCipher;
import net.md_5.bungee.netty.ChannelWrapper;
import net.md_5.bungee.netty.HandlerBoss;
import net.md_5.bungee.netty.PacketHandler;
import net.md_5.bungee.netty.PipelineUtils;
import net.md_5.bungee.netty.cipher.CipherDecoder;
import net.md_5.bungee.netty.cipher.CipherEncoder;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.PacketWrapper;
import net.md_5.bungee.protocol.PlayerPublicKey;
import net.md_5.bungee.protocol.Protocol;
import net.md_5.bungee.protocol.ProtocolConstants;
import net.md_5.bungee.protocol.packet.EncryptionRequest;
import net.md_5.bungee.protocol.packet.EncryptionResponse;
import net.md_5.bungee.protocol.packet.Handshake;
import net.md_5.bungee.protocol.packet.Kick;
import net.md_5.bungee.protocol.packet.LegacyHandshake;
import net.md_5.bungee.protocol.packet.LegacyPing;
import net.md_5.bungee.protocol.packet.LoginPayloadResponse;
import net.md_5.bungee.protocol.packet.LoginRequest;
import net.md_5.bungee.protocol.packet.LoginSuccess;
import net.md_5.bungee.protocol.packet.PingPacket;
import net.md_5.bungee.protocol.packet.PluginMessage;
import net.md_5.bungee.protocol.packet.StatusRequest;
import net.md_5.bungee.protocol.packet.StatusResponse;
import net.md_5.bungee.util.AllowedCharacters;
import net.md_5.bungee.util.BufUtil;
import net.md_5.bungee.util.QuietException;
@RequiredArgsConstructor
public class InitialHandler extends PacketHandler implements PendingConnection
{
private final BungeeCord bungee;
private ChannelWrapper ch;
@Getter
private final ListenerInfo listener;
@Getter
private Handshake handshake;
@Getter
private LoginRequest loginRequest;
private EncryptionRequest request;
@Getter
private PluginMessage brandMessage;
@Getter
private final Set<String> registeredChannels = new HashSet<>();
private State thisState = State.HANDSHAKE;
private final Unsafe unsafe = new Unsafe()
{
@Override
public void sendPacket(DefinedPacket packet)
{
ch.write( packet );
}
};
@Getter
private boolean onlineMode = BungeeCord.getInstance().config.isOnlineMode();
@Getter
private InetSocketAddress virtualHost;
private String name;
@Getter
private UUID uniqueId;
@Getter
private UUID offlineId;
@Getter
private LoginResult loginProfile;
@Getter
private boolean legacy;
@Getter
private String extraDataInHandshake = "";
private UserConnection userCon;
@Getter
private BaseComponent disconnectMessage;
@Override
public boolean shouldHandle(PacketWrapper packet) throws Exception
{
return !ch.isClosing();
}
private enum State
{
HANDSHAKE, STATUS, PING, USERNAME, ENCRYPT, FINISHING;
}
private boolean canSendKickMessage()
{
return thisState == State.USERNAME || thisState == State.ENCRYPT || thisState == State.FINISHING;
}
@Override
public void connected(ChannelWrapper channel) throws Exception
{
this.ch = channel;
}
@Override
public void exception(Throwable t) throws Exception
{
// if the connection is a login attempt, directly send a Kick with the Exception to the client
// we can't use disconnect() here as the method delays the Kick packet sending by 250ms and the HandlerBoss
// will close the channel before the packet is sent
// also we don't want to print the exception twice
ch.close( canSendKickMessage() ? new Kick( TextComponent.fromLegacy( ChatColor.RED + Util.exception( t ) ) ) : null );
}
@Override
public void handle(PacketWrapper packet) throws Exception
{
if ( packet.packet == null )
{
throw new QuietException( "Unexpected packet received during login process! " + BufUtil.dump( packet.buf, 16 ) );
}
}
@Override
public void handle(PluginMessage pluginMessage) throws Exception
{
this.relayMessage( pluginMessage );
}
@Override
public void handle(LegacyHandshake legacyHandshake) throws Exception
{
this.legacy = true;
ch.close( bungee.getTranslation( "outdated_client", bungee.getGameVersion() ) );
}
@Override
public void handle(LegacyPing ping) throws Exception
{
this.legacy = true;
final boolean v1_5 = ping.isV1_5();
ServerInfo forced = AbstractReconnectHandler.getForcedHost( this );
final String motd = ( forced != null ) ? forced.getMotd() : listener.getMotd();
final int protocol = bungee.getProtocolVersion();
Callback<ServerPing> pingBack = new Callback<ServerPing>()
{
@Override
public void done(ServerPing result, Throwable error)
{
if ( error != null )
{
result = getPingInfo( bungee.getTranslation( "ping_cannot_connect" ), protocol );
bungee.getLogger().log( Level.WARNING, "Error pinging remote server", error );
}
Callback<ProxyPingEvent> callback = new Callback<ProxyPingEvent>()
{
@Override
public void done(ProxyPingEvent result, Throwable error)
{
if ( ch.isClosed() )
{
return;
}
ServerPing legacy = result.getResponse();
String kickMessage;
if ( v1_5 )
{
kickMessage = ChatColor.DARK_BLUE
+ "\00" + 127
+ '\00' + legacy.getVersion().getName()
+ '\00' + getFirstLine( legacy.getDescription() )
+ '\00' + ( ( legacy.getPlayers() != null ) ? legacy.getPlayers().getOnline() : "-1" )
+ '\00' + ( ( legacy.getPlayers() != null ) ? legacy.getPlayers().getMax() : "-1" );
} else
{
// Clients <= 1.3 don't support colored motds because the color char is used as delimiter
kickMessage = ChatColor.stripColor( getFirstLine( legacy.getDescription() ) )
+ '\u00a7' + ( ( legacy.getPlayers() != null ) ? legacy.getPlayers().getOnline() : "-1" )
+ '\u00a7' + ( ( legacy.getPlayers() != null ) ? legacy.getPlayers().getMax() : "-1" );
}
ch.close( kickMessage );
}
};
bungee.getPluginManager().callEvent( new ProxyPingEvent( InitialHandler.this, result, callback ) );
}
};
if ( forced != null && listener.isPingPassthrough() )
{
( (BungeeServerInfo) forced ).ping( pingBack, bungee.getProtocolVersion() );
} else
{
pingBack.done( getPingInfo( motd, protocol ), null );
}
}
private static String getFirstLine(String str)
{
int pos = str.indexOf( '\n' );
return pos == -1 ? str : str.substring( 0, pos );
}
private ServerPing getPingInfo(String motd, int protocol)
{
return new ServerPing(
new ServerPing.Protocol( bungee.getName() + " " + bungee.getGameVersion(), protocol ),
new ServerPing.Players( listener.getMaxPlayers(), bungee.getOnlineCount(), null ),
motd, BungeeCord.getInstance().config.getFaviconObject()
);
}
@Override
public void handle(StatusRequest statusRequest) throws Exception
{
Preconditions.checkState( thisState == State.STATUS, "Not expecting STATUS" );
ServerInfo forced = AbstractReconnectHandler.getForcedHost( this );
final String motd = ( forced != null ) ? forced.getMotd() : listener.getMotd();
final int protocol = ( ProtocolConstants.SUPPORTED_VERSION_IDS.contains( handshake.getProtocolVersion() ) ) ? handshake.getProtocolVersion() : bungee.getProtocolVersion();
Callback<ServerPing> pingBack = new Callback<ServerPing>()
{
@Override
public void done(ServerPing result, Throwable error)
{
if ( error != null )
{
result = getPingInfo( bungee.getTranslation( "ping_cannot_connect" ), protocol );
bungee.getLogger().log( Level.WARNING, "Error pinging remote server", error );
}
Callback<ProxyPingEvent> callback = new Callback<ProxyPingEvent>()
{
@Override
public void done(ProxyPingEvent pingResult, Throwable error)
{
Gson gson = BungeeCord.getInstance().gson;
unsafe.sendPacket( new StatusResponse( gson.toJson( pingResult.getResponse() ) ) );
if ( bungee.getConnectionThrottle() != null )
{
bungee.getConnectionThrottle().unthrottle( getSocketAddress() );
}
}
};
bungee.getPluginManager().callEvent( new ProxyPingEvent( InitialHandler.this, result, callback ) );
}
};
if ( forced != null && listener.isPingPassthrough() )
{
( (BungeeServerInfo) forced ).ping( pingBack, handshake.getProtocolVersion() );
} else
{
pingBack.done( getPingInfo( motd, protocol ), null );
}
thisState = State.PING;
}
@Override
public void handle(PingPacket ping) throws Exception
{
Preconditions.checkState( thisState == State.PING, "Not expecting PING" );
unsafe.sendPacket( ping );
disconnect( "" );
}
@Override
public void handle(Handshake handshake) throws Exception
{
Preconditions.checkState( thisState == State.HANDSHAKE, "Not expecting HANDSHAKE" );
this.handshake = handshake;
ch.setVersion( handshake.getProtocolVersion() );
ch.getHandle().pipeline().remove( PipelineUtils.LEGACY_KICKER );
// Starting with FML 1.8, a "\0FML\0" token is appended to the handshake. This interferes
// with Bungee's IP forwarding, so we detect it, and remove it from the host string, for now.
// We know FML appends \00FML\00. However, we need to also consider that other systems might
// add their own data to the end of the string. So, we just take everything from the \0 character
// and save it for later.
if ( handshake.getHost().contains( "\0" ) )
{
String[] split = handshake.getHost().split( "\0", 2 );
handshake.setHost( split[0] );
extraDataInHandshake = "\0" + split[1];
}
// SRV records can end with a . depending on DNS / client.
if ( handshake.getHost().endsWith( "." ) )
{
handshake.setHost( handshake.getHost().substring( 0, handshake.getHost().length() - 1 ) );
}
this.virtualHost = InetSocketAddress.createUnresolved( handshake.getHost(), handshake.getPort() );
bungee.getPluginManager().callEvent( new PlayerHandshakeEvent( InitialHandler.this, handshake ) );
switch ( handshake.getRequestedProtocol() )
{
case 1:
// Ping
if ( bungee.getConfig().isLogPings() )
{
bungee.getLogger().log( Level.INFO, "{0} has pinged", this );
}
thisState = State.STATUS;
ch.setProtocol( Protocol.STATUS );
break;
case 2:
// Login
bungee.getLogger().log( Level.INFO, "{0} has connected", this );
thisState = State.USERNAME;
ch.setProtocol( Protocol.LOGIN );
if ( !ProtocolConstants.SUPPORTED_VERSION_IDS.contains( handshake.getProtocolVersion() ) )
{
if ( handshake.getProtocolVersion() > bungee.getProtocolVersion() )
{
disconnect( bungee.getTranslation( "outdated_server", bungee.getGameVersion() ) );
} else
{
disconnect( bungee.getTranslation( "outdated_client", bungee.getGameVersion() ) );
}
return;
}
break;
default:
throw new QuietException( "Cannot request protocol " + handshake.getRequestedProtocol() );
}
}
@Override
public void handle(LoginRequest loginRequest) throws Exception
{
Preconditions.checkState( thisState == State.USERNAME, "Not expecting USERNAME" );
if ( !AllowedCharacters.isValidName( loginRequest.getData(), onlineMode ) )
{
disconnect( bungee.getTranslation( "name_invalid" ) );
return;
}
if ( BungeeCord.getInstance().config.isEnforceSecureProfile() && getVersion() < ProtocolConstants.MINECRAFT_1_19_3 )
{
PlayerPublicKey publicKey = loginRequest.getPublicKey();
if ( publicKey == null )
{
disconnect( bungee.getTranslation( "secure_profile_required" ) );
return;
}
if ( Instant.ofEpochMilli( publicKey.getExpiry() ).isBefore( Instant.now() ) )
{
disconnect( bungee.getTranslation( "secure_profile_expired" ) );
return;
}
if ( getVersion() < ProtocolConstants.MINECRAFT_1_19_1 )
{
if ( !EncryptionUtil.check( publicKey, null ) )
{
disconnect( bungee.getTranslation( "secure_profile_invalid" ) );
return;
}
}
}
this.loginRequest = loginRequest;
int limit = BungeeCord.getInstance().config.getPlayerLimit();
if ( limit > 0 && bungee.getOnlineCount() >= limit )
{
disconnect( bungee.getTranslation( "proxy_full" ) );
return;
}
// If offline mode and they are already on, don't allow connect
// We can just check by UUID here as names are based on UUID
if ( !isOnlineMode() && bungee.getPlayer( getUniqueId() ) != null )
{
disconnect( bungee.getTranslation( "already_connected_proxy" ) );
return;
}
Callback<PreLoginEvent> callback = new Callback<PreLoginEvent>()
{
@Override
public void done(PreLoginEvent result, Throwable error)
{
if ( result.isCancelled() )
{
BaseComponent reason = result.getReason();
disconnect( ( reason != null ) ? reason : TextComponent.fromLegacy( bungee.getTranslation( "kick_message" ) ) );
return;
}
if ( ch.isClosed() )
{
return;
}
if ( onlineMode )
{
thisState = State.ENCRYPT;
unsafe().sendPacket( request = EncryptionUtil.encryptRequest() );
} else
{
thisState = State.FINISHING;
finish();
}
}
};
// fire pre login event
bungee.getPluginManager().callEvent( new PreLoginEvent( InitialHandler.this, callback ) );
}
@Override
public void handle(final EncryptionResponse encryptResponse) throws Exception
{
Preconditions.checkState( thisState == State.ENCRYPT, "Not expecting ENCRYPT" );
Preconditions.checkState( EncryptionUtil.check( loginRequest.getPublicKey(), encryptResponse, request ), "Invalid verification" );
SecretKey sharedKey = EncryptionUtil.getSecret( encryptResponse, request );
BungeeCipher decrypt = EncryptionUtil.getCipher( false, sharedKey );
ch.addBefore( PipelineUtils.FRAME_DECODER, PipelineUtils.DECRYPT_HANDLER, new CipherDecoder( decrypt ) );
BungeeCipher encrypt = EncryptionUtil.getCipher( true, sharedKey );
ch.addBefore( PipelineUtils.FRAME_PREPENDER, PipelineUtils.ENCRYPT_HANDLER, new CipherEncoder( encrypt ) );
String encName = URLEncoder.encode( InitialHandler.this.getName(), "UTF-8" );
MessageDigest sha = MessageDigest.getInstance( "SHA-1" );
for ( byte[] bit : new byte[][]
{
request.getServerId().getBytes( "ISO_8859_1" ), sharedKey.getEncoded(), EncryptionUtil.keys.getPublic().getEncoded()
} )
{
sha.update( bit );
}
String encodedHash = URLEncoder.encode( new BigInteger( sha.digest() ).toString( 16 ), "UTF-8" );
String preventProxy = ( BungeeCord.getInstance().config.isPreventProxyConnections() && getSocketAddress() instanceof InetSocketAddress ) ? "&ip=" + URLEncoder.encode( getAddress().getAddress().getHostAddress(), "UTF-8" ) : "";
String authURL = "https://sessionserver.mojang.com/session/minecraft/hasJoined?username=" + encName + "&serverId=" + encodedHash + preventProxy;
Callback<String> handler = new Callback<String>()
{
@Override
public void done(String result, Throwable error)
{
if ( error == null )
{
LoginResult obj = BungeeCord.getInstance().gson.fromJson( result, LoginResult.class );
if ( obj != null && obj.getId() != null )
{
loginProfile = obj;
name = obj.getName();
uniqueId = Util.getUUID( obj.getId() );
finish();
return;
}
disconnect( bungee.getTranslation( "offline_mode_player" ) );
} else
{
disconnect( bungee.getTranslation( "mojang_fail" ) );
bungee.getLogger().log( Level.SEVERE, "Error authenticating " + getName() + " with minecraft.net", error );
}
}
};
thisState = State.FINISHING;
HttpClient.get( authURL, ch.getHandle().eventLoop(), handler );
}
private void finish()
{
offlineId = UUID.nameUUIDFromBytes( ( "OfflinePlayer:" + getName() ).getBytes( StandardCharsets.UTF_8 ) );
if ( uniqueId == null )
{
uniqueId = offlineId;
}
if ( BungeeCord.getInstance().config.isEnforceSecureProfile() )
{
if ( getVersion() >= ProtocolConstants.MINECRAFT_1_19_1 && getVersion() < ProtocolConstants.MINECRAFT_1_19_3 )
{
boolean secure = false;
try
{
secure = EncryptionUtil.check( loginRequest.getPublicKey(), uniqueId );
} catch ( GeneralSecurityException ex )
{
}
if ( !secure )
{
disconnect( bungee.getTranslation( "secure_profile_invalid" ) );
return;
}
}
}
if ( isOnlineMode() )
{
// Check for multiple connections
// We have to check for the old name first
ProxiedPlayer oldName = bungee.getPlayer( getName() );
if ( oldName != null )
{
// TODO See #1218
disconnect( bungee.getTranslation( "already_connected_proxy" ) );
}
// And then also for their old UUID
ProxiedPlayer oldID = bungee.getPlayer( getUniqueId() );
if ( oldID != null )
{
// TODO See #1218
disconnect( bungee.getTranslation( "already_connected_proxy" ) );
}
} else
{
// In offline mode the existing user stays and we kick the new one
ProxiedPlayer oldName = bungee.getPlayer( getName() );
if ( oldName != null )
{
// TODO See #1218
disconnect( bungee.getTranslation( "already_connected_proxy" ) );
return;
}
}
Callback<LoginEvent> complete = new Callback<LoginEvent>()
{
@Override
public void done(LoginEvent result, Throwable error)
{
if ( result.isCancelled() )
{
BaseComponent reason = result.getReason();
disconnect( ( reason != null ) ? reason : TextComponent.fromLegacy( bungee.getTranslation( "kick_message" ) ) );
return;
}
if ( ch.isClosed() )
{
return;
}
ch.getHandle().eventLoop().execute( new Runnable()
{
@Override
public void run()
{
if ( !ch.isClosing() )
{
userCon = new UserConnection( bungee, ch, getName(), InitialHandler.this );
userCon.setCompressionThreshold( BungeeCord.getInstance().config.getCompressionThreshold() );
if ( getVersion() < ProtocolConstants.MINECRAFT_1_20_2 )
{
unsafe.sendPacket( new LoginSuccess( getUniqueId(), getName(), ( loginProfile == null ) ? null : loginProfile.getProperties() ) );
ch.setProtocol( Protocol.GAME );
}
finish2();
}
}
} );
}
};
// fire login event
bungee.getPluginManager().callEvent( new LoginEvent( InitialHandler.this, complete ) );
}
private void finish2()
{
if ( !userCon.init() )
{
disconnect( bungee.getTranslation( "already_connected_proxy" ) );
return;
}
ch.getHandle().pipeline().get( HandlerBoss.class ).setHandler( new UpstreamBridge( bungee, userCon ) );
bungee.getPluginManager().callEvent( new PostLoginEvent( userCon ) );
ServerInfo server;
if ( bungee.getReconnectHandler() != null )
{
server = bungee.getReconnectHandler().getServer( userCon );
} else
{
server = AbstractReconnectHandler.getForcedHost( InitialHandler.this );
}
if ( server == null )
{
server = bungee.getServerInfo( listener.getDefaultServer() );
}
userCon.connect( server, null, true, ServerConnectEvent.Reason.JOIN_PROXY );
}
@Override
public void handle(LoginPayloadResponse response) throws Exception
{
disconnect( "Unexpected custom LoginPayloadResponse" );
}
@Override
public void disconnect(String reason)
{
if ( canSendKickMessage() )
{
disconnect( TextComponent.fromLegacy( reason ) );
} else
{
ch.close();
}
}
@Override
public void disconnect(final BaseComponent... reason)
{
disconnect( TextComponent.fromArray( reason ) );
}
@Override
public void disconnect(BaseComponent reason)
{
if ( canSendKickMessage() )
{
ch.delayedClose( new Kick( this.disconnectMessage = reason ) );
} else
{
ch.close();
}
}
@Override
public String getName()
{
return ( name != null ) ? name : ( loginRequest == null ) ? null : loginRequest.getData();
}
@Override
public int getVersion()
{
return ( handshake == null ) ? -1 : handshake.getProtocolVersion();
}
@Override
public InetSocketAddress getAddress()
{
return (InetSocketAddress) getSocketAddress();
}
@Override
public SocketAddress getSocketAddress()
{
return ch.getRemoteAddress();
}
@Override
public Unsafe unsafe()
{
return unsafe;
}
@Override
public void setOnlineMode(boolean onlineMode)
{
Preconditions.checkState( thisState == State.USERNAME, "Can only set online mode status whilst state is username" );
this.onlineMode = onlineMode;
}
@Override
public void setUniqueId(UUID uuid)
{
Preconditions.checkState( thisState == State.USERNAME, "Can only set uuid while state is username" );
Preconditions.checkState( !onlineMode, "Can only set uuid when online mode is false" );
this.uniqueId = uuid;
}
@Override
public String getUUID()
{
return uniqueId.toString().replace( "-", "" );
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append( '[' );
String currentName = getName();
if ( currentName != null )
{
sb.append( currentName );
sb.append( ',' );
}
sb.append( getSocketAddress() );
sb.append( "] <-> InitialHandler" );
return sb.toString();
}
@Override
public boolean isConnected()
{
return !ch.isClosed();
}
public void relayMessage(PluginMessage input) throws Exception
{
if ( input.getTag().equals( "REGISTER" ) || input.getTag().equals( "minecraft:register" ) )
{
String content = new String( input.getData(), StandardCharsets.UTF_8 );
for ( String id : content.split( "\0" ) )
{
Preconditions.checkState( registeredChannels.size() < 128, "Too many registered channels" );
Preconditions.checkArgument( id.length() < 128, "Channel name too long" );
registeredChannels.add( id );
}
} else if ( input.getTag().equals( "UNREGISTER" ) || input.getTag().equals( "minecraft:unregister" ) )
{
String content = new String( input.getData(), StandardCharsets.UTF_8 );
for ( String id : content.split( "\0" ) )
{
registeredChannels.remove( id );
}
} else if ( input.getTag().equals( "MC|Brand" ) || input.getTag().equals( "minecraft:brand" ) )
{
brandMessage = input;
}
}
}