Skip to content

Commit 8c5daf7

Browse files
committed
Improved handling of different hostnames with the same IP
1 parent d2cc09d commit 8c5daf7

File tree

3 files changed

+58
-53
lines changed

3 files changed

+58
-53
lines changed

src/main/java/com/kamesuta/bungeeviaproxy/BungeeViaProxy.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.kamesuta.bungeeviaproxy;
22

3+
import net.md_5.bungee.BungeeServerInfo;
34
import net.md_5.bungee.api.ProxyServer;
45
import net.md_5.bungee.api.config.ServerInfo;
56
import net.md_5.bungee.api.connection.PendingConnection;
@@ -28,19 +29,18 @@ public void onEnable() {
2829
// In Bungeecord, even if the hostnames are different, the equals method returns true if the resolved IP addresses are the same.
2930
// This results in the error "You are already connected to this server!".
3031
// To avoid this, we use unresolved InetSocketAddress objects so that different hostnames are treated as distinct servers.
31-
ProxyServer.getInstance().getServers().values().forEach(serverInfo -> {
32-
// Convert address to unresolved address
33-
SocketAddress socketAddress = serverInfo.getSocketAddress();
34-
if (socketAddress instanceof InetSocketAddress) {
35-
InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
36-
// Only convert if the address is a ViaProxy address
37-
if (inetSocketAddress.getHostString().contains(IDENTIFIER) && ReflectionUtil.isBungeeSeverInfo(serverInfo)) {
38-
ReflectionUtil.setBungeeServerInfoSocketAddress(
39-
serverInfo,
40-
InetSocketAddress.createUnresolved(inetSocketAddress.getHostString(), inetSocketAddress.getPort())
41-
);
42-
}
43-
}
32+
ProxyServer.getInstance().getServers().replaceAll((name, serverInfo) -> {
33+
// if the serverInfo is already a HostnameBungeeServerInfo, return it as is
34+
if (serverInfo instanceof HostnameBungeeServerInfo) return serverInfo;
35+
// if the serverInfo is not a BungeeServerInfo, return it as is
36+
if (!(serverInfo instanceof BungeeServerInfo)) return serverInfo;
37+
// wrap the BungeeServerInfo to HostnameBungeeServerInfo
38+
return new HostnameBungeeServerInfo(
39+
serverInfo.getName(),
40+
serverInfo.getSocketAddress(),
41+
serverInfo.getMotd(),
42+
serverInfo.isRestricted()
43+
);
4444
});
4545
}
4646

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.kamesuta.bungeeviaproxy;
2+
3+
import net.md_5.bungee.BungeeServerInfo;
4+
import net.md_5.bungee.api.config.ServerInfo;
5+
6+
import java.net.InetSocketAddress;
7+
import java.net.SocketAddress;
8+
import java.util.Objects;
9+
10+
public class HostnameBungeeServerInfo extends BungeeServerInfo {
11+
12+
public HostnameBungeeServerInfo(String name, SocketAddress socketAddress, String motd, boolean restricted) {
13+
super(name, socketAddress, motd, restricted);
14+
}
15+
16+
@Override
17+
public boolean equals(Object obj) {
18+
if (!(obj instanceof ServerInfo)) return false;
19+
20+
InetSocketAddress address = this.getAddress();
21+
@SuppressWarnings("deprecation")
22+
InetSocketAddress objAddress = ((ServerInfo) obj).getAddress();
23+
24+
if (address == null && objAddress == null) return true;
25+
if (address == null || objAddress == null) return false;
26+
27+
// BungeeServerInfo is treated as the same server if the resolved IP addresses are the same,
28+
// so treat them as different servers if the hostnames are different.
29+
return Objects.equals(address.getHostName(), objAddress.getHostName()) && address.getPort() == objAddress.getPort();
30+
}
31+
32+
@Override
33+
public int hashCode() {
34+
InetSocketAddress address = this.getAddress();
35+
if (address == null) return 0;
36+
37+
int result = 17;
38+
result = 31 * result + address.getPort();
39+
if (address.getHostName() != null) {
40+
result = 31 * result + address.getHostName().hashCode();
41+
}
42+
return result;
43+
}
44+
45+
}

src/main/java/com/kamesuta/bungeeviaproxy/ReflectionUtil.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)