Skip to content
This repository has been archived by the owner on Apr 7, 2021. It is now read-only.

Commit

Permalink
Allow changing remote address, port and bind port via cli arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeeey committed Apr 29, 2020
1 parent 58288b8 commit 6bfc709
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void onEnable() {
getDataFolder().mkdir();

// Initialize the main proxy class
proxy = new DragonProxy(PlatformType.BUKKIT, getDataFolder(), -1);
proxy = new DragonProxy(PlatformType.BUKKIT, getDataFolder());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public static void main(String[] args) {
OptionParser optionParser = new OptionParser();
optionParser.accepts("version", "Displays the proxy version");
OptionSpec<String> bedrockPortOption = optionParser.accepts("bedrockPort", "Overrides the bedrock UDP bind port").withRequiredArg();
OptionSpec<String> remoteAddressOption = optionParser.accepts("remoteAddress", "Overrides the remote address in the config").withRequiredArg();
OptionSpec<String> remotePortOption = optionParser.accepts("remotePort", "Overrides the remote port in the config").withRequiredArg();
optionParser.accepts("help", "Display help/usage information").forHelp();

// Handle command-line options
Expand All @@ -53,8 +55,10 @@ public static void main(String[] args) {
}

int bedrockPort = options.has(bedrockPortOption) ? Integer.parseInt(options.valueOf(bedrockPortOption)) : -1;
String remoteAddress = options.has(remoteAddressOption) ? options.valueOf(remoteAddressOption) : null;
int remotePort = options.has(remotePortOption) ? Integer.parseInt(options.valueOf(remotePortOption)) : -1;

DragonProxy proxy = new DragonProxy(PlatformType.STANDALONE, new File("."), bedrockPort);
DragonProxy proxy = new DragonProxy(PlatformType.STANDALONE, new File("."), bedrockPort, remoteAddress, remotePort);

Runtime.getRuntime().addShutdownHook(new Thread(proxy::shutdown, "Shutdown thread"));
}
Expand Down
29 changes: 28 additions & 1 deletion proxy/src/main/java/org/dragonet/proxy/DragonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,38 @@ public class DragonProxy {

private long startTime;
private int bindPort;
private String remoteAddress;
private int remotePort;

@Getter
private File dataFolder;
@Getter
private PlatformType platformType;

/**
* Constructs a new instance of the DragonProxy class.
* This is the main class for the proxy (although the command line option parsing is in the `bootstrap` module)
*/
public DragonProxy(PlatformType type, File dataPath) {
this(type, dataPath, -1, null, -1);
}

/**
* Constructs a new instance of the DragonProxy class.
* This is the main class for the proxy (although the command line option parsing is in the `bootstrap` module)
*
* @param bedrockPort a custom port provided from a command line option
* to override the bind port in the config
*/
public DragonProxy(PlatformType type, File dataPath, int bedrockPort) {
public DragonProxy(PlatformType type, File dataPath, int bedrockPort, String remoteAddress, int remotePort) {
INSTANCE = this;

startTime = System.currentTimeMillis();
platformType = type;
dataFolder = dataPath;
bindPort = bedrockPort;
this.remoteAddress = remoteAddress;
this.remotePort = remotePort;

log.info("Welcome to DragonProxy version " + getVersion());

Expand Down Expand Up @@ -162,6 +175,8 @@ private void initialize() throws IOException {
return;
}

handleConfigOverrides();

if(!RELEASE) {
log.warn("This is a development build. It may contain bugs. Do not use in production.");
}
Expand Down Expand Up @@ -223,6 +238,18 @@ private void initialize() throws IOException {
}
}

private void handleConfigOverrides() {
if(bindPort != -1) {
configuration.setBindPort(bindPort);
}
if(remoteAddress != null) {
configuration.setRemoteAddress(remoteAddress);
}
if(remotePort != -1) {
configuration.setRemotePort(remotePort);
}
}

public void shutdown() {
if (!shutdownInProgress.compareAndSet(false, true)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.dragonet.proxy.remote.RemoteAuthType;

@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class DragonConfiguration {
@JsonProperty("config-version")
Expand Down

0 comments on commit 6bfc709

Please sign in to comment.