Made version parsing more lenient and improved CLI error handling

This commit is contained in:
RaphiMC 2023-06-22 19:50:14 +02:00
parent a0e1732bb9
commit 353b1301e8
No known key found for this signature in database
GPG key ID: 0F6BB0657A03AC94
3 changed files with 43 additions and 38 deletions

View file

@ -84,13 +84,14 @@ public class ViaProxy {
}
try {
transformerManager.hookInstrumentation(Agents.getInstrumentation());
injectedMain("Runtime Agent", args);
} catch (Throwable t) {
final InjectionClassLoader injectionClassLoader = new InjectionClassLoader(transformerManager, ClassLoaders.getSystemClassPath());
injectionClassLoader.setPriority(EnumLoaderPriority.PARENT_FIRST);
Thread.currentThread().setContextClassLoader(injectionClassLoader);
Methods.invoke(null, Methods.getDeclaredMethod(injectionClassLoader.loadClass(ViaProxy.class.getName()), "injectedMain", String.class, String[].class), "Injection ClassLoader", args);
return;
}
injectedMain("Runtime Agent", args);
}
public static void injectedMain(final String injectionMethod, final String[] args) throws InterruptedException, IOException {

View file

@ -85,49 +85,49 @@ public class Options {
final OptionSet options;
try {
options = parser.parse(args);
if (options.has(help)) {
parser.formatHelpWith(new BetterHelpFormatter());
parser.printHelpOn(Logger.SYSOUT);
System.exit(1);
}
BIND_ADDRESS = options.valueOf(bindAddress);
BIND_PORT = options.valueOf(bindPort);
SRV_MODE = options.has(srvMode);
INTERNAL_SRV_MODE = options.has(iSrvMode);
ONLINE_MODE = options.has(onlineMode);
CONNECT_ADDRESS = options.valueOf(connectAddress);
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);
BETACRAFT_AUTH = options.has(betaCraftAuth);
if (options.has(resourcePackUrl)) {
RESOURCE_PACK_URL = options.valueOf(resourcePackUrl);
}
if (options.has(proxyUrl)) {
try {
PROXY_URL = new URI(options.valueOf(proxyUrl));
} catch (URISyntaxException e) {
Logger.LOGGER.error("Invalid proxy url: " + options.valueOf(proxyUrl));
Logger.LOGGER.error("Proxy url format: type://address:port or type://username:password@address:port");
System.exit(1);
}
}
SERVER_HAPROXY_PROTOCOL = options.has(serverHaProxyProtocol);
LEGACY_CLIENT_PASSTHROUGH = options.has(legacyClientPassthrough);
PluginManager.EVENT_MANAGER.call(new PostOptionsParseEvent(options));
} catch (OptionException e) {
Logger.LOGGER.error("Error parsing options: " + e.getMessage());
parser.formatHelpWith(new BetterHelpFormatter());
parser.printHelpOn(Logger.SYSOUT);
System.exit(1);
return;
}
if (options.has(help)) {
parser.formatHelpWith(new BetterHelpFormatter());
parser.printHelpOn(Logger.SYSOUT);
System.exit(1);
}
BIND_ADDRESS = options.valueOf(bindAddress);
BIND_PORT = options.valueOf(bindPort);
SRV_MODE = options.has(srvMode);
INTERNAL_SRV_MODE = options.has(iSrvMode);
ONLINE_MODE = options.has(onlineMode);
CONNECT_ADDRESS = options.valueOf(connectAddress);
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);
BETACRAFT_AUTH = options.has(betaCraftAuth);
if (options.has(resourcePackUrl)) {
RESOURCE_PACK_URL = options.valueOf(resourcePackUrl);
}
if (options.has(proxyUrl)) {
try {
PROXY_URL = new URI(options.valueOf(proxyUrl));
} catch (URISyntaxException e) {
Logger.LOGGER.error("Invalid proxy url: " + options.valueOf(proxyUrl));
Logger.LOGGER.error("Proxy url format: type://address:port or type://username:password@address:port");
System.exit(1);
}
}
SERVER_HAPROXY_PROTOCOL = options.has(serverHaProxyProtocol);
LEGACY_CLIENT_PASSTHROUGH = options.has(legacyClientPassthrough);
PluginManager.EVENT_MANAGER.call(new PostOptionsParseEvent(options));
}
}

View file

@ -28,6 +28,10 @@ public class VersionEnumConverter implements ValueConverter<VersionEnum> {
for (VersionEnum version : VersionEnum.getAllVersions()) {
if (version.getName().equalsIgnoreCase(s)) return version;
}
for (VersionEnum version : VersionEnum.getAllVersions()) {
if (version.getProtocol().getIncludedVersions().contains(s)) return version;
}
throw new ValueConversionException("Unable to find version '" + s + "'");
}