From 3911b6e867ea5d6ab6ce3353ff2761d03ba65848 Mon Sep 17 00:00:00 2001 From: ME1312 Date: Sun, 7 Oct 2018 19:25:08 -0400 Subject: [PATCH] Add UPnP Forwarding Option --- SubServers.Bungee/pom.xml | 6 +++++ .../Bungee/Host/External/ExternalHost.java | 4 +-- .../Bungee/Host/Internal/InternalHost.java | 6 +++++ .../Bungee/Library/Files/config.yml | 4 +++ .../Network/Packet/PacketExAddServer.java | 7 +++-- .../Bungee/Network/SubDataServer.java | 3 +++ .../ME1312/SubServers/Bungee/SubPlugin.java | 27 +++++++++++++------ SubServers.Client/Bukkit/src/plugin.yml | 2 +- .../SubServers/Client/Sponge/SubPlugin.java | 2 +- SubServers.Host/pom.xml | 6 +++++ .../net/ME1312/SubServers/Host/ExHost.java | 8 +++++- .../SubServers/Host/Executable/SubServer.java | 13 ++++++++- .../SubServers/Host/Library/Files/config.yml | 2 ++ .../Network/Packet/PacketExAddServer.java | 6 +++-- .../Network/Packet/PacketExDeleteServer.java | 2 ++ .../Network/Packet/PacketExRemoveServer.java | 3 +++ SubServers.Sync/pom.xml | 6 +++++ .../SubServers/Sync/Library/Files/config.yml | 2 ++ .../net/ME1312/SubServers/Sync/SubPlugin.java | 19 +++++++++++-- 19 files changed, 108 insertions(+), 20 deletions(-) diff --git a/SubServers.Bungee/pom.xml b/SubServers.Bungee/pom.xml index 99e27316..c0c4a989 100644 --- a/SubServers.Bungee/pom.xml +++ b/SubServers.Bungee/pom.xml @@ -29,6 +29,12 @@ 0.8.16 compile + + com.dosse.upnp + WaifUPnP + 1.1 + compile + com.imaginarycode.minecraft RedisBungee diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/External/ExternalHost.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/External/ExternalHost.java index 2c77bc8f..6381e6ba 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/External/ExternalHost.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/External/ExternalHost.java @@ -86,7 +86,7 @@ public class ExternalHost extends Host implements ClientHandler { clean = true; } for (SubServer server : servers.values()) { - client.get().sendPacket(new PacketExAddServer(server.getName(), server.isEnabled(), server.isLogging(), server.getPath(), ((ExternalSubServer) server).exec, server.getStopCommand(), (server.isRunning())?((ExternalSubLogger) server.getLogger()).getExternalAddress():null)); + client.get().sendPacket(new PacketExAddServer(server.getName(), server.isEnabled(), server.getAddress().getPort(), server.isLogging(), server.getPath(), ((ExternalSubServer) server).exec, server.getStopCommand(), (server.isRunning())?((ExternalSubLogger) server.getLogger()).getExternalAddress():null)); } while (queue.size() != 0) { client.get().sendPacket(queue.get(0)); @@ -148,7 +148,7 @@ public class ExternalHost extends Host implements ClientHandler { SubAddServerEvent event = new SubAddServerEvent(player, this, server); plugin.getPluginManager().callEvent(event); if (!event.isCancelled()) { - queue(new PacketExAddServer(name, enabled, log, directory, executable, stopcmd, (server.isRunning())?((ExternalSubLogger) server.getLogger()).getExternalAddress():null)); + queue(new PacketExAddServer(name, enabled, port, log, directory, executable, stopcmd, (server.isRunning())?((ExternalSubLogger) server.getLogger()).getExternalAddress():null)); servers.put(name.toLowerCase(), server); return server; } else { diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Internal/InternalHost.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Internal/InternalHost.java index fbca3894..fcdbe9f1 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Internal/InternalHost.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Internal/InternalHost.java @@ -1,5 +1,6 @@ package net.ME1312.SubServers.Bungee.Host.Internal; +import com.dosse.upnp.UPnP; import net.ME1312.SubServers.Bungee.Event.SubAddServerEvent; import net.ME1312.SubServers.Bungee.Event.SubRemoveServerEvent; import net.ME1312.SubServers.Bungee.Host.Executable; @@ -101,6 +102,7 @@ public class InternalHost extends Host { plugin.getPluginManager().callEvent(event); if (!event.isCancelled()) { servers.put(name.toLowerCase(), server); + if (UPnP.isUPnPAvailable() && plugin.config.get().getSection("Settings").getSection("UPnP", new YAMLSection()).getBoolean("Forward-Servers", false)) UPnP.openPortTCP(port); return server; } else { return null; @@ -118,6 +120,8 @@ public class InternalHost extends Host { getSubServer(server).stop(); getSubServer(server).waitFor(); } + if (UPnP.isUPnPAvailable() && UPnP.isMappedTCP(getSubServer(server).getAddress().getPort())) + UPnP.closePortTCP(getSubServer(server).getAddress().getPort()); servers.remove(server.toLowerCase()); return true; } else return false; @@ -132,6 +136,8 @@ public class InternalHost extends Host { if (getSubServer(server).isRunning()) { getSubServer(server).terminate(); } + if (UPnP.isUPnPAvailable() && UPnP.isMappedTCP(getSubServer(server).getAddress().getPort())) + UPnP.closePortTCP(getSubServer(server).getAddress().getPort()); servers.remove(server.toLowerCase()); return true; } diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Library/Files/config.yml b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Library/Files/config.yml index afb3aab4..8b14a373 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Library/Files/config.yml +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Library/Files/config.yml @@ -2,6 +2,10 @@ Settings: Version: '2.11.2a+' Log-Creator: true Override-Bungee-Commands: true + UPnP: + Forward-Proxy: true + Forward-SubData: false + Forward-Servers: false SubData: Address: '127.0.0.1:4391' Password: 'default' diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Network/Packet/PacketExAddServer.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Network/Packet/PacketExAddServer.java index a9a912d2..cbe4c8f4 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Network/Packet/PacketExAddServer.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Network/Packet/PacketExAddServer.java @@ -19,6 +19,7 @@ public class PacketExAddServer implements PacketIn, PacketOut { private static HashMap[]> callbacks = new HashMap[]>(); private String name; private boolean enabled; + private int port; private boolean log; private String directory; private Executable executable; @@ -41,10 +42,11 @@ public class PacketExAddServer implements PacketIn, PacketOut { * @param executable Executable */ @SafeVarargs - public PacketExAddServer(String name, boolean enabled, boolean log, String directory, Executable executable, String stopcmd, UUID running, Callback... callback) { + public PacketExAddServer(String name, boolean enabled, int port, boolean log, String directory, Executable executable, String stopcmd, UUID running, Callback... callback) { if (Util.isNull(name, enabled, log, directory, executable, callback)) throw new NullPointerException(); this.name = name; this.enabled = enabled; + this.port = port; this.log = log; this.directory = directory; this.executable = executable; @@ -61,6 +63,7 @@ public class PacketExAddServer implements PacketIn, PacketOut { YAMLSection server = new YAMLSection(); server.set("name", name); server.set("enabled", enabled); + server.set("port", port); server.set("log", log); server.set("dir", directory); server.set("exec", executable.toString()); @@ -78,6 +81,6 @@ public class PacketExAddServer implements PacketIn, PacketOut { @Override public Version getVersion() { - return new Version("2.11.0a"); + return new Version("2.13.1b"); } } \ No newline at end of file diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Network/SubDataServer.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Network/SubDataServer.java index b2130c0e..152bf833 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Network/SubDataServer.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Network/SubDataServer.java @@ -1,5 +1,6 @@ package net.ME1312.SubServers.Bungee.Network; +import com.dosse.upnp.UPnP; import com.google.gson.Gson; import net.ME1312.SubServers.Bungee.Event.SubNetworkConnectEvent; import net.ME1312.SubServers.Bungee.Event.SubNetworkDisconnectEvent; @@ -56,6 +57,7 @@ public final class SubDataServer { server = new ServerSocket(port, MAX_QUEUE, address); allowConnection(address.getHostAddress()); } + if (UPnP.isUPnPAvailable() && plugin.config.get().getSection("Settings").getSection("UPnP", new YAMLSection()).getBoolean("Forward-SubData", false)) UPnP.openPortTCP(port); this.plugin = plugin; this.cipher = (cipher != null)?cipher:new Cipher() { @Override @@ -478,6 +480,7 @@ public final class SubDataServer { removeClient((Client) clients.values().toArray()[0]); } server.close(); + if (UPnP.isUPnPAvailable() && UPnP.isMappedTCP(server.getLocalPort())) UPnP.closePortTCP(server.getLocalPort()); System.out.println("SubServers > The SubData Listener has been closed"); plugin.subdata = null; } diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/SubPlugin.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/SubPlugin.java index 876c484f..65387d8a 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/SubPlugin.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/SubPlugin.java @@ -1,5 +1,6 @@ package net.ME1312.SubServers.Bungee; +import com.dosse.upnp.UPnP; import com.google.gson.Gson; import net.ME1312.SubServers.Bungee.Event.SubAddProxyEvent; import net.ME1312.SubServers.Bungee.Event.SubRemoveProxyEvent; @@ -11,24 +12,19 @@ import net.ME1312.SubServers.Bungee.Library.Config.YAMLSection; import net.ME1312.SubServers.Bungee.Library.Exception.InvalidHostException; import net.ME1312.SubServers.Bungee.Library.Exception.InvalidServerException; import net.ME1312.SubServers.Bungee.Library.Version.Version; -import net.ME1312.SubServers.Bungee.Library.Version.VersionType; import net.ME1312.SubServers.Bungee.Network.Cipher; import net.ME1312.SubServers.Bungee.Network.ClientHandler; import net.ME1312.SubServers.Bungee.Network.Packet.PacketOutReload; import net.ME1312.SubServers.Bungee.Network.SubDataServer; import net.md_5.bungee.BungeeCord; import net.md_5.bungee.BungeeServerInfo; +import net.md_5.bungee.api.config.ListenerInfo; import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.event.ServerConnectEvent; import net.md_5.bungee.api.event.ServerKickEvent; import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.event.EventHandler; -import org.w3c.dom.Document; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.xml.sax.InputSource; -import javax.xml.parsers.DocumentBuilderFactory; import java.io.*; import java.lang.reflect.InvocationTargetException; import java.math.BigInteger; @@ -61,7 +57,7 @@ public final class SubPlugin extends BungeeCord implements Listener { public final SubAPI api = new SubAPI(this); public SubDataServer subdata = null; public SubServer sudo = null; - public static final Version version = Version.fromString("2.13.1a"); + public static final Version version = Version.fromString("2.13.1b"); public Proxy redis = null; public boolean canSudo = false; @@ -519,6 +515,13 @@ public final class SubPlugin extends BungeeCord implements Listener { for (Server server : api.getServers().values()) if (server.getSubData() != null) server.getSubData().sendPacket(new PacketOutReload(null)); } + if (UPnP.isUPnPAvailable()) { + if (config.get().getSection("Settings").getSection("UPnP", new YAMLSection()).getBoolean("Forward-Proxy", true)) for (ListenerInfo listener : getConfig().getListeners()) { + UPnP.openPortTCP(listener.getHost().getPort()); + } + } else { + getLogger().warning("UPnP is currently unavailable; Ports may not be automatically forwarded on this device"); + } System.out.println("SubServers > " + ((plugins > 0)?plugins+" Plugin"+((plugins == 1)?"":"s")+", ":"") + ((proxies > 1)?proxies+" Proxies, ":"") + hosts + " Host"+((hosts == 1)?"":"s")+", " + servers + " Server"+((servers == 1)?"":"s")+", and " + subservers + " SubServer"+((subservers == 1)?"":"s")+" "+((status)?"re":"")+"loaded in " + new DecimalFormat("0.000").format((Calendar.getInstance().getTime().getTime() - begin) / 1000D) + "s"); } @@ -592,6 +595,10 @@ public final class SubPlugin extends BungeeCord implements Listener { shutdown(); subdata.destroy(); + + for (ListenerInfo listener : getConfig().getListeners()) { + if (UPnP.isUPnPAvailable() && UPnP.isMappedTCP(listener.getHost().getPort())) UPnP.closePortTCP(listener.getHost().getPort()); + } } catch (Exception e) { e.printStackTrace(); } @@ -614,6 +621,10 @@ public final class SubPlugin extends BungeeCord implements Listener { getPluginManager().callEvent(new SubRemoveProxyEvent(proxies.get(proxy))); } proxies.clear(); + + for (ListenerInfo listener : getConfig().getListeners()) { + if (UPnP.isUPnPAvailable() && UPnP.isMappedTCP(listener.getHost().getPort())) UPnP.closePortTCP(listener.getHost().getPort()); + } } String getNewSignature() { @@ -709,7 +720,7 @@ public final class SubPlugin extends BungeeCord implements Listener { } @SuppressWarnings("deprecation") - @EventHandler(priority = Byte.MAX_VALUE) + @EventHandler(priority = Byte.MIN_VALUE) public void fallback(ServerKickEvent e) { if (e.getPlayer().getPendingConnection().getListener().isForceDefault()) { int i = 0; diff --git a/SubServers.Client/Bukkit/src/plugin.yml b/SubServers.Client/Bukkit/src/plugin.yml index ea36679a..23a5a172 100644 --- a/SubServers.Client/Bukkit/src/plugin.yml +++ b/SubServers.Client/Bukkit/src/plugin.yml @@ -1,6 +1,6 @@ name: 'SubServers-Client-Bukkit' main: 'net.ME1312.SubServers.Client.Bukkit.SubPlugin' -version: '2.13.1a' +version: '2.13.1b' authors: [ME1312] softdepend: [Vault, TitleManager] website: 'https://github.com/ME1312/SubServers-2' diff --git a/SubServers.Client/Sponge/src/net/ME1312/SubServers/Client/Sponge/SubPlugin.java b/SubServers.Client/Sponge/src/net/ME1312/SubServers/Client/Sponge/SubPlugin.java index 694df15e..dd78173a 100644 --- a/SubServers.Client/Sponge/src/net/ME1312/SubServers/Client/Sponge/SubPlugin.java +++ b/SubServers.Client/Sponge/src/net/ME1312/SubServers/Client/Sponge/SubPlugin.java @@ -46,7 +46,7 @@ import java.util.concurrent.TimeUnit; /** * SubServers Client Plugin Class */ -@Plugin(id = "subservers-client-sponge", name = "SubServers-Client-Sponge", authors = "ME1312", version = "2.13.1a", url = "https://github.com/ME1312/SubServers-2", description = "Access your SubServers from Anywhere") +@Plugin(id = "subservers-client-sponge", name = "SubServers-Client-Sponge", authors = "ME1312", version = "2.13.1b", url = "https://github.com/ME1312/SubServers-2", description = "Access your SubServers from Anywhere") public final class SubPlugin { protected NamedContainer>> lang = null; public YAMLConfig config; diff --git a/SubServers.Host/pom.xml b/SubServers.Host/pom.xml index 76213809..1669de7a 100644 --- a/SubServers.Host/pom.xml +++ b/SubServers.Host/pom.xml @@ -29,6 +29,12 @@ 0.8.16 compile + + com.dosse.upnp + WaifUPnP + 1.1 + compile + diff --git a/SubServers.Host/src/net/ME1312/SubServers/Host/ExHost.java b/SubServers.Host/src/net/ME1312/SubServers/Host/ExHost.java index a8b2a047..9d712988 100644 --- a/SubServers.Host/src/net/ME1312/SubServers/Host/ExHost.java +++ b/SubServers.Host/src/net/ME1312/SubServers/Host/ExHost.java @@ -1,5 +1,6 @@ package net.ME1312.SubServers.Host; +import com.dosse.upnp.UPnP; import net.ME1312.Galaxi.Engine.GalaxiEngine; import net.ME1312.Galaxi.Library.Config.YAMLConfig; import net.ME1312.Galaxi.Library.Config.YAMLSection; @@ -32,7 +33,7 @@ import java.util.jar.Manifest; /** * SubServers.Host Main Class */ -@Plugin(name = "SubServers.Host", version = "2.13.1a", authors = "ME1312", description = "Host SubServers from other Machines", website = "https://github.com/ME1312/SubServers-2") +@Plugin(name = "SubServers.Host", version = "2.13.1b", authors = "ME1312", description = "Host SubServers from other Machines", website = "https://github.com/ME1312/SubServers-2") public final class ExHost { protected NamedContainer>> lang = null; public HashMap templates = new HashMap(); @@ -209,6 +210,10 @@ public final class ExHost { }); engine.start(this::stop); + + if (!UPnP.isUPnPAvailable()) { + log.warn.println("UPnP is currently unavailable; Ports may not be automatically forwarded on this device"); + } } catch (Exception e) { log.error.println(e); stop(); @@ -256,6 +261,7 @@ public final class ExHost { } catch (Exception e) { log.error.println(e); } + if (UPnP.isUPnPAvailable() && UPnP.isMappedTCP(servers.get(server).getPort())) UPnP.closePortTCP(servers.get(server).getPort()); } servers.clear(); diff --git a/SubServers.Host/src/net/ME1312/SubServers/Host/Executable/SubServer.java b/SubServers.Host/src/net/ME1312/SubServers/Host/Executable/SubServer.java index b2d25976..622ab6fd 100644 --- a/SubServers.Host/src/net/ME1312/SubServers/Host/Executable/SubServer.java +++ b/SubServers.Host/src/net/ME1312/SubServers/Host/Executable/SubServer.java @@ -22,6 +22,7 @@ public class SubServer { private ExHost host; private String name; private boolean enabled; + private int port; private Container log; private String dir; private File directory; @@ -46,11 +47,12 @@ public class SubServer { * @param stopcmd Stop Command * @throws InvalidServerException */ - public SubServer(ExHost host, String name, boolean enabled, boolean log, String directory, Executable executable, String stopcmd) throws InvalidServerException { + public SubServer(ExHost host, String name, boolean enabled, int port, boolean log, String directory, Executable executable, String stopcmd) throws InvalidServerException { if (Util.isNull(host, name, enabled, log, directory, executable)) throw new NullPointerException(); this.host = host; this.name = name; this.enabled = enabled; + this.port = port; this.log = new Container(log); this.dir = directory; this.directory = new File(host.host.getRawString("Directory"), directory); @@ -237,6 +239,15 @@ public class SubServer { enabled = value; } + /** + * Get the Port of the Server + * + * @return Server Port Number + */ + public int getPort() { + return port; + } + /** * If the Server is Logging * diff --git a/SubServers.Host/src/net/ME1312/SubServers/Host/Library/Files/config.yml b/SubServers.Host/src/net/ME1312/SubServers/Host/Library/Files/config.yml index 98796dde..5ff70930 100644 --- a/SubServers.Host/src/net/ME1312/SubServers/Host/Library/Files/config.yml +++ b/SubServers.Host/src/net/ME1312/SubServers/Host/Library/Files/config.yml @@ -3,6 +3,8 @@ Settings: Console-Log: true Network-Log: true Server-Bind: '127.0.0.1' + UPnP: + Forward-Servers: false SubData: Name: 'undefined' Address: '127.0.0.1:4391' diff --git a/SubServers.Host/src/net/ME1312/SubServers/Host/Network/Packet/PacketExAddServer.java b/SubServers.Host/src/net/ME1312/SubServers/Host/Network/Packet/PacketExAddServer.java index fff66eae..b62c3fff 100644 --- a/SubServers.Host/src/net/ME1312/SubServers/Host/Network/Packet/PacketExAddServer.java +++ b/SubServers.Host/src/net/ME1312/SubServers/Host/Network/Packet/PacketExAddServer.java @@ -1,5 +1,6 @@ package net.ME1312.SubServers.Host.Network.Packet; +import com.dosse.upnp.UPnP; import net.ME1312.Galaxi.Library.Config.YAMLSection; import net.ME1312.Galaxi.Library.Log.Logger; import net.ME1312.Galaxi.Library.Util; @@ -69,9 +70,10 @@ public class PacketExAddServer implements PacketIn, PacketOut { if (host.servers.keySet().contains(data.getSection("server").getRawString("name").toLowerCase())) { host.subdata.sendPacket(new PacketExAddServer(0, "Server Already Added", (data.contains("id"))?data.getRawString("id"):null)); } else { - SubServer server = new SubServer(host, data.getSection("server").getRawString("name"), data.getSection("server").getBoolean("enabled"), data.getSection("server").getBoolean("log"), + SubServer server = new SubServer(host, data.getSection("server").getRawString("name"), data.getSection("server").getBoolean("enabled"), data.getSection("server").getInt("port"), data.getSection("server").getBoolean("log"), data.getSection("server").getRawString("dir"), new Executable(data.getSection("server").getRawString("exec")), data.getSection("server").getRawString("stopcmd")); host.servers.put(data.getSection("server").getRawString("name").toLowerCase(), server); + if (UPnP.isUPnPAvailable() && host.config.get().getSection("Settings").getSection("UPnP", new YAMLSection()).getBoolean("Forward-Servers", false)) UPnP.openPortTCP(server.getPort()); log.info.println("Added SubServer: " + data.getSection("server").getRawString("name")); if (data.getSection("server").contains("running")) server.start(UUID.fromString(data.getSection("server").getRawString("running"))); host.subdata.sendPacket(new PacketExAddServer(0, "Server Added Successfully", (data.contains("id"))?data.getRawString("id"):null)); @@ -84,6 +86,6 @@ public class PacketExAddServer implements PacketIn, PacketOut { @Override public Version getVersion() { - return new Version("2.11.0a"); + return new Version("2.13.1b"); } } \ No newline at end of file diff --git a/SubServers.Host/src/net/ME1312/SubServers/Host/Network/Packet/PacketExDeleteServer.java b/SubServers.Host/src/net/ME1312/SubServers/Host/Network/Packet/PacketExDeleteServer.java index 4a253ee4..2779464c 100644 --- a/SubServers.Host/src/net/ME1312/SubServers/Host/Network/Packet/PacketExDeleteServer.java +++ b/SubServers.Host/src/net/ME1312/SubServers/Host/Network/Packet/PacketExDeleteServer.java @@ -1,5 +1,6 @@ package net.ME1312.SubServers.Host.Network.Packet; +import com.dosse.upnp.UPnP; import net.ME1312.Galaxi.Engine.GalaxiEngine; import net.ME1312.Galaxi.Library.Config.YAMLSection; import net.ME1312.Galaxi.Library.Log.Logger; @@ -102,6 +103,7 @@ public class PacketExDeleteServer implements PacketIn, PacketOut { } catch (Exception e) { e.printStackTrace(); } + if (UPnP.isUPnPAvailable() && UPnP.isMappedTCP(server.getPort())) UPnP.closePortTCP(server.getPort()); log.info.println("Deleted SubServer: " + data.getRawString("server")); host.subdata.sendPacket(new PacketExDeleteServer(0, "Server Deleted Successfully", (data.contains("id"))?data.getRawString("id"):null)); }).start(); diff --git a/SubServers.Host/src/net/ME1312/SubServers/Host/Network/Packet/PacketExRemoveServer.java b/SubServers.Host/src/net/ME1312/SubServers/Host/Network/Packet/PacketExRemoveServer.java index b9a1d706..c517d57f 100644 --- a/SubServers.Host/src/net/ME1312/SubServers/Host/Network/Packet/PacketExRemoveServer.java +++ b/SubServers.Host/src/net/ME1312/SubServers/Host/Network/Packet/PacketExRemoveServer.java @@ -1,5 +1,6 @@ package net.ME1312.SubServers.Host.Network.Packet; +import com.dosse.upnp.UPnP; import net.ME1312.Galaxi.Library.Config.YAMLSection; import net.ME1312.Galaxi.Library.Log.Logger; import net.ME1312.Galaxi.Library.Util; @@ -68,6 +69,8 @@ public class PacketExRemoveServer implements PacketIn, PacketOut { } else if (host.servers.get(data.getRawString("server").toLowerCase()).isRunning()) { host.subdata.sendPacket(new PacketExRemoveServer(2, "That server is still running.", (data.contains("id"))?data.getRawString("id"):null)); } else { + if (UPnP.isUPnPAvailable() && UPnP.isMappedTCP(host.servers.get(data.getRawString("server").toLowerCase()).getPort())) + UPnP.closePortTCP(host.servers.get(data.getRawString("server").toLowerCase()).getPort()); host.servers.remove(data.getRawString("server").toLowerCase()); log.info.println("Removed SubServer: " + data.getRawString("server")); host.subdata.sendPacket(new PacketExRemoveServer(0, "Server Removed Successfully", (data.contains("id"))?data.getRawString("id"):null)); diff --git a/SubServers.Sync/pom.xml b/SubServers.Sync/pom.xml index 4939f3c8..ce574f40 100644 --- a/SubServers.Sync/pom.xml +++ b/SubServers.Sync/pom.xml @@ -37,6 +37,12 @@ 0.8.16 compile + + com.dosse.upnp + WaifUPnP + 1.1 + compile + com.imaginarycode.minecraft RedisBungee diff --git a/SubServers.Sync/src/net/ME1312/SubServers/Sync/Library/Files/config.yml b/SubServers.Sync/src/net/ME1312/SubServers/Sync/Library/Files/config.yml index 5eb4ecf7..1d757a70 100644 --- a/SubServers.Sync/src/net/ME1312/SubServers/Sync/Library/Files/config.yml +++ b/SubServers.Sync/src/net/ME1312/SubServers/Sync/Library/Files/config.yml @@ -1,6 +1,8 @@ Settings: Version: '2.11.2a+' Override-Bungee-Commands: true + UPnP: + Forward-Proxy: true SubData: Address: '127.0.0.1:4391' Password: 'default' diff --git a/SubServers.Sync/src/net/ME1312/SubServers/Sync/SubPlugin.java b/SubServers.Sync/src/net/ME1312/SubServers/Sync/SubPlugin.java index 3538c534..b0c8c9ab 100644 --- a/SubServers.Sync/src/net/ME1312/SubServers/Sync/SubPlugin.java +++ b/SubServers.Sync/src/net/ME1312/SubServers/Sync/SubPlugin.java @@ -1,5 +1,6 @@ package net.ME1312.SubServers.Sync; +import com.dosse.upnp.UPnP; import com.google.gson.Gson; import net.ME1312.SubServers.Sync.Event.*; import net.ME1312.SubServers.Sync.Library.Config.YAMLConfig; @@ -16,6 +17,7 @@ import net.ME1312.SubServers.Sync.Network.SubDataClient; import net.ME1312.SubServers.Sync.Server.Server; import net.ME1312.SubServers.Sync.Server.SubServer; import net.md_5.bungee.BungeeCord; +import net.md_5.bungee.api.config.ListenerInfo; import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.event.ServerConnectEvent; import net.md_5.bungee.api.event.ServerKickEvent; @@ -50,7 +52,7 @@ public final class SubPlugin extends BungeeCord implements Listener { public boolean redis = false; public final SubAPI api = new SubAPI(this); public SubDataClient subdata = null; - public static final Version version = Version.fromString("2.13.1a"); + public static final Version version = Version.fromString("2.13.1b"); public final boolean isPatched; public long lastReload = -1; @@ -110,6 +112,15 @@ public final class SubPlugin extends BungeeCord implements Listener { Integer.parseInt(config.get().getSection("Settings").getSection("SubData").getRawString("Address", "127.0.0.1:4391").split(":")[1]), cipher); super.startListeners(); + + if (UPnP.isUPnPAvailable()) { + if (config.get().getSection("Settings").getSection("UPnP", new YAMLSection()).getBoolean("Forward-Proxy", true)) for (ListenerInfo listener : getConfig().getListeners()) { + UPnP.openPortTCP(listener.getHost().getPort()); + } + } else { + getLogger().warning("UPnP is currently unavailable; Ports may not be automatically forwarded on this device"); + } + if (!posted) { posted = true; post(); @@ -224,6 +235,10 @@ public final class SubPlugin extends BungeeCord implements Listener { servers.clear(); subdata.destroy(0); + + for (ListenerInfo listener : getConfig().getListeners()) { + if (UPnP.isUPnPAvailable() && UPnP.isMappedTCP(listener.getHost().getPort())) UPnP.closePortTCP(listener.getHost().getPort()); + } } catch (Exception e) { e.printStackTrace(); } @@ -245,7 +260,7 @@ public final class SubPlugin extends BungeeCord implements Listener { } @SuppressWarnings("deprecation") - @EventHandler(priority = Byte.MAX_VALUE) + @EventHandler(priority = Byte.MIN_VALUE) public void fallback(ServerKickEvent e) { if (e.getPlayer().getPendingConnection().getListener().isForceDefault()) { int i = 0;