Beispiel #1
0
        private static void DoWorkToValidateServer(ServerBrowserEntry server_data, string hash)
        {
            string hash_base = "";
            bool   found     = false;

            for (int i = 0; i < 1000000; i++)
            {
                hash_base = i + "";

                string test_hash = SystemHelpers.ComputeSHA256Hash(hash_base);
                if (hash == test_hash)
                {
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                LogHelpers.Log("Server browser processing failed; no matching hash.");
                return;
            }

            var output_obj = new ServerBrowserWorkProof {
                ServerIP  = server_data.ServerIP,
                Port      = server_data.Port,
                WorldName = server_data.WorldName,
                HashBase  = hash_base
            };

            string json_str = JsonConvert.SerializeObject(output_obj, Formatting.None);

            byte[] json_bytes = Encoding.UTF8.GetBytes(json_str);

            Thread.Sleep(1500);

            NetHelpers.MakePostRequestAsync(ServerBrowserReporter.URL, json_bytes, delegate(string output) {
                LogHelpers.Log("Server browser processing complete.");
            }, delegate(Exception e, string output) {
                LogHelpers.Log("Server browser reply returned error: " + e.ToString());
            });

            ServerBrowserReporter.LastSendTimestamp = SystemHelpers.TimeStampInSeconds();
        }
        private static void HandleServerAnnounceOutputAsync(ServerBrowserEntry server_data, string output)
        {
            try {
                var reply = JsonConvert.DeserializeObject <ServerBrowserWorkAssignment>(output);

                if (reply.ProofOfWorkNeeded)
                {
                    ServerBrowserReporter.DoWorkToValidateServer(server_data, reply.Hash);

                    LogHelpers.Log("Server data added to browser? " + reply.Success + " - Beginning validation...");
                }
                else
                {
                    LogHelpers.Log("Server updated on browser? " + reply.Success);
                }
            } catch (Exception e) {
                if (!(e is JsonReaderException))
                {
                    LogHelpers.Log("HandleServerAnnounceOutput - " + e.ToString() + " - " + (output.Length > 256 ? output.Substring(0, 256) : output));
                }
            }
        }
        ////////////////

        public static void AnnounceServer()
        {
            ServerBrowserMod mymod = ServerBrowserMod.Instance;
            Version          vers  = mymod.Version;

            int port = Netplay.ListenPort;
            //if( mymod.Config.ServerBrowserCustomPort != -1 ) {
            //	port = mymod.Config.ServerBrowserCustomPort;
            //}

            int pvp = 0;

            bool[] team_checks = new bool[10];
            ServerBrowserReporter server_browser = mymod.Reporter;

            for (int i = 0; i < Main.player.Length; i++)
            {
                Player player = Main.player[i];
                if (player == null || !player.active)
                {
                    continue;
                }

                if (player.hostile)
                {
                    pvp++;
                }
                team_checks[player.team] = true;
            }

            int team_count = 0;

            for (int i = 1; i < team_checks.Length; i++)
            {
                if (team_checks[i])
                {
                    team_count++;
                }
            }

            try {
                var server_data = new ServerBrowserEntry();
                server_data.ServerIP       = NetHelpers.GetPublicIP();
                server_data.Port           = port;
                server_data.IsPassworded   = Netplay.ServerPassword != "";
                server_data.Motd           = Main.motd;
                server_data.WorldName      = Main.worldName;
                server_data.WorldProgress  = InfoHelpers.GetVanillaProgress();
                server_data.WorldEvent     = string.Join(", ", InfoHelpers.GetCurrentVanillaEvents().ToArray());
                server_data.MaxPlayerCount = Main.maxNetPlayers;
                server_data.PlayerCount    = Main.ActivePlayersCount;
                server_data.PlayerPvpCount = pvp;
                server_data.TeamsCount     = team_count;
                server_data.AveragePing    = InfoHelpers.GetAveragePingOfServer();
                server_data.Mods           = new Dictionary <string, string>();
                server_data.Version        = (vers.Major * 1000000) + (vers.Minor * 10000) + (vers.Build * 100) + vers.Revision;

                foreach (Mod mod in ModLoader.LoadedMods)
                {
                    if (mod.File == null)
                    {
                        continue;
                    }
                    server_data.Mods[mod.DisplayName] = mod.Version.ToString();
                }

                string json_str   = JsonConvert.SerializeObject(server_data, Formatting.None);
                byte[] json_bytes = Encoding.UTF8.GetBytes(json_str);

                Action <string> on_response = delegate(string output) {
                    ServerBrowserReporter.HandleServerAnnounceOutputAsync(server_data, output);
                };
                Action <Exception, string> on_error = delegate(Exception e, string output) {
                    LogHelpers.Log("Server browser returned error: " + e.ToString());
                };

                NetHelpers.MakePostRequestAsync(ServerBrowserReporter.URL, json_bytes, on_response, on_error);

                ServerBrowserReporter.LastSendTimestamp = SystemHelpers.TimeStampInSeconds();
            } catch (Exception e) {
                LogHelpers.Log("AnnounceServer - " + e.ToString());
                return;
            }
        }