testserver/patches/server/0648-Add-Unix-domain-socket...

142 lines
9.0 KiB
Diff
Raw Normal View History

2021-06-11 12:02:28 +00:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Andrew Steinborn <git@steinborn.me>
Date: Tue, 11 May 2021 17:39:22 -0400
Subject: [PATCH] Add Unix domain socket support
For Windows and ARM support, JEP-380 is required:
https://inside.java/2021/02/03/jep380-unix-domain-sockets-channels/
This will be possible as of the Minecraft 1.17 Java version bump.
Tested-by: Mariell Hoversholm <proximyst@proximyst.com>
Reviewed-by: Mariell Hoversholm <proximyst@proximyst.com>
diff --git a/src/main/java/net/minecraft/network/Connection.java b/src/main/java/net/minecraft/network/Connection.java
2022-06-08 09:31:06 +00:00
index 4798ac3a9c7a7d34eaca5cb50b013b071793ed94..00ab93e02a884e51cd6cc647c7e58c2e53b8a7cc 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/network/Connection.java
+++ b/src/main/java/net/minecraft/network/Connection.java
2022-06-08 09:31:06 +00:00
@@ -628,6 +628,11 @@ public class Connection extends SimpleChannelInboundHandler<Packet<?>> {
2021-06-11 12:02:28 +00:00
// Spigot Start
public SocketAddress getRawAddress()
{
+ // Paper start - this can be nullable in the case of a Unix domain socket, so if it is, fake something
+ if (this.channel.remoteAddress() == null) {
+ return new java.net.InetSocketAddress(java.net.InetAddress.getLoopbackAddress(), 0);
+ }
+ // Paper end
return this.channel.remoteAddress();
}
// Spigot End
diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
2022-06-08 09:31:06 +00:00
index 949e2050dae9048f53cd5b8452763be5fbd70958..5cc3a55b90135f0aa97c5faaf6c1b6f4d6fe9a0e 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
+++ b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
2022-06-08 09:31:06 +00:00
@@ -226,6 +226,20 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface
2021-06-11 12:02:28 +00:00
this.setEnforceWhitelist(dedicatedserverproperties.enforceWhitelist);
2021-06-15 02:59:31 +00:00
// this.worldData.setGameType(dedicatedserverproperties.gamemode); // CraftBukkit - moved to world loading
2021-06-11 12:02:28 +00:00
DedicatedServer.LOGGER.info("Default game type: {}", dedicatedserverproperties.gamemode);
+ // Paper start - Unix domain socket support
+ java.net.SocketAddress bindAddress;
+ if (this.getLocalIp().startsWith("unix:")) {
+ if (!io.netty.channel.epoll.Epoll.isAvailable()) {
2022-02-28 20:34:23 +00:00
+ DedicatedServer.LOGGER.error("**** INVALID CONFIGURATION!");
+ DedicatedServer.LOGGER.error("You are trying to use a Unix domain socket but you're not on a supported OS.");
2021-06-11 12:02:28 +00:00
+ return false;
+ } else if (!com.destroystokyo.paper.PaperConfig.velocitySupport && !org.spigotmc.SpigotConfig.bungee) {
2022-02-28 20:34:23 +00:00
+ DedicatedServer.LOGGER.error("**** INVALID CONFIGURATION!");
+ DedicatedServer.LOGGER.error("Unix domain sockets require IPs to be forwarded from a proxy.");
2021-06-11 12:02:28 +00:00
+ return false;
+ }
+ bindAddress = new io.netty.channel.unix.DomainSocketAddress(this.getLocalIp().substring("unix:".length()));
+ } else {
InetAddress inetaddress = null;
if (!this.getLocalIp().isEmpty()) {
2022-06-08 09:31:06 +00:00
@@ -235,12 +249,15 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface
2021-06-11 12:02:28 +00:00
if (this.getPort() < 0) {
this.setPort(dedicatedserverproperties.serverPort);
}
+ bindAddress = new java.net.InetSocketAddress(inetaddress, this.getPort());
+ }
+ // Paper end
this.initializeKeyPair();
DedicatedServer.LOGGER.info("Starting Minecraft server on {}:{}", this.getLocalIp().isEmpty() ? "*" : this.getLocalIp(), this.getPort());
try {
- this.getConnection().startTcpServerListener(inetaddress, this.getPort());
+ this.getConnection().bind(bindAddress); // Paper - Unix domain socket support
} catch (IOException ioexception) {
DedicatedServer.LOGGER.warn("**** FAILED TO BIND TO PORT!");
DedicatedServer.LOGGER.warn("The exception was: {}", ioexception.toString());
diff --git a/src/main/java/net/minecraft/server/network/ServerConnectionListener.java b/src/main/java/net/minecraft/server/network/ServerConnectionListener.java
2022-06-08 09:31:06 +00:00
index 076c1d5fb69a4a30b3e412e85b17db09fd9a1def..9e7154c9df96d5a0577886d43a98a73626a8f189 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/server/network/ServerConnectionListener.java
+++ b/src/main/java/net/minecraft/server/network/ServerConnectionListener.java
2022-06-08 09:31:06 +00:00
@@ -79,7 +79,12 @@ public class ServerConnectionListener {
2021-06-11 12:02:28 +00:00
this.running = true;
}
+ // Paper start
public void startTcpServerListener(@Nullable InetAddress address, int port) throws IOException {
+ bind(new java.net.InetSocketAddress(address, port));
+ }
+ public void bind(java.net.SocketAddress address) throws IOException {
+ // Paper end
List list = this.channels;
synchronized (this.channels) {
2022-06-08 09:31:06 +00:00
@@ -87,7 +92,11 @@ public class ServerConnectionListener {
2021-06-11 12:02:28 +00:00
LazyLoadedValue lazyinitvar;
if (Epoll.isAvailable() && this.server.isEpollEnabled()) {
+ if (address instanceof io.netty.channel.unix.DomainSocketAddress) {
+ oclass = io.netty.channel.epoll.EpollServerDomainSocketChannel.class;
+ } else {
oclass = EpollServerSocketChannel.class;
+ }
lazyinitvar = ServerConnectionListener.SERVER_EPOLL_EVENT_GROUP;
ServerConnectionListener.LOGGER.info("Using epoll channel type");
} else {
2022-06-08 09:31:06 +00:00
@@ -115,7 +124,7 @@ public class ServerConnectionListener {
2021-06-11 12:02:28 +00:00
((Connection) object).setListener(new ServerHandshakePacketListenerImpl(ServerConnectionListener.this.server, (Connection) object));
io.papermc.paper.network.ChannelInitializeListenerHolder.callListeners(channel); // Paper
}
- }).group((EventLoopGroup) lazyinitvar.get()).localAddress(address, port)).option(ChannelOption.AUTO_READ, false).bind().syncUninterruptibly()); // CraftBukkit
+ }).group((EventLoopGroup) lazyinitvar.get()).localAddress(address)).option(ChannelOption.AUTO_READ, false).bind().syncUninterruptibly()); // CraftBukkit // Paper
}
}
diff --git a/src/main/java/net/minecraft/server/network/ServerHandshakePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerHandshakePacketListenerImpl.java
2022-06-08 09:31:06 +00:00
index a54bb43a3acf31be0278fee3d23de9acfdfc64cf..95340e4067ff1cbc9539615adb260377a5187cb7 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/server/network/ServerHandshakePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerHandshakePacketListenerImpl.java
2022-06-08 09:31:06 +00:00
@@ -43,6 +43,7 @@ public class ServerHandshakePacketListenerImpl implements ServerHandshakePacketL
2021-06-11 12:02:28 +00:00
this.connection.setProtocol(ConnectionProtocol.LOGIN);
// CraftBukkit start - Connection throttle
try {
+ if (!(this.connection.channel.localAddress() instanceof io.netty.channel.unix.DomainSocketAddress)) { // Paper - the connection throttle is useless when you have a Unix domain socket
long currentTime = System.currentTimeMillis();
long connectionThrottle = this.server.server.getConnectionThrottle();
InetAddress address = ((java.net.InetSocketAddress) this.connection.getRemoteAddress()).getAddress();
2022-06-08 09:31:06 +00:00
@@ -71,6 +72,7 @@ public class ServerHandshakePacketListenerImpl implements ServerHandshakePacketL
2021-06-11 12:02:28 +00:00
}
}
}
+ } // Paper - add closing bracket for if check above
} catch (Throwable t) {
org.apache.logging.log4j.LogManager.getLogger().debug("Failed to check connection throttle", t);
}
2022-06-08 09:31:06 +00:00
@@ -119,8 +121,11 @@ public class ServerHandshakePacketListenerImpl implements ServerHandshakePacketL
// Paper end
// if (org.spigotmc.SpigotConfig.bungee) { // Paper - comment out, we check above!
2021-06-15 02:59:31 +00:00
if ( ( split.length == 3 || split.length == 4 ) && ( ServerHandshakePacketListenerImpl.BYPASS_HOSTCHECK || ServerHandshakePacketListenerImpl.HOST_PATTERN.matcher( split[1] ).matches() ) ) { // Paper
2021-06-11 12:02:28 +00:00
+ // Paper start - Unix domain socket support
+ java.net.SocketAddress socketAddress = connection.getRemoteAddress();
packet.hostName = split[0];
2021-06-15 02:59:31 +00:00
- connection.address = new java.net.InetSocketAddress(split[1], ((java.net.InetSocketAddress) this.connection.getRemoteAddress()).getPort());
2021-06-11 12:02:28 +00:00
+ connection.address = new java.net.InetSocketAddress(split[1], socketAddress instanceof java.net.InetSocketAddress ? ((java.net.InetSocketAddress) socketAddress).getPort() : 0);
+ // Paper end
connection.spoofedUUID = com.mojang.util.UUIDTypeAdapter.fromString( split[2] );
} else
{