Skip to content

Commit

Permalink
Refactored address handling to support unix socket files
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Dec 17, 2023
1 parent 2b27f48 commit b24145f
Show file tree
Hide file tree
Showing 20 changed files with 236 additions and 293 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ dependencies {
6. Have fun!

Here is an example command to allow players to join on yourserverip:25568 and connect to a beta 1.7.3 server running on port 25565:
``java -jar ViaProxy-whateverversion.jar --bind_port 25568 --target_ip 127.0.0.1 --target_port 25565 --version b1.7-b1.7.3``
``java -jar ViaProxy-whateverversion.jar --bind_address 0.0.0.0:25568 --target_ip 127.0.0.1:25565 --version b1.7-b1.7.3``

### Configuring the protocol translation
To change the protocol translation settings/features you can look into the ViaProtocolHack folder.
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ dependencies {
include "net.lenni0451.classtransform:additionalclassprovider:1.13.0-SNAPSHOT"
include "net.lenni0451:Reflect:1.3.1"
include "net.lenni0451:LambdaEvents:2.4.0"
include("net.lenni0451:MCPing:1.3.0") {
include("net.lenni0451:MCPing:1.4.0-SNAPSHOT") {
exclude group: "com.google.code.gson", module: "gson"
}
include("net.raphimc.netminecraft:all:2.3.8-SNAPSHOT") {
include("net.raphimc.netminecraft:all:2.4.0-SNAPSHOT") {
exclude group: "com.google.code.gson", module: "gson"
}
include("net.raphimc:MinecraftAuth:3.0.1-SNAPSHOT") {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ org.gradle.configureondemand=true

maven_group=net.raphimc
maven_name=ViaProxy
maven_version=3.0.24-SNAPSHOT
maven_version=3.1.0-SNAPSHOT
6 changes: 2 additions & 4 deletions src/main/java/net/raphimc/viaproxy/ViaProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import net.raphimc.viaproxy.plugins.events.ProxyStartEvent;
import net.raphimc.viaproxy.plugins.events.ProxyStopEvent;
import net.raphimc.viaproxy.plugins.events.ViaProxyLoadedEvent;
import net.raphimc.viaproxy.proxy.EventListener;
import net.raphimc.viaproxy.proxy.client2proxy.Client2ProxyChannelInitializer;
import net.raphimc.viaproxy.proxy.client2proxy.Client2ProxyHandler;
import net.raphimc.viaproxy.proxy.session.ProxyConnection;
Expand Down Expand Up @@ -139,7 +138,6 @@ public static void injectedMain(final String injectionMethod, final String[] arg

SAVE_MANAGER = new SaveManager();
PLUGIN_MANAGER = new PluginManager();
EVENT_MANAGER.register(EventListener.class);

final Thread loaderThread = new Thread(new LoaderTask(), "ViaLoader");
final Thread updateCheckThread = new Thread(new UpdateCheckTask(hasUI), "UpdateCheck");
Expand Down Expand Up @@ -191,8 +189,8 @@ public static void startProxy() {
Logger.LOGGER.info("Starting proxy server");
currentProxyServer = new NetServer(() -> EVENT_MANAGER.call(new Client2ProxyHandlerCreationEvent(new Client2ProxyHandler(), false)).getHandler(), Client2ProxyChannelInitializer::new);
EVENT_MANAGER.call(new ProxyStartEvent());
Logger.LOGGER.info("Binding proxy server to " + Options.BIND_ADDRESS + ":" + Options.BIND_PORT);
currentProxyServer.bind(Options.BIND_ADDRESS, Options.BIND_PORT, false);
Logger.LOGGER.info("Binding proxy server to " + Options.BIND_ADDRESS);
currentProxyServer.bind(Options.BIND_ADDRESS, false);
} catch (Throwable e) {
currentProxyServer = null;
throw e;
Expand Down
31 changes: 11 additions & 20 deletions src/main/java/net/raphimc/viaproxy/cli/options/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
import joptsimple.OptionSpec;
import net.raphimc.vialoader.util.VersionEnum;
import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.plugins.events.GetDefaultPortEvent;
import net.raphimc.viaproxy.plugins.events.PostOptionsParseEvent;
import net.raphimc.viaproxy.plugins.events.PreOptionsParseEvent;
import net.raphimc.viaproxy.saves.impl.accounts.Account;
import net.raphimc.viaproxy.util.AddressUtil;
import net.raphimc.viaproxy.util.logging.Logger;

import java.io.IOException;
import java.net.SocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
Expand All @@ -38,10 +39,8 @@

public class Options {

public static String BIND_ADDRESS = "0.0.0.0";
public static int BIND_PORT = 25568;
public static String CONNECT_ADDRESS;
public static int CONNECT_PORT = 25565;
public static SocketAddress BIND_ADDRESS;
public static SocketAddress CONNECT_ADDRESS;
public static VersionEnum PROTOCOL_VERSION;
public static boolean ONLINE_MODE;
public static boolean OPENAUTHMOD_AUTH;
Expand All @@ -58,7 +57,7 @@ public class Options {
// CLI only config options
public static int COMPRESSION_THRESHOLD = 256;
public static boolean SRV_MODE; // Example: lenni0451.net_25565_1.8.x.viaproxy.127.0.0.1.nip.io
public static boolean INTERNAL_SRV_MODE; // Example: ip\7port\7version\7mppass
public static boolean INTERNAL_SRV_MODE; // Example: ip:port\7version\7mppass
public static String RESOURCE_PACK_URL; // Example: http://example.com/resourcepack.zip
public static boolean SERVER_HAPROXY_PROTOCOL;
public static boolean LEGACY_CLIENT_PASSTHROUGH;
Expand All @@ -67,15 +66,13 @@ public static void parse(final String[] args) throws IOException {
final OptionParser parser = new OptionParser();
final OptionSpec<Void> help = parser.acceptsAll(asList("help", "h", "?"), "Get a list of all arguments").forHelp();

final OptionSpec<String> bindAddress = parser.acceptsAll(asList("bind_address", "bind_ip", "ba"), "The address the proxy should bind to").withRequiredArg().ofType(String.class).defaultsTo(BIND_ADDRESS);
final OptionSpec<Integer> bindPort = parser.acceptsAll(asList("bind_port", "bp"), "The port the proxy should bind to").withRequiredArg().ofType(Integer.class).defaultsTo(BIND_PORT);
final OptionSpec<String> bindAddress = parser.acceptsAll(asList("bind_address", "bind_ip", "ba"), "The address the proxy should bind to").withRequiredArg().ofType(String.class).defaultsTo("0.0.0.0:25568");
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<VersionEnum> version = parser.acceptsAll(asList("version", "v"), "The version of the target server").withRequiredArg().withValuesConvertedBy(new VersionEnumConverter()).required();
final OptionSpec<Void> srvMode = parser.acceptsAll(asList("srv_mode", "srv", "s"), "Enable srv mode");
final OptionSpec<Void> iSrvMode = parser.acceptsAll(asList("internal_srv_mode", "isrv"), "Enable internal srv mode").availableUnless(srvMode);
final OptionSpec<Void> proxyOnlineMode = parser.acceptsAll(asList("online_mode", "om", "o"), "Enable proxy online mode");
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);
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"), "Use OpenAuthMod for joining online mode servers");
final OptionSpec<Integer> guiAccountIndex = parser.acceptsAll(asList("gui_account_index", "gui_account"), "Use an account from the ViaProxy GUI for joining online mode servers (Specify -1 for instructions)").withRequiredArg().ofType(Integer.class);
final OptionSpec<Void> betaCraftAuth = parser.accepts("betacraft_auth", "Use BetaCraft authentication for classic servers");
Expand All @@ -96,18 +93,12 @@ public static void parse(final String[] args) throws IOException {
System.exit(1);
}

BIND_ADDRESS = options.valueOf(bindAddress);
BIND_PORT = options.valueOf(bindPort);
PROTOCOL_VERSION = options.valueOf(version);
BIND_ADDRESS = AddressUtil.parse(options.valueOf(bindAddress), null);
CONNECT_ADDRESS = AddressUtil.parse(options.valueOf(connectAddress), PROTOCOL_VERSION);
SRV_MODE = options.has(srvMode);
INTERNAL_SRV_MODE = options.has(iSrvMode);
ONLINE_MODE = options.has(proxyOnlineMode);
CONNECT_ADDRESS = options.valueOf(connectAddress);
PROTOCOL_VERSION = options.valueOf(version);
if (options.has(connectPort)) {
CONNECT_PORT = options.valueOf(connectPort);
} else {
CONNECT_PORT = ViaProxy.EVENT_MANAGER.call(new GetDefaultPortEvent(PROTOCOL_VERSION, 25565)).getDefaultPort();
}
COMPRESSION_THRESHOLD = options.valueOf(compressionThreshold);
OPENAUTHMOD_AUTH = options.has(openAuthModAuth);
if (options.has(guiAccountIndex)) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,43 @@
package net.raphimc.viaproxy.plugins.events;

import io.netty.channel.Channel;
import net.raphimc.netminecraft.util.ServerAddress;
import net.raphimc.vialoader.util.VersionEnum;
import net.raphimc.viaproxy.plugins.events.types.EventCancellable;

import java.net.SocketAddress;

public class PreConnectEvent extends EventCancellable {

private final ServerAddress serverAddress;
private final VersionEnum serverVersion;
private SocketAddress serverAddress;
private VersionEnum serverVersion;
private final VersionEnum clientVersion;
private final Channel clientChannel;

private String cancelMessage = "§cCould not connect to the backend server! (Server is blacklisted)";

public PreConnectEvent(final ServerAddress serverAddress, final VersionEnum serverVersion, final VersionEnum clientVersion, final Channel clientChannel) {
public PreConnectEvent(final SocketAddress serverAddress, final VersionEnum serverVersion, final VersionEnum clientVersion, final Channel clientChannel) {
this.serverAddress = serverAddress;
this.serverVersion = serverVersion;
this.clientVersion = clientVersion;
this.clientChannel = clientChannel;
}

public ServerAddress getServerAddress() {
public SocketAddress getServerAddress() {
return this.serverAddress;
}

public void setServerAddress(final SocketAddress serverAddress) {
this.serverAddress = serverAddress;
}

public VersionEnum getServerVersion() {
return this.serverVersion;
}

public void setServerVersion(final VersionEnum serverVersion) {
this.serverVersion = serverVersion;
}

public VersionEnum getClientVersion() {
return this.clientVersion;
}
Expand Down

This file was deleted.

33 changes: 0 additions & 33 deletions src/main/java/net/raphimc/viaproxy/proxy/EventListener.java

This file was deleted.

Loading

0 comments on commit b24145f

Please sign in to comment.