Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

Commit

Permalink
Merge branch 'SpigotMC-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfriedrich committed Jul 7, 2021
2 parents 31d9e3c + dbbab41 commit 2026bb5
Show file tree
Hide file tree
Showing 26 changed files with 90 additions and 40 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "native/mbedtls"]
path = native/mbedtls
url = https://github.com/ARMmbed/mbedtls.git
[submodule "native/zlib"]
path = native/zlib
url = https://github.com/cloudflare/zlib.git
2 changes: 2 additions & 0 deletions log/src/main/java/net/md_5/bungee/log/BungeeLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ public class BungeeLogger extends Logger

private final LogDispatcher dispatcher = new LogDispatcher( this );

// CHECKSTYLE:OFF
@SuppressWarnings(
{
"CallToPrintStackTrace", "CallToThreadStartDuringObjectConstruction"
})
// CHECKSTYLE:ON
@SuppressFBWarnings("SC_START_IN_CTOR")
public BungeeLogger(String loggerName, String filePattern, ConsoleReader reader)
{
Expand Down
14 changes: 11 additions & 3 deletions native/compile-native.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#!/bin/sh

CXX="g++ -shared -fPIC -O3 -Wall -Werror -I$JAVA_HOME/include/ -I$JAVA_HOME/include/linux/"
set -eu

$CXX src/main/c/NativeCipherImpl.cpp -o src/main/resources/native-cipher.so -lcrypto
$CXX src/main/c/NativeCompressImpl.cpp -o src/main/resources/native-compress.so -lz
echo "Compiling mbedtls"
(cd mbedtls && make no_test)

echo "Compiling zlib"
(cd zlib && CFLAGS=-fPIC ./configure --static && make)

CXX="g++ -shared -fPIC -Wl,--wrap=memcpy -O3 -Wall -Werror -I$JAVA_HOME/include/ -I$JAVA_HOME/include/linux/"

$CXX -Imbedtls/include src/main/c/NativeCipherImpl.cpp -o src/main/resources/native-cipher.so mbedtls/library/libmbedcrypto.a
$CXX -Izlib src/main/c/NativeCompressImpl.cpp -o src/main/resources/native-compress.so zlib/libz.a
1 change: 1 addition & 0 deletions native/mbedtls
Submodule mbedtls added at e483a7
9 changes: 6 additions & 3 deletions native/src/main/c/NativeCipherImpl.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// Support for CentOS 6
__asm__(".symver memcpy,memcpy@GLIBC_2.2.5");

#include <stdlib.h>
#include <string.h>

#include <mbedtls/aes.h>
#include "net_md_5_bungee_jni_cipher_NativeCipherImpl.h"

// Support for CentOS 6
__asm__(".symver memcpy,memcpy@GLIBC_2.2.5");
extern "C" void *__wrap_memcpy(void *dest, const void *src, size_t n) {
return memcpy(dest, src, n);
}

typedef unsigned char byte;

struct crypto_context {
Expand Down
8 changes: 8 additions & 0 deletions native/src/main/c/NativeCompressImpl.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
#include <stdlib.h>
#include <string.h>

#include <zlib.h>
#include "net_md_5_bungee_jni_zlib_NativeCompressImpl.h"

// Support for CentOS 6
__asm__(".symver memcpy,memcpy@GLIBC_2.2.5");
extern "C" void *__wrap_memcpy(void *dest, const void *src, size_t n) {
return memcpy(dest, src, n);
}

typedef unsigned char byte;

static jfieldID consumedID;
Expand Down
15 changes: 5 additions & 10 deletions native/src/main/java/net/md_5/bungee/jni/NativeCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.function.Supplier;
import net.md_5.bungee.jni.cipher.BungeeCipher;

public final class NativeCode<T>
{

private final String name;
private final Class<? extends T> javaImpl;
private final Class<? extends T> nativeImpl;
private final Supplier<? extends T> javaImpl;
private final Supplier<? extends T> nativeImpl;
//
private boolean loaded;

public NativeCode(String name, Class<? extends T> javaImpl, Class<? extends T> nativeImpl)
public NativeCode(String name, Supplier<? extends T> javaImpl, Supplier<? extends T> nativeImpl)
{
this.name = name;
this.javaImpl = javaImpl;
Expand All @@ -26,13 +27,7 @@ public NativeCode(String name, Class<? extends T> javaImpl, Class<? extends T> n

public T newInstance()
{
try
{
return ( loaded ) ? nativeImpl.getDeclaredConstructor().newInstance() : javaImpl.getDeclaredConstructor().newInstance();
} catch ( ReflectiveOperationException ex )
{
throw new RuntimeException( "Error getting instance", ex );
}
return ( loaded ) ? nativeImpl.get() : javaImpl.get();
}

public boolean load()
Expand Down
10 changes: 8 additions & 2 deletions native/src/main/java/net/md_5/bungee/jni/cipher/JavaCipher.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@ protected byte[] initialValue()
}
}

public JavaCipher() throws GeneralSecurityException
public JavaCipher()
{
this.cipher = Cipher.getInstance( "AES/CFB8/NoPadding" );
try
{
this.cipher = Cipher.getInstance( "AES/CFB8/NoPadding" );
} catch ( GeneralSecurityException ex )
{
throw new RuntimeException( ex );
}
}

@Override
Expand Down
Binary file modified native/src/main/resources/native-cipher.so
Binary file not shown.
Binary file modified native/src/main/resources/native-compress.so
Binary file not shown.
2 changes: 1 addition & 1 deletion native/src/test/java/net/md_5/bungee/NativeCipherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class NativeCipherTest
private final SecretKey secret = new SecretKeySpec( new byte[ 16 ], "AES" );
private static final int BENCHMARK_COUNT = 4096;
//
private static final NativeCode<BungeeCipher> factory = new NativeCode<>( "native-cipher", JavaCipher.class, NativeCipher.class );
private static final NativeCode<BungeeCipher> factory = new NativeCode<>( "native-cipher", JavaCipher::new, NativeCipher::new );

@Test
public void testNative() throws Exception
Expand Down
2 changes: 1 addition & 1 deletion native/src/test/java/net/md_5/bungee/NativeZlibTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class NativeZlibTest
{

private final NativeCode<BungeeZlib> factory = new NativeCode<>( "native-compress", JavaZlib.class, NativeZlib.class );
private final NativeCode<BungeeZlib> factory = new NativeCode<>( "native-compress", JavaZlib::new, NativeZlib::new );

@Test
public void doTest() throws DataFormatException
Expand Down
1 change: 1 addition & 0 deletions native/zlib
Submodule zlib added at 959b4e
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
<executions>
<execution>
<phase>process-classes</phase>
Expand All @@ -156,14 +156,14 @@
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>8.36.2</version>
<version>8.44</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId>
<version>1.19</version>
<version>1.20</version>
<executions>
<execution>
<phase>process-classes</phase>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class ProtocolConstants
public static final int MINECRAFT_1_16_3 = 753;
public static final int MINECRAFT_1_16_4 = 754;
public static final int MINECRAFT_1_17 = 755;
public static final int MINECRAFT_1_17_1 = 756;
public static final List<String> SUPPORTED_VERSIONS;
public static final List<Integer> SUPPORTED_VERSION_IDS;

Expand Down Expand Up @@ -85,7 +86,8 @@ public class ProtocolConstants
ProtocolConstants.MINECRAFT_1_16_2,
ProtocolConstants.MINECRAFT_1_16_3,
ProtocolConstants.MINECRAFT_1_16_4,
ProtocolConstants.MINECRAFT_1_17
ProtocolConstants.MINECRAFT_1_17,
ProtocolConstants.MINECRAFT_1_17_1
);

if ( SNAPSHOT_SUPPORT )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Chat(String message, byte position, UUID sender)
@Override
public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion)
{
message = readString( buf, ( direction == ProtocolConstants.Direction.TO_CLIENT ) ? 262144 : 256 );
message = readString( buf, ( direction == ProtocolConstants.Direction.TO_CLIENT ) ? 262144 : ( protocolVersion >= ProtocolConstants.MINECRAFT_1_11 ? 256 : 100 ) );
if ( direction == ProtocolConstants.Direction.TO_CLIENT && protocolVersion >= ProtocolConstants.MINECRAFT_1_8 )
{
position = buf.readByte();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protoco
{
transactionId = readVarInt( buf );
}
cursor = readString( buf, 32500 );
cursor = readString( buf, ( protocolVersion > ProtocolConstants.MINECRAFT_1_13 ? 32500 : ( protocolVersion == ProtocolConstants.MINECRAFT_1_13 ? 256 : 32767 ) ) );

if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_8 && protocolVersion < ProtocolConstants.MINECRAFT_1_13 )
{
Expand Down
2 changes: 1 addition & 1 deletion proxy/src/main/java/net/md_5/bungee/BungeeCord.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public BungeeCord() throws IOException
public void start() throws Exception
{
System.setProperty( "io.netty.selectorAutoRebuildThreshold", "0" ); // Seems to cause Bungee to stop accepting connections
if ( System.getProperty( "io.netty.leakDetectionLevel" ) == null )
if ( System.getProperty( "io.netty.leakDetectionLevel" ) == null && System.getProperty( "io.netty.leakDetection.level" ) == null )
{
ResourceLeakDetector.setLevel( ResourceLeakDetector.Level.DISABLED ); // Eats performance
}
Expand Down
2 changes: 1 addition & 1 deletion proxy/src/main/java/net/md_5/bungee/EncryptionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class EncryptionUtil
public static final KeyPair keys;
@Getter
private static final SecretKey secret = new SecretKeySpec( new byte[ 16 ], "AES" );
public static final NativeCode<BungeeCipher> nativeFactory = new NativeCode<>( "native-cipher", JavaCipher.class, NativeCipher.class );
public static final NativeCode<BungeeCipher> nativeFactory = new NativeCode<>( "native-cipher", JavaCipher::new, NativeCipher::new );

static
{
Expand Down
16 changes: 14 additions & 2 deletions proxy/src/main/java/net/md_5/bungee/UserConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ConnectTimeoutException;
import io.netty.util.internal.PlatformDependent;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
Expand Down Expand Up @@ -359,10 +360,10 @@ public void operationComplete(ChannelFuture future) throws Exception
connect( def, null, true, ServerConnectEvent.Reason.LOBBY_FALLBACK );
} else if ( dimensionChange )
{
disconnect( bungee.getTranslation( "fallback_kick", future.cause().getClass().getName() ) );
disconnect( bungee.getTranslation( "fallback_kick", connectionFailMessage( future.cause() ) ) );
} else
{
sendMessage( bungee.getTranslation( "fallback_kick", future.cause().getClass().getName() ) );
sendMessage( bungee.getTranslation( "fallback_kick", connectionFailMessage( future.cause() ) ) );
}
}
}
Expand All @@ -381,6 +382,17 @@ public void operationComplete(ChannelFuture future) throws Exception
b.connect().addListener( listener );
}

private String connectionFailMessage(Throwable cause)
{
if ( cause instanceof ConnectTimeoutException )
{
return bungee.getTranslation( "timeout" );
} else
{
return cause.getClass().getName();
}
}

@Override
public void disconnect(String reason)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
public class CompressFactory
{

public static final NativeCode<BungeeZlib> zlib = new NativeCode<>( "native-compress", JavaZlib.class, NativeZlib.class );
public static final NativeCode<BungeeZlib> zlib = new NativeCode<>( "native-compress", JavaZlib::new, NativeZlib::new );
}
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,6 @@ public void handle(LoginRequest loginRequest) throws Exception
return;
}

if ( getName().length() > 16 )
{
disconnect( bungee.getTranslation( "name_too_long" ) );
return;
}

int limit = BungeeCord.getInstance().config.getPlayerLimit();
if ( limit > 0 && bungee.getOnlineCount() >= limit )
{
Expand Down
12 changes: 10 additions & 2 deletions proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,16 @@ public void handle(KeepAlive alive) throws Exception
@Override
public void handle(Chat chat) throws Exception
{
int maxLength = ( con.getPendingConnection().getVersion() >= ProtocolConstants.MINECRAFT_1_11 ) ? 256 : 100;
Preconditions.checkArgument( chat.getMessage().length() <= maxLength, "Chat message too long" ); // Mojang limit, check on updates
for ( int index = 0, length = chat.getMessage().length(); index < length; index++ )
{
char c = chat.getMessage().charAt( index );
// Section symbol, control sequences, and delete
if ( c == '\u00A7' || c < ' ' || c == 127 )
{
con.disconnect( bungee.getTranslation( "illegal_chat_characters", String.format( "\\u%04x", (int) c ) ) );
throw CancelSendSignal.INSTANCE;
}
}

ChatEvent chatEvent = new ChatEvent( con, con.getServer(), chat.getMessage() );
if ( !bungee.getPluginManager().callEvent( chatEvent ).isCancelled() )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public static EntityMap getEntityMap(int version)
case ProtocolConstants.MINECRAFT_1_16_4:
return EntityMap_1_16_2.INSTANCE_1_16_2;
case ProtocolConstants.MINECRAFT_1_17:
case ProtocolConstants.MINECRAFT_1_17_1:
return EntityMap_1_16_2.INSTANCE_1_17;
}
throw new RuntimeException( "Version " + version + " has no entity map" );
Expand Down
2 changes: 2 additions & 0 deletions proxy/src/main/java/net/md_5/bungee/module/ModuleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ public ModuleManager()
knownSources.put( "travis-ci", new TravisCiModuleSource() );
}

// CHECKSTYLE:OFF
@SuppressFBWarnings(
{
"SF_SWITCH_FALLTHROUGH", "SF_SWITCH_NO_DEFAULT"
})
// CHECKSTYLE:ON
public void load(ProxyServer proxy, File moduleDirectory) throws Exception
{
moduleDirectory.mkdir();
Expand Down
3 changes: 2 additions & 1 deletion proxy/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ connect_kick=\u00a7cKicked whilst connecting to {0}: {1}
current_server=\u00a76You are currently connected to {0}.
fallback_kick=\u00a7cCould not connect to a default or fallback server, please try again later: {0}
fallback_lobby=\u00a7cCould not connect to target server, you have been moved to a fallback server.
timeout=Server not reachable (timeout). Offline? Incorrectly configured address/port/firewall?
lost_connection=[Proxy] Lost connection to server.
mojang_fail=Error occurred while contacting login servers, are they down?
no_permission=\u00a7cYou do not have permission to execute this command!
Expand All @@ -20,7 +21,6 @@ server_kick=[Kicked]
server_list=\u00a76You may connect to the following servers at this time:
server_went_down=\u00a7cThe server you were previously on went down, you have been connected to a fallback server
total_players=Total players online: {0}
name_too_long=Cannot have username longer than 16 characters
name_invalid=Username contains invalid characters.
ping_cannot_connect=\u00a7c[Bungee] Can''t connect to server.
offline_mode_player=Not authenticated with Minecraft.net
Expand All @@ -38,3 +38,4 @@ you_got_summoned=\u00a76Summoned to {0} by {1}
command_perms_groups=\u00a76You have the following groups: {0}
command_perms_permission=\u00a79- {0}
command_ip=\u00a79IP of {0} is {1}
illegal_chat_characters=\u00a7cillegal characters in chat ({0})

0 comments on commit 2026bb5

Please sign in to comment.