Added events for changing the default port and srv behaviour

This commit is contained in:
RaphiMC 2023-02-16 20:44:45 +01:00
parent 347544f89a
commit 40fd7dc9c0
5 changed files with 116 additions and 4 deletions

View File

@ -21,6 +21,8 @@ import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import net.raphimc.viaprotocolhack.util.VersionEnum;
import net.raphimc.viaproxy.plugins.PluginManager;
import net.raphimc.viaproxy.plugins.events.GetDefaultPortEvent;
import net.raphimc.viaproxy.saves.impl.accounts.Account;
import net.raphimc.viaproxy.util.logging.Logger;
@ -62,7 +64,7 @@ public class Options {
final OptionSpec<Integer> nettyThreads = parser.acceptsAll(asList("netty_threads", "t"), "The amount of netty threads to use").withRequiredArg().ofType(Integer.class).defaultsTo(NETTY_THREADS);
final OptionSpec<Integer> compressionThreshold = parser.acceptsAll(asList("compression_threshold", "ct", "c"), "The threshold for packet compression").withRequiredArg().ofType(Integer.class).defaultsTo(COMPRESSION_THRESHOLD);
final OptionSpec<String> connectAddress = parser.acceptsAll(asList("connect_address", "target_ip", "ca", "a"), "The address of the target server").withRequiredArg().ofType(String.class).required();
final OptionSpec<Integer> connectPort = parser.acceptsAll(asList("connect_port", "target_port", "cp", "p"), "The port of the target server").withRequiredArg().ofType(Integer.class).defaultsTo(CONNECT_PORT);
final OptionSpec<Integer> connectPort = parser.acceptsAll(asList("connect_port", "target_port", "cp", "p"), "The port of the target server").withRequiredArg().ofType(Integer.class);
final OptionSpec<VersionEnum> version = parser.acceptsAll(asList("version", "v"), "The version of the target server").withRequiredArg().withValuesConvertedBy(new VersionEnumConverter()).required();
final OptionSpec<Void> openAuthModAuth = parser.acceptsAll(asList("openauthmod_auth", "oam_auth"), "Enable OpenAuthMod authentication");
final OptionSpec<Void> localSocketAuth = parser.accepts("local_socket_auth", "Enable authentication over a local socket");
@ -82,8 +84,12 @@ public class Options {
ONLINE_MODE = options.has(onlineMode);
NETTY_THREADS = options.valueOf(nettyThreads);
CONNECT_ADDRESS = options.valueOf(connectAddress);
CONNECT_PORT = options.valueOf(connectPort);
PROTOCOL_VERSION = options.valueOf(version);
if (options.has(connectPort)) {
CONNECT_PORT = options.valueOf(connectPort);
} else {
CONNECT_PORT = PluginManager.EVENT_MANAGER.call(new GetDefaultPortEvent(PROTOCOL_VERSION, 25565)).getDefaultPort();
}
COMPRESSION_THRESHOLD = options.valueOf(compressionThreshold);
OPENAUTHMOD_AUTH = options.has(openAuthModAuth);
LOCAL_SOCKET_AUTH = options.has(localSocketAuth);

View File

@ -0,0 +1,44 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2023 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.viaproxy.plugins.events;
import net.raphimc.viaprotocolhack.util.VersionEnum;
public class GetDefaultPortEvent {
private final VersionEnum serverVersion;
private int defaultPort;
public GetDefaultPortEvent(final VersionEnum serverVersion, final int defaultPort) {
this.serverVersion = serverVersion;
this.defaultPort = defaultPort;
}
public VersionEnum getServerVersion() {
return this.serverVersion;
}
public int getDefaultPort() {
return this.defaultPort;
}
public void setDefaultPort(final int defaultPort) {
this.defaultPort = defaultPort;
}
}

View File

@ -0,0 +1,55 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2023 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.viaproxy.plugins.events;
import net.raphimc.viaprotocolhack.util.VersionEnum;
import net.raphimc.viaproxy.plugins.events.types.EventCancellable;
public class ResolveSrvEvent extends EventCancellable {
private final VersionEnum serverVersion;
private String host;
private int port;
public ResolveSrvEvent(final VersionEnum serverVersion, final String host, final int port) {
this.serverVersion = serverVersion;
this.host = host;
this.port = port;
}
public VersionEnum getServerVersion() {
return this.serverVersion;
}
public String getHost() {
return host;
}
public void setHost(final String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(final int port) {
this.port = port;
}
}

View File

@ -42,6 +42,7 @@ import net.raphimc.viaproxy.cli.options.Options;
import net.raphimc.viaproxy.plugins.PluginManager;
import net.raphimc.viaproxy.plugins.events.PreConnectEvent;
import net.raphimc.viaproxy.plugins.events.Proxy2ServerHandlerCreationEvent;
import net.raphimc.viaproxy.plugins.events.ResolveSrvEvent;
import net.raphimc.viaproxy.proxy.LoginState;
import net.raphimc.viaproxy.proxy.ProxyConnection;
import net.raphimc.viaproxy.proxy.external_interface.AuthLibServices;
@ -202,8 +203,12 @@ public class Client2ProxyHandler extends SimpleChannelInboundHandler<IPacket> {
}
}
final ResolveSrvEvent resolveSrvEvent = PluginManager.EVENT_MANAGER.call(new ResolveSrvEvent(serverVersion, connectIP, connectPort));
connectIP = resolveSrvEvent.getHost();
connectPort = resolveSrvEvent.getPort();
final ServerAddress serverAddress;
if (serverVersion.isOlderThan(VersionEnum.r1_3_1tor1_3_2)) {
if (resolveSrvEvent.isCancelled() || serverVersion.isOlderThan(VersionEnum.r1_3_1tor1_3_2)) {
serverAddress = new ServerAddress(connectIP, connectPort);
} else {
serverAddress = ServerAddress.fromSRV(connectIP + ":" + connectPort);

View File

@ -21,6 +21,8 @@ import com.google.common.net.HostAndPort;
import net.raphimc.viaprotocolhack.util.VersionEnum;
import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.cli.options.Options;
import net.raphimc.viaproxy.plugins.PluginManager;
import net.raphimc.viaproxy.plugins.events.GetDefaultPortEvent;
import net.raphimc.viaproxy.saves.impl.UISave;
import net.raphimc.viaproxy.ui.AUITab;
import net.raphimc.viaproxy.ui.ViaProxyUI;
@ -206,8 +208,8 @@ public class GeneralTab extends AUITab {
Options.BIND_PORT = bindPort;
Options.ONLINE_MODE = proxyOnlineMode;
Options.CONNECT_ADDRESS = hostAndPort.getHost();
Options.CONNECT_PORT = hostAndPort.getPortOrDefault(25565);
Options.PROTOCOL_VERSION = serverVersion;
Options.CONNECT_PORT = hostAndPort.getPortOrDefault(PluginManager.EVENT_MANAGER.call(new GetDefaultPortEvent(serverVersion, 25565)).getDefaultPort());
Options.BETACRAFT_AUTH = betaCraftAuth;
if (authMethod != 1) {