Search in sources :

Example 1 with ServerInfo

use of de.dytanic.cloudnet.lib.server.info.ServerInfo in project CloudNet by Dytanic.

the class PacketInCopyDirectory method handleInput.

@Override
public void handleInput(Document data, PacketSender packetSender) {
    if (!data.contains("serverInfo") || !data.contains("directory"))
        return;
    ServerInfo info = data.getObject("serverInfo", ServerInfo.TYPE);
    Wrapper wrapper = CloudNet.getInstance().getWrappers().get(info.getServiceId().getWrapperId());
    if (wrapper != null && wrapper.getChannel() != null)
        wrapper.sendPacket(new Packet(PacketRC.CN_CORE + 14, data));
}
Also used : Wrapper(de.dytanic.cloudnetcore.network.components.Wrapper) Packet(de.dytanic.cloudnet.lib.network.protocol.packet.Packet) ServerInfo(de.dytanic.cloudnet.lib.server.info.ServerInfo)

Example 2 with ServerInfo

use of de.dytanic.cloudnet.lib.server.info.ServerInfo in project CloudNet by Dytanic.

the class CloudProxy method fallback.

public String fallback(ProxiedPlayer cloudPlayer) {
    for (ServerFallback serverFallback : CloudProxy.getInstance().getProxyGroup().getProxyConfig().getDynamicFallback().getFallbacks()) {
        if (serverFallback.getGroup().equals(CloudProxy.getInstance().getProxyGroup().getProxyConfig().getDynamicFallback().getDefaultFallback()))
            continue;
        if (serverFallback.getPermission() != null) {
            if (!cloudPlayer.hasPermission(serverFallback.getPermission()))
                continue;
            List<String> servers = CloudProxy.getInstance().getServers(serverFallback.getGroup());
            if (servers.size() != 0) {
                return servers.get(NetworkUtils.RANDOM.nextInt(servers.size()));
            }
        }
    }
    String fallback = getProxyGroup().getProxyConfig().getDynamicFallback().getDefaultFallback();
    List<String> liste = new ArrayList<>(MapWrapper.filter(cachedServers, new Acceptable<ServerInfo>() {

        @Override
        public boolean isAccepted(ServerInfo value) {
            return value.getServiceId().getGroup().equalsIgnoreCase(fallback);
        }
    }).keySet());
    if (liste.size() == 0)
        return null;
    else
        return liste.get(NetworkUtils.RANDOM.nextInt(liste.size()));
}
Also used : ServerInfo(de.dytanic.cloudnet.lib.server.info.ServerInfo) ServerFallback(de.dytanic.cloudnet.lib.proxylayout.ServerFallback) ArrayList(java.util.ArrayList)

Example 3 with ServerInfo

use of de.dytanic.cloudnet.lib.server.info.ServerInfo in project CloudNet by Dytanic.

the class CloudProxy method fallbackOnEnabledKick.

public String fallbackOnEnabledKick(ProxiedPlayer cloudPlayer, String group, String kickedFrom) {
    for (ServerFallback serverFallback : CloudProxy.getInstance().getProxyGroup().getProxyConfig().getDynamicFallback().getFallbacks()) {
        if (serverFallback.getGroup().equals(CloudProxy.getInstance().getProxyGroup().getProxyConfig().getDynamicFallback().getDefaultFallback()))
            continue;
        if (serverFallback.getPermission() != null) {
            if (!cloudPlayer.hasPermission(serverFallback.getPermission()))
                continue;
            List<String> servers = CloudProxy.getInstance().getServers(serverFallback.getGroup());
            servers.remove(kickedFrom);
            if (servers.size() != 0) {
                return servers.get(NetworkUtils.RANDOM.nextInt(servers.size()));
            }
        }
    }
    {
        List<String> liste = new ArrayList<>(MapWrapper.filter(cachedServers, new Acceptable<ServerInfo>() {

            @Override
            public boolean isAccepted(ServerInfo value) {
                return value.getServiceId().getGroup().equalsIgnoreCase(group);
            }
        }).keySet());
        liste.remove(kickedFrom);
        if (liste.size() != 0) {
            return liste.get(NetworkUtils.RANDOM.nextInt(liste.size()));
        }
    }
    String fallback = getProxyGroup().getProxyConfig().getDynamicFallback().getDefaultFallback();
    List<String> liste = new ArrayList<>(MapWrapper.filter(cachedServers, new Acceptable<ServerInfo>() {

        @Override
        public boolean isAccepted(ServerInfo value) {
            return value.getServiceId().getGroup().equalsIgnoreCase(fallback);
        }
    }).keySet());
    liste.remove(kickedFrom);
    if (liste.size() == 0)
        return null;
    else
        return liste.get(NetworkUtils.RANDOM.nextInt(liste.size()));
}
Also used : ServerInfo(de.dytanic.cloudnet.lib.server.info.ServerInfo) Acceptable(de.dytanic.cloudnet.lib.utility.Acceptable) ServerFallback(de.dytanic.cloudnet.lib.proxylayout.ServerFallback) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) TabList(de.dytanic.cloudnet.lib.proxylayout.TabList) List(java.util.List)

Example 4 with ServerInfo

use of de.dytanic.cloudnet.lib.server.info.ServerInfo in project CloudNet by Dytanic.

the class CloudServer method updateDisable.

public void updateDisable() {
    List<String> list = new CopyOnWriteArrayList<>();
    for (Player all : Bukkit.getOnlinePlayers()) list.add(all.getName());
    ServerInfo serverInfo = new ServerInfo(CloudAPI.getInstance().getServiceId(), hostAdress, port, false, list, memory, motd, Bukkit.getOnlinePlayers().size(), maxPlayers, serverState, serverConfig, template);
    CloudAPI.getInstance().getNetworkConnection().sendPacketSynchronized(new PacketOutUpdateServerInfo(serverInfo));
}
Also used : OfflinePlayer(de.dytanic.cloudnet.lib.player.OfflinePlayer) Player(org.bukkit.entity.Player) CloudPlayer(de.dytanic.cloudnet.lib.player.CloudPlayer) PacketOutUpdateServerInfo(de.dytanic.cloudnet.api.network.packet.out.PacketOutUpdateServerInfo) ServerInfo(de.dytanic.cloudnet.lib.server.info.ServerInfo) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) PacketOutUpdateServerInfo(de.dytanic.cloudnet.api.network.packet.out.PacketOutUpdateServerInfo)

Example 5 with ServerInfo

use of de.dytanic.cloudnet.lib.server.info.ServerInfo 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

ServerInfo (de.dytanic.cloudnet.lib.server.info.ServerInfo)24 TypeToken (com.google.gson.reflect.TypeToken)6 Wrapper (de.dytanic.cloudnetcore.network.components.Wrapper)5 ArrayList (java.util.ArrayList)5 ServerGroup (de.dytanic.cloudnet.lib.server.ServerGroup)4 Document (de.dytanic.cloudnet.lib.utility.document.Document)4 CloudPlayer (de.dytanic.cloudnet.lib.player.CloudPlayer)3 ServerFallback (de.dytanic.cloudnet.lib.proxylayout.ServerFallback)3 Properties (java.util.Properties)3 PacketOutUpdateServerInfo (de.dytanic.cloudnet.api.network.packet.out.PacketOutUpdateServerInfo)2 ProxiedPlayerFallbackEvent (de.dytanic.cloudnet.bridge.event.proxied.ProxiedPlayerFallbackEvent)2 ConnectableAddress (de.dytanic.cloudnet.lib.ConnectableAddress)2 OfflinePlayer (de.dytanic.cloudnet.lib.player.OfflinePlayer)2 ServerConfig (de.dytanic.cloudnet.lib.server.ServerConfig)2 Template (de.dytanic.cloudnet.lib.server.template.Template)2 ServerInstallablePlugin (de.dytanic.cloudnet.lib.service.plugin.ServerInstallablePlugin)2 SimpledUser (de.dytanic.cloudnet.lib.user.SimpledUser)2 Acceptable (de.dytanic.cloudnet.lib.utility.Acceptable)2 ScheduledTask (de.dytanic.cloudnet.lib.utility.threading.ScheduledTask)2 CloudServer (de.dytanic.cloudnetcore.network.components.CloudServer)2