Search in sources :

Example 6 with Template

use of de.dytanic.cloudnet.lib.server.template.Template in project CloudNet by Dytanic.

the class CommandCloudDev method execute.

@Override
public void execute(CommandSender commandSender, String[] args) {
    if (!(commandSender instanceof ProxiedPlayer))
        return;
    switch(args.length) {
        case 0:
            for (ServerInfo serverInfo : CloudProxy.getInstance().getCachedServers().values()) {
                if (serverInfo.getServerConfig().getProperties().contains(NetworkUtils.DEV_PROPERTY)) {
                    DevService service = serverInfo.getServerConfig().getProperties().getObject(NetworkUtils.DEV_PROPERTY, DevService.class);
                    if (service.getUniqueId().equals(((ProxiedPlayer) commandSender).getUniqueId())) {
                        ((ProxiedPlayer) commandSender).connect(ProxyServer.getInstance().getServerInfo(serverInfo.getServiceId().getServerId()));
                        return;
                    }
                }
            }
            commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "/cdev forTemplate <group> <template>");
            commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "/cdev forTemplate <group>");
            commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "/cdev testServer <group>");
            commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "/cdev testServer <group> <template>");
            break;
        case 2:
            {
                for (ServerInfo serverInfo : CloudProxy.getInstance().getCachedServers().values()) {
                    if (serverInfo.getServerConfig().getProperties().contains(NetworkUtils.DEV_PROPERTY)) {
                        DevService service = serverInfo.getServerConfig().getProperties().getObject(NetworkUtils.DEV_PROPERTY, DevService.class);
                        if (service.getUniqueId().equals(((ProxiedPlayer) commandSender).getUniqueId())) {
                            ((ProxiedPlayer) commandSender).connect(ProxyServer.getInstance().getServerInfo(serverInfo.getServiceId().getServerId()));
                            return;
                        }
                    }
                }
                if (args[0].equalsIgnoreCase("forTemplate")) {
                    if (!CloudAPI.getInstance().getServerGroupMap().containsKey(args[1])) {
                        commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The group doesn't exist");
                        return;
                    }
                    ServerConfig serverConfig = new ServerConfig(true, commandSender.getName(), new Document(NetworkUtils.DEV_PROPERTY, new DevService(((ProxiedPlayer) commandSender).getUniqueId(), true)), System.currentTimeMillis());
                    CloudAPI.getInstance().startGameServer(CloudAPI.getInstance().getServerGroupData(args[1]), serverConfig, "Dev" + args[1] + "-" + commandSender.getName());
                    ((ProxiedPlayer) commandSender).sendMessage(CloudAPI.getInstance().getPrefix() + "The server will start up now...");
                    return;
                }
                if (args[0].equalsIgnoreCase("testServer")) {
                    if (!CloudAPI.getInstance().getServerGroupMap().containsKey(args[1])) {
                        commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The group doesn't exist");
                        return;
                    }
                    ServerConfig serverConfig = new ServerConfig(true, commandSender.getName(), new Document(), System.currentTimeMillis());
                    CloudAPI.getInstance().startCloudServer("TestServer-" + ((ProxiedPlayer) commandSender).getUniqueId(), serverConfig, 356, true);
                    ((ProxiedPlayer) commandSender).sendMessage(CloudAPI.getInstance().getPrefix() + "The server will start up now...");
                    return;
                }
            }
        case 3:
            {
                for (ServerInfo serverInfo : CloudProxy.getInstance().getCachedServers().values()) {
                    if (serverInfo.getServerConfig().getProperties().contains(NetworkUtils.DEV_PROPERTY)) {
                        DevService service = serverInfo.getServerConfig().getProperties().getObject(NetworkUtils.DEV_PROPERTY, DevService.class);
                        if (service.getUniqueId().equals(((ProxiedPlayer) commandSender).getUniqueId())) {
                            ((ProxiedPlayer) commandSender).connect(ProxyServer.getInstance().getServerInfo(serverInfo.getServiceId().getServerId()));
                            return;
                        }
                    }
                }
                if (args[0].equalsIgnoreCase("forTemplate")) {
                    if (!CloudAPI.getInstance().getServerGroupMap().containsKey(args[1])) {
                        commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The group doesn't exist");
                        return;
                    }
                    ServerGroup serverGroup = CloudAPI.getInstance().getServerGroup(args[1]);
                    if (CollectionWrapper.filter(serverGroup.getTemplates(), new Acceptable<Template>() {

                        @Override
                        public boolean isAccepted(Template template) {
                            return template.getName().equalsIgnoreCase(args[2]);
                        }
                    }) == null) {
                        commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The template doesn't exist");
                        return;
                    }
                    ServerConfig serverConfig = new ServerConfig(true, commandSender.getName(), new Document(NetworkUtils.DEV_PROPERTY, new DevService(((ProxiedPlayer) commandSender).getUniqueId(), true)), System.currentTimeMillis());
                    CloudAPI.getInstance().startGameServer(CloudAPI.getInstance().getServerGroupData(args[1]), serverConfig, CloudAPI.getInstance().getServerGroupData(args[1]).getMemory(), true, new Properties(), CollectionWrapper.filter(serverGroup.getTemplates(), new Acceptable<Template>() {

                        @Override
                        public boolean isAccepted(Template e) {
                            return e.getName().equalsIgnoreCase(args[2]);
                        }
                    }), "Dev" + args[1] + "-" + commandSender.getName());
                    ((ProxiedPlayer) commandSender).sendMessage(CloudAPI.getInstance().getPrefix() + "The server will start up now...");
                    return;
                }
                if (args[0].equalsIgnoreCase("testServer")) {
                    if (!CloudAPI.getInstance().getServerGroupMap().containsKey(args[1])) {
                        commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The group doesn't exist");
                        return;
                    }
                    ServerGroup serverGroup = CloudAPI.getInstance().getServerGroup(args[1]);
                    if (CollectionWrapper.filter(serverGroup.getTemplates(), new Acceptable<Template>() {

                        @Override
                        public boolean isAccepted(Template template) {
                            return template.getName().equalsIgnoreCase(args[2]);
                        }
                    }) == null) {
                        commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The template doesn't exist");
                        return;
                    }
                    ServerConfig serverConfig = new ServerConfig(true, commandSender.getName(), new Document(), System.currentTimeMillis());
                    /*
                    CloudAPI.getInstance().startGameServer(CloudAPI.getInstance().getServerGroupData(args[1]), serverConfig,
                            CloudAPI.getInstance().getServerGroupData(args[1]).getMemory(),
                            new String[0], null, "TestServer-" + commandSender.getName(), false, true, new Properties(),
                            null, Arrays.asList()
                            , "Dev" + args[1] + "-" + commandSender.getName());
                    */
                    CloudAPI.getInstance().startCloudServer("TestServer-" + ((ProxiedPlayer) commandSender).getUniqueId(), serverConfig, 356, true);
                    ((ProxiedPlayer) commandSender).sendMessage(CloudAPI.getInstance().getPrefix() + "The server will start up now...");
                    return;
                }
            }
            break;
    }
}
Also used : ProxiedPlayer(net.md_5.bungee.api.connection.ProxiedPlayer) ServerConfig(de.dytanic.cloudnet.lib.server.ServerConfig) ServerGroup(de.dytanic.cloudnet.lib.server.ServerGroup) ServerInfo(de.dytanic.cloudnet.lib.server.info.ServerInfo) DevService(de.dytanic.cloudnet.lib.server.advanced.DevService) Document(de.dytanic.cloudnet.lib.utility.document.Document) Properties(java.util.Properties) Template(de.dytanic.cloudnet.lib.server.template.Template)

Example 7 with Template

use of de.dytanic.cloudnet.lib.server.template.Template in project CloudNet by Dytanic.

the class Wrapper method getUsedMemoryAndWaitings.

public int getUsedMemoryAndWaitings() {
    AtomicInteger integer = new AtomicInteger(getUsedMemory());
    CollectionWrapper.iterator(this.waitingServices.values(), new Runnabled<Quad<Integer, Integer, ServiceId, Template>>() {

        @Override
        public void run(Quad<Integer, Integer, ServiceId, Template> obj) {
            integer.addAndGet(obj.getSecond());
        }
    });
    return integer.get();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Quad(de.dytanic.cloudnet.lib.utility.Quad) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServiceId(de.dytanic.cloudnet.lib.service.ServiceId) Template(de.dytanic.cloudnet.lib.server.template.Template)

Example 8 with Template

use of de.dytanic.cloudnet.lib.server.template.Template in project CloudNet by Dytanic.

the class CommandCreate method onExecuteCommand.

@Override
public void onExecuteCommand(CommandSender sender, String[] args) {
    if (args.length > 2) {
        if (args[0].equalsIgnoreCase("dispatchCommand")) {
            // create dispatchCommand name create
            StringBuilder builder = new StringBuilder();
            for (short i = 2; i < args.length; i++) {
                builder.append(args[i]);
            }
            CloudNet.getInstance().getDbHandlers().getCommandDispatcherDatabase().appendCommand(args[1], builder.substring(0, (builder.substring(0).endsWith(" ") ? builder.length() - 1 : builder.length())));
            sender.sendMessage("A dispatcher was created \"" + args[1] + "\": \"" + builder.substring(0) + "\"");
            return;
        }
    }
    switch(args.length) {
        case 2:
            if (args[0].equalsIgnoreCase("proxy") || args[0].equalsIgnoreCase("-p")) {
                if (CloudNet.getInstance().getProxyGroups().containsKey(args[1])) {
                    CloudNet.getInstance().startProxy(CloudNet.getInstance().getProxyGroups().get(args[1]));
                    sender.sendMessage("Trying to startup a proxy server...");
                } else {
                    sender.sendMessage("The proxy group doesn't exist");
                }
                return;
            }
            if (args[0].equalsIgnoreCase("server") || args[0].equalsIgnoreCase("-s")) {
                if (CloudNet.getInstance().getServerGroups().containsKey(args[1])) {
                    CloudNet.getInstance().startGameServer(CloudNet.getInstance().getServerGroups().get(args[1]));
                    sender.sendMessage("Trying to startup a game server...");
                } else {
                    sender.sendMessage("The server group doesn't exists");
                }
                return;
            }
            if (args[0].equalsIgnoreCase("wrapper") && !CloudNet.getInstance().getWrappers().containsKey(args[1])) {
                new SetupWrapper(sender, args[1]);
                return;
            }
            if (args[0].equalsIgnoreCase("serverGroup")) {
                new SetupServerGroup(sender, args[1]);
                return;
            }
            if (args[0].equalsIgnoreCase("proxyGroup")) {
                new SetupProxyGroup(sender, args[1]);
                return;
            }
            break;
        case 3:
            if ((args[0].equalsIgnoreCase("proxy") || args[0].equalsIgnoreCase("-p")) && NetworkUtils.checkIsNumber(args[2])) {
                if (CloudNet.getInstance().getProxyGroups().containsKey(args[1])) {
                    for (short i = 0; i < Integer.parseInt(args[2]); i++) {
                        CloudNet.getInstance().startProxy(CloudNet.getInstance().getProxyGroups().get(args[1]));
                        NetworkUtils.sleepUninterruptedly(2000L);
                    }
                    sender.sendMessage("Trying to startup a proxy server...");
                } else {
                    sender.sendMessage("The proxy group doesn't exists");
                }
                return;
            }
            if ((args[0].equalsIgnoreCase("server") || args[0].equalsIgnoreCase("-s")) && NetworkUtils.checkIsNumber(args[2])) {
                if (CloudNet.getInstance().getServerGroups().containsKey(args[1])) {
                    for (short i = 0; i < Integer.parseInt(args[2]); i++) {
                        CloudNet.getInstance().startGameServer(CloudNet.getInstance().getServerGroups().get(args[1]));
                        NetworkUtils.sleepUninterruptedly(2000L);
                    }
                    sender.sendMessage("Trying to startup a game server...");
                } else {
                    sender.sendMessage("The server group doesn't exists");
                }
                return;
            }
            if (args[0].equalsIgnoreCase("user")) {
                if (!CloudNet.getInstance().getUsers().contains(args[1])) {
                    User user = new BasicUser(args[1], args[2], Arrays.asList());
                    CloudNet.getInstance().getUsers().add(user);
                    CloudNet.getInstance().getConfig().save(CloudNet.getInstance().getUsers());
                    sender.sendMessage("The user was created!");
                } else {
                    sender.sendMessage("The user already exists!");
                }
            }
            break;
        case 4:
            if (args[0].equalsIgnoreCase("cloudserver") || args[0].equalsIgnoreCase("cs")) {
                if (NetworkUtils.checkIsNumber(args[2])) {
                    CloudNet.getInstance().startCloudServer(args[1], Integer.parseInt(args[2]), args[3].equalsIgnoreCase("true"));
                    sender.sendMessage("Trying to startup a cloud server...");
                } else {
                    sender.sendMessage("Invalid argument!");
                }
                return;
            }
            if (args[0].equalsIgnoreCase("TEMPLATE")) {
                if (CloudNet.getInstance().getServerGroups().containsKey(args[2])) {
                    if (args[3].equalsIgnoreCase("LOCAL")) {
                        ServerGroup serverGroup = CloudNet.getInstance().getServerGroups().get(args[2]);
                        serverGroup.getTemplates().add(new Template(args[1], TemplateResource.LOCAL, null, new String[] {}, Arrays.asList()));
                        CloudNet.getInstance().getConfig().createGroup(serverGroup);
                        NetworkUtils.addAll(CloudNet.getInstance().getServerGroups(), CloudNet.getInstance().getConfig().getServerGroups(), new Acceptable<ServerGroup>() {

                            @Override
                            public boolean isAccepted(ServerGroup value) {
                                return true;
                            }
                        });
                        NetworkUtils.addAll(CloudNet.getInstance().getProxyGroups(), CloudNet.getInstance().getConfig().getProxyGroups(), new Acceptable<ProxyGroup>() {

                            @Override
                            public boolean isAccepted(ProxyGroup value) {
                                return true;
                            }
                        });
                        CloudNet.getInstance().getWrappers().values().forEach(new Consumer<Wrapper>() {

                            @Override
                            public void accept(Wrapper wrapper) {
                                wrapper.updateWrapper();
                            }
                        });
                        sender.sendMessage("The template was created and all wrappers were updated!");
                    }
                    if (args[3].equalsIgnoreCase("MASTER")) {
                        ServerGroup serverGroup = CloudNet.getInstance().getServerGroups().get(args[2]);
                        serverGroup.getTemplates().add(new Template(args[1], TemplateResource.MASTER, null, new String[] {}, Arrays.asList()));
                        CloudNet.getInstance().getConfig().createGroup(serverGroup);
                        NetworkUtils.addAll(CloudNet.getInstance().getServerGroups(), CloudNet.getInstance().getConfig().getServerGroups(), new Acceptable<ServerGroup>() {

                            @Override
                            public boolean isAccepted(ServerGroup value) {
                                return true;
                            }
                        });
                        NetworkUtils.addAll(CloudNet.getInstance().getProxyGroups(), CloudNet.getInstance().getConfig().getProxyGroups(), new Acceptable<ProxyGroup>() {

                            @Override
                            public boolean isAccepted(ProxyGroup value) {
                                return true;
                            }
                        });
                        CloudNet.getInstance().getWrappers().values().forEach(new Consumer<Wrapper>() {

                            @Override
                            public void accept(Wrapper wrapper) {
                                wrapper.updateWrapper();
                            }
                        });
                        sender.sendMessage("The template was created and all wrappers were updated!");
                    }
                } else {
                    sender.sendMessage("The server group doesn't exist");
                }
            }
            break;
        case 5:
            if (args[0].equalsIgnoreCase("TEMPLATE")) {
                if (CloudNet.getInstance().getServerGroups().containsKey(args[2])) {
                    if (args[3].equalsIgnoreCase("URL")) {
                        ServerGroup serverGroup = CloudNet.getInstance().getServerGroups().get(args[2]);
                        serverGroup.getTemplates().add(new Template(args[1], TemplateResource.URL, args[4], new String[] { ("-Dtest=true") }, Arrays.asList()));
                        CloudNet.getInstance().getConfig().createGroup(serverGroup);
                        NetworkUtils.addAll(CloudNet.getInstance().getServerGroups(), CloudNet.getInstance().getConfig().getServerGroups(), new Acceptable<ServerGroup>() {

                            @Override
                            public boolean isAccepted(ServerGroup value) {
                                return true;
                            }
                        });
                        NetworkUtils.addAll(CloudNet.getInstance().getProxyGroups(), CloudNet.getInstance().getConfig().getProxyGroups(), new Acceptable<ProxyGroup>() {

                            @Override
                            public boolean isAccepted(ProxyGroup value) {
                                return true;
                            }
                        });
                        CloudNet.getInstance().getWrappers().values().forEach(new Consumer<Wrapper>() {

                            @Override
                            public void accept(Wrapper wrapper) {
                                wrapper.updateWrapper();
                            }
                        });
                        sender.sendMessage("The template was created and all wrappers were updated!");
                    }
                } else {
                    sender.sendMessage("The server group doesn't exists");
                }
            }
            break;
        default:
            sender.sendMessage("create PROXY <proxyGroup> <count> | Creates a proxy server of a proxy group. <count> is not mandatory", "create SERVER <serverGroup> <count> | Creates a game server of a server group. <count> is not mandatory", "create CLOUDSERVER <name> <memory> <priorityStop>", "create USER <name> <password> | Creates a new user with specified name and password", "create PROXYGROUP <name> | Creates a completely new proxy group for BungeeCord with its own configurations, etc.", "create SERVERGROUP <name> | Creates a completely new server group for Minecraft servers with its own configurations, etc.", "create DISPATCHCOMMAND <main-command> <command> | Creates a simple command alias", "create WRAPPER <name> | Creates and whitelists a new wrapper. The wrapper can also have the same IP of a previous wrapper", "create TEMPLATE <name> <group> LOCAL | Creates a new locale (Wrapper locales) template for a server group", "create TEMPLATE <name> <group> MASTER | Creates a new master backend (Master locales) template for a server group", "create TEMPLATE <name> <group> URL <url> | Creates a new template of a server group via url");
            break;
    }
}
Also used : SetupWrapper(de.dytanic.cloudnetcore.setup.SetupWrapper) SetupWrapper(de.dytanic.cloudnetcore.setup.SetupWrapper) Wrapper(de.dytanic.cloudnetcore.network.components.Wrapper) SetupProxyGroup(de.dytanic.cloudnetcore.setup.SetupProxyGroup) ProxyGroup(de.dytanic.cloudnet.lib.server.ProxyGroup) User(de.dytanic.cloudnet.lib.user.User) BasicUser(de.dytanic.cloudnet.lib.user.BasicUser) SetupServerGroup(de.dytanic.cloudnetcore.setup.SetupServerGroup) SetupServerGroup(de.dytanic.cloudnetcore.setup.SetupServerGroup) Template(de.dytanic.cloudnet.lib.server.template.Template) BasicUser(de.dytanic.cloudnet.lib.user.BasicUser) SetupProxyGroup(de.dytanic.cloudnetcore.setup.SetupProxyGroup)

Example 9 with Template

use of de.dytanic.cloudnet.lib.server.template.Template in project CloudNet by Dytanic.

the class CloudNet method startGameServerAsync.

public void startGameServerAsync(Wrapper wrapper, String serverId, ServerGroup serverGroup, ServerConfig config, int memory, boolean prioritystop, String url, String[] processParameters, boolean onlineMode, Collection<ServerInstallablePlugin> plugins, String customServerName, Properties serverProperties) {
    if (wrapper == null)
        return;
    if (serverGroup.getTemplates().size() == 0)
        return;
    Map<String, Integer> templateMap = new WeakHashMap<>();
    Collection<Integer> collection = CollectionWrapper.getCollection(wrapper.getServers(), new Catcher<Integer, MinecraftServer>() {

        @Override
        public Integer doCatch(MinecraftServer key) {
            return key.getServerInfo().getPort();
        }
    });
    collection.addAll(wrapper.getBinndedPorts());
    CollectionWrapper.iterator(getServers(serverGroup.getName()), new Runnabled<MinecraftServer>() {

        @Override
        public void run(MinecraftServer obj) {
            Template template = obj.getProcessMeta().getTemplate();
            if (!templateMap.containsKey(template.getName()))
                templateMap.put(template.getName(), 1);
            else
                templateMap.put(template.getName(), templateMap.get(template.getName()) + 1);
        }
    });
    CollectionWrapper.iterator(wrapper.getWaitingServices().values(), new Runnabled<Quad<Integer, Integer, ServiceId, Template>>() {

        @Override
        public void run(Quad<Integer, Integer, ServiceId, Template> obj) {
            Template template = obj.getFourth();
            if (template != null) {
                if (!templateMap.containsKey(template.getName()))
                    templateMap.put(template.getName(), 1);
                else
                    templateMap.put(template.getName(), templateMap.get(template.getName()) + 1);
            }
        }
    });
    for (Template template : serverGroup.getTemplates()) {
        if (!templateMap.containsKey(template.getName()))
            templateMap.put(template.getName(), 1);
        else
            templateMap.put(template.getName(), templateMap.get(template.getName()) + 1);
    }
    Map.Entry<String, Integer> entry = null;
    for (Map.Entry<String, Integer> values : templateMap.entrySet()) {
        if (entry == null) {
            entry = values;
        } else {
            if (entry.getValue() >= values.getValue()) {
                entry = values;
            }
        }
    }
    Template template = null;
    for (Template t : serverGroup.getTemplates()) {
        if (entry.getKey().equalsIgnoreCase(t.getName())) {
            template = t;
            break;
        }
    }
    if (template == null)
        return;
    int startport = wrapper.getWrapperInfo().getStartPort();
    startport = (startport + NetworkUtils.RANDOM.nextInt(20) + 1);
    while (collection.contains(startport)) {
        startport = (startport + NetworkUtils.RANDOM.nextInt(20) + 1);
    }
    List<Template> templates = CollectionWrapper.transform(serverGroup.getTemplates());
    if (templates.size() == 0)
        return;
    ServerProcessMeta serverProcessMeta = new ServerProcessMeta(newServiceId(serverGroup, wrapper, serverId), memory, prioritystop, url, processParameters, onlineMode, plugins, config, customServerName, startport, serverProperties, template);
    wrapper.startGameServerAsync(serverProcessMeta);
}
Also used : Template(de.dytanic.cloudnet.lib.server.template.Template) ServiceId(de.dytanic.cloudnet.lib.service.ServiceId) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 10 with Template

use of de.dytanic.cloudnet.lib.server.template.Template in project CloudNet by Dytanic.

the class BungeeCord method bootstrap.

@Override
public boolean bootstrap() throws Exception {
    long startupTime = System.currentTimeMillis();
    if (proxyGroup.getTemplate().getBackend().equals(TemplateResource.URL)) {
    }
    for (ServerInstallablePlugin url : proxyProcessMeta.getDownloadablePlugins()) {
        if (!Files.exists(Paths.get("local/cache/web_plugins/" + url.getName() + ".jar"))) {
            try {
                URLConnection urlConnection = new java.net.URL(url.getUrl()).openConnection();
                urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
                Files.copy(urlConnection.getInputStream(), Paths.get("local/cache/web_plugins/" + url.getName() + ".jar"));
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
    for (ServerInstallablePlugin url : proxyGroup.getTemplate().getInstallablePlugins()) {
        switch(url.getPluginResourceType()) {
            case URL:
                {
                    if (!Files.exists(Paths.get("local/cache/web_plugins/" + url.getName() + ".jar"))) {
                        try {
                            URLConnection urlConnection = new java.net.URL(url.getUrl()).openConnection();
                            urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
                            Files.copy(urlConnection.getInputStream(), Paths.get("local/cache/web_plugins/" + url.getName() + ".jar"));
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }
                break;
            case MASTER:
                {
                    if (!Files.exists(Paths.get("local/cache/web_plugins/" + url.getName() + ".jar")) && CloudNetWrapper.getInstance().getSimpledUser() != null) {
                        try {
                            URLConnection urlConnection = new java.net.URL(new StringBuilder(CloudNetWrapper.getInstance().getOptionSet().has("ssl") ? "https://" : "http://").append(CloudNetWrapper.getInstance().getWrapperConfig().getCloudnetHost()).append(":").append(CloudNetWrapper.getInstance().getWrapperConfig().getWebPort()).append("/cloudnet/api/v1/download").substring(0)).openConnection();
                            urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
                            SimpledUser simpledUser = CloudNetWrapper.getInstance().getSimpledUser();
                            urlConnection.setRequestProperty("-Xcloudnet-user", simpledUser.getUserName());
                            urlConnection.setRequestProperty("-Xcloudnet-token", simpledUser.getApiToken());
                            urlConnection.setRequestProperty("-Xmessage", "plugin");
                            urlConnection.setRequestProperty("-Xvalue", url.getName());
                            urlConnection.connect();
                            Files.copy(urlConnection.getInputStream(), Paths.get("local/cache/web_plugins/" + url.getName() + ".jar"));
                            ((HttpURLConnection) urlConnection).disconnect();
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }
                break;
            default:
                break;
        }
    }
    if (proxyGroup.getProxyGroupMode().equals(ProxyGroupMode.STATIC)) {
        if (!Files.exists(dir)) {
            Files.createDirectories(dir);
            if (proxyProcessMeta.getUrl() != null) {
                Files.createDirectory(Paths.get(path + "/plugins"));
                for (ServerInstallablePlugin plugin : proxyProcessMeta.getDownloadablePlugins()) {
                    FileCopy.copyFileToDirectory(new File("local/cache/web_plugins/" + plugin.getName() + ".jar"), new File(path + "/plugins"));
                }
                TemplateLoader templateLoader = new TemplateLoader(proxyProcessMeta.getUrl(), path + "/template.zip");
                System.out.println("Downloading template for " + this.proxyProcessMeta.getServiceId().getServerId());
                templateLoader.load();
                templateLoader.unZip(path);
            } else {
                Files.createDirectory(Paths.get(path + "/plugins"));
                for (ServerInstallablePlugin plugin : proxyProcessMeta.getDownloadablePlugins()) {
                    FileCopy.copyFileToDirectory(new File("local/cache/web_plugins/" + plugin.getName() + ".jar"), new File(path + "/plugins"));
                }
                for (ServerInstallablePlugin plugin : proxyGroup.getTemplate().getInstallablePlugins()) {
                    FileCopy.copyFileToDirectory(new File("local/cache/web_plugins/" + plugin.getName() + ".jar"), new File(path + "/plugins"));
                }
                Template template = proxyGroup.getTemplate();
                if (template.getBackend().equals(TemplateResource.URL) && template.getUrl() != null) {
                    String groupTemplates = "local/cache/web_templates/" + proxyGroup.getName();
                    TemplateLoader templateLoader = new TemplateLoader(template.getUrl(), groupTemplates + "/template.zip");
                    System.out.println("Downloading template for " + this.proxyProcessMeta.getServiceId().getGroup());
                    templateLoader.load();
                    templateLoader.unZip(groupTemplates);
                    FileCopy.copyFilesInDirectory(new File("local/cache/web_templates/" + proxyGroup.getName()), new File(path));
                } else if (template.getBackend().equals(TemplateResource.MASTER) && CloudNetWrapper.getInstance().getSimpledUser() != null) {
                    String groupTemplates = "local/cache/web_templates/" + proxyGroup.getName() + NetworkUtils.SLASH_STRING + template.getName();
                    if (!Files.exists(Paths.get(groupTemplates))) {
                        Files.createDirectories(Paths.get(groupTemplates));
                        MasterTemplateLoader templateLoader = new MasterTemplateLoader(new StringBuilder(CloudNetWrapper.getInstance().getOptionSet().has("ssl") ? "https://" : "http://").append(CloudNetWrapper.getInstance().getWrapperConfig().getCloudnetHost()).append(":").append(CloudNetWrapper.getInstance().getWrapperConfig().getWebPort()).append("/cloudnet/api/v1/download").substring(0), groupTemplates + "/template.zip", CloudNetWrapper.getInstance().getSimpledUser(), template, proxyGroup.getName(), null);
                        System.out.println("Downloading template for " + this.proxyProcessMeta.getServiceId().getGroup());
                        templateLoader.load();
                        templateLoader.unZip(groupTemplates);
                    }
                    FileCopy.copyFilesInDirectory(new File(groupTemplates), new File(path));
                } else if (Files.exists(Paths.get("local/templates/" + proxyGroup.getName()))) {
                    FileCopy.copyFilesInDirectory(new File("local/templates/" + proxyGroup.getName()), new File(path));
                } else {
                    return false;
                }
            }
        }
    } else {
        try {
            FileUtils.deleteDirectory(new File(path));
        } catch (Exception ex) {
        }
        Files.createDirectories(dir);
        if (proxyProcessMeta.getUrl() != null) {
            Files.createDirectory(Paths.get(path + "/plugins"));
            for (ServerInstallablePlugin plugin : proxyProcessMeta.getDownloadablePlugins()) FileCopy.copyFileToDirectory(new File("local/cache/web_plugins/" + plugin.getName() + ".jar"), new File(path + "/plugins"));
            for (ServerInstallablePlugin plugin : proxyGroup.getTemplate().getInstallablePlugins()) FileCopy.copyFileToDirectory(new File("local/cache/web_plugins/" + plugin.getName() + ".jar"), new File(path + "/plugins"));
            TemplateLoader templateLoader = new TemplateLoader(proxyProcessMeta.getUrl(), "local/templates/" + proxyGroup.getName() + "/template.zip");
            System.out.println("Downloading template for " + this.proxyProcessMeta.getServiceId().getServerId());
            templateLoader.load();
            templateLoader.unZip(path);
        } else {
            Files.createDirectory(Paths.get(path + "/plugins"));
            for (ServerInstallablePlugin plugin : proxyProcessMeta.getDownloadablePlugins()) {
                FileCopy.copyFileToDirectory(new File("local/cache/web_plugins/" + plugin.getName() + ".jar"), new File(path + "/plugins"));
            }
            Template template = proxyGroup.getTemplate();
            if (template.getBackend().equals(TemplateResource.URL) && template.getUrl() != null) {
                String groupTemplates = "local/cache/web_templates/" + proxyGroup.getName();
                TemplateLoader templateLoader = new TemplateLoader(template.getUrl(), groupTemplates + "/template.zip");
                System.out.println("Downloading template for " + this.proxyProcessMeta.getServiceId().getGroup());
                templateLoader.load();
                templateLoader.unZip(groupTemplates);
                FileCopy.copyFilesInDirectory(new File("local/cache/web_templates/" + proxyGroup.getName()), new File(path));
            } else if (template.getBackend().equals(TemplateResource.MASTER) && CloudNetWrapper.getInstance().getSimpledUser() != null) {
                String groupTemplates = "local/cache/web_templates/" + proxyGroup.getName() + NetworkUtils.SLASH_STRING + template.getName();
                if (!Files.exists(Paths.get(groupTemplates))) {
                    Files.createDirectories(Paths.get(groupTemplates));
                    MasterTemplateLoader templateLoader = new MasterTemplateLoader(new StringBuilder(CloudNetWrapper.getInstance().getOptionSet().has("ssl") ? "https://" : "http://").append(CloudNetWrapper.getInstance().getWrapperConfig().getCloudnetHost()).append(":").append(CloudNetWrapper.getInstance().getWrapperConfig().getWebPort()).append("/cloudnet/api/v1/download").substring(0), groupTemplates + "/template.zip", CloudNetWrapper.getInstance().getSimpledUser(), template, proxyGroup.getName(), null);
                    System.out.println("Downloading template for " + this.proxyProcessMeta.getServiceId().getGroup());
                    templateLoader.load();
                    templateLoader.unZip(groupTemplates);
                }
                FileCopy.copyFilesInDirectory(new File(groupTemplates), new File(path));
            } else if (Files.exists(Paths.get("local/templates/" + proxyGroup.getName()))) {
                FileCopy.copyFilesInDirectory(new File("local/templates/" + proxyGroup.getName()), new File(path));
            } else {
                return false;
            }
        }
    }
    if (!Files.exists(Paths.get(path + "/config.yml"))) {
        FileCopy.insertData("files/config.yml", path + "/config.yml");
    }
    if (!Files.exists(Paths.get(path + "/BungeeCord.jar"))) {
        MultiValue<String, String> version = ProxyVersion.url(proxyGroup.getProxyVersion());
        Path path = Paths.get("local/proxy_versions/" + version.getSecond());
        if (!Files.exists(path)) {
            try {
                URLConnection urlConnection = new URL(version.getFirst()).openConnection();
                urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
                urlConnection.connect();
                System.out.println("Downloading " + version.getSecond() + "...");
                Files.copy(urlConnection.getInputStream(), path);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        FileCopy.copyFileToDirectory(new File("local/proxy_versions/" + version.getSecond()), new File(this.path));
        new File(this.path + NetworkUtils.SLASH_STRING + version.getSecond()).renameTo(new File(this.path + "/BungeeCord.jar"));
    }
    if (!Files.exists(Paths.get(path + "/server-icon.png")) && Files.exists(Paths.get("local/server-icon.png")))
        FileCopy.copyFileToDirectory(new File("local/server-icon.png"), new File(path));
    Files.deleteIfExists(Paths.get(path + "/plugins/CloudNetAPI.jar"));
    FileCopy.insertData("files/CloudNetAPI.jar", path + "/plugins/CloudNetAPI.jar");
    FileCopy.rewriteFileUtils(new File(path + "/config.yml"), "\"" + CloudNetWrapper.getInstance().getWrapperConfig().getProxy_config_host() + ":" + this.proxyProcessMeta.getPort() + "\"");
    if (CloudNetWrapper.getInstance().getWrapperConfig().isViaVersion()) {
        if (!Files.exists(Paths.get("local/ViaVersion-Proxied.jar"))) {
            try {
                System.out.println("Downloading ViaVersion...");
                URLConnection url = new URL("https://ci.viaversion.com/job/ViaVersion/lastSuccessfulBuild/artifact/jar/target/ViaVersion-1.3.0.jar").openConnection();
                url.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
                url.connect();
                Files.copy(url.getInputStream(), Paths.get("local/ViaVersion-Proxied.jar"));
                ((HttpURLConnection) url).disconnect();
                System.out.println("Download complete successfully!");
            } catch (Exception ex) {
            }
        }
        FileCopy.copyFileToDirectory(new File("local/ViaVersion-Proxied.jar"), new File(path + "/plugins"));
    }
    this.proxyInfo = new ProxyInfo(proxyProcessMeta.getServiceId(), CloudNetWrapper.getInstance().getWrapperConfig().getInternalIP(), proxyProcessMeta.getPort(), false, new LinkedList<>(), proxyProcessMeta.getMemory(), 0);
    if (!Files.exists(Paths.get(path + "/CLOUD")))
        Files.createDirectory(Paths.get(path + "/CLOUD"));
    new Document().append("serviceId", proxyProcessMeta.getServiceId()).append("proxyProcess", proxyProcessMeta).append("host", CloudNetWrapper.getInstance().getWrapperConfig().getProxy_config_host() + ":" + this.proxyProcessMeta.getPort()).append("proxyInfo", proxyInfo).append("ssl", CloudNetWrapper.getInstance().getOptionSet().has("ssl")).append("memory", proxyProcessMeta.getMemory()).saveAsConfig(Paths.get(path + "/CLOUD/config.json"));
    new Document().append("connection", new ConnectableAddress(CloudNetWrapper.getInstance().getWrapperConfig().getCloudnetHost(), CloudNetWrapper.getInstance().getWrapperConfig().getCloudnetPort())).saveAsConfig(Paths.get(path + "/CLOUD/connection.json"));
    StringBuilder commandBuilder = new StringBuilder();
    commandBuilder.append("java ");
    for (String command : proxyProcessMeta.getProcessParameters()) commandBuilder.append(command).append(NetworkUtils.SPACE_STRING);
    for (String command : proxyGroup.getTemplate().getProcessPreParameters()) commandBuilder.append(command).append(NetworkUtils.SPACE_STRING);
    // commandBuilder.append("-XX:+UseG1GC -XX:MaxGCPauseMillis=50 -Djline.terminal=jline.UnsupportedTerminal -XX:MaxPermSize=256M -DIReallyKnowWhatIAmDoingISwear=true -Xmx" + proxyProcessMeta.getMemory() + "M -jar BungeeCord.jar -o true -p");
    commandBuilder.append("-XX:+UseG1GC -XX:MaxGCPauseMillis=50 -XX:MaxPermSize=256M -XX:-UseAdaptiveSizePolicy -Dio.netty.leakDetectionLevel=DISABLED -Dfile.encoding=UTF-8 -Dio.netty.maxDirectMemory=0 -Dio.netty.recycler.maxCapacity=0 -Dio.netty.recycler.maxCapacity.default=0 -Djline.terminal=jline.UnsupportedTerminal -DIReallyKnowWhatIAmDoingISwear=true -Xmx" + proxyProcessMeta.getMemory() + "M -jar BungeeCord.jar -o true -p");
    CloudNetWrapper.getInstance().getNetworkConnection().sendPacket(new PacketOutAddProxy(proxyInfo, proxyProcessMeta));
    System.out.println("Proxy " + toString() + " started in [" + (System.currentTimeMillis() - startupTime) + " milliseconds]");
    this.instance = Runtime.getRuntime().exec(commandBuilder.substring(0).split(NetworkUtils.SPACE_STRING), null, new File(path));
    CloudNetWrapper.getInstance().getProxys().put(this.proxyProcessMeta.getServiceId().getServerId(), this);
    return true;
}
Also used : Path(java.nio.file.Path) ServerInstallablePlugin(de.dytanic.cloudnet.lib.service.plugin.ServerInstallablePlugin) MasterTemplateLoader(de.dytanic.cloudnet.lib.server.template.MasterTemplateLoader) URL(java.net.URL) TemplateLoader(de.dytanic.cloudnet.lib.server.template.TemplateLoader) MasterTemplateLoader(de.dytanic.cloudnet.lib.server.template.MasterTemplateLoader) PacketOutAddProxy(de.dytanic.cloudnetwrapper.network.packet.out.PacketOutAddProxy) Document(de.dytanic.cloudnet.lib.utility.document.Document) SimpledUser(de.dytanic.cloudnet.lib.user.SimpledUser) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) IOException(java.io.IOException) URL(java.net.URL) LinkedList(java.util.LinkedList) Template(de.dytanic.cloudnet.lib.server.template.Template) ConnectableAddress(de.dytanic.cloudnet.lib.ConnectableAddress) ProxyInfo(de.dytanic.cloudnet.lib.server.info.ProxyInfo) HttpURLConnection(java.net.HttpURLConnection) File(java.io.File)

Aggregations

Template (de.dytanic.cloudnet.lib.server.template.Template)11 ServiceId (de.dytanic.cloudnet.lib.service.ServiceId)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 ProxyGroup (de.dytanic.cloudnet.lib.server.ProxyGroup)3 ServerGroup (de.dytanic.cloudnet.lib.server.ServerGroup)3 Document (de.dytanic.cloudnet.lib.utility.document.Document)3 Path (java.nio.file.Path)3 ServerConfig (de.dytanic.cloudnet.lib.server.ServerConfig)2 ProxyInfo (de.dytanic.cloudnet.lib.server.info.ProxyInfo)2 ServerInfo (de.dytanic.cloudnet.lib.server.info.ServerInfo)2 LocalCloudWrapper (de.dytanic.cloudnetcore.setup.LocalCloudWrapper)2 IOException (java.io.IOException)2 HttpURLConnection (java.net.HttpURLConnection)2 URL (java.net.URL)2 URLConnection (java.net.URLConnection)2 TypeToken (com.google.gson.reflect.TypeToken)1 ConnectableAddress (de.dytanic.cloudnet.lib.ConnectableAddress)1 WrapperInfo (de.dytanic.cloudnet.lib.network.WrapperInfo)1 CloudPlayer (de.dytanic.cloudnet.lib.player.CloudPlayer)1 AutoSlot (de.dytanic.cloudnet.lib.proxylayout.AutoSlot)1