Search in sources :

Example 1 with PacketOutAddServer

use of de.dytanic.cloudnetwrapper.network.packet.out.PacketOutAddServer in project CloudNet by Dytanic.

the class GameServer method bootstrap.

@Override
public boolean bootstrap() throws Exception {
    serverProcess.setServerStage(ServerStage.DOWNLOAD);
    long startupTime = System.currentTimeMillis();
    for (ServerInstallablePlugin url : serverProcess.getMeta().getDownloadablePlugins()) {
        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();
                            System.out.println("Downloading " + url.getName() + ".jar");
                            Files.copy(urlConnection.getInputStream(), Paths.get("local/cache/web_plugins/" + url.getName() + ".jar"));
                            System.out.println("Download was completed successfully!");
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }
                break;
            default:
                break;
        }
    }
    for (ServerInstallablePlugin url : serverProcess.getMeta().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();
                            System.out.println("Downloading " + url.getName() + ".jar");
                            Files.copy(urlConnection.getInputStream(), Paths.get("local/cache/web_plugins/" + url.getName() + ".jar"));
                            System.out.println("Download was completed successfully!");
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }
                break;
            default:
                break;
        }
    }
    if (serverGroup.getTemplates().size() == 0 && serverProcess.getMeta().getUrl() == null)
        return false;
    if (serverGroup.getGroupMode().equals(ServerGroupMode.STATIC)) {
        if (!Files.exists(dir))
            if (!a())
                return false;
    } else if (!a())
        return false;
    for (ServerInstallablePlugin plugin : serverProcess.getMeta().getDownloadablePlugins()) FileCopy.copyFileToDirectory(new File("local/cache/web_plugins/" + plugin.getName() + ".jar"), new File(path + "/plugins"));
    for (ServerInstallablePlugin plugin : serverProcess.getMeta().getTemplate().getInstallablePlugins()) FileCopy.copyFileToDirectory(new File("local/cache/web_plugins/" + plugin.getName() + ".jar"), new File(path + "/plugins"));
    serverProcess.setServerStage(ServerStage.COPY);
    if (serverGroup.getServerType().equals(ServerGroupType.BUKKIT))
        if (!Files.exists(Paths.get(path + "/spigot.jar")))
            FileCopy.copyFileToDirectory(new File("local/spigot.jar"), new File(path));
    if (serverGroup.getServerType().equals(ServerGroupType.GLOWSTONE)) {
        if (!Files.exists(Paths.get(path + "/config")))
            Files.createDirectories(Paths.get(path + "/config"));
        if (!Files.exists(Paths.get(path + "/config/glowstone.yml")))
            FileCopy.insertData("files/glowstone.yml", path + "/config/glowstone.yml");
    }
    if (!Files.exists(Paths.get(path + "/server.properties")))
        FileCopy.insertData("files/server.properties", path + "/server.properties");
    if (!Files.exists(Paths.get(path + "/bukkit.yml")))
        FileCopy.insertData("files/bukkit.yml", path + "/bukkit.yml");
    if (!serverProcess.getMeta().isOnlineMode())
        if (!Files.exists(Paths.get(path + "/spigot.yml")))
            FileCopy.insertData("files/spigot.yml", path + "/spigot.yml");
    Files.deleteIfExists(Paths.get(path + "/plugins/CloudNetAPI.jar"));
    FileCopy.insertData("files/CloudNetAPI.jar", path + "/plugins/CloudNetAPI.jar");
    FileCopy.copyFilesInDirectory(new File("local/global"), new File(path));
    String motd = "Default Motd";
    int maxPlayers = 0;
    if (!serverGroup.getServerType().equals(ServerGroupType.GLOWSTONE)) {
        Properties properties = new Properties();
        try (InputStreamReader inputStreamReader = new InputStreamReader(Files.newInputStream(Paths.get(path + "/server.properties")))) {
            properties.load(inputStreamReader);
        }
        Enumeration enumeration = this.serverProcess.getMeta().getServerProperties().keys();
        while (enumeration.hasMoreElements()) {
            String x = enumeration.nextElement().toString();
            properties.setProperty(x, this.serverProcess.getMeta().getServerProperties().getProperty(x));
        }
        properties.setProperty("server-ip", CloudNetWrapper.getInstance().getWrapperConfig().getInternalIP());
        properties.setProperty("server-port", serverProcess.getMeta().getPort() + NetworkUtils.EMPTY_STRING);
        properties.setProperty("online-mode", serverProcess.getMeta().isOnlineMode() + NetworkUtils.EMPTY_STRING);
        // properties.setProperty("server-name", serverProcess.getMeta().getServiceId().getServerId());
        motd = properties.getProperty("motd");
        maxPlayers = Integer.parseInt(properties.getProperty("max-players"));
        try (OutputStream outputStream = Files.newOutputStream(Paths.get(path + "/server.properties"))) {
            properties.store(outputStream, "CloudNet-Wrapper EDIT");
        }
    } else {
        try (InputStreamReader inputStreamReader = new InputStreamReader(Files.newInputStream(Paths.get(path + "/config/glowstone.yml")), StandardCharsets.UTF_8)) {
            Configuration configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(inputStreamReader);
            Configuration section = configuration.getSection("server");
            section.set("ip", CloudNetWrapper.getInstance().getWrapperConfig().getInternalIP());
            section.set("port", serverProcess.getMeta().getPort());
            maxPlayers = section.getInt("max-players");
            motd = section.getString("motd");
            configuration.set("server", section);
            configuration.set("console.use-jline", false);
            try (OutputStreamWriter outputStreamWriter = new OutputStreamWriter(Files.newOutputStream(Paths.get(path + "/config/glowstone.yml")), StandardCharsets.UTF_8)) {
                ConfigurationProvider.getProvider(YamlConfiguration.class).save(configuration, outputStreamWriter);
            }
        }
    }
    this.serverInfo = new ServerInfo(serverProcess.getMeta().getServiceId(), CloudNetWrapper.getInstance().getWrapperConfig().getInternalIP(), this.getServerProcess().getMeta().getPort(), false, new ArrayList<>(), serverProcess.getMeta().getMemory(), motd, 0, maxPlayers, ServerState.OFFLINE, this.serverProcess.getMeta().getServerConfig(), serverProcess.getMeta().getTemplate());
    if (!Files.exists(Paths.get(path + "/CLOUD"))) {
        Files.createDirectory(Paths.get(path + "/CLOUD"));
    }
    new Document().append("serviceId", serverProcess.getMeta().getServiceId()).append("serverProcess", serverProcess.getMeta()).append("serverInfo", serverInfo).append("ssl", CloudNetWrapper.getInstance().getOptionSet().has("ssl")).append("memory", serverProcess.getMeta().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"));
    CloudNetWrapper.getInstance().getNetworkConnection().sendPacket(new PacketOutAddServer(this.serverInfo, this.serverProcess.getMeta()));
    System.out.println("Server " + toString() + " started in [" + (System.currentTimeMillis() - startupTime) + " milliseconds]");
    this.startupTimeStamp = System.currentTimeMillis();
    startProcess();
    serverProcess.setServerStage(ServerStage.PROCESS);
    CloudNetWrapper.getInstance().getServers().put(this.serverProcess.getMeta().getServiceId().getServerId(), this);
    serverProcess.setServerStage(ServerStage.NET_INIT);
    return true;
}
Also used : ServerInstallablePlugin(de.dytanic.cloudnet.lib.service.plugin.ServerInstallablePlugin) PacketOutAddServer(de.dytanic.cloudnetwrapper.network.packet.out.PacketOutAddServer) Enumeration(java.util.Enumeration) Configuration(net.md_5.bungee.config.Configuration) YamlConfiguration(net.md_5.bungee.config.YamlConfiguration) ServerInfo(de.dytanic.cloudnet.lib.server.info.ServerInfo) ArrayList(java.util.ArrayList) Properties(java.util.Properties) YamlConfiguration(net.md_5.bungee.config.YamlConfiguration) Document(de.dytanic.cloudnet.lib.utility.document.Document) SimpledUser(de.dytanic.cloudnet.lib.user.SimpledUser) URLConnection(java.net.URLConnection) ConnectableAddress(de.dytanic.cloudnet.lib.ConnectableAddress)

Aggregations

ConnectableAddress (de.dytanic.cloudnet.lib.ConnectableAddress)1 ServerInfo (de.dytanic.cloudnet.lib.server.info.ServerInfo)1 ServerInstallablePlugin (de.dytanic.cloudnet.lib.service.plugin.ServerInstallablePlugin)1 SimpledUser (de.dytanic.cloudnet.lib.user.SimpledUser)1 Document (de.dytanic.cloudnet.lib.utility.document.Document)1 PacketOutAddServer (de.dytanic.cloudnetwrapper.network.packet.out.PacketOutAddServer)1 URLConnection (java.net.URLConnection)1 ArrayList (java.util.ArrayList)1 Enumeration (java.util.Enumeration)1 Properties (java.util.Properties)1 Configuration (net.md_5.bungee.config.Configuration)1 YamlConfiguration (net.md_5.bungee.config.YamlConfiguration)1