mirror of
https://github.com/ViaVersion/ViaProxy.git
synced 2024-12-22 16:38:04 +01:00
Added new CLI interface
This commit is contained in:
parent
0c04ac45d9
commit
1e32284e8a
@ -108,11 +108,54 @@ public class ViaProxy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void injectedMain(final String injectionMethod, final String[] args) throws InterruptedException, IOException, InvocationTargetException {
|
public static void injectedMain(final String injectionMethod, final String[] args) throws InterruptedException, IOException, InvocationTargetException {
|
||||||
final boolean hasUI = args.length == 0 && !GraphicsEnvironment.isHeadless();
|
Logger.setup();
|
||||||
final boolean legacyCLI = args.length > 0 && args[0].startsWith("-");
|
|
||||||
|
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 SplashScreen splashScreen;
|
||||||
final Consumer<String> progressConsumer;
|
final Consumer<String> progressConsumer;
|
||||||
if (hasUI) {
|
if (useUI) {
|
||||||
final float progressStep = 1F / 7F;
|
final float progressStep = 1F / 7F;
|
||||||
foregroundWindow = splashScreen = new SplashScreen();
|
foregroundWindow = splashScreen = new SplashScreen();
|
||||||
progressConsumer = (text) -> {
|
progressConsumer = (text) -> {
|
||||||
@ -130,42 +173,13 @@ public class ViaProxy {
|
|||||||
}
|
}
|
||||||
progressConsumer.accept("Initializing 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();
|
ConsoleHandler.hookConsole();
|
||||||
ViaProxy.loadNetty();
|
ViaProxy.loadNetty();
|
||||||
ClassLoaderPriorityUtil.loadOverridingJars();
|
ClassLoaderPriorityUtil.loadOverridingJars();
|
||||||
|
|
||||||
final File viaProxyConfigFile;
|
final File viaProxyConfigFile;
|
||||||
if (!hasUI && !legacyCLI) {
|
if (useConfig) {
|
||||||
viaProxyConfigFile = new File(args[0]);
|
viaProxyConfigFile = new File(args[1]);
|
||||||
} else {
|
} else {
|
||||||
viaProxyConfigFile = new File("viaproxy.yml");
|
viaProxyConfigFile = new File("viaproxy.yml");
|
||||||
}
|
}
|
||||||
@ -181,7 +195,7 @@ public class ViaProxy {
|
|||||||
CONFIG = new ViaProxyConfig(viaProxyConfigFile);
|
CONFIG = new ViaProxyConfig(viaProxyConfigFile);
|
||||||
CONFIG.reload();
|
CONFIG.reload();
|
||||||
|
|
||||||
if (hasUI) {
|
if (useUI) {
|
||||||
progressConsumer.accept("Loading GUI");
|
progressConsumer.accept("Loading GUI");
|
||||||
SwingUtilities.invokeAndWait(() -> {
|
SwingUtilities.invokeAndWait(() -> {
|
||||||
try {
|
try {
|
||||||
@ -199,7 +213,11 @@ public class ViaProxy {
|
|||||||
EVENT_MANAGER.call(new ViaProxyLoadedEvent());
|
EVENT_MANAGER.call(new ViaProxyLoadedEvent());
|
||||||
Logger.LOGGER.info("ViaProxy started successfully!");
|
Logger.LOGGER.info("ViaProxy started successfully!");
|
||||||
} else {
|
} 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);
|
Options.parse(args);
|
||||||
} else if (firstStart) {
|
} 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.");
|
Logger.LOGGER.info("This is the first start of ViaProxy. Please configure the settings in the " + viaProxyConfigFile.getName() + " file and restart ViaProxy.");
|
||||||
|
@ -15,24 +15,28 @@
|
|||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* 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.BuiltinHelpFormatter;
|
||||||
import joptsimple.OptionDescriptor;
|
import joptsimple.OptionDescriptor;
|
||||||
import joptsimple.internal.Classes;
|
import joptsimple.internal.Classes;
|
||||||
import joptsimple.internal.Strings;
|
import joptsimple.internal.Strings;
|
||||||
|
|
||||||
@Deprecated(forRemoval = true)
|
|
||||||
public class BetterHelpFormatter extends BuiltinHelpFormatter {
|
public class BetterHelpFormatter extends BuiltinHelpFormatter {
|
||||||
|
|
||||||
public BetterHelpFormatter() {
|
public BetterHelpFormatter() {
|
||||||
super(250, 4);
|
super(80, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String extractTypeIndicator(OptionDescriptor descriptor) {
|
protected String extractTypeIndicator(OptionDescriptor descriptor) {
|
||||||
String indicator = descriptor.argumentTypeIndicator();
|
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";
|
return !Strings.isNullOrEmpty(indicator) && !String.class.getName().equals(indicator) ? Classes.shortNameOf(indicator) : "String";
|
||||||
}
|
}
|
||||||
|
|
@ -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 {
|
||||||
|
}
|
@ -15,18 +15,18 @@
|
|||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* 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 com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
|
||||||
import joptsimple.ValueConversionException;
|
import joptsimple.ValueConversionException;
|
||||||
import joptsimple.ValueConverter;
|
import joptsimple.ValueConverter;
|
||||||
|
import net.raphimc.viaproxy.util.ProtocolVersionUtil;
|
||||||
|
|
||||||
@Deprecated(forRemoval = true)
|
|
||||||
public class ProtocolVersionConverter implements ValueConverter<ProtocolVersion> {
|
public class ProtocolVersionConverter implements ValueConverter<ProtocolVersion> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ProtocolVersion convert(String s) {
|
public ProtocolVersion convert(String s) {
|
||||||
final ProtocolVersion version = ProtocolVersion.getClosest(s);
|
final ProtocolVersion version = ProtocolVersionUtil.fromNameLenient(s);
|
||||||
if (version == null) {
|
if (version == null) {
|
||||||
throw new ValueConversionException("Unable to find version '" + s + "'");
|
throw new ValueConversionException("Unable to find version '" + s + "'");
|
||||||
}
|
}
|
@ -23,6 +23,8 @@ import joptsimple.OptionParser;
|
|||||||
import joptsimple.OptionSet;
|
import joptsimple.OptionSet;
|
||||||
import joptsimple.OptionSpec;
|
import joptsimple.OptionSpec;
|
||||||
import net.raphimc.viaproxy.ViaProxy;
|
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.PostOptionsParseEvent;
|
||||||
import net.raphimc.viaproxy.plugins.events.PreOptionsParseEvent;
|
import net.raphimc.viaproxy.plugins.events.PreOptionsParseEvent;
|
||||||
import net.raphimc.viaproxy.protocoltranslator.viaproxy.ViaProxyConfig;
|
import net.raphimc.viaproxy.protocoltranslator.viaproxy.ViaProxyConfig;
|
||||||
@ -63,7 +65,7 @@ public class Options {
|
|||||||
|
|
||||||
public static void parse(final String[] args) throws IOException {
|
public static void parse(final String[] args) throws IOException {
|
||||||
Logger.LOGGER.fatal("===============================================================================================================");
|
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("Waiting 10 seconds before continuing...");
|
||||||
Logger.LOGGER.fatal("===============================================================================================================");
|
Logger.LOGGER.fatal("===============================================================================================================");
|
||||||
try {
|
try {
|
||||||
|
@ -19,7 +19,6 @@ package net.raphimc.viaproxy.plugins.events;
|
|||||||
|
|
||||||
import joptsimple.OptionSet;
|
import joptsimple.OptionSet;
|
||||||
|
|
||||||
@Deprecated(forRemoval = true)
|
|
||||||
public class PostOptionsParseEvent {
|
public class PostOptionsParseEvent {
|
||||||
|
|
||||||
private final OptionSet options;
|
private final OptionSet options;
|
||||||
|
@ -19,7 +19,6 @@ package net.raphimc.viaproxy.plugins.events;
|
|||||||
|
|
||||||
import joptsimple.OptionParser;
|
import joptsimple.OptionParser;
|
||||||
|
|
||||||
@Deprecated(forRemoval = true)
|
|
||||||
public class PreOptionsParseEvent {
|
public class PreOptionsParseEvent {
|
||||||
|
|
||||||
private final OptionParser parser;
|
private final OptionParser parser;
|
||||||
|
@ -19,14 +19,24 @@ package net.raphimc.viaproxy.protocoltranslator.viaproxy;
|
|||||||
|
|
||||||
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
|
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
|
||||||
import com.viaversion.viaversion.util.Config;
|
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.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.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.protocoltranslator.ProtocolTranslator;
|
||||||
import net.raphimc.viaproxy.saves.impl.accounts.Account;
|
import net.raphimc.viaproxy.saves.impl.accounts.Account;
|
||||||
import net.raphimc.viaproxy.util.AddressUtil;
|
import net.raphimc.viaproxy.util.AddressUtil;
|
||||||
import net.raphimc.viaproxy.util.logging.Logger;
|
import net.raphimc.viaproxy.util.logging.Logger;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
@ -38,48 +48,73 @@ import java.util.Map;
|
|||||||
|
|
||||||
public class ViaProxyConfig extends Config implements com.viaversion.viaversion.api.configuration.Config {
|
public class ViaProxyConfig extends Config implements com.viaversion.viaversion.api.configuration.Config {
|
||||||
|
|
||||||
private SocketAddress bindAddress;
|
private OptionParser optionParser;
|
||||||
private SocketAddress targetAddress;
|
private final OptionSpec<Void> optionHelp;
|
||||||
private ProtocolVersion targetVersion;
|
private final OptionSpec<String> optionBindAddress;
|
||||||
private boolean proxyOnlineMode;
|
private final OptionSpec<String> optionTargetAddress;
|
||||||
private AuthMethod authMethod;
|
private final OptionSpec<ProtocolVersion> optionTargetVersion;
|
||||||
private Account account;
|
private final OptionSpec<Boolean> optionProxyOnlineMode;
|
||||||
private boolean betacraftAuth;
|
private final OptionSpec<AuthMethod> optionAuthMethod;
|
||||||
private URI backendProxyUrl;
|
private final OptionSpec<Boolean> optionBetacraftAuth;
|
||||||
private boolean backendHaProxy;
|
private final OptionSpec<String> optionBackendProxyUrl;
|
||||||
private boolean chatSigning;
|
private final OptionSpec<Boolean> optionBackendHaProxy;
|
||||||
private int compressionThreshold;
|
private final OptionSpec<Boolean> optionChatSigning;
|
||||||
private boolean allowBetaPinging;
|
private final OptionSpec<Integer> optionCompressionThreshold;
|
||||||
private boolean ignoreProtocolTranslationErrors;
|
private final OptionSpec<Boolean> optionAllowBetaPinging;
|
||||||
private boolean allowLegacyClientPassthrough;
|
private final OptionSpec<Boolean> optionIgnoreProtocolTranslationErrors;
|
||||||
private String resourcePackUrl;
|
private final OptionSpec<Boolean> optionAllowLegacyClientPassthrough;
|
||||||
private WildcardDomainHandling wildcardDomainHandling;
|
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) {
|
public ViaProxyConfig(final File configFile) {
|
||||||
super(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
|
@Override
|
||||||
public void reload() {
|
public void reload() {
|
||||||
super.reload();
|
super.reload();
|
||||||
this.loadFields();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void loadFields() {
|
this.bindAddress = AddressUtil.parse(this.getString("bind-address", AddressUtil.toString(this.bindAddress)), null);
|
||||||
this.bindAddress = AddressUtil.parse(this.getString("bind-address", "0.0.0.0:25568"), null);
|
this.targetVersion = ProtocolVersion.getClosest(this.getString("target-version", this.targetVersion.getName()));
|
||||||
this.targetVersion = ProtocolVersion.getClosest(this.getString("target-version", ProtocolTranslator.AUTO_DETECT_PROTOCOL.getName()));
|
this.checkTargetVersion();
|
||||||
if (this.targetVersion == null) {
|
this.targetAddress = AddressUtil.parse(this.getString("target-address", AddressUtil.toString(this.targetAddress)), this.targetVersion);
|
||||||
this.targetVersion = ProtocolTranslator.AUTO_DETECT_PROTOCOL;
|
this.proxyOnlineMode = this.getBoolean("proxy-online-mode", this.proxyOnlineMode);
|
||||||
Logger.LOGGER.info("Invalid target version: " + this.getString("target-version", "") + ". Defaulting to auto detect.");
|
this.authMethod = AuthMethod.byName(this.getString("auth-method", this.authMethod.name()));
|
||||||
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"));
|
|
||||||
final List<Account> accounts = ViaProxy.getSaveManager().accountsSave.getAccounts();
|
final List<Account> accounts = ViaProxy.getSaveManager().accountsSave.getAccounts();
|
||||||
final int accountIndex = this.getInt("minecraft-account-index", 0);
|
final int accountIndex = this.getInt("minecraft-account-index", 0);
|
||||||
if (this.authMethod == AuthMethod.ACCOUNT && accountIndex >= 0 && accountIndex < accounts.size()) {
|
if (this.authMethod == AuthMethod.ACCOUNT && accountIndex >= 0 && accountIndex < accounts.size()) {
|
||||||
@ -87,23 +122,54 @@ public class ViaProxyConfig extends Config implements com.viaversion.viaversion.
|
|||||||
} else {
|
} else {
|
||||||
this.account = null;
|
this.account = null;
|
||||||
}
|
}
|
||||||
this.betacraftAuth = this.getBoolean("betacraft-auth", false);
|
this.betacraftAuth = this.getBoolean("betacraft-auth", this.betacraftAuth);
|
||||||
final String proxyUrl = this.getString("backend-proxy-url", "");
|
this.backendProxyUrl = this.parseProxyUrl(this.getString("backend-proxy-url", ""));
|
||||||
if (!proxyUrl.isBlank()) {
|
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 {
|
try {
|
||||||
this.backendProxyUrl = new URI(proxyUrl);
|
ViaProxy.EVENT_MANAGER.call(new PreOptionsParseEvent(this.optionParser));
|
||||||
} catch (URISyntaxException e) {
|
final OptionSet options = this.optionParser.parse(args);
|
||||||
throw new IllegalArgumentException("Invalid proxy url: " + proxyUrl + ". Proxy url format: type://address:port or type://username:password@address:port");
|
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.optionParser.formatHelpWith(new BetterHelpFormatter());
|
||||||
this.compressionThreshold = this.getInt("compression-threshold", 256);
|
this.optionParser.printHelpOn(Logger.SYSOUT);
|
||||||
this.allowBetaPinging = this.getBoolean("allow-beta-pinging", false);
|
Logger.LOGGER.info("For a more detailed explanation of the options, please refer to the viaproxy.yml file.");
|
||||||
this.ignoreProtocolTranslationErrors = this.getBoolean("ignore-protocol-translation-errors", false);
|
System.exit(1);
|
||||||
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"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -274,6 +340,30 @@ public class ViaProxyConfig extends Config implements com.viaversion.viaversion.
|
|||||||
this.set("wildcard-domain-handling", wildcardDomainHandling.name().toLowerCase(Locale.ROOT));
|
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 {
|
public enum AuthMethod {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -48,6 +48,7 @@ import net.raphimc.viaproxy.saves.impl.accounts.ClassicAccount;
|
|||||||
import net.raphimc.viaproxy.util.AddressUtil;
|
import net.raphimc.viaproxy.util.AddressUtil;
|
||||||
import net.raphimc.viaproxy.util.ArrayHelper;
|
import net.raphimc.viaproxy.util.ArrayHelper;
|
||||||
import net.raphimc.viaproxy.util.ProtocolVersionDetector;
|
import net.raphimc.viaproxy.util.ProtocolVersionDetector;
|
||||||
|
import net.raphimc.viaproxy.util.ProtocolVersionUtil;
|
||||||
import net.raphimc.viaproxy.util.logging.Logger;
|
import net.raphimc.viaproxy.util.logging.Logger;
|
||||||
|
|
||||||
import java.net.ConnectException;
|
import java.net.ConnectException;
|
||||||
@ -138,24 +139,18 @@ public class Client2ProxyHandler extends SimpleChannelInboundHandler<IPacket> {
|
|||||||
throw CloseAndReturn.INSTANCE;
|
throw CloseAndReturn.INSTANCE;
|
||||||
}
|
}
|
||||||
final String versionString = arrayHelper.get(arrayHelper.getLength() - 1);
|
final String versionString = arrayHelper.get(arrayHelper.getLength() - 1);
|
||||||
serverVersion = ProtocolVersion.getClosest(versionString);
|
serverVersion = ProtocolVersionUtil.fromNameLenient(versionString);
|
||||||
if (serverVersion == null) {
|
|
||||||
serverVersion = ProtocolVersion.getClosest(versionString.replace("-", " "));
|
|
||||||
}
|
|
||||||
if (serverVersion == null) throw CloseAndReturn.INSTANCE;
|
if (serverVersion == null) throw CloseAndReturn.INSTANCE;
|
||||||
final String connectIP = arrayHelper.getAsString(0, arrayHelper.getLength() - 3, "_");
|
final String connectIP = arrayHelper.getAsString(0, arrayHelper.getLength() - 3, "_");
|
||||||
final int connectPort = arrayHelper.getInteger(arrayHelper.getLength() - 2);
|
final int connectPort = arrayHelper.getInteger(arrayHelper.getLength() - 2);
|
||||||
serverAddress = AddressUtil.parse(connectIP + ":" + connectPort, serverVersion);
|
serverAddress = AddressUtil.parse(connectIP + ":" + connectPort, serverVersion);
|
||||||
} catch (CloseAndReturn e) {
|
} 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) {
|
} else if (ViaProxy.getConfig().getWildcardDomainHandling() == ViaProxyConfig.WildcardDomainHandling.INTERNAL) {
|
||||||
final ArrayHelper arrayHelper = ArrayHelper.instanceOf(handshakeParts[0].split("\7"));
|
final ArrayHelper arrayHelper = ArrayHelper.instanceOf(handshakeParts[0].split("\7"));
|
||||||
final String versionString = arrayHelper.get(1);
|
final String versionString = arrayHelper.get(1);
|
||||||
serverVersion = ProtocolVersion.getClosest(versionString);
|
serverVersion = ProtocolVersionUtil.fromNameLenient(versionString);
|
||||||
if (serverVersion == null) {
|
|
||||||
serverVersion = ProtocolVersion.getClosest(versionString.replace("-", " "));
|
|
||||||
}
|
|
||||||
if (serverVersion == null) throw CloseAndReturn.INSTANCE;
|
if (serverVersion == null) throw CloseAndReturn.INSTANCE;
|
||||||
serverAddress = AddressUtil.parse(arrayHelper.get(0), serverVersion);
|
serverAddress = AddressUtil.parse(arrayHelper.get(0), serverVersion);
|
||||||
if (arrayHelper.isIndexValid(2)) {
|
if (arrayHelper.isIndexValid(2)) {
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user