Skip to content

Commit e12c6cc

Browse files
committed
Fix unix domain socket support
1 parent 8987f91 commit e12c6cc

File tree

12 files changed

+121
-30
lines changed

12 files changed

+121
-30
lines changed

paper-api/src/main/java/com/destroystokyo/paper/event/server/PaperServerListPingEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class PaperServerListPingEvent extends ServerListPingEvent implements Can
5858
@ApiStatus.Internal
5959
public PaperServerListPingEvent(@NotNull StatusClient client, @NotNull net.kyori.adventure.text.Component motd, int numPlayers, int maxPlayers,
6060
@NotNull String version, int protocolVersion, @Nullable CachedServerIcon favicon) {
61-
super("", client.getAddress().getAddress(), motd, numPlayers, maxPlayers);
61+
super("", client.getSocketAddress(), motd, numPlayers, maxPlayers);
6262
this.client = client;
6363
this.numPlayers = numPlayers;
6464
this.version = version;

paper-api/src/main/java/com/destroystokyo/paper/network/NetworkClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.destroystokyo.paper.network;
22

33
import java.net.InetSocketAddress;
4+
import java.net.SocketAddress;
45
import org.jspecify.annotations.NullMarked;
56
import org.jspecify.annotations.Nullable;
67

@@ -15,6 +16,15 @@ public interface NetworkClient {
1516
*
1617
* @return The client's socket address
1718
*/
19+
SocketAddress getSocketAddress();
20+
21+
/**
22+
* Returns an instance of {@link InetSocketAddress} associated with the
23+
* client's socket address.
24+
*
25+
* @return The client's {@link InetSocketAddress}, or the loopback address
26+
* if this is a Unix socket connection
27+
*/
1828
InetSocketAddress getAddress();
1929

2030
/**

paper-api/src/main/java/org/bukkit/entity/Player.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.papermc.paper.math.Position;
88
import java.net.InetAddress;
99
import java.net.InetSocketAddress;
10+
import java.net.SocketAddress;
1011
import java.time.Duration;
1112
import java.time.Instant;
1213
import java.util.Collection;
@@ -271,7 +272,17 @@ default net.kyori.adventure.identity.Identity identity() {
271272
* @return the player's address
272273
*/
273274
@Nullable
274-
public InetSocketAddress getAddress();
275+
SocketAddress getSocketAddress();
276+
277+
/**
278+
* Returns an instance of {@link InetSocketAddress} associated with the
279+
* player's socket address.
280+
*
281+
* @return the player's {@link InetSocketAddress}, or the loopback address
282+
* if the player is connecting through a Unix socket
283+
*/
284+
@Nullable
285+
InetSocketAddress getAddress();
275286

276287
// Paper start - Add API to get player's proxy address
277288
/**

paper-api/src/main/java/org/bukkit/event/player/AsyncPlayerPreLoginEvent.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.bukkit.event.player;
22

33
import java.net.InetAddress;
4+
import java.net.InetSocketAddress;
5+
import java.net.SocketAddress;
46
import java.util.UUID;
57
import com.destroystokyo.paper.profile.PlayerProfile;
68
import net.kyori.adventure.text.Component;
@@ -31,7 +33,7 @@ public class AsyncPlayerPreLoginEvent extends Event {
3133
private static final HandlerList HANDLER_LIST = new HandlerList();
3234

3335
private final InetAddress ipAddress;
34-
private final InetAddress rawAddress;
36+
private final SocketAddress rawAddress;
3537
private final String hostname;
3638
private final boolean transferred;
3739
private Result result;
@@ -65,11 +67,11 @@ public AsyncPlayerPreLoginEvent(@NotNull final String name, @NotNull final InetA
6567
@ApiStatus.Internal
6668
@Deprecated(forRemoval = true)
6769
public AsyncPlayerPreLoginEvent(@NotNull final String name, @NotNull final InetAddress ipAddress, @NotNull final InetAddress rawAddress, @NotNull final UUID uniqueId, boolean transferred, @NotNull com.destroystokyo.paper.profile.PlayerProfile profile) {
68-
this(name, ipAddress, rawAddress, uniqueId, transferred, profile, "", null);
70+
this(name, ipAddress, new InetSocketAddress(rawAddress, 0), uniqueId, transferred, profile, "", null);
6971
}
7072

7173
@ApiStatus.Internal
72-
public AsyncPlayerPreLoginEvent(@NotNull final String name, @NotNull final InetAddress ipAddress, @NotNull final InetAddress rawAddress, @NotNull final UUID uniqueId, boolean transferred, @NotNull com.destroystokyo.paper.profile.PlayerProfile profile, @NotNull String hostname, final PlayerLoginConnection playerLoginConnection) {
74+
public AsyncPlayerPreLoginEvent(@NotNull final String name, @NotNull final InetAddress ipAddress, @NotNull final SocketAddress rawAddress, @NotNull final UUID uniqueId, boolean transferred, @NotNull com.destroystokyo.paper.profile.PlayerProfile profile, @NotNull String hostname, final PlayerLoginConnection playerLoginConnection) {
7375
super(true);
7476
this.result = Result.ALLOWED;
7577
this.message = Component.empty();
@@ -278,13 +280,24 @@ public void setPlayerProfile(@NotNull com.destroystokyo.paper.profile.PlayerProf
278280
this.profile = profile;
279281
}
280282

283+
/**
284+
* Gets the raw socket address of the player logging in
285+
* @return The socket address
286+
*/
287+
@NotNull
288+
public SocketAddress getRawSocketAddress() {
289+
return this.rawAddress;
290+
}
291+
281292
/**
282293
* Gets the raw address of the player logging in
283-
* @return The address
294+
* @return The address, or the loopback address if the player is connecting
295+
* through a Unix socket
284296
*/
285297
@NotNull
286298
public InetAddress getRawAddress() {
287-
return this.rawAddress;
299+
if (this.rawAddress instanceof InetSocketAddress inet) return inet.getAddress();
300+
return InetAddress.getLoopbackAddress();
288301
}
289302

290303
/**

paper-api/src/main/java/org/bukkit/event/player/PlayerLoginEvent.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.bukkit.event.player;
22

33
import java.net.InetAddress;
4+
import java.net.InetSocketAddress;
5+
import java.net.SocketAddress;
46
import io.papermc.paper.event.connection.PlayerConnectionValidateLoginEvent;
57
import org.bukkit.Warning;
68
import net.kyori.adventure.text.Component;
@@ -32,18 +34,23 @@ public class PlayerLoginEvent extends PlayerEvent {
3234

3335
private final String hostname;
3436
private final InetAddress address;
35-
private final InetAddress realAddress;
37+
private final SocketAddress realAddress;
3638
private Result result = Result.ALLOWED;
3739
private Component message = Component.empty();
3840

3941
@ApiStatus.Internal
40-
public PlayerLoginEvent(@NotNull final Player player, @NotNull final String hostname, @NotNull final InetAddress address, final @NotNull InetAddress realAddress) {
42+
public PlayerLoginEvent(@NotNull final Player player, @NotNull final String hostname, @NotNull final InetAddress address, final @NotNull SocketAddress realAddress) {
4143
super(player);
4244
this.hostname = hostname;
4345
this.address = address;
4446
this.realAddress = realAddress;
4547
}
4648

49+
@ApiStatus.Internal
50+
public PlayerLoginEvent(@NotNull final Player player, @NotNull final String hostname, @NotNull final InetAddress address, final @NotNull InetAddress realAddress) {
51+
this(player, hostname, address, new InetSocketAddress(address, 0));
52+
}
53+
4754
@ApiStatus.Internal
4855
public PlayerLoginEvent(@NotNull final Player player, @NotNull final String hostname, @NotNull final InetAddress address) {
4956
this(player, hostname, address, address);
@@ -88,16 +95,30 @@ public InetAddress getAddress() {
8895
return this.address;
8996
}
9097

98+
99+
/**
100+
* Gets the connection socket address of this player, regardless of whether
101+
* it has been spoofed or not.
102+
*
103+
* @return the player's connection socket address
104+
*/
105+
@NotNull
106+
public SocketAddress getRealSocketAddress() {
107+
return this.realAddress;
108+
}
109+
91110
/**
92111
* Gets the connection address of this player, regardless of whether it has
93112
* been spoofed or not.
94113
*
95-
* @return the player's connection address
114+
* @return the player's connection address, or the loopback address if the
115+
* player is connecting through a Unix socket
96116
* @see #getAddress()
97117
*/
98118
@NotNull
99119
public InetAddress getRealAddress() {
100-
return this.realAddress;
120+
if (this.realAddress instanceof InetSocketAddress inet) return inet.getAddress();
121+
return InetAddress.getLoopbackAddress();
101122
}
102123

103124
/**

paper-api/src/main/java/org/bukkit/event/server/ServerListPingEvent.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.google.common.base.Preconditions;
44
import java.net.InetAddress;
5+
import java.net.InetSocketAddress;
6+
import java.net.SocketAddress;
57
import java.util.Iterator;
68
import net.kyori.adventure.text.Component;
79
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
@@ -28,14 +30,14 @@ public class ServerListPingEvent extends ServerEvent implements Iterable<Player>
2830
private static final HandlerList HANDLER_LIST = new HandlerList();
2931

3032
private final String hostname;
31-
private final InetAddress address;
33+
private final SocketAddress address;
3234
private final int numPlayers;
3335
private Component motd;
3436
private int maxPlayers;
3537

3638
@ApiStatus.Internal
3739
@Deprecated(forRemoval = true)
38-
public ServerListPingEvent(@NotNull final String hostname, @NotNull final InetAddress address, @NotNull final String motd, final int numPlayers, final int maxPlayers) {
40+
public ServerListPingEvent(@NotNull final String hostname, @NotNull final SocketAddress address, @NotNull final String motd, final int numPlayers, final int maxPlayers) {
3941
super(true);
4042
Preconditions.checkArgument(numPlayers >= 0, "Cannot have negative number of players online", numPlayers);
4143
this.hostname = hostname;
@@ -47,7 +49,7 @@ public ServerListPingEvent(@NotNull final String hostname, @NotNull final InetAd
4749

4850
@ApiStatus.Internal
4951
@Deprecated(forRemoval = true)
50-
protected ServerListPingEvent(@NotNull final String hostname, @NotNull final InetAddress address, @NotNull final String motd, final int maxPlayers) {
52+
protected ServerListPingEvent(@NotNull final String hostname, @NotNull final SocketAddress address, @NotNull final String motd, final int maxPlayers) {
5153
super(true);
5254
this.numPlayers = MAGIC_PLAYER_COUNT;
5355
this.hostname = hostname;
@@ -59,11 +61,11 @@ protected ServerListPingEvent(@NotNull final String hostname, @NotNull final Ine
5961
@ApiStatus.Internal
6062
@Deprecated(forRemoval = true)
6163
public ServerListPingEvent(@NotNull final InetAddress address, @NotNull final Component motd, final int numPlayers, final int maxPlayers) {
62-
this("", address, motd, numPlayers, maxPlayers);
64+
this("", new InetSocketAddress(address, 0), motd, numPlayers, maxPlayers);
6365
}
6466

6567
@ApiStatus.Internal
66-
public ServerListPingEvent(@NotNull final String hostname, @NotNull final InetAddress address, @NotNull final Component motd, final int numPlayers, final int maxPlayers) {
68+
public ServerListPingEvent(@NotNull final String hostname, @NotNull final SocketAddress address, @NotNull final Component motd, final int numPlayers, final int maxPlayers) {
6769
super(true);
6870
this.hostname = hostname;
6971
this.address = address;
@@ -75,7 +77,7 @@ public ServerListPingEvent(@NotNull final String hostname, @NotNull final InetAd
7577
@ApiStatus.Internal
7678
@Deprecated(forRemoval = true)
7779
protected ServerListPingEvent(@NotNull final InetAddress address, @NotNull final Component motd, final int maxPlayers) {
78-
this("", address, motd, maxPlayers);
80+
this("", new InetSocketAddress(address, 0), motd, maxPlayers);
7981
}
8082

8183
/*
@@ -84,7 +86,7 @@ protected ServerListPingEvent(@NotNull final InetAddress address, @NotNull final
8486
* count.
8587
*/
8688
@ApiStatus.Internal
87-
protected ServerListPingEvent(final @NotNull String hostname, final @NotNull InetAddress address, final @NotNull Component motd, final int maxPlayers) {
89+
protected ServerListPingEvent(final @NotNull String hostname, final @NotNull SocketAddress address, final @NotNull Component motd, final int maxPlayers) {
8890
this.numPlayers = MAGIC_PLAYER_COUNT;
8991
this.hostname = hostname;
9092
this.address = address;
@@ -103,14 +105,26 @@ public String getHostname() {
103105
return this.hostname;
104106
}
105107

108+
/**
109+
* Gets the socket address the ping is coming from.
110+
*
111+
* @return the socket address
112+
*/
113+
@NotNull
114+
public SocketAddress getSocketAddress() {
115+
return this.address;
116+
}
117+
106118
/**
107119
* Get the address the ping is coming from.
108120
*
109-
* @return the address
121+
* @return the socket address, or the loopback address if the ping is
122+
* coming from a Unix socket
110123
*/
111124
@NotNull
112125
public InetAddress getAddress() {
113-
return this.address;
126+
if (this.address instanceof InetSocketAddress inet) return inet.getAddress();
127+
return InetAddress.getLoopbackAddress();
114128
}
115129

116130
/**

paper-server/patches/sources/net/minecraft/server/network/LegacyQueryHandler.java.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
- String body = createVersion0Response(this.server);
3737
+ LOGGER.debug("Ping: (<1.3.x) from {}", net.minecraft.server.MinecraftServer.getServer().logIPs() ? socket : "<ip address withheld>"); // Paper - Respect logIPs option
3838
+ // Paper start - Call PaperServerListPingEvent and use results
39-
+ com.destroystokyo.paper.event.server.PaperServerListPingEvent event = com.destroystokyo.paper.network.PaperLegacyStatusClient.processRequest(net.minecraft.server.MinecraftServer.getServer(), (java.net.InetSocketAddress) socket, 39, null);
39+
+ com.destroystokyo.paper.event.server.PaperServerListPingEvent event = com.destroystokyo.paper.network.PaperLegacyStatusClient.processRequest(net.minecraft.server.MinecraftServer.getServer(), socket, 39, null);
4040
+ if (event == null) {
4141
+ ctx.close();
4242
+ in.release();

paper-server/patches/sources/net/minecraft/server/network/ServerLoginPacketListenerImpl.java.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@
239239
+ final org.bukkit.craftbukkit.CraftServer server = ServerLoginPacketListenerImpl.this.server.server;
240240
+
241241
+ // Paper start - Add more fields to AsyncPlayerPreLoginEvent
242-
+ final InetAddress rawAddress = ((InetSocketAddress) this.connection.channel.remoteAddress()).getAddress();
242+
+ final SocketAddress rawAddress = this.connection.channel.remoteAddress();
243243
+ com.destroystokyo.paper.profile.PlayerProfile profile = com.destroystokyo.paper.profile.CraftPlayerProfile.asBukkitCopy(gameprofile); // Paper - setPlayerProfileAPI
244244
+ AsyncPlayerPreLoginEvent asyncEvent = new AsyncPlayerPreLoginEvent(playerName, address, rawAddress, uniqueId, this.transferred, profile, this.connection.hostname, this.paperLoginConnection); // Paper
245245
+ server.getPluginManager().callEvent(asyncEvent);

paper-server/src/main/java/com/destroystokyo/paper/network/PaperLegacyStatusClient.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,35 @@
66
import net.minecraft.server.MinecraftServer;
77
import org.apache.commons.lang3.StringUtils;
88

9+
import java.net.InetAddress;
910
import java.net.InetSocketAddress;
11+
import java.net.SocketAddress;
1012

1113
import javax.annotation.Nullable;
1214

1315
public final class PaperLegacyStatusClient implements StatusClient {
1416

15-
private final InetSocketAddress address;
17+
private final SocketAddress address;
1618
private final int protocolVersion;
1719
@Nullable private final InetSocketAddress virtualHost;
1820

19-
private PaperLegacyStatusClient(InetSocketAddress address, int protocolVersion, @Nullable InetSocketAddress virtualHost) {
21+
private PaperLegacyStatusClient(SocketAddress address, int protocolVersion, @Nullable InetSocketAddress virtualHost) {
2022
this.address = address;
2123
this.protocolVersion = protocolVersion;
2224
this.virtualHost = virtualHost;
2325
}
2426

2527
@Override
26-
public InetSocketAddress getAddress() {
28+
public SocketAddress getSocketAddress() {
2729
return this.address;
2830
}
2931

32+
@Override
33+
public InetSocketAddress getAddress() {
34+
if (this.address instanceof InetSocketAddress inet) return inet;
35+
return new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
36+
}
37+
3038
@Override
3139
public int getProtocolVersion() {
3240
return this.protocolVersion;
@@ -44,7 +52,7 @@ public boolean isLegacy() {
4452
}
4553

4654
public static PaperServerListPingEvent processRequest(MinecraftServer server,
47-
InetSocketAddress address, int protocolVersion, @Nullable InetSocketAddress virtualHost) {
55+
SocketAddress address, int protocolVersion, @Nullable InetSocketAddress virtualHost) {
4856

4957
PaperServerListPingEvent event = new PaperServerListPingEventImpl(server,
5058
new PaperLegacyStatusClient(address, protocolVersion, virtualHost), Byte.MAX_VALUE, null);

paper-server/src/main/java/com/destroystokyo/paper/network/PaperNetworkClient.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.destroystokyo.paper.network;
22

3+
import java.net.InetAddress;
34
import java.net.InetSocketAddress;
5+
import java.net.SocketAddress;
46

57
import javax.annotation.Nullable;
68
import net.minecraft.network.Connection;
@@ -13,9 +15,15 @@ public class PaperNetworkClient implements NetworkClient {
1315
this.connection = connection;
1416
}
1517

18+
@Override
19+
public SocketAddress getSocketAddress() {
20+
return this.connection.getRemoteAddress();
21+
}
22+
1623
@Override
1724
public InetSocketAddress getAddress() {
18-
return (InetSocketAddress) this.connection.getRemoteAddress();
25+
if (this.connection.getRemoteAddress() instanceof InetSocketAddress inet) return inet;
26+
return new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
1927
}
2028

2129
@Override

0 commit comments

Comments
 (0)