Skip to content

Commit

Permalink
Merge remote-tracking branch 'upsteream-bc/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	native/src/main/java/net/md_5/bungee/jni/zlib/BungeeZlib.java
#	native/src/main/java/net/md_5/bungee/jni/zlib/JavaZlib.java
#	native/src/main/java/net/md_5/bungee/jni/zlib/NativeZlib.java
#	proxy/src/main/java/net/md_5/bungee/compress/PacketCompressor.java
#	proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java
  • Loading branch information
Leymooo committed Oct 3, 2024
2 parents ef5fd5d + 01a5f36 commit 3f94dfd
Show file tree
Hide file tree
Showing 32 changed files with 371 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,18 @@ public interface PendingConnection extends Connection
*/
@ApiStatus.Experimental
CompletableFuture<byte[]> retrieveCookie(String cookie);

/**
* Sends a login payload request to the client.
*
* @param channel the channel to send this data via
* @param data the data to send
* @return a {@link CompletableFuture} that will be completed when the Login
* Payload response is received. If the Vanilla client doesn't know the
* channel, the {@link CompletableFuture} will complete with a null value
* @throws IllegalStateException if the player's version is not at least
* 1.13
*/
@ApiStatus.Experimental
CompletableFuture<byte[]> sendData(String channel, byte[] data);
}
18 changes: 18 additions & 0 deletions chat/src/main/java/net/md_5/bungee/chat/ComponentSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.md_5.bungee.api.chat.SelectorComponent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.TranslatableComponent;
import net.md_5.bungee.api.chat.hover.content.Content;
import net.md_5.bungee.api.chat.hover.content.Entity;
import net.md_5.bungee.api.chat.hover.content.EntitySerializer;
import net.md_5.bungee.api.chat.hover.content.Item;
Expand Down Expand Up @@ -162,11 +163,28 @@ public static JsonElement toJson(ComponentStyle style)
return gson.toJsonTree( style );
}

/**
* @param object the object to serialize
* @return the JSON string representation of the object
* @deprecated Error-prone, be careful which object you input here
*/
@Deprecated
public static String toString(Object object)
{
return gson.toJson( object );
}

/**
* @param content the content to serialize
* @return the JSON string representation of the object
* @deprecated for legacy internal use only
*/
@Deprecated
public static String toString(Content content)
{
return gson.toJson( content );
}

public static String toString(BaseComponent component)
{
return gson.toJson( component );
Expand Down
29 changes: 29 additions & 0 deletions native/compile-native-arm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh

set -eu

CWD=$(pwd)

echo "Compiling mbedtls"
(cd mbedtls && CFLAGS="-fPIC -I$CWD/src/main/c -DMBEDTLS_USER_CONFIG_FILE='<mbedtls_custom_config.h>'" make CC=aarch64-linux-gnu-gcc AR=aarch64-linux-gnu-ar no_test)

echo "Compiling zlib"
(cd zlib && CFLAGS="-fPIC -DNO_GZIP" CC=aarch64-linux-gnu-gcc CHOST=arm64 ./configure --target="aarch64" --static && make CFLAGS="-fPIC -march=armv8-a+crc" CC=aarch64-linux-gnu-gcc AR=aarch64-linux-gnu-ar)

CC="aarch64-linux-gnu-gcc"
CFLAGS="-c -fPIC -O3 -Wall -Werror -I$JAVA_HOME/include/ -I$JAVA_HOME/include/linux/"
LDFLAGS="-shared"

echo "Compiling bungee"
$CC $CFLAGS -o shared.o src/main/c/shared.c
$CC $CFLAGS -Imbedtls/include -o NativeCipherImpl.o src/main/c/NativeCipherImpl.c
$CC $CFLAGS -Izlib -o NativeCompressImpl.o src/main/c/NativeCompressImpl.c

echo "Linking native-cipher-arm.so"
$CC $LDFLAGS -o src/main/resources/native-cipher-arm.so shared.o NativeCipherImpl.o mbedtls/library/libmbedcrypto.a

echo "Linking native-compress-arm.so"
$CC $LDFLAGS -o src/main/resources/native-compress-arm.so shared.o NativeCompressImpl.o zlib/libz.a

echo "Cleaning up"
rm shared.o NativeCipherImpl.o NativeCompressImpl.o
8 changes: 7 additions & 1 deletion native/src/main/c/NativeCompressImpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

#include <zlib.h>
#include "shared.h"
#if !defined(__aarch64__)
#include "cpuid_helper.h"
#endif
#include "net_md_5_bungee_jni_zlib_NativeCompressImpl.h"

typedef unsigned char byte;
Expand All @@ -28,7 +30,11 @@ jint throwException(JNIEnv *env, const char* message, int err) {
}

JNIEXPORT jboolean JNICALL Java_net_md_15_bungee_jni_zlib_NativeCompressImpl_checkSupported(JNIEnv* env, jobject obj) {
return (jboolean) checkCompressionNativesSupport();
#if !defined(__aarch64__)
return (jboolean) checkCompressionNativesSupport();
#else
return JNI_TRUE;
#endif
}

void JNICALL Java_net_md_15_bungee_jni_zlib_NativeCompressImpl_reset(JNIEnv* env, jobject obj, jlong ctx, jboolean compress) {
Expand Down
13 changes: 12 additions & 1 deletion native/src/main/java/net/md_5/bungee/jni/NativeCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public boolean load()
{
if ( enableNativeFlag && !loaded && isSupported() )
{
String name = this.name + ( isAarch64() ? "-arm" : "" );
String fullName = "bungeecord-" + name;

try
Expand Down Expand Up @@ -94,6 +95,16 @@ public boolean load()

public static boolean isSupported()
{
return "Linux".equals( System.getProperty( "os.name" ) ) && "amd64".equals( System.getProperty( "os.arch" ) );
return "Linux".equals( System.getProperty( "os.name" ) ) && ( isAmd64() || isAarch64() );
}

private static boolean isAmd64()
{
return "amd64".equals( System.getProperty( "os.arch" ) );
}

private static boolean isAarch64()
{
return "aarch64".equals( System.getProperty( "os.arch" ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ public interface BungeeCipher
void cipher(ByteBuf in, ByteBuf out) throws GeneralSecurityException;

ByteBuf cipher(ChannelHandlerContext ctx, ByteBuf in) throws GeneralSecurityException;

/*
* This indicates whether the input ByteBuf is allowed to be a CompositeByteBuf.
* If you need access to a memory address, you should not allow composite buffers.
*/
boolean allowComposite();
}
13 changes: 10 additions & 3 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 @@ -2,6 +2,7 @@

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.concurrent.FastThreadLocal;
import java.security.GeneralSecurityException;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
Expand All @@ -12,10 +13,10 @@ public class JavaCipher implements BungeeCipher
{

private final Cipher cipher;
private static final ThreadLocal<byte[]> heapInLocal = new EmptyByteThreadLocal();
private static final ThreadLocal<byte[]> heapOutLocal = new EmptyByteThreadLocal();
private static final FastThreadLocal<byte[]> heapInLocal = new EmptyByteThreadLocal();
private static final FastThreadLocal<byte[]> heapOutLocal = new EmptyByteThreadLocal();

private static class EmptyByteThreadLocal extends ThreadLocal<byte[]>
private static class EmptyByteThreadLocal extends FastThreadLocal<byte[]>
{

@Override
Expand Down Expand Up @@ -88,4 +89,10 @@ private byte[] bufToByte(ByteBuf in)
in.readBytes( heapIn, 0, readableBytes );
return heapIn;
}

@Override
public boolean allowComposite()
{
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,10 @@ public ByteBuf cipher(ChannelHandlerContext ctx, ByteBuf in) throws GeneralSecur

return heapOut;
}

@Override
public boolean allowComposite()
{
return false;
}
}
7 changes: 7 additions & 0 deletions native/src/main/java/net/md_5/bungee/jni/zlib/BungeeZlib.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@
public interface BungeeZlib
{

public static final int OUTPUT_BUFFER_SIZE = 8192;

void init(boolean compress, int level);

void free();

void process(ByteBuf in, ByteBuf out) throws DataFormatException;

void process(ByteBuf in, ByteBuf out, boolean preallocatedBuffer) throws DataFormatException; //BotFilter
/*
* This indicates whether the input ByteBuf is allowed to be a CompositeByteBuf.
* If you need access to a memory address, you should not allow composite buffers.
*/
boolean allowComposite();
}
5 changes: 5 additions & 0 deletions native/src/main/java/net/md_5/bungee/jni/zlib/JavaZlib.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@ public void process(ByteBuf in, ByteBuf out, boolean preallocatedBuffer) throws
process( in, out );
}
//BotFilter end
@Override
public boolean allowComposite()
{
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void process(ByteBuf in, ByteBuf out, boolean preallocatedBuffer) throws

if ( !preallocatedBuffer ) //BotFilter start
{
out.ensureWritable( 8192 );
out.ensureWritable( OUTPUT_BUFFER_SIZE );
} //BotFilter end

int processed;
Expand All @@ -84,4 +84,10 @@ public void process(ByteBuf in, ByteBuf out, boolean preallocatedBuffer) throws
nativeCompress.consumed = 0;
nativeCompress.finished = false;
}

@Override
public boolean allowComposite()
{
return false;
}
}
Binary file added native/src/main/resources/native-cipher-arm.so
Binary file not shown.
Binary file added native/src/main/resources/native-compress-arm.so
Binary file not shown.
Loading

0 comments on commit 3f94dfd

Please sign in to comment.