Added new CLI interface

This commit is contained in:
RaphiMC 2024-04-21 01:31:02 +02:00
parent 0c04ac45d9
commit 1e32284e8a
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
10 changed files with 263 additions and 103 deletions

View File

@ -108,11 +108,54 @@ public class ViaProxy {
}
public static void injectedMain(final String injectionMethod, final String[] args) throws InterruptedException, IOException, InvocationTargetException {
final boolean hasUI = args.length == 0 && !GraphicsEnvironment.isHeadless();
final boolean legacyCLI = args.length > 0 && args[0].startsWith("-");
Logger.setup();
final boolean useUI = args.length == 0 && !GraphicsEnvironment.isHeadless();
final boolean useConfig = args.length > 0 && args[0].equals("config");
final boolean useCLI = args.length > 0 && args[0].equals("cli");
final boolean useLegacyCLI = args.length > 0 && args[0].startsWith("-");
if (!useUI && !useConfig && !useCLI && !useLegacyCLI) {
String fileName = "ViaProxy.jar";
try {
fileName = new File(ViaProxy.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getName();
} catch (Throwable ignored) {
}
Logger.LOGGER.info("Usage: java -jar " + fileName + " | Starts ViaProxy in graphical mode if available");
Logger.LOGGER.info("Usage: java -jar " + fileName + " config <config file> | Starts ViaProxy with the specified config file");
Logger.LOGGER.info("Usage: java -jar " + fileName + " cli --help | Starts ViaProxy in CLI mode");
System.exit(1);
}
Logger.LOGGER.info("Initializing ViaProxy {} v{} ({}) (Injected using {})...", useUI ? "GUI" : "CLI", VERSION, IMPL_VERSION, injectionMethod);
Logger.LOGGER.info("Using java version: " + System.getProperty("java.vm.name") + " " + System.getProperty("java.version") + " (" + System.getProperty("java.vendor") + ") on " + System.getProperty("os.name"));
Logger.LOGGER.info("Available memory (bytes): " + Runtime.getRuntime().maxMemory());
Logger.LOGGER.info("Working directory: " + System.getProperty("user.dir"));
if (System.getProperty("ignoreSystemRequirements") == null) {
if ("32".equals(System.getProperty("sun.arch.data.model")) && Runtime.getRuntime().maxMemory() < 256 * 1024 * 1024) {
Logger.LOGGER.fatal("ViaProxy is not able to run on 32bit Java.");
if (useUI) {
JOptionPane.showMessageDialog(null, "ViaProxy is not able to run on 32bit Java. Please install 64bit Java", "ViaProxy", JOptionPane.ERROR_MESSAGE);
}
System.exit(1);
}
if (Runtime.getRuntime().maxMemory() < 256 * 1024 * 1024) {
Logger.LOGGER.fatal("ViaProxy is not able to run with less than 256MB of RAM.");
if (useUI) {
JOptionPane.showMessageDialog(null, "ViaProxy is not able to run with less than 256MB of RAM.", "ViaProxy", JOptionPane.ERROR_MESSAGE);
}
System.exit(1);
} else if (Runtime.getRuntime().maxMemory() < 512 * 1024 * 1024) {
Logger.LOGGER.warn("ViaProxy has less than 512MB of RAM. This may cause issues with multiple clients connected.");
if (useUI) {
JOptionPane.showMessageDialog(null, "ViaProxy has less than 512MB of RAM. This may cause issues with multiple clients connected.", "ViaProxy", JOptionPane.WARNING_MESSAGE);
}
}
}
final SplashScreen splashScreen;
final Consumer<String> progressConsumer;
if (hasUI) {
if (useUI) {
final float progressStep = 1F / 7F;
foregroundWindow = splashScreen = new SplashScreen();
progressConsumer = (text) -> {
@ -130,42 +173,13 @@ public class ViaProxy {
}
progressConsumer.accept("Initializing ViaProxy");
Logger.setup();
Logger.LOGGER.info("Initializing ViaProxy {} v{} ({}) (Injected using {})...", hasUI ? "GUI" : "CLI", VERSION, IMPL_VERSION, injectionMethod);
Logger.LOGGER.info("Using java version: " + System.getProperty("java.vm.name") + " " + System.getProperty("java.version") + " (" + System.getProperty("java.vendor") + ") on " + System.getProperty("os.name"));
Logger.LOGGER.info("Available memory (bytes): " + Runtime.getRuntime().maxMemory());
Logger.LOGGER.info("Working directory: " + System.getProperty("user.dir"));
if (System.getProperty("ignoreSystemRequirements") == null) {
if ("32".equals(System.getProperty("sun.arch.data.model")) && Runtime.getRuntime().maxMemory() < 256 * 1024 * 1024) {
Logger.LOGGER.fatal("ViaProxy is not able to run on 32bit Java.");
if (hasUI) {
JOptionPane.showMessageDialog(null, "ViaProxy is not able to run on 32bit Java. Please install 64bit Java", "ViaProxy", JOptionPane.ERROR_MESSAGE);
}
System.exit(1);
}
if (Runtime.getRuntime().maxMemory() < 256 * 1024 * 1024) {
Logger.LOGGER.fatal("ViaProxy is not able to run with less than 256MB of RAM.");
if (hasUI) {
JOptionPane.showMessageDialog(null, "ViaProxy is not able to run with less than 256MB of RAM.", "ViaProxy", JOptionPane.ERROR_MESSAGE);
}
System.exit(1);
} else if (Runtime.getRuntime().maxMemory() < 512 * 1024 * 1024) {
Logger.LOGGER.warn("ViaProxy has less than 512MB of RAM. This may cause issues with multiple clients connected.");
if (hasUI) {
JOptionPane.showMessageDialog(null, "ViaProxy has less than 512MB of RAM. This may cause issues with multiple clients connected.", "ViaProxy", JOptionPane.WARNING_MESSAGE);
}
}
}
ConsoleHandler.hookConsole();
ViaProxy.loadNetty();
ClassLoaderPriorityUtil.loadOverridingJars();
final File viaProxyConfigFile;
if (!hasUI && !legacyCLI) {
viaProxyConfigFile = new File(args[0]);
if (useConfig) {
viaProxyConfigFile = new File(args[1]);
} else {
viaProxyConfigFile = new File("viaproxy.yml");
}
@ -181,7 +195,7 @@ public class ViaProxy {
CONFIG = new ViaProxyConfig(viaProxyConfigFile);
CONFIG.reload();
if (hasUI) {
if (useUI) {
progressConsumer.accept("Loading GUI");
SwingUtilities.invokeAndWait(() -> {
try {
@ -199,7 +213,11 @@ public class ViaProxy {
EVENT_MANAGER.call(new ViaProxyLoadedEvent());
Logger.LOGGER.info("ViaProxy started successfully!");
} else {
if (legacyCLI) {
if (useCLI) {
final String[] cliArgs = new String[args.length - 1];
System.arraycopy(args, 1, cliArgs, 0, cliArgs.length);
CONFIG.loadFromArguments(cliArgs);
} else if (useLegacyCLI) {
Options.parse(args);
} else if (firstStart) {
Logger.LOGGER.info("This is the first start of ViaProxy. Please configure the settings in the " + viaProxyConfigFile.getName() + " file and restart ViaProxy.");

View File

@ -15,24 +15,28 @@
* 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.cli.options;
package net.raphimc.viaproxy.cli;
import joptsimple.BuiltinHelpFormatter;
import joptsimple.OptionDescriptor;
import joptsimple.internal.Classes;
import joptsimple.internal.Strings;
@Deprecated(forRemoval = true)
public class BetterHelpFormatter extends BuiltinHelpFormatter {
public BetterHelpFormatter() {
super(250, 4);
super(80, 2);
}
@Override
protected String extractTypeIndicator(OptionDescriptor descriptor) {
String indicator = descriptor.argumentTypeIndicator();
if (indicator != null && indicator.startsWith("[")) return indicator.substring(1, indicator.length() - 1);
if (indicator != null) {
indicator = indicator.substring(indicator.indexOf('$') + 1);
if (indicator.startsWith("[")) {
return indicator.substring(1, indicator.length() - 1);
}
}
return !Strings.isNullOrEmpty(indicator) && !String.class.getName().equals(indicator) ? Classes.shortNameOf(indicator) : "String";
}

View File

@ -0,0 +1,21 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2021-2024 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.cli;
public class HelpRequestedException extends RuntimeException {
}

View File

@ -15,18 +15,18 @@
* 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.cli.options;
package net.raphimc.viaproxy.cli;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import joptsimple.ValueConversionException;
import joptsimple.ValueConverter;
import net.raphimc.viaproxy.util.ProtocolVersionUtil;
@Deprecated(forRemoval = true)
public class ProtocolVersionConverter implements ValueConverter<ProtocolVersion> {
@Override
public ProtocolVersion convert(String s) {
final ProtocolVersion version = ProtocolVersion.getClosest(s);
final ProtocolVersion version = ProtocolVersionUtil.fromNameLenient(s);
if (version == null) {
throw new ValueConversionException("Unable to find version '" + s + "'");
}

View File

@ -23,6 +23,8 @@ import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.cli.BetterHelpFormatter;
import net.raphimc.viaproxy.cli.ProtocolVersionConverter;
import net.raphimc.viaproxy.plugins.events.PostOptionsParseEvent;
import net.raphimc.viaproxy.plugins.events.PreOptionsParseEvent;
import net.raphimc.viaproxy.protocoltranslator.viaproxy.ViaProxyConfig;
@ -63,7 +65,7 @@ public class Options {
public static void parse(final String[] args) throws IOException {
Logger.LOGGER.fatal("===============================================================================================================");
Logger.LOGGER.fatal("ViaProxy CLI is deprecated and will be removed in the future. Please use the config file instead. See the ViaProxy README on GitHub for more information.");
Logger.LOGGER.fatal("You are using the old ViaProxy CLI which is deprecated and will be removed in the next version of ViaProxy. Please use the new CLI or config file instead. See the ViaProxy README on GitHub for more information.");
Logger.LOGGER.fatal("Waiting 10 seconds before continuing...");
Logger.LOGGER.fatal("===============================================================================================================");
try {

View File

@ -19,7 +19,6 @@ package net.raphimc.viaproxy.plugins.events;
import joptsimple.OptionSet;
@Deprecated(forRemoval = true)
public class PostOptionsParseEvent {
private final OptionSet options;

View File

@ -19,7 +19,6 @@ package net.raphimc.viaproxy.plugins.events;
import joptsimple.OptionParser;
@Deprecated(forRemoval = true)
public class PreOptionsParseEvent {
private final OptionParser parser;

View File

@ -19,14 +19,24 @@ package net.raphimc.viaproxy.protocoltranslator.viaproxy;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import com.viaversion.viaversion.util.Config;
import joptsimple.OptionException;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.cli.BetterHelpFormatter;
import net.raphimc.viaproxy.cli.HelpRequestedException;
import net.raphimc.viaproxy.cli.ProtocolVersionConverter;
import net.raphimc.viaproxy.cli.options.Options;
import net.raphimc.viaproxy.plugins.events.PostOptionsParseEvent;
import net.raphimc.viaproxy.plugins.events.PreOptionsParseEvent;
import net.raphimc.viaproxy.protocoltranslator.ProtocolTranslator;
import net.raphimc.viaproxy.saves.impl.accounts.Account;
import net.raphimc.viaproxy.util.AddressUtil;
import net.raphimc.viaproxy.util.logging.Logger;
import java.io.File;
import java.io.IOException;
import java.net.SocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
@ -38,48 +48,73 @@ import java.util.Map;
public class ViaProxyConfig extends Config implements com.viaversion.viaversion.api.configuration.Config {
private SocketAddress bindAddress;
private SocketAddress targetAddress;
private ProtocolVersion targetVersion;
private boolean proxyOnlineMode;
private AuthMethod authMethod;
private Account account;
private boolean betacraftAuth;
private URI backendProxyUrl;
private boolean backendHaProxy;
private boolean chatSigning;
private int compressionThreshold;
private boolean allowBetaPinging;
private boolean ignoreProtocolTranslationErrors;
private boolean allowLegacyClientPassthrough;
private String resourcePackUrl;
private WildcardDomainHandling wildcardDomainHandling;
private OptionParser optionParser;
private final OptionSpec<Void> optionHelp;
private final OptionSpec<String> optionBindAddress;
private final OptionSpec<String> optionTargetAddress;
private final OptionSpec<ProtocolVersion> optionTargetVersion;
private final OptionSpec<Boolean> optionProxyOnlineMode;
private final OptionSpec<AuthMethod> optionAuthMethod;
private final OptionSpec<Boolean> optionBetacraftAuth;
private final OptionSpec<String> optionBackendProxyUrl;
private final OptionSpec<Boolean> optionBackendHaProxy;
private final OptionSpec<Boolean> optionChatSigning;
private final OptionSpec<Integer> optionCompressionThreshold;
private final OptionSpec<Boolean> optionAllowBetaPinging;
private final OptionSpec<Boolean> optionIgnoreProtocolTranslationErrors;
private final OptionSpec<Boolean> optionAllowLegacyClientPassthrough;
private final OptionSpec<String> optionResourcePackUrl;
private final OptionSpec<WildcardDomainHandling> optionWildcardDomainHandling;
private SocketAddress bindAddress = AddressUtil.parse("0.0.0.0:25568", null);
private SocketAddress targetAddress = AddressUtil.parse("127.0.0.1:25565", null);
private ProtocolVersion targetVersion = ProtocolTranslator.AUTO_DETECT_PROTOCOL;
private boolean proxyOnlineMode = false;
private AuthMethod authMethod = AuthMethod.NONE;
private Account account = null;
private boolean betacraftAuth = false;
private URI backendProxyUrl = null;
private boolean backendHaProxy = false;
private boolean chatSigning = true;
private int compressionThreshold = 256;
private boolean allowBetaPinging = false;
private boolean ignoreProtocolTranslationErrors = false;
private boolean allowLegacyClientPassthrough = false;
private String resourcePackUrl = "";
private WildcardDomainHandling wildcardDomainHandling = WildcardDomainHandling.NONE;
public ViaProxyConfig(final File configFile) {
super(configFile);
this.optionParser = new OptionParser();
this.optionHelp = this.optionParser.accepts("help").forHelp();
this.optionBindAddress = this.optionParser.accepts("bind-address").withRequiredArg().ofType(String.class).defaultsTo(AddressUtil.toString(this.bindAddress));
this.optionTargetAddress = this.optionParser.accepts("target-address").withRequiredArg().ofType(String.class).defaultsTo(AddressUtil.toString(this.targetAddress));
this.optionTargetVersion = this.optionParser.accepts("target-version").withRequiredArg().withValuesConvertedBy(new ProtocolVersionConverter()).defaultsTo(this.targetVersion);
this.optionProxyOnlineMode = this.optionParser.accepts("proxy-online-mode").withRequiredArg().ofType(Boolean.class).defaultsTo(this.proxyOnlineMode);
this.optionAuthMethod = this.optionParser.accepts("auth-method").withRequiredArg().ofType(AuthMethod.class).defaultsTo(this.authMethod);
this.optionBetacraftAuth = this.optionParser.accepts("betacraft-auth").withRequiredArg().ofType(Boolean.class).defaultsTo(this.betacraftAuth);
this.optionBackendProxyUrl = this.optionParser.accepts("backend-proxy-url").withRequiredArg().ofType(String.class).defaultsTo("");
this.optionBackendHaProxy = this.optionParser.accepts("backend-haproxy").withRequiredArg().ofType(Boolean.class).defaultsTo(this.backendHaProxy);
this.optionChatSigning = this.optionParser.accepts("chat-signing").withRequiredArg().ofType(Boolean.class).defaultsTo(this.chatSigning);
this.optionCompressionThreshold = this.optionParser.accepts("compression-threshold").withRequiredArg().ofType(Integer.class).defaultsTo(this.compressionThreshold);
this.optionAllowBetaPinging = this.optionParser.accepts("allow-beta-pinging").withRequiredArg().ofType(Boolean.class).defaultsTo(this.allowBetaPinging);
this.optionIgnoreProtocolTranslationErrors = this.optionParser.accepts("ignore-protocol-translation-errors").withRequiredArg().ofType(Boolean.class).defaultsTo(this.ignoreProtocolTranslationErrors);
this.optionAllowLegacyClientPassthrough = this.optionParser.accepts("allow-legacy-client-passthrough").withRequiredArg().ofType(Boolean.class).defaultsTo(this.allowLegacyClientPassthrough);
this.optionResourcePackUrl = this.optionParser.accepts("resource-pack-url").withRequiredArg().ofType(String.class).defaultsTo(this.resourcePackUrl);
this.optionWildcardDomainHandling = this.optionParser.accepts("wildcard-domain-handling").withRequiredArg().ofType(WildcardDomainHandling.class).defaultsTo(this.wildcardDomainHandling);
}
@Override
public void reload() {
super.reload();
this.loadFields();
}
private void loadFields() {
this.bindAddress = AddressUtil.parse(this.getString("bind-address", "0.0.0.0:25568"), null);
this.targetVersion = ProtocolVersion.getClosest(this.getString("target-version", ProtocolTranslator.AUTO_DETECT_PROTOCOL.getName()));
if (this.targetVersion == null) {
this.targetVersion = ProtocolTranslator.AUTO_DETECT_PROTOCOL;
Logger.LOGGER.info("Invalid target version: " + this.getString("target-version", "") + ". Defaulting to auto detect.");
Logger.LOGGER.info("=== Supported Protocol Versions ===");
for (ProtocolVersion version : ProtocolVersion.getProtocols()) {
Logger.LOGGER.info(version.getName());
}
Logger.LOGGER.info("===================================");
}
this.targetAddress = AddressUtil.parse(this.getString("target-address", "127.0.0.1:25565"), this.targetVersion);
this.proxyOnlineMode = this.getBoolean("proxy-online-mode", false);
this.authMethod = AuthMethod.byName(this.getString("auth-method", "none"));
this.bindAddress = AddressUtil.parse(this.getString("bind-address", AddressUtil.toString(this.bindAddress)), null);
this.targetVersion = ProtocolVersion.getClosest(this.getString("target-version", this.targetVersion.getName()));
this.checkTargetVersion();
this.targetAddress = AddressUtil.parse(this.getString("target-address", AddressUtil.toString(this.targetAddress)), this.targetVersion);
this.proxyOnlineMode = this.getBoolean("proxy-online-mode", this.proxyOnlineMode);
this.authMethod = AuthMethod.byName(this.getString("auth-method", this.authMethod.name()));
final List<Account> accounts = ViaProxy.getSaveManager().accountsSave.getAccounts();
final int accountIndex = this.getInt("minecraft-account-index", 0);
if (this.authMethod == AuthMethod.ACCOUNT && accountIndex >= 0 && accountIndex < accounts.size()) {
@ -87,23 +122,54 @@ public class ViaProxyConfig extends Config implements com.viaversion.viaversion.
} else {
this.account = null;
}
this.betacraftAuth = this.getBoolean("betacraft-auth", false);
final String proxyUrl = this.getString("backend-proxy-url", "");
if (!proxyUrl.isBlank()) {
this.betacraftAuth = this.getBoolean("betacraft-auth", this.betacraftAuth);
this.backendProxyUrl = this.parseProxyUrl(this.getString("backend-proxy-url", ""));
this.backendHaProxy = this.getBoolean("backend-haproxy", this.backendHaProxy);
this.chatSigning = this.getBoolean("chat-signing", this.chatSigning);
this.compressionThreshold = this.getInt("compression-threshold", this.compressionThreshold);
this.allowBetaPinging = this.getBoolean("allow-beta-pinging", this.allowBetaPinging);
this.ignoreProtocolTranslationErrors = this.getBoolean("ignore-protocol-translation-errors", this.ignoreProtocolTranslationErrors);
this.allowLegacyClientPassthrough = this.getBoolean("allow-legacy-client-passthrough", this.allowLegacyClientPassthrough);
this.resourcePackUrl = this.getString("resource-pack-url", this.resourcePackUrl);
this.wildcardDomainHandling = WildcardDomainHandling.byName(this.getString("wildcard-domain-handling", this.wildcardDomainHandling.name()));
Options.loadFromConfig(this);
}
public void loadFromArguments(final String[] args) throws IOException {
try {
this.backendProxyUrl = new URI(proxyUrl);
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Invalid proxy url: " + proxyUrl + ". Proxy url format: type://address:port or type://username:password@address:port");
ViaProxy.EVENT_MANAGER.call(new PreOptionsParseEvent(this.optionParser));
final OptionSet options = this.optionParser.parse(args);
if (options.has(this.optionHelp)) {
throw new HelpRequestedException();
}
this.bindAddress = AddressUtil.parse(options.valueOf(this.optionBindAddress), null);
this.targetVersion = options.valueOf(this.optionTargetVersion);
this.checkTargetVersion();
this.targetAddress = AddressUtil.parse(options.valueOf(this.optionTargetAddress), this.targetVersion);
this.proxyOnlineMode = options.valueOf(this.optionProxyOnlineMode);
this.authMethod = options.valueOf(this.optionAuthMethod);
this.betacraftAuth = options.valueOf(this.optionBetacraftAuth);
this.backendProxyUrl = this.parseProxyUrl(options.valueOf(this.optionBackendProxyUrl));
this.backendHaProxy = options.valueOf(this.optionBackendHaProxy);
this.chatSigning = options.valueOf(this.optionChatSigning);
this.compressionThreshold = options.valueOf(this.optionCompressionThreshold);
this.allowBetaPinging = options.valueOf(this.optionAllowBetaPinging);
this.ignoreProtocolTranslationErrors = options.valueOf(this.optionIgnoreProtocolTranslationErrors);
this.allowLegacyClientPassthrough = options.valueOf(this.optionAllowLegacyClientPassthrough);
this.resourcePackUrl = options.valueOf(this.optionResourcePackUrl);
this.wildcardDomainHandling = options.valueOf(this.optionWildcardDomainHandling);
ViaProxy.EVENT_MANAGER.call(new PostOptionsParseEvent(options));
Options.loadFromConfig(this);
return;
} catch (OptionException e) {
Logger.LOGGER.error("Error parsing CLI options: " + e.getMessage());
} catch (HelpRequestedException ignored) {
}
this.backendHaProxy = this.getBoolean("backend-haproxy", false);
this.chatSigning = this.getBoolean("chat-signing", true);
this.compressionThreshold = this.getInt("compression-threshold", 256);
this.allowBetaPinging = this.getBoolean("allow-beta-pinging", false);
this.ignoreProtocolTranslationErrors = this.getBoolean("ignore-protocol-translation-errors", false);
this.allowLegacyClientPassthrough = this.getBoolean("allow-legacy-client-passthrough", false);
this.resourcePackUrl = this.getString("resource-pack-url", "");
this.wildcardDomainHandling = WildcardDomainHandling.byName(this.getString("wildcard-domain-handling", "none"));
this.optionParser.formatHelpWith(new BetterHelpFormatter());
this.optionParser.printHelpOn(Logger.SYSOUT);
Logger.LOGGER.info("For a more detailed explanation of the options, please refer to the viaproxy.yml file.");
System.exit(1);
}
@Override
@ -274,6 +340,30 @@ public class ViaProxyConfig extends Config implements com.viaversion.viaversion.
this.set("wildcard-domain-handling", wildcardDomainHandling.name().toLowerCase(Locale.ROOT));
}
private void checkTargetVersion() {
if (this.targetVersion == null) {
this.targetVersion = ProtocolTranslator.AUTO_DETECT_PROTOCOL;
Logger.LOGGER.info("Invalid target version: " + this.getString("target-version", "") + ". Defaulting to auto detect.");
Logger.LOGGER.info("=== Supported Protocol Versions ===");
for (ProtocolVersion version : ProtocolVersion.getProtocols()) {
Logger.LOGGER.info(version.getName());
}
Logger.LOGGER.info("===================================");
}
}
private URI parseProxyUrl(final String proxyUrl) {
if (!proxyUrl.isBlank()) {
try {
return new URI(proxyUrl);
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Invalid proxy url: " + proxyUrl + ". Proxy url format: type://address:port or type://username:password@address:port");
}
}
return null;
}
public enum AuthMethod {
/**

View File

@ -48,6 +48,7 @@ import net.raphimc.viaproxy.saves.impl.accounts.ClassicAccount;
import net.raphimc.viaproxy.util.AddressUtil;
import net.raphimc.viaproxy.util.ArrayHelper;
import net.raphimc.viaproxy.util.ProtocolVersionDetector;
import net.raphimc.viaproxy.util.ProtocolVersionUtil;
import net.raphimc.viaproxy.util.logging.Logger;
import java.net.ConnectException;
@ -138,24 +139,18 @@ public class Client2ProxyHandler extends SimpleChannelInboundHandler<IPacket> {
throw CloseAndReturn.INSTANCE;
}
final String versionString = arrayHelper.get(arrayHelper.getLength() - 1);
serverVersion = ProtocolVersion.getClosest(versionString);
if (serverVersion == null) {
serverVersion = ProtocolVersion.getClosest(versionString.replace("-", " "));
}
serverVersion = ProtocolVersionUtil.fromNameLenient(versionString);
if (serverVersion == null) throw CloseAndReturn.INSTANCE;
final String connectIP = arrayHelper.getAsString(0, arrayHelper.getLength() - 3, "_");
final int connectPort = arrayHelper.getInteger(arrayHelper.getLength() - 2);
serverAddress = AddressUtil.parse(connectIP + ":" + connectPort, serverVersion);
} catch (CloseAndReturn e) {
this.proxyConnection.kickClient("§cWrong wildcard syntax! §6Please use:\n§7address_port_version.viaproxy.hostname");
this.proxyConnection.kickClient("§cWrong domain syntax! §6Please use:\n§7address_port_version.viaproxy.hostname");
}
} else if (ViaProxy.getConfig().getWildcardDomainHandling() == ViaProxyConfig.WildcardDomainHandling.INTERNAL) {
final ArrayHelper arrayHelper = ArrayHelper.instanceOf(handshakeParts[0].split("\7"));
final String versionString = arrayHelper.get(1);
serverVersion = ProtocolVersion.getClosest(versionString);
if (serverVersion == null) {
serverVersion = ProtocolVersion.getClosest(versionString.replace("-", " "));
}
serverVersion = ProtocolVersionUtil.fromNameLenient(versionString);
if (serverVersion == null) throw CloseAndReturn.INSTANCE;
serverAddress = AddressUtil.parse(arrayHelper.get(0), serverVersion);
if (arrayHelper.isIndexValid(2)) {

View File

@ -0,0 +1,32 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2021-2024 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.util;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
public class ProtocolVersionUtil {
public static ProtocolVersion fromNameLenient(final String name) {
final ProtocolVersion version = ProtocolVersion.getClosest(name);
if (version == null) {
return ProtocolVersion.getClosest(name.replace("-", " "));
}
return version;
}
}