2021-06-11 14:02:28 +02: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
diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
2024-10-23 20:15:25 +02:00
index cbff8de02006b65f6d58416fc0950741cc14908d..e30a5ad17d7ba8bcec8911a72281830c419b0288 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
+++ b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
2024-10-23 20:15:25 +02:00
@@ -235,6 +235,20 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface
2021-06-11 14:02:28 +02:00
this.setEnforceWhitelist(dedicatedserverproperties.enforceWhitelist);
2021-06-15 04:59:31 +02:00
// this.worldData.setGameType(dedicatedserverproperties.gamemode); // CraftBukkit - moved to world loading
2021-06-11 14:02:28 +02: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 21:34:23 +01: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 14:02:28 +02:00
+ return false;
2022-06-09 10:51:45 +02:00
+ } else if (!io.papermc.paper.configuration.GlobalConfiguration.get().proxies.velocity.enabled && !org.spigotmc.SpigotConfig.bungee) {
2022-02-28 21:34:23 +01:00
+ DedicatedServer.LOGGER.error("**** INVALID CONFIGURATION!");
+ DedicatedServer.LOGGER.error("Unix domain sockets require IPs to be forwarded from a proxy.");
2021-06-11 14:02:28 +02:00
+ return false;
+ }
+ bindAddress = new io.netty.channel.unix.DomainSocketAddress(this.getLocalIp().substring("unix:".length()));
+ } else {
InetAddress inetaddress = null;
if (!this.getLocalIp().isEmpty()) {
2024-10-23 20:15:25 +02:00
@@ -244,12 +258,15 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface
2021-06-11 14:02:28 +02:00
if (this.getPort() < 0) {
this.setPort(dedicatedserverproperties.serverPort);
}
+ bindAddress = new java.net.InetSocketAddress(inetaddress, this.getPort());
+ }
2024-01-19 13:22:30 +01:00
+ // Paper end - Unix domain socket support
2021-06-11 14:02:28 +02:00
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
2024-04-24 15:46:45 +02:00
index d6d7f1c446ba5507f67038ff27775ba75156f4a7..c63c194c44646e6bc1a59426552787011fc2ced5 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/network/ServerConnectionListener.java
+++ b/src/main/java/net/minecraft/server/network/ServerConnectionListener.java
2024-01-23 15:43:48 +01:00
@@ -76,7 +76,12 @@ public class ServerConnectionListener {
2021-06-11 14:02:28 +02:00
this.running = true;
}
2024-01-19 13:22:30 +01:00
+ // Paper start - Unix domain socket support
2021-06-11 14:02:28 +02:00
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 {
2024-01-19 13:22:30 +01:00
+ // Paper end - Unix domain socket support
2021-06-11 14:02:28 +02:00
List list = this.channels;
synchronized (this.channels) {
2024-01-23 15:43:48 +01:00
@@ -84,7 +89,13 @@ public class ServerConnectionListener {
2023-09-22 07:41:27 +02:00
EventLoopGroup eventloopgroup;
2021-06-11 14:02:28 +02:00
if (Epoll.isAvailable() && this.server.isEpollEnabled()) {
2024-01-19 13:22:30 +01:00
+ // Paper start - Unix domain socket support
2021-06-11 14:02:28 +02:00
+ if (address instanceof io.netty.channel.unix.DomainSocketAddress) {
+ oclass = io.netty.channel.epoll.EpollServerDomainSocketChannel.class;
+ } else {
oclass = EpollServerSocketChannel.class;
+ }
2024-01-19 13:22:30 +01:00
+ // Paper end - Unix domain socket support
2023-09-22 07:41:27 +02:00
eventloopgroup = (EventLoopGroup) ServerConnectionListener.SERVER_EPOLL_EVENT_GROUP.get();
2021-06-11 14:02:28 +02:00
ServerConnectionListener.LOGGER.info("Using epoll channel type");
} else {
2024-04-24 15:46:45 +02:00
@@ -117,7 +128,7 @@ public class ServerConnectionListener {
2023-09-22 07:41:27 +02:00
((Connection) object).setListenerForServerboundHandshake(new ServerHandshakePacketListenerImpl(ServerConnectionListener.this.server, (Connection) object));
2024-01-19 17:54:05 +01:00
io.papermc.paper.network.ChannelInitializeListenerHolder.callListeners(channel); // Paper - Add Channel initialization listeners
2021-06-11 14:02:28 +02:00
}
2023-09-22 07:41:27 +02:00
- }).group(eventloopgroup).localAddress(address, port)).option(ChannelOption.AUTO_READ, false).bind().syncUninterruptibly()); // CraftBukkit
2024-01-19 13:22:30 +01:00
+ }).group(eventloopgroup).localAddress(address)).option(ChannelOption.AUTO_READ, false).bind().syncUninterruptibly()); // CraftBukkit // Paper - Unix domain socket support
2021-06-11 14:02:28 +02:00
}
}
2023-03-14 21:25:13 +01:00
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
2024-10-24 19:29:35 +02:00
index 6839d13bc5cfad7c408d55e0cd477085333c7e41..674149f3a392a600a506e55f20db044619328cd2 100644
2023-03-14 21:25:13 +01:00
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
2024-10-24 19:29:35 +02:00
@@ -2569,6 +2569,11 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
2023-03-14 21:25:13 +01:00
// Spigot Start
public SocketAddress getRawAddress()
{
2024-01-19 13:22:30 +01:00
+ // Paper start - Unix domain socket support; this can be nullable in the case of a Unix domain socket, so if it is, fake something
2023-03-14 21:25:13 +01:00
+ if (connection.channel.remoteAddress() == null) {
+ return new java.net.InetSocketAddress(java.net.InetAddress.getLoopbackAddress(), 0);
+ }
2024-01-19 13:22:30 +01:00
+ // Paper end - Unix domain socket support
2023-03-14 21:25:13 +01:00
return this.connection.channel.remoteAddress();
}
// Spigot End
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/server/network/ServerHandshakePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerHandshakePacketListenerImpl.java
2024-06-14 01:32:45 +02:00
index a5bbea6a073e00c10c3c5facd997eb8473fd9a5f..ddf42645402afefc0f5caebc684b191eef9d6ec2 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/network/ServerHandshakePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerHandshakePacketListenerImpl.java
2024-06-14 01:32:45 +02:00
@@ -81,6 +81,7 @@ public class ServerHandshakePacketListenerImpl implements ServerHandshakePacketL
2024-04-24 15:46:45 +02:00
this.connection.setupOutboundProtocol(LoginProtocols.CLIENTBOUND);
// CraftBukkit start - Connection throttle
try {
+ if (!(this.connection.channel.localAddress() instanceof io.netty.channel.unix.DomainSocketAddress)) { // Paper - Unix domain socket support; 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();
2024-06-14 01:32:45 +02:00
@@ -109,6 +110,7 @@ public class ServerHandshakePacketListenerImpl implements ServerHandshakePacketL
2021-06-11 14:02:28 +02:00
}
}
2024-04-24 15:46:45 +02:00
}
+ } // Paper - Unix domain socket support
} catch (Throwable t) {
org.apache.logging.log4j.LogManager.getLogger().debug("Failed to check connection throttle", t);
}
2024-06-14 01:32:45 +02:00
@@ -166,8 +168,11 @@ public class ServerHandshakePacketListenerImpl implements ServerHandshakePacketL
2024-04-24 15:46:45 +02:00
if (!handledByEvent && proxyLogicEnabled) { // Paper
// if (org.spigotmc.SpigotConfig.bungee) { // Paper - comment out, we check above!
if ( ( split.length == 3 || split.length == 4 ) && ( ServerHandshakePacketListenerImpl.BYPASS_HOSTCHECK || ServerHandshakePacketListenerImpl.HOST_PATTERN.matcher( split[1] ).matches() ) ) { // Paper - Add bypass host check
+ // Paper start - Unix domain socket support
+ java.net.SocketAddress socketAddress = this.connection.getRemoteAddress();
this.connection.hostname = split[0];
- this.connection.address = new java.net.InetSocketAddress(split[1], ((java.net.InetSocketAddress) this.connection.getRemoteAddress()).getPort());
+ this.connection.address = new java.net.InetSocketAddress(split[1], socketAddress instanceof java.net.InetSocketAddress ? ((java.net.InetSocketAddress) socketAddress).getPort() : 0);
+ // Paper end - Unix domain socket support
this.connection.spoofedUUID = com.mojang.util.UndashedUuid.fromStringLenient( split[2] );
} else
{