Esempio n. 1
0
 public ServerListRequestHandler(ServerListProxy proxy)
 {
     _params         = proxy.Parameters;
     _hostHeader     = $"{_params.Host}:{_params.Port}";
     _serverListPath = $"/servers/list.{_params.Region.ToRegionString()}";
     _servers        = GetAndAdjustServers(proxy);
 }
Esempio n. 2
0
            string GetAndAdjustServers(ServerListProxy proxy)
            {
                _log.Basic("Fetching official server list...");

                HttpResponseMessage resp;

                using (var client = new HttpClient())
                {
                    client.Timeout = _params.Timeout;

                    var retriesSoFar = 0;

                    while (true)
                    {
                        var req = new HttpRequestMessage(HttpMethod.Get,
                                                         GetUri(_serverListPath));

                        req.Headers.Host = _hostHeader;

                        try
                        {
                            resp = client.SendAsync(req).Result.EnsureSuccessStatusCode();
                        }
                        catch (AggregateException)
                        {
                            if (retriesSoFar < _params.Retries)
                            {
                                _log.Error("Could not fetch official server list, retrying...");
                                retriesSoFar++;
                                continue;
                            }

                            throw;
                        }

                        break;
                    }
                }

                _log.Basic("Fetched official server list");

                var doc     = XDocument.Parse(resp.Content.ReadAsStringAsync().Result);
                var servers = new List <ServerInfo>();

                foreach (var elem in doc.Root.Elements("server"))
                {
                    var ipElem = elem.Element("ip");

                    var id    = int.Parse(elem.Element("id").Value);
                    var name  = elem.Element("name").Value;
                    var cat   = elem.Element("category").Value;
                    var ip    = IPAddress.Parse(ipElem.Value);
                    var port  = int.Parse(elem.Element("port").Value);
                    var newIP = _params.GameAddress;

                    ipElem.Value = newIP.ToString();

                    servers.Add(new ServerInfo(id, name, cat, ip, newIP, port));

                    _log.Info("Redirected {0}: {1}:{2} -> {3}:{2}",
                              name, ip, port, newIP);
                }

                proxy.Servers = servers;

                _log.Basic("Redirected {0} servers", servers.Count);

                return(doc.ToString());
            }