using IP2Region; using ProtoCSStruct; using Sog; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Realmlist { public static class RecommendRealmByIP { private static DbSearcher m_search; private static Dictionary cache = new Dictionary(); public static void OnReloadConfig() { cache.Clear(); foreach (var item in CountryAreaNameMapDescMgr.Instance.ItemTable.Values) { if(!cache.ContainsKey(item.name)) { cache.Add(item.name, item.areaName); TraceLog.Debug("RecommendRealmByIP.OnReloadConfig name {0} area {1} timezone {2}", item.name, item.areaName,item.timezone); } } } public static void TestIp() { var cur = DateTime.Now; Test("60.246.49.9"); Test("119.28.228.38"); Test("175.97.158.95"); Test("183.192.62.65"); Test("66.249.65.56"); Test("127.0.0.1"); Test("192.168.1.60"); Test("162.125.32.9"); Test("84.170.88.33"); Test("80.141.135.243"); Test("185.5.70.217"); Test("110.138.168.102"); Test("188.106.62.74"); Test("189.40.102.157"); Test("139.47.118.213"); Test("178.168.216.84"); Test("175.97.158.95"); Test("180.244.130.43"); Test("116.206.31.84"); Test("175.176.69.190"); Test("140.213.204.147"); Test("136.158.57.132"); Test("45.89.89.181"); Test("50.116.43.86"); Test("165.186.33.33"); Test("43.229.44.44"); Test("61.13.55.55"); Test("12.63.184.204"); Test("216.244.208.11"); Test("15.15.15.15"); Test("57.57.57.57"); Test("62.184.8.32"); Test("194.112.177.132"); Test("202.41.41.41"); Test("62.32.33.33"); Test("62.16.64.44"); Test("194.117.107.65"); Test("19.203.239.31"); Test("193.188.48.55"); Test("41.32.22.22"); Test("5.134.92.4"); Test("1.128.11.11"); Test("31.13.194.11"); Test("2.56.68.55"); Test("210.52.109.11"); Test("17.253.106.33"); Test("23.37.96.33"); Test("190.15.144.44"); Test("2.21.243.33"); Test("43.231.64.44"); Test("57.87.144.44"); Test("45.85.4.44"); Test("1.9.114.33"); Test("103.29.199.11"); Test("43.245.116.44"); Test("45.231.32.33"); Test("23.204.128.33"); Test("103.38.236.33"); Test("41.78.208.33"); Test("81.199.131.22"); Test("5.43.48.208"); Test("2.21.192.11"); Test("5.153.112.22"); Test("1.0.182.224"); Test("5.44.80.11"); Test("64.76.65.11"); Test("2.58.116.11"); Test("45.66.40.11"); Test("27.252.48.11"); Test("5.63.204.44"); Test("94.102.80.55"); Test("37.238.224.44"); Test("5.226.48.55"); Test("37.61.230.55"); Test("2.32.4.33"); Test("1.52.69.160"); Test("45.167.192.44"); Test("1.22.57.44"); Test("11.191.39.11"); Test("31.12.128.44"); Test("46.150.192.11"); Test("102.143.128.11"); Test("45.15.44.11"); Test("1.33.88.44"); Test("2.148.100.33"); Test("102.64.124.11"); Test("45.219.255.11"); Test("103.68.139.11"); Test("78.141.128.11"); Test("157.119.180.11"); Test("91.102.232.11"); Test("8.40.30.11"); Test("13.88.224.11"); Test("94.102.234.11"); Test("181.189.224.11"); Test("196.49.10.11"); Test("169.159.208.11"); Test("196.223.144.11"); Test("183.81.132.11"); Test("190.52.48.11"); Test("89.104.128.11"); Test("23.251.128.11"); Test("138.186.12.11"); Test("82.163.96.11"); Test("43.245.128.11"); Test("23.210.60.11"); Test("46.226.136.11"); Test("23.223.204.11"); Test("103.53.16.11"); Test("192.245.148.11"); Test("91.226.220.11"); Test("88.151.192.11"); Test("169.239.132.11"); Test("17.253.114.11"); Test("14.13.176.11"); Test("218.184.11.22"); Test("83.40.189.11"); Test("66.91.192.253"); Test("64.140.149.70"); Test("181.42.29.193"); Test("179.9.191.155"); TraceLog.Debug("RecommendRealmByIP.OnTickTest Cost Time {0} ms", (DateTime.Now - cur).TotalMilliseconds); } private static void Test(string ip) { string country, city; TraceLog.Debug("RecommendRealmByIP.Test ip {0} region {1} realm {2}", ip, GetRegionByIP(ip), GetRealmInfoByIP(ip, out country, out city)); } private static string GetRegionByIP(string ip) { if(string.IsNullOrEmpty(ip)) { return null; } try { if (m_search == null) { string filePath = @"../cfg/sog/ip2region.db"; if (!File.Exists(filePath)) { TraceLog.Error("RecommendRealmByIP.GetRegionByIP not find file {0}", filePath); return null; } m_search = new DbSearcher(filePath); } return m_search.MemorySearch(ip)?.Region; } catch (Exception e) { TraceLog.Error("RecommendRealmByIP.GetRegionByIP Exception:{0}", e.Message); TraceLog.Error("RecommendRealmByIP.GetRegionByIP Exception StackTrace:{0}", e.StackTrace); return null; } } public static AreaRealm GetAreaByIP(string ip) { string country, city; var areaName = GetRealmInfoByIP(ip, out country, out city); //测试代码 //if (ip == "192.168.1.189") //{ // areaName = "NA"; //} if (string.IsNullOrEmpty(areaName)) { areaName = "Asia"; TraceLog.Error("RecommendRealmByIP.GetAreaByIP not config area region info ,ip {0} country {1} city {2}", ip, country, city); // return null; } if(RealmlistSvc.areaDict.ContainsKey(areaName)) { return RealmlistSvc.areaDict[areaName]; } return null; } public static void GetIpCountry(string ip, out string country, out string city) { city = string.Empty; country = string.Empty; string region = GetRegionByIP(ip); //格式 中国|0|上海|上海市|移动 if (region == null) { return; } string[] strs = region.Split("|"); if (strs.Length >= 3)//城市 { city = strs[2]; } if (strs.Length >= 1)//国家 { country = strs[0]; } } //在返回给前端的list中的推荐服中,根据ip找到合适的推荐服 private static string GetRealmInfoByIP(string ip, out string country, out string city) { GetIpCountry(ip, out country, out city); //格式 中国|0|上海|上海市|移动 if(string.IsNullOrEmpty(country)) { return null; } string areaNameByCity = string.Empty; string areaNameByCountry = string.Empty; if(cache.ContainsKey(city))//城市 { areaNameByCity = cache[city]; } if (cache.ContainsKey(country))//国家 { areaNameByCountry = cache[country]; } TraceLog.Trace("RecommendRealmByIP.GetRealmInfoByIP ip {0} country {1} city {2} areaNameByCity {3} areaNameByCountry {4}", ip, country, city, areaNameByCity, areaNameByCountry); if (!string.IsNullOrEmpty(areaNameByCity)) { return areaNameByCity; } else if (!string.IsNullOrEmpty(areaNameByCountry)) { return areaNameByCountry; } return null; } } }