aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches/0325-Add-Velocity-IP-Forwarding-Support.patch
blob: 853c468b40d90435e84b1742d7bf900e60c55954 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
From 2a1652d5c16477c05e7937b1df54b8c091267a63 Mon Sep 17 00:00:00 2001
From: Andrew Steinborn <git@steinborn.me>
Date: Mon, 8 Oct 2018 14:36:14 -0400
Subject: [PATCH] Add Velocity IP Forwarding Support

While Velocity supports BungeeCord-style IP forwarding, it is not secure. Users
have a lot of problems setting up firewalls or setting up plugins like IPWhitelist.
Further, the BungeeCord IP forwarding protocol still retains essentially its original
form, when there is brand new support for custom login plugin messages in 1.13.

Velocity's modern IP forwarding uses an HMAC-SHA256 code to ensure authenticity
of messages, is packed into a binary format that is smaller than BungeeCord's
forwarding, and is integrated into the Minecraft login process by using the 1.13
login plugin message packet.

diff --git a/src/main/java/com/destroystokyo/paper/PaperConfig.java b/src/main/java/com/destroystokyo/paper/PaperConfig.java
index d2c039ea2f..63d8602927 100644
--- a/src/main/java/com/destroystokyo/paper/PaperConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperConfig.java
@@ -8,6 +8,7 @@ import java.io.IOException;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
+import java.nio.charset.StandardCharsets;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -246,7 +247,7 @@ public class PaperConfig {
     }
 
     public static boolean isProxyOnlineMode() {
-        return Bukkit.getOnlineMode() || (SpigotConfig.bungee && bungeeOnlineMode);
+        return Bukkit.getOnlineMode() || (SpigotConfig.bungee && bungeeOnlineMode) || (velocitySupport && velocityOnlineMode);
     }
 
     public static int packetInSpamThreshold = 300;
@@ -360,4 +361,18 @@ public class PaperConfig {
             }
         }
     }
+
+    public static boolean velocitySupport;
+    public static boolean velocityOnlineMode;
+    public static byte[] velocitySecretKey;
+    private static void velocitySupport() {
+        velocitySupport = getBoolean("settings.velocity-support.enabled", false);
+        velocityOnlineMode = getBoolean("settings.velocity-support.online-mode", false);
+        String secret = getString("settings.velocity-support.secret", "");
+        if (velocitySupport && secret.isEmpty()) {
+            fatal("Velocity support is enabled, but no secret key was specified. A secret key is required!");
+        } else {
+            velocitySecretKey = secret.getBytes(StandardCharsets.UTF_8);
+        }
+    }
 }
diff --git a/src/main/java/com/destroystokyo/paper/proxy/VelocityProxy.java b/src/main/java/com/destroystokyo/paper/proxy/VelocityProxy.java
new file mode 100644
index 0000000000..fdd8708f97
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/proxy/VelocityProxy.java
@@ -0,0 +1,67 @@
+package com.destroystokyo.paper.proxy;
+
+import com.destroystokyo.paper.PaperConfig;
+import com.google.common.net.InetAddresses;
+import com.mojang.authlib.GameProfile;
+import com.mojang.authlib.properties.Property;
+import net.minecraft.server.MinecraftKey;
+import net.minecraft.server.PacketDataSerializer;
+
+import java.net.InetAddress;
+import java.security.InvalidKeyException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+
+public class VelocityProxy {
+    private static final int SUPPORTED_FORWARDING_VERSION = 1;
+    public static final MinecraftKey PLAYER_INFO_CHANNEL = new MinecraftKey("velocity", "player_info");
+
+    public static boolean checkIntegrity(final PacketDataSerializer buf) {
+        final byte[] signature = new byte[32];
+        buf.readBytes(signature);
+
+        final byte[] data = new byte[buf.readableBytes()];
+        buf.getBytes(buf.readerIndex(), data);
+
+        try {
+            final Mac mac = Mac.getInstance("HmacSHA256");
+            mac.init(new SecretKeySpec(PaperConfig.velocitySecretKey, "HmacSHA256"));
+            final byte[] mySignature = mac.doFinal(data);
+            if (!MessageDigest.isEqual(signature, mySignature)) {
+                return false;
+            }
+        } catch (final InvalidKeyException | NoSuchAlgorithmException e) {
+            throw new AssertionError(e);
+        }
+
+        int version = buf.readVarInt();
+        if (version != SUPPORTED_FORWARDING_VERSION) {
+            throw new IllegalStateException("Unsupported forwarding version " + version + ", wanted " + SUPPORTED_FORWARDING_VERSION);
+        }
+
+        return true;
+    }
+
+    public static InetAddress readAddress(final PacketDataSerializer buf) {
+        return InetAddresses.forString(buf.readUTF(Short.MAX_VALUE));
+    }
+
+    public static GameProfile createProfile(final PacketDataSerializer buf) {
+        final GameProfile profile = new GameProfile(buf.readUUID(), buf.readUTF(16));
+        readProperties(buf, profile);
+        return profile;
+    }
+
+    private static void readProperties(final PacketDataSerializer buf, final GameProfile profile) {
+        final int properties = buf.readVarInt();
+        for (int i1 = 0; i1 < properties; i1++) {
+            final String name = buf.readUTF(Short.MAX_VALUE);
+            final String value = buf.readUTF(Short.MAX_VALUE);
+            final String signature = buf.readBoolean() ? buf.readUTF(Short.MAX_VALUE) : null;
+            profile.getProperties().put(name, new Property(name, value, signature));
+        }
+    }
+}
diff --git a/src/main/java/net/minecraft/server/LoginListener.java b/src/main/java/net/minecraft/server/LoginListener.java
index d6befec184..41b2c90c3a 100644
--- a/src/main/java/net/minecraft/server/LoginListener.java
+++ b/src/main/java/net/minecraft/server/LoginListener.java
@@ -42,6 +42,7 @@ public class LoginListener implements PacketLoginInListener {
     private SecretKey loginKey;
     private EntityPlayer l;
     public String hostname = ""; // CraftBukkit - add field
+    private int velocityLoginMessageId = -1; // Paper - Velocity support
 
     public LoginListener(MinecraftServer minecraftserver, NetworkManager networkmanager) {
         this.g = LoginListener.EnumProtocolState.HELLO;
@@ -193,6 +194,14 @@ public class LoginListener implements PacketLoginInListener {
             this.g = LoginListener.EnumProtocolState.KEY;
             this.networkManager.sendPacket(new PacketLoginOutEncryptionBegin("", this.server.getKeyPair().getPublic(), this.e));
         } else {
+            // Paper start - Velocity support
+            if (com.destroystokyo.paper.PaperConfig.velocitySupport) {
+                this.velocityLoginMessageId = java.util.concurrent.ThreadLocalRandom.current().nextInt();
+                PacketLoginOutCustomPayload packet = new PacketLoginOutCustomPayload(this.velocityLoginMessageId, com.destroystokyo.paper.proxy.VelocityProxy.PLAYER_INFO_CHANNEL, new PacketDataSerializer(io.netty.buffer.Unpooled.EMPTY_BUFFER));
+                this.networkManager.sendPacket(packet);
+                return;
+            }
+            // Paper end
             // Spigot start
             // Paper start - Cache authenticator threads
             authenticatorPool.execute(new Runnable() {
@@ -285,6 +294,12 @@ public class LoginListener implements PacketLoginInListener {
     public class LoginHandler {
 
         public void fireEvents() throws Exception {
+                            // Paper start - Velocity support
+                            if (LoginListener.this.velocityLoginMessageId == -1 && com.destroystokyo.paper.PaperConfig.velocitySupport) {
+                                disconnect("This server requires you to connect with Velocity.");
+                                return;
+                            }
+                            // Paper end
                             String playerName = i.getName();
                             java.net.InetAddress address = ((java.net.InetSocketAddress) networkManager.getSocketAddress()).getAddress();
                             java.util.UUID uniqueId = i.getId();
@@ -332,6 +347,35 @@ public class LoginListener implements PacketLoginInListener {
     // Spigot end
 
     public void a(PacketLoginInCustomPayload packetloginincustompayload) {
+        // Paper start - Velocity support
+        if (com.destroystokyo.paper.PaperConfig.velocitySupport && packetloginincustompayload.getId() == this.velocityLoginMessageId) {
+            PacketDataSerializer buf = packetloginincustompayload.getBuf();
+            if (buf == null) {
+                this.disconnect("This server requires you to connect with Velocity.");
+                return;
+            }
+
+            if (!com.destroystokyo.paper.proxy.VelocityProxy.checkIntegrity(buf)) {
+                this.disconnect("Unable to verify player details");
+                return;
+            }
+
+            this.networkManager.setSpoofedRemoteAddress(new java.net.InetSocketAddress(com.destroystokyo.paper.proxy.VelocityProxy.readAddress(buf), ((java.net.InetSocketAddress) this.networkManager.getSocketAddress()).getPort()));
+
+            this.setGameProfile(com.destroystokyo.paper.proxy.VelocityProxy.createProfile(buf));
+
+            // Proceed with login
+            authenticatorPool.execute(() -> {
+                try {
+                    new LoginHandler().fireEvents();
+                } catch (Exception ex) {
+                    disconnect("Failed to verify username!");
+                    server.server.getLogger().log(java.util.logging.Level.WARNING, "Exception verifying " + i.getName(), ex);
+                }
+            });
+            return;
+        }
+        // Paper end
         this.disconnect(new ChatMessage("multiplayer.disconnect.unexpected_query_response", new Object[0]));
     }
 
diff --git a/src/main/java/net/minecraft/server/NetworkManager.java b/src/main/java/net/minecraft/server/NetworkManager.java
index 08e314af7c..046196d54d 100644
--- a/src/main/java/net/minecraft/server/NetworkManager.java
+++ b/src/main/java/net/minecraft/server/NetworkManager.java
@@ -44,7 +44,7 @@ public class NetworkManager extends SimpleChannelInboundHandler<Packet<?>> {
     private final EnumProtocolDirection h;
     private final Queue<NetworkManager.QueuedPacket> packetQueue = Queues.newConcurrentLinkedQueue();
     public Channel channel;
-    public SocketAddress socketAddress;
+    public SocketAddress socketAddress; public void setSpoofedRemoteAddress(SocketAddress address) { this.socketAddress = address; } // Paper - OBFHELPER
     // Spigot Start
     public java.util.UUID spoofedUUID;
     public com.mojang.authlib.properties.Property[] spoofedProfile;
diff --git a/src/main/java/net/minecraft/server/PacketDataSerializer.java b/src/main/java/net/minecraft/server/PacketDataSerializer.java
index fa2d3ce8cb..dac560c63f 100644
--- a/src/main/java/net/minecraft/server/PacketDataSerializer.java
+++ b/src/main/java/net/minecraft/server/PacketDataSerializer.java
@@ -140,6 +140,7 @@ public class PacketDataSerializer extends ByteBuf {
         return this.d(oenum.ordinal());
     }
 
+    public int readVarInt() { return i(); } // Paper - OBFHELPER
     public int i() {
         int i = 0;
         int j = 0;
@@ -180,6 +181,7 @@ public class PacketDataSerializer extends ByteBuf {
         return this;
     }
 
+    public UUID readUUID() { return k(); } // Paper - OBFHELPER
     public UUID k() {
         return new UUID(this.readLong(), this.readLong());
     }
@@ -298,6 +300,7 @@ public class PacketDataSerializer extends ByteBuf {
         }
     }
 
+    public String readUTF(int maxLength) { return this.e(maxLength); } // Paper - OBFHELPER
     public String e(int i) {
         int j = this.i();
 
diff --git a/src/main/java/net/minecraft/server/PacketLoginInCustomPayload.java b/src/main/java/net/minecraft/server/PacketLoginInCustomPayload.java
index 4d1f441395..c1ca6f9501 100644
--- a/src/main/java/net/minecraft/server/PacketLoginInCustomPayload.java
+++ b/src/main/java/net/minecraft/server/PacketLoginInCustomPayload.java
@@ -4,8 +4,8 @@ import java.io.IOException;
 
 public class PacketLoginInCustomPayload implements Packet<PacketLoginInListener> {
 
-    private int a;
-    private PacketDataSerializer b;
+    private int a; public int getId() { return a; } // Paper - OBFHELPER
+    private PacketDataSerializer b; public PacketDataSerializer getBuf() { return b; } // Paper - OBFHELPER
 
     public PacketLoginInCustomPayload() {}
 
diff --git a/src/main/java/net/minecraft/server/PacketLoginOutCustomPayload.java b/src/main/java/net/minecraft/server/PacketLoginOutCustomPayload.java
index ae74dc9e18..7eb230f1b2 100644
--- a/src/main/java/net/minecraft/server/PacketLoginOutCustomPayload.java
+++ b/src/main/java/net/minecraft/server/PacketLoginOutCustomPayload.java
@@ -10,6 +10,14 @@ public class PacketLoginOutCustomPayload implements Packet<PacketLoginOutListene
 
     public PacketLoginOutCustomPayload() {}
 
+    // Paper start
+    public PacketLoginOutCustomPayload(int id, MinecraftKey channel, PacketDataSerializer buf) {
+        this.a = id;
+        this.b = channel;
+        this.c = buf;
+    }
+    // Paper end
+
     @Override
     public void a(PacketDataSerializer packetdataserializer) throws IOException {
         this.a = packetdataserializer.i();
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
index d0834ecaab..145de4fe6e 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -644,7 +644,7 @@ public final class CraftServer implements Server {
     @Override
     public long getConnectionThrottle() {
         // Spigot Start - Automatically set connection throttle for bungee configurations
-        if (org.spigotmc.SpigotConfig.bungee) {
+        if (org.spigotmc.SpigotConfig.bungee || com.destroystokyo.paper.PaperConfig.velocitySupport) { // Paper - Velocity support
             return -1;
         } else {
             return this.configuration.getInt("settings.connection-throttle");
-- 
2.22.0