Add UPnP Forwarding Option

This commit is contained in:
ME1312 2018-10-07 19:25:08 -04:00
parent e5ba821754
commit 3911b6e867
No known key found for this signature in database
GPG Key ID: FEFFE2F698E88FA8
19 changed files with 108 additions and 20 deletions

View File

@ -29,6 +29,12 @@
<version>0.8.16</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.dosse.upnp</groupId>
<artifactId>WaifUPnP</artifactId>
<version>1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.imaginarycode.minecraft</groupId>
<artifactId>RedisBungee</artifactId>

View File

@ -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 {

View File

@ -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;
}

View File

@ -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'

View File

@ -19,6 +19,7 @@ public class PacketExAddServer implements PacketIn, PacketOut {
private static HashMap<String, Callback<YAMLSection>[]> callbacks = new HashMap<String, Callback<YAMLSection>[]>();
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<YAMLSection>... callback) {
public PacketExAddServer(String name, boolean enabled, int port, boolean log, String directory, Executable executable, String stopcmd, UUID running, Callback<YAMLSection>... 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");
}
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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'

View File

@ -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<Long, Map<String, Map<String, String>>> lang = null;
public YAMLConfig config;

View File

@ -29,6 +29,12 @@
<version>0.8.16</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.dosse.upnp</groupId>
<artifactId>WaifUPnP</artifactId>
<version>1.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>

View File

@ -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<Long, Map<String, Map<String, String>>> lang = null;
public HashMap<String, SubCreator.ServerTemplate> templates = new HashMap<String, SubCreator.ServerTemplate>();
@ -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();

View File

@ -22,6 +22,7 @@ public class SubServer {
private ExHost host;
private String name;
private boolean enabled;
private int port;
private Container<Boolean> 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<Boolean>(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
*

View File

@ -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'

View File

@ -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");
}
}

View File

@ -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();

View File

@ -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));

View File

@ -37,6 +37,12 @@
<version>0.8.16</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.dosse.upnp</groupId>
<artifactId>WaifUPnP</artifactId>
<version>1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.imaginarycode.minecraft</groupId>
<artifactId>RedisBungee</artifactId>

View File

@ -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'

View File

@ -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;