· 7 years ago · Oct 27, 2018, 05:20 PM
1if ( getName().contains( "." ) )
2{
3 disconnect( bungee.getTranslation( "name_invalid" ) );
4 return;
5}
6
7if ( getName().length() > 16 )
8{
9 disconnect( bungee.getTranslation( "name_too_long" ) );
10 return;
11}
12
13int limit = BungeeCord.getInstance().config.getPlayerLimit();
14if ( limit > 0 && bungee.getOnlineCount() > limit )
15{
16 disconnect( bungee.getTranslation( "proxy_full" ) );
17 return;
18}
19
20// If offline mode and they are already on, don't allow connect
21// We can just check by UUID here as names are based on UUID
22if ( !isOnlineMode() && bungee.getPlayer( getUniqueId() ) != null )
23{
24 disconnect( bungee.getTranslation( "already_connected_proxy" ) );
25 return;
26}
27
28Callback<PreLoginEvent> callback = new Callback<PreLoginEvent>()
29{
30
31 @Override
32 public void done(PreLoginEvent result, Throwable error)
33 {
34 if ( result.isCancelled() )
35 {
36 disconnect( result.getCancelReasonComponents() );
37 return;
38 }
39 if ( ch.isClosed() )
40 {
41 return;
42 }
43 unsafe().sendPacket( request = EncryptionUtil.encryptRequest() );
44 thisState = State.ENCRYPT;
45 }
46};
47
48// fire pre login event
49bungee.getPluginManager().callEvent( new PreLoginEvent( InitialHandler.this, callback ) );
50
51SecretKey sharedKey = EncryptionUtil.getSecret( encryptResponse, request );
52BungeeCipher decrypt = EncryptionUtil.getCipher( false, sharedKey );
53ch.addBefore( PipelineUtils.FRAME_DECODER, PipelineUtils.DECRYPT_HANDLER, new CipherDecoder( decrypt ) );
54BungeeCipher encrypt = EncryptionUtil.getCipher( true, sharedKey );
55ch.addBefore( PipelineUtils.FRAME_PREPENDER, PipelineUtils.ENCRYPT_HANDLER, new CipherEncoder( encrypt ) );
56
57String encName = URLEncoder.encode( InitialHandler.this.getName(), "UTF-8" );
58
59MessageDigest sha = MessageDigest.getInstance( "SHA-1" );
60for ( byte[] bit : new byte[][]
61{
62 request.getServerId().getBytes( "ISO_8859_1" ), sharedKey.getEncoded(), EncryptionUtil.keys.getPublic().getEncoded()
63} )
64{
65 sha.update( bit );
66}
67String encodedHash = URLEncoder.encode( new BigInteger( sha.digest() ).toString( 16 ), "UTF-8" );
68
69String preventProxy = ( ( BungeeCord.getInstance().config.isPreventProxyConnections() ) ? "&ip=" + URLEncoder.encode( getAddress().getAddress().getHostAddress(), "UTF-8" ) : "" );
70String authURL = "https://sessionserver.mojang.com/session/minecraft/hasJoined?username=" + encName + "&serverId=" + encodedHash + preventProxy;
71
72Callback<String> handler = new Callback<String>()
73{
74 @Override
75 public void done(String result, Throwable error)
76 {
77 if ( error == null )
78 {
79 LoginResult obj = BungeeCord.getInstance().gson.fromJson( result, LoginResult.class );
80 if ( obj != null && obj.getId() != null )
81 {
82 loginProfile = obj;
83 name = obj.getName();
84 uniqueId = Util.getUUID( obj.getId() );
85 authenticated = true;
86 finish();
87 return;
88 }
89 if(isOnlineMode()) {
90 disconnect(bungee.getTranslation("offline_mode_player"));
91 return;
92 }
93
94 finish();
95 return;
96 } else
97 {
98 disconnect( bungee.getTranslation( "mojang_fail" ) );
99 bungee.getLogger().log( Level.SEVERE, "Error authenticating " + getName() + " with minecraft.net", error );
100 }
101 }
102};
103
104HttpClient.get( authURL, ch.getHandle().eventLoop(), handler );