|
@@ -17,6 +17,8 @@ using WingInterfaceLibrary.Enum;
|
|
|
using WingInterfaceLibrary.Interface;
|
|
|
using WingInterfaceLibrary.Request.Authentication;
|
|
|
using WingInterfaceLibrary.Result.Device;
|
|
|
+using WingServerCommon.Config;
|
|
|
+using WingServerCommon.Config.Parameters;
|
|
|
using WingServerCommon.Interfaces.Cache;
|
|
|
using WingServerCommon.Log;
|
|
|
using WingServerCommon.Service;
|
|
@@ -41,8 +43,20 @@ namespace WingCloudServer.Plugin
|
|
|
protected IAuthenticationService _authenticationService;
|
|
|
private string _apiName = string.Empty;
|
|
|
|
|
|
+ private string _ipLocationServiceUrl = ConfigurationManager.GetParammeter<StringParameter>("FastServer", "IpLocationServiceUrl").Value;
|
|
|
+
|
|
|
+ private bool _isUsePositionService = ConfigurationManager.GetParammeter<BoolParameter>("FastServer", "IsUsePositionService").Value;
|
|
|
+
|
|
|
+ private const string _china = "ä¸å›½";
|
|
|
+
|
|
|
+ private const string _hongKong = "香港";
|
|
|
+
|
|
|
+ private const string _macao = "澳门";
|
|
|
+
|
|
|
+ private const string _taiwan = "�湾";
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
public override void Load(JsonRpcClientPool jsonRpcClientPool)
|
|
|
{
|
|
@@ -51,9 +65,9 @@ namespace WingCloudServer.Plugin
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
public PluginProcessResult PreProcess(IJsonRpcHttpContext context, byte[] requestData)
|
|
@@ -65,28 +79,36 @@ namespace WingCloudServer.Plugin
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
public PluginProcessResult PostProcess(IJsonRpcHttpContext context, byte[] responseData)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
+ if (_apiName != "IVinnoServerService/GetServerInfoListAsync")
|
|
|
+ {
|
|
|
+ return new PluginProcessResult(responseData, false);
|
|
|
+ }
|
|
|
var ipAddress = context.RemoteEndPoint?.Address.ToString();
|
|
|
var ipLongAddress = IpToLong(ipAddress);
|
|
|
var serverInformationList = CacheMaintenance.Instance.Get<IDistributedServerInfosManager>()
|
|
|
.Where(x => !string.IsNullOrEmpty(x.Name) && !string.IsNullOrEmpty(x.ServerUrl))?.ToList();
|
|
|
var fasterServerInfo = new ServerInfoDTO();
|
|
|
var parallelOption = new ParallelOptions() { MaxDegreeOfParallelism = 4 };
|
|
|
-
|
|
|
- Parallel.ForEach(serverInformationList, parallelOption, (item, parallelLoopState) => {
|
|
|
- if (item.AssignClientIPList != null && item.AssignClientIPList.Any()) {
|
|
|
+
|
|
|
+ Parallel.ForEach(serverInformationList, parallelOption, (item, parallelLoopState) =>
|
|
|
+ {
|
|
|
+ if (item.AssignClientIPList != null && item.AssignClientIPList.Any())
|
|
|
+ {
|
|
|
var ipInfo = item.AssignClientIPList.Find(c => c.LongStartIP >= ipLongAddress && c.LongEndIP <= ipLongAddress);
|
|
|
- if (ipInfo != null && !string.IsNullOrEmpty(ipInfo.StartIp)) {
|
|
|
+ if (ipInfo != null && !string.IsNullOrEmpty(ipInfo.StartIp))
|
|
|
+ {
|
|
|
|
|
|
- fasterServerInfo = new ServerInfoDTO() {
|
|
|
+ fasterServerInfo = new ServerInfoDTO()
|
|
|
+ {
|
|
|
Name = item.Name,
|
|
|
Host = item.ServerUrl
|
|
|
};
|
|
@@ -96,63 +118,84 @@ namespace WingCloudServer.Plugin
|
|
|
}
|
|
|
});
|
|
|
List<ServerInfoDTO> list = new List<ServerInfoDTO>();
|
|
|
- if (fasterServerInfo != null && !string.IsNullOrEmpty(fasterServerInfo.Name)) {
|
|
|
+ if (fasterServerInfo != null && !string.IsNullOrEmpty(fasterServerInfo.Name))
|
|
|
+ {
|
|
|
|
|
|
list.Add(fasterServerInfo);
|
|
|
-
|
|
|
- var otherServerList = serverInformationList.FindAll(c => c.Name != fasterServerInfo.Name).Select(c => new ServerInfoDTO() {
|
|
|
+
|
|
|
+ var otherServerList = serverInformationList.FindAll(c => c.Name != fasterServerInfo.Name).Select(c => new ServerInfoDTO()
|
|
|
+ {
|
|
|
Name = c.Name,
|
|
|
Host = c.ServerUrl,
|
|
|
}).ToList();
|
|
|
list.AddRange(otherServerList);
|
|
|
}
|
|
|
- else {
|
|
|
-
|
|
|
+ else if (_isUsePositionService)
|
|
|
+ {
|
|
|
+
|
|
|
var locationInfo = GetIpLocationInfo(ipAddress);
|
|
|
- if (locationInfo.ad_info != null && !string.IsNullOrEmpty(locationInfo.ad_info.nation)) {
|
|
|
- if (locationInfo.ad_info.nation == "ä¸å›½") {
|
|
|
+ if (locationInfo.ad_info != null && !string.IsNullOrEmpty(locationInfo.ad_info.nation))
|
|
|
+ {
|
|
|
+ if (locationInfo.ad_info.nation == _china
|
|
|
+ && !locationInfo.ad_info.province.Contains(_hongKong)
|
|
|
+ && !locationInfo.ad_info.province.Contains(_macao)
|
|
|
+ && !locationInfo.ad_info.province.Contains(_taiwan))
|
|
|
+ {
|
|
|
|
|
|
- var fastServerInfo = serverInformationList.FindAll(c => c.Name.Contains("北京") || c.Name.Contains("BJ"))?.Select(c => new ServerInfoDTO() {
|
|
|
+ var fastServerInfo = serverInformationList.FindAll(c => c.Name.Contains("北京") || c.Name.Contains("BJ"))?.Select(c => new ServerInfoDTO()
|
|
|
+ {
|
|
|
Name = c.Name,
|
|
|
Host = c.ServerUrl,
|
|
|
})?.FirstOrDefault() ?? new ServerInfoDTO();
|
|
|
list.Add(fastServerInfo);
|
|
|
-
|
|
|
- var otherServerList = serverInformationList.FindAll(c => c.Name != fastServerInfo.Name).Select(c => new ServerInfoDTO() {
|
|
|
+
|
|
|
+ var otherServerList = serverInformationList.FindAll(c => c.Name != fastServerInfo.Name).Select(c => new ServerInfoDTO()
|
|
|
+ {
|
|
|
Name = c.Name,
|
|
|
Host = c.ServerUrl,
|
|
|
}).ToList();
|
|
|
list.AddRange(otherServerList);
|
|
|
}
|
|
|
- else
|
|
|
+ else
|
|
|
{
|
|
|
var extendList = new List<ServerInfoExtend>();
|
|
|
- foreach(var item in serverInformationList) {
|
|
|
-
|
|
|
- double beelineDistanceTemp = GetDistance(locationInfo.location.lat, locationInfo.location.lng, 0,0);
|
|
|
- var extendInfo = new ServerInfoExtend() {
|
|
|
+ foreach (var item in serverInformationList)
|
|
|
+ {
|
|
|
+
|
|
|
+ double beelineDistanceTemp = GetDistance(locationInfo.location.lat, locationInfo.location.lng, Convert.ToDouble(item.Lat), Convert.ToDouble(item.Lng));
|
|
|
+ var extendInfo = new ServerInfoExtend()
|
|
|
+ {
|
|
|
Name = item.Name,
|
|
|
Host = item.ServerUrl,
|
|
|
BeelineDistance = beelineDistanceTemp
|
|
|
};
|
|
|
extendList.Add(extendInfo);
|
|
|
}
|
|
|
- list = extendList.OrderBy(c => c.BeelineDistance).Select(e => new ServerInfoDTO() {
|
|
|
+ list = extendList.OrderBy(c => c.BeelineDistance).Select(e => new ServerInfoDTO()
|
|
|
+ {
|
|
|
Name = e.Name,
|
|
|
Host = e.Host,
|
|
|
}).ToList();
|
|
|
}
|
|
|
}
|
|
|
- else {
|
|
|
-
|
|
|
- list = serverInformationList.OrderBy(c => c.IsMaster).Select(e => new ServerInfoDTO() {
|
|
|
- Name = e.Name,
|
|
|
- Host = e.ServerUrl,
|
|
|
- }).ToList();
|
|
|
- }
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+ else
|
|
|
+ {
|
|
|
+
|
|
|
+ list = serverInformationList.OrderBy(c => c.IsMaster).Select(e => new ServerInfoDTO()
|
|
|
+ {
|
|
|
+ Name = e.Name,
|
|
|
+ Host = e.ServerUrl,
|
|
|
+ }).ToList();
|
|
|
+ }
|
|
|
+
|
|
|
+ var responses = JsonRpcCodec.DecodeResponsesAsync(responseData, new CancellationToken()).Result;
|
|
|
+ var rpcResults = new List<JsonRpcLite.Services.JsonRpcResponse>();
|
|
|
+ var rpcResulut = new JsonRpcLite.Services.JsonRpcResponse(responses[0].Id);
|
|
|
+ rpcResulut.WriteResult(list);
|
|
|
+ rpcResults.Add(rpcResulut);
|
|
|
+ responseData = JsonRpcCodec.EncodeResponsesAsync(rpcResults.ToArray(), new System.Threading.CancellationToken()).Result;
|
|
|
+ return new PluginProcessResult(responseData, false);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
@@ -160,7 +203,7 @@ namespace WingCloudServer.Plugin
|
|
|
}
|
|
|
return new PluginProcessResult(responseData, false);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -175,13 +218,13 @@ namespace WingCloudServer.Plugin
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
public double GetDistance(double lat1, double lng1, double lat2, double lng2)
|
|
|
{
|
|
@@ -194,7 +237,7 @@ namespace WingCloudServer.Plugin
|
|
|
double result = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2))) * EARTH_RADIUS;
|
|
|
return result;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -206,33 +249,36 @@ namespace WingCloudServer.Plugin
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
- private IPServiceResult GetIpLocationInfo(string ip) {
|
|
|
+ private IPServiceResult GetIpLocationInfo(string ip)
|
|
|
+ {
|
|
|
IPServiceResult info = new IPServiceResult();
|
|
|
- string key = "4Z3BZ-L5RK6-MJ5SC-EXNFA-3V7PH-RSB3U";
|
|
|
- var url = $"https://apis.map.qq.com/ws/location/v1/ip?ip={ip}&key={key}";
|
|
|
+ var url = string.Format(_ipLocationServiceUrl, ip);
|
|
|
try
|
|
|
{
|
|
|
var json = HttpGet(url);
|
|
|
- if (!string.IsNullOrEmpty(json)) {
|
|
|
+ if (!string.IsNullOrEmpty(json))
|
|
|
+ {
|
|
|
var serviceGuideInfo = JsonConvert.DeserializeObject<IPServiceGuide>(json);
|
|
|
- if (serviceGuideInfo != null && serviceGuideInfo.status == 0 && serviceGuideInfo.result != null)
|
|
|
+ if (serviceGuideInfo != null && serviceGuideInfo.status == 0 && serviceGuideInfo.result != null)
|
|
|
{
|
|
|
info = serviceGuideInfo.result;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- catch(Exception ex) {
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
|
|
|
+ Logger.WriteLineError($"GetIpLocationInfo err:{ex},{ex.StackTrace}");
|
|
|
}
|
|
|
return info;
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -244,20 +290,20 @@ namespace WingCloudServer.Plugin
|
|
|
HttpWebRequest request = null;
|
|
|
HttpWebResponse response = null;
|
|
|
|
|
|
-
|
|
|
+
|
|
|
try
|
|
|
{
|
|
|
|
|
|
ServicePointManager.DefaultConnectionLimit = 40;
|
|
|
|
|
|
|
|
|
- * 下�设置HttpWebRequest的相关属性
|
|
|
+ * 下é�¢è®¾ç½®HttpWebRequest的相关属æ€
|
|
|
* ************************************************************/
|
|
|
request = (HttpWebRequest)WebRequest.Create(url);
|
|
|
|
|
|
request.Method = "GET";
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
response = (HttpWebResponse)request.GetResponse();
|
|
|
|
|
|
|
|
@@ -286,45 +332,45 @@ namespace WingCloudServer.Plugin
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
class IPServiceGuide
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
- public int status { get; set;}
|
|
|
+ public int status { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- public string message { get; set;}
|
|
|
+ public string message { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- public IPServiceResult result { get; set;}
|
|
|
+ public IPServiceResult result { get; set; }
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
class IPServiceResult
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
- public string ip { get; set;}
|
|
|
+ public string ip { get; set; }
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
- public IPServiceLocationResult location { get; set;}
|
|
|
+ public IPServiceLocationResult location { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- public IPServiceNationResult ad_info { get; set;}
|
|
|
+ public IPServiceNationResult ad_info { get; set; }
|
|
|
}
|
|
|
|
|
|
|
|
@@ -335,43 +381,43 @@ namespace WingCloudServer.Plugin
|
|
|
|
|
|
|
|
|
|
|
|
- public double lat { get; set;}
|
|
|
+ public double lat { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- public double lng { get; set;}
|
|
|
+ public double lng { get; set; }
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
class IPServiceNationResult
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
- public string nation { get; set;}
|
|
|
+ public string nation { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- public string province { get; set;}
|
|
|
+ public string province { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- public string city { get; set;}
|
|
|
+ public string city { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- public string district { get; set;}
|
|
|
+ public string district { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- public string adcode { get; set;}
|
|
|
+ public string adcode { get; set; }
|
|
|
}
|
|
|
|
|
|
|
|
@@ -380,8 +426,8 @@ namespace WingCloudServer.Plugin
|
|
|
class ServerInfoExtend : ServerInfoDTO
|
|
|
{
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
- public double BeelineDistance { get; set;}
|
|
|
+ public double BeelineDistance { get; set; }
|
|
|
}
|
|
|
}
|