XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEServerManager

【Java】我的世界XFE服务器管理器

公开
关注 0 Fork 0 Star 0
UTF-8
package com.xfestudio.xfeservermanager.tooling;

import java.lang.instrument.Instrumentation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.channels.Selector;

/**
 * Build-only workaround for restricted Windows hosts that expose AF_UNIX but
 * reject local AF_UNIX connections. It forces the JDK selector wakeup pipe to
 * use its TCP loopback fallback. This class is never packaged in the mod.
 */
public final class ForceTcpLoopbackAgent {
    private ForceTcpLoopbackAgent() {
    }

    public static void main(String[] arguments) {
        try (Selector ignored = Selector.open()) {
            // Successful construction means the host supports the JDK's default wakeup pipe.
        }
        catch (Throwable throwable) {
            System.exit(2);
        }
    }

    public static void premain(String arguments, Instrumentation instrumentation) {
        try {
            Class<?> unsafeType = Class.forName("sun.misc.Unsafe");
            Field singletonField = unsafeType.getDeclaredField("theUnsafe");
            singletonField.setAccessible(true);
            Object unsafe = singletonField.get(null);

            Class<?> socketsType = Class.forName("sun.nio.ch.UnixDomainSockets", true, null);
            Field supportedField = socketsType.getDeclaredField("supported");
            Method staticFieldBase = unsafeType.getMethod("staticFieldBase", Field.class);
            Method staticFieldOffset = unsafeType.getMethod("staticFieldOffset", Field.class);
            Method putBoolean = unsafeType.getMethod("putBoolean", Object.class, long.class, boolean.class);
            Object base = staticFieldBase.invoke(unsafe, supportedField);
            long offset = ((Number) staticFieldOffset.invoke(unsafe, supportedField)).longValue();
            putBoolean.invoke(unsafe, base, offset, false);
        }
        catch (ReflectiveOperationException exception) {
            throw new IllegalStateException("Unable to force TCP loopback for the build JVM", exception);
        }
    }
}