Skip to content

Commit 19c4d49

Browse files
committed
Let ViaProxy handle host/port splitting
1 parent 8cf53ef commit 19c4d49

5 files changed

Lines changed: 20 additions & 23 deletions

File tree

src/main/java/net/lenni0451/miniconnect/model/ConnectionInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
import javax.annotation.Nullable;
77

8-
public record ConnectionInfo(String handshakeAddress, int handshakePort, String host, int port, ProtocolVersion protocolVersion, @Nullable Account account) {
8+
public record ConnectionInfo(String handshakeAddress, int handshakePort, String address, ProtocolVersion protocolVersion, @Nullable Account account) {
99
}

src/main/java/net/lenni0451/miniconnect/proxy/event/RedirectionHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
import net.lenni0451.miniconnect.proxy.StateRegistry;
99
import net.lenni0451.miniconnect.server.protocol.ProtocolConstants;
1010
import net.lenni0451.miniconnect.utils.ChannelUtils;
11-
import net.raphimc.netminecraft.util.MinecraftServerAddress;
1211
import net.raphimc.viaproxy.ViaProxy;
1312
import net.raphimc.viaproxy.plugins.events.ConnectEvent;
1413
import net.raphimc.viaproxy.plugins.events.PreConnectEvent;
1514
import net.raphimc.viaproxy.plugins.events.ViaProxyLoadedEvent;
1615
import net.raphimc.viaproxy.proxy.session.UserOptions;
16+
import net.raphimc.viaproxy.util.AddressUtil;
1717

1818
import java.net.InetAddress;
1919

@@ -35,7 +35,7 @@ public void onPreConnect(final PreConnectEvent event) {
3535
//First transfer from the lobby to the target server
3636
//Set the target server address and version for the player to connect
3737
ConnectionInfo target = stateRegistry.getConnectionTargets().remove(channelAddress);
38-
event.setServerAddress(MinecraftServerAddress.ofResolved(target.host(), target.port()));
38+
event.setServerAddress(AddressUtil.parse(target.address(), target.protocolVersion()));
3939
event.setServerVersion(target.protocolVersion());
4040
event.getClientChannel().attr(AttributeKeys.CONNECTION_INFO).set(target);
4141
} else if (stateRegistry.getReconnectTargets().containsKey(channelAddress) && !event.getServerAddress().equals(DUMMY_SOCKET_ADDRESS)) {

src/main/java/net/lenni0451/miniconnect/server/LobbyServerHandler.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,13 @@ private void checkHandshakeAddress() {
102102
if (parsedDomain != null && parsedDomain.version() != null) {
103103
InetSocketAddress socketAddress = (InetSocketAddress) parsedDomain.address();
104104
this.playerConfig.serverAddress = socketAddress.getHostString();
105-
this.playerConfig.serverPort = socketAddress.getPort();
106105
this.playerConfig.targetVersion = parsedDomain.version();
107106
}
108107
} else if (handshakeAddress.toLowerCase().contains("viaproxy.")) { // Format 1: address_port_version.viaproxy.hostname
109108
WildcardDomainParser.ParsedDomain parsedDomain = WildcardDomainParser.parseFormat1(handshakeAddress);
110109
if (parsedDomain != null && parsedDomain.version() != null) {
111110
InetSocketAddress socketAddress = (InetSocketAddress) parsedDomain.address();
112111
this.playerConfig.serverAddress = socketAddress.getHostString();
113-
this.playerConfig.serverPort = socketAddress.getPort();
114112
this.playerConfig.targetVersion = parsedDomain.version();
115113
}
116114
}

src/main/java/net/lenni0451/miniconnect/server/model/PlayerConfig.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
44
import net.lenni0451.commons.gson.GsonParser;
55
import net.lenni0451.commons.gson.elements.GsonObject;
6-
import net.lenni0451.commons.gson.elements.GsonPrimitive;
76
import net.lenni0451.miniconnect.Main;
87
import net.lenni0451.miniconnect.model.ConnectionInfo;
98
import net.lenni0451.miniconnect.utils.AESEncryption;
109
import net.lenni0451.miniconnect.utils.UUIDUtils;
1110
import net.raphimc.viaproxy.saves.impl.accounts.Account;
1211
import net.raphimc.viaproxy.saves.impl.accounts.MicrosoftAccount;
13-
import net.raphimc.viaproxy.util.AddressUtil;
1412

1513
import javax.annotation.Nullable;
1614
import java.io.File;
@@ -32,8 +30,6 @@ public static File baseDir() {
3230
@Nullable
3331
public String serverAddress;
3432
@Nullable
35-
public Integer serverPort;
36-
@Nullable
3733
public ProtocolVersion targetVersion;
3834
@Nullable
3935
public Account account;
@@ -49,15 +45,13 @@ public PlayerConfig(final UUID uuid) {
4945
}
5046

5147
public void applyConnectionInfo(final ConnectionInfo connectionInfo) {
52-
this.serverAddress = connectionInfo.host();
53-
this.serverPort = connectionInfo.port();
48+
this.serverAddress = connectionInfo.address();
5449
this.targetVersion = connectionInfo.protocolVersion();
5550
this.account = connectionInfo.account();
5651
}
5752

5853
public ConnectionInfo toConnectionInfo() {
59-
int serverPort = this.serverPort == null || this.serverPort == -1 ? AddressUtil.getDefaultPort(this.targetVersion) : this.serverPort;
60-
return new ConnectionInfo(this.handshakeAddress, this.handshakePort, this.serverAddress, serverPort, this.targetVersion, this.account);
54+
return new ConnectionInfo(this.handshakeAddress, this.handshakePort, this.serverAddress, this.targetVersion, this.account);
6155
}
6256

6357
public void load() throws Exception {
@@ -72,7 +66,6 @@ public void load() throws Exception {
7266
String json = new String(decryptedData, StandardCharsets.UTF_8);
7367
GsonObject object = GsonParser.parse(json).asObject();
7468
this.serverAddress = object.getString("serverAddress", null);
75-
this.serverPort = object.optPrimitive("serverPort").map(GsonPrimitive::asInt).orElse(null);
7669
this.targetVersion = object.optPrimitive("targetVersion").map(p -> ProtocolVersion.getProtocol(p.asInt())).orElse(null);
7770
this.account = object.optObject("account").map(GsonObject::getJsonObject).map(MicrosoftAccount::new).orElse(null);
7871
}
@@ -85,7 +78,6 @@ public void save() throws Exception {
8578

8679
GsonObject object = new GsonObject();
8780
if (this.serverAddress != null) object.add("serverAddress", this.serverAddress);
88-
if (this.serverPort != null) object.add("serverPort", this.serverPort);
8981
if (this.targetVersion != null) object.add("targetVersion", this.targetVersion.getOriginalVersion());
9082
if (this.account != null) object.add("account", this.account.toJson());
9183
byte[] key = UUIDUtils.toBytes(this.uuid);

src/main/java/net/lenni0451/miniconnect/server/states/play/screen/impl/MainScreen.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public void init(ScreenHandler screenHandler, ItemList itemList) {
5353
itemList.set(11, item(Items.NAMETAG).named(new StringComponent(Messages.MainScreen.SetServerAddress.ItemName)).setGlint(hasAddress).calculate(builder -> {
5454
builder.lore(Messages.format(Messages.MainScreen.SetServerAddress.ItemLore));
5555
if (hasAddress) {
56-
String address = playerConfig.serverAddress + (playerConfig.serverPort == null || playerConfig.serverPort == -1 ? "" : (":" + playerConfig.serverPort));
57-
builder.lore(Messages.format(Messages.MainScreen.SetServerAddress.ItemLoreAddressSet, address));
56+
builder.lore(Messages.format(Messages.MainScreen.SetServerAddress.ItemLoreAddressSet, playerConfig.serverAddress));
5857
} else {
5958
builder.lore(Messages.format(Messages.MainScreen.SetServerAddress.ItemLoreNoAddressSet));
6059
}
@@ -70,11 +69,15 @@ public void init(ScreenHandler screenHandler, ItemList itemList) {
7069
}
7170
} else {
7271
try {
73-
HostAndPort hostAndPort = HostAndPort.fromString(s);
74-
if (hostAndPort.getHost().isBlank()) throw new IllegalArgumentException();
75-
if (InetUtils.isLocal(InetAddress.getByName(hostAndPort.getHost()))) throw new IllegalArgumentException();
76-
playerConfig.serverAddress = hostAndPort.getHost();
77-
playerConfig.serverPort = hostAndPort.getPortOrDefault(-1);
72+
try {
73+
HostAndPort hostAndPort = HostAndPort.fromString(s);
74+
if (hostAndPort.getHost().isBlank()) throw new InvalidAddressException();
75+
if (InetUtils.isLocal(InetAddress.getByName(hostAndPort.getHost()))) throw new InvalidAddressException();
76+
} catch (InvalidAddressException e) {
77+
throw e;
78+
} catch (Throwable ignored) {
79+
}
80+
playerConfig.serverAddress = s;
7881
if (GeyserAPI.isGeyserPlayer(playerConfig.uuid)) {
7982
//Respawn the player two times to force the chat to close
8083
//This only needs to be done because bedrock doesn't allow closing the chat otherwise
@@ -156,7 +159,7 @@ public void init(ScreenHandler screenHandler, ItemList itemList) {
156159
builder.lore(Messages.format(Messages.MainScreen.ConnectToServer.ItemLoreMissingRequirements));
157160
return;
158161
}
159-
builder.lore(Messages.format(Messages.MainScreen.SetServerAddress.ItemLoreAddressSet, playerConfig.serverAddress + (playerConfig.serverPort == null || playerConfig.serverPort == -1 ? "" : (":" + playerConfig.serverPort))));
162+
builder.lore(Messages.format(Messages.MainScreen.SetServerAddress.ItemLoreAddressSet, playerConfig.serverAddress));
160163
builder.lore(Messages.format(Messages.MainScreen.SetProtocolVersion.ItemLoreVersionSet, playerConfig.targetVersion.getName()));
161164
if (hasAccount) builder.lore(Messages.format(Messages.MainScreen.Login.ItemLoreLoggedIn, playerConfig.account.getDisplayString()));
162165
}).get(), () -> {
@@ -235,4 +238,8 @@ public void close(ScreenHandler screenHandler) {
235238
screenHandler.getStateHandler().sendAndClose(new S2CPlayDisconnectPacket(new StringComponent(Messages.MainScreen.Disconnect.DisconnectMessage)));
236239
}
237240

241+
242+
private static class InvalidAddressException extends RuntimeException {
243+
}
244+
238245
}

0 commit comments

Comments
 (0)