UPnP Port Forwarding (#489)

Co-authored-by: CyberFlame <cyberflameu@gmail.com>
Co-authored-by: granny <granny@pl3x.net>
This commit is contained in:
omega24
2021-10-02 18:53:46 -04:00
committed by GitHub
parent 2e68181235
commit 3e188aac86
2 changed files with 87 additions and 4 deletions

View File

@@ -5,7 +5,7 @@ Subject: [PATCH] Rebrand
diff --git a/build.gradle.kts b/build.gradle.kts
index c1e70d5f127804deabcf626b725390863e896d38..28566e02511fa5542aa1090a15d0ca00a3bef86c 100644
index c1e70d5f127804deabcf626b725390863e896d38..b89c6db4dccc275924c408633061361e36f9db0a 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -32,7 +32,7 @@ repositories {
@@ -17,15 +17,16 @@ index c1e70d5f127804deabcf626b725390863e896d38..28566e02511fa5542aa1090a15d0ca00
implementation("io.papermc.paper:paper-mojangapi:1.17.1-R0.1-SNAPSHOT") // Airplane
// Paper start
implementation("org.jline:jline-terminal-jansi:3.12.1")
@@ -64,6 +64,7 @@ dependencies {
@@ -64,6 +64,8 @@ dependencies {
implementation("co.aikar:cleaner:1.0-SNAPSHOT") // Paper
implementation("io.netty:netty-all:4.1.65.Final") // Paper
+ implementation("cat.inspiracio:rhino-js-engine:1.7.7.1") // Purpur
+ implementation("dev.omega24:upnp4j:1.0") // Purpur
implementation("org.quiltmc:tiny-mappings-parser:0.3.0") // Paper - needed to read mappings for stacktrace deobfuscation
implementation("com.velocitypowered:velocity-native:1.1.0-SNAPSHOT") // Paper
@@ -92,8 +93,7 @@ tasks.jar {
@@ -92,8 +94,7 @@ tasks.jar {
attributes(
"Main-Class" to "org.bukkit.craftbukkit.Main",
"Implementation-Title" to "CraftBukkit",
@@ -35,7 +36,7 @@ index c1e70d5f127804deabcf626b725390863e896d38..28566e02511fa5542aa1090a15d0ca00
"Implementation-Vendor" to date, // Paper
"Specification-Title" to "Bukkit",
"Specification-Version" to project.version,
@@ -234,7 +234,7 @@ tasks.test {
@@ -234,7 +235,7 @@ tasks.test {
fun TaskContainer.registerRunTask(
name: String, block: JavaExec.() -> Unit
): TaskProvider<JavaExec> = register<JavaExec>(name) {

View File

@@ -0,0 +1,82 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <Blake.Galbreath@GMail.com>
Date: Wed, 22 Jan 2020 20:13:40 -0600
Subject: [PATCH] UPnP Port Forwarding
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index ba6b329334f2c2c1c9149c51ccfb707eec0e29bd..90ee25e5ec25a1adae6d3d50607cf5ba2de8ca49 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -311,6 +311,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
public final SlackActivityAccountant slackActivityAccountant = new SlackActivityAccountant();
// Spigot end
public static long currentTickLong = 0L; // Paper
+ protected boolean upnp = false; // Purpur
public volatile Thread shutdownThread; // Paper
public volatile boolean abnormalExit = false; // Paper
@@ -1042,6 +1043,14 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
// CraftBukkit end
MinecraftServer.LOGGER.info("Stopping server");
MinecraftTimings.stopServer(); // Paper
+ // Purpur start
+ if (upnp) {
+ if (dev.omega24.upnp4j.UPnP4J.close(this.getPort(), dev.omega24.upnp4j.util.Protocol.TCP)) {
+ LOGGER.info("[UPnP] Port {} closed", this.getPort());
+ } else {
+ LOGGER.error("[UPnP] Failed to close port {}", this.getPort());
+ }
+ }
// CraftBukkit start
if (this.server != null) {
this.server.disablePlugins();
diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
index 79a9e8dd970b10ceb03d814224a568a9331208c9..a9ba71573e4e3dfadaf3212400ffd023deada042 100644
--- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
+++ b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
@@ -281,6 +281,30 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface
DedicatedServer.LOGGER.warn("Perhaps a server is already running on that port?");
return false;
}
+ // Purpur start
+ if (net.pl3x.purpur.PurpurConfig.useUPnP) {
+ LOGGER.info("[UPnP] Attempting to start UPnP port forwarding service...");
+ if (dev.omega24.upnp4j.UPnP4J.isUPnPAvailable()) {
+ if (dev.omega24.upnp4j.UPnP4J.isOpen(this.getPort(), dev.omega24.upnp4j.util.Protocol.TCP)) {
+ this.upnp = false;
+ LOGGER.info("[UPnP] Port {} is already open", this.getPort());
+ } else if (dev.omega24.upnp4j.UPnP4J.open(this.getPort(), dev.omega24.upnp4j.util.Protocol.TCP)) {
+ this.upnp = true;
+ LOGGER.info("[UPnP] Successfully opened port {}", this.getPort());
+ } else {
+ this.upnp = false;
+ LOGGER.info("[UPnP] Failed to open port {}", this.getPort());
+ }
+
+ if (upnp) {
+ LOGGER.info("[UPnP] {}:{}", dev.omega24.upnp4j.UPnP4J.getExternalIP(), this.getPort());
+ }
+ } else {
+ this.upnp = false;
+ LOGGER.error("[UPnP] Service is unavailable");
+ }
+ }
+ // Purpur end
// CraftBukkit start
// this.a((PlayerList) (new DedicatedPlayerList(this, this.customRegistry, this.worldNBTStorage))); // Spigot - moved up
diff --git a/src/main/java/net/pl3x/purpur/PurpurConfig.java b/src/main/java/net/pl3x/purpur/PurpurConfig.java
index df12c714aaa2817b653a5fe6278e0dc39aada34d..646cab345fc96d3ebaae49bb61925b8094dc68d9 100644
--- a/src/main/java/net/pl3x/purpur/PurpurConfig.java
+++ b/src/main/java/net/pl3x/purpur/PurpurConfig.java
@@ -398,4 +398,9 @@ public class PurpurConfig {
private static void tpsCatchup() {
tpsCatchup = getBoolean("settings.tps-catchup", tpsCatchup);
}
+
+ public static boolean useUPnP = false;
+ private static void networkSettings() {
+ useUPnP = getBoolean("settings.network.upnp-port-forwarding", useUPnP);
+ }
}