|
@@ -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 = "�湾";
|
|
|
+
|
|
|
/// <summary>
|
|
|
- /// �始化
|
|
|
+ /// åˆ�始åŒ
|
|
|
/// </summary>
|
|
|
public override void Load(JsonRpcClientPool jsonRpcClientPool)
|
|
|
{
|
|
@@ -51,9 +65,9 @@ namespace WingCloudServer.Plugin
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// �处�
|
|
|
+ /// å‰�处ç�
|
|
|
/// </summary>
|
|
|
- /// <param name="context">上下文</param>
|
|
|
+ /// <param name="context">上下æ–/param>
|
|
|
/// <param name="requestData">请求�数</param>
|
|
|
/// <returns></returns>
|
|
|
public PluginProcessResult PreProcess(IJsonRpcHttpContext context, byte[] requestData)
|
|
@@ -65,28 +79,36 @@ namespace WingCloudServer.Plugin
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// �处�
|
|
|
+ /// å�Žå¤„ç�
|
|
|
/// </summary>
|
|
|
- /// <param name="context">上下文</param>
|
|
|
+ /// <param name="context">上下æ–/param>
|
|
|
/// <param name="responseData">�应�数</param>
|
|
|
/// <returns></returns>
|
|
|
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 };
|
|
|
- //本地IP段检索
|
|
|
- Parallel.ForEach(serverInformationList, parallelOption, (item, parallelLoopState) => {
|
|
|
- if (item.AssignClientIPList != null && item.AssignClientIPList.Any()) {
|
|
|
+ //本地IP段检ç´
|
|
|
+ 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))
|
|
|
+ {
|
|
|
//本地库找到æ¤IP
|
|
|
- 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);//item.lat, item.lng);
|
|
|
- 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();
|
|
|
- }
|
|
|
}
|
|
|
-
|
|
|
- //todo 处�输出数�
|
|
|
+ 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);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// ip地�转long
|
|
|
/// </summary>
|
|
@@ -175,13 +218,13 @@ namespace WingCloudServer.Plugin
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 计算两点�置的�离,返回两点的�离,�� 米
|
|
|
- /// 该公�为GOOGLE�供,误差�于0.2米
|
|
|
+ /// 计算两点ä½�置的è·�离,返回两点的è·�离,å�•ä½� ç±
|
|
|
+ /// 该公å¼�为GOOGLEæ��供,误差å°�äº.2ç±
|
|
|
/// </summary>
|
|
|
- /// <param name="lat1">第一点纬度</param>
|
|
|
- /// <param name="lng1">第一点�度</param>
|
|
|
- /// <param name="lat2">第二点纬度</param>
|
|
|
- /// <param name="lng2">第二点�度</param>
|
|
|
+ /// <param name="lat1">第一点纬åº/param>
|
|
|
+ /// <param name="lng1">第一点ç»�åº/param>
|
|
|
+ /// <param name="lat2">第二点纬åº/param>
|
|
|
+ /// <param name="lng2">第二点ç»�åº/param>
|
|
|
/// <returns></returns>
|
|
|
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;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// �纬度转化�弧度
|
|
|
/// </summary>
|
|
@@ -206,33 +249,36 @@ namespace WingCloudServer.Plugin
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 获�ip的定�信�
|
|
|
+ /// 获å�–ip的定ä½�ä¿¡æ�
|
|
|
/// </summary>
|
|
|
/// <param name="d"></param>
|
|
|
/// <returns></returns>
|
|
|
- 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;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 处�http GET请求,返回数�
|
|
|
+ /// 处ç�†http GET请求,返回数æ�
|
|
|
/// </summary>
|
|
|
/// <param name="url">请求的url地�</param>
|
|
|
/// <returns>http GET�功�返回的数�,失败抛WebException异常</returns>
|
|
@@ -244,20 +290,20 @@ namespace WingCloudServer.Plugin
|
|
|
HttpWebRequest request = null;
|
|
|
HttpWebResponse response = null;
|
|
|
|
|
|
- //请求url以获�数�
|
|
|
+ //请求url以获å�–æ•°æ�
|
|
|
try
|
|
|
{
|
|
|
//设置最大连接数
|
|
|
ServicePointManager.DefaultConnectionLimit = 40;
|
|
|
|
|
|
/***************************************************************
|
|
|
- * 下�设置HttpWebRequest的相关属性
|
|
|
+ * 下é�¢è®¾ç½®HttpWebRequest的相关属æ€
|
|
|
* ************************************************************/
|
|
|
request = (HttpWebRequest)WebRequest.Create(url);
|
|
|
|
|
|
request.Method = "GET";
|
|
|
-
|
|
|
- //获��务器返回
|
|
|
+
|
|
|
+ //获å�–æœ�务器返å›
|
|
|
response = (HttpWebResponse)request.GetResponse();
|
|
|
|
|
|
//获�HTTP返回数�
|
|
@@ -286,45 +332,45 @@ namespace WingCloudServer.Plugin
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 腾讯云定��应实体
|
|
|
+ /// 腾讯云定ä½�å“�应实ä½
|
|
|
/// </summary>
|
|
|
class IPServiceGuide
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 状æ€�ç �ï¼›æ£å¸¸ä¸º0
|
|
|
/// </summary>
|
|
|
- public int status { get; set;}
|
|
|
+ public int status { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
|
/// �应信�
|
|
|
/// </summary>
|
|
|
- public string message { get; set;}
|
|
|
+ public string message { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
|
/// �应结果
|
|
|
/// </summary>
|
|
|
- public IPServiceResult result { get; set;}
|
|
|
+ public IPServiceResult result { get; set; }
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 腾讯云定��应结果
|
|
|
+ /// 腾讯云定ä½�å“�应结æž
|
|
|
/// </summary>
|
|
|
class IPServiceResult
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// ip地�
|
|
|
/// </summary>
|
|
|
- public string ip { get; set;}
|
|
|
+ public string ip { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
|
- /// �纬度信�
|
|
|
+ /// ç»�纬度信æ�
|
|
|
/// </summary>
|
|
|
- public IPServiceLocationResult location { get; set;}
|
|
|
+ public IPServiceLocationResult location { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
|
/// 地区信�
|
|
|
/// </summary>
|
|
|
- public IPServiceNationResult ad_info { get; set;}
|
|
|
+ public IPServiceNationResult ad_info { get; set; }
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -335,43 +381,43 @@ namespace WingCloudServer.Plugin
|
|
|
/// <summary>
|
|
|
/// 纬度
|
|
|
/// </summary>
|
|
|
- public double lat { get; set;}
|
|
|
+ public double lat { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
|
/// �度
|
|
|
/// </summary>
|
|
|
- public double lng { get; set;}
|
|
|
+ public double lng { get; set; }
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 腾讯云定�地区信�
|
|
|
+ /// 腾讯云定ä½�地区信æ�
|
|
|
/// </summary>
|
|
|
class IPServiceNationResult
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 国家
|
|
|
/// </summary>
|
|
|
- public string nation { get; set;}
|
|
|
+ public string nation { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
|
/// �份
|
|
|
/// </summary>
|
|
|
- public string province { get; set;}
|
|
|
+ public string province { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
|
/// 城市
|
|
|
/// </summary>
|
|
|
- public string city { get; set;}
|
|
|
+ public string city { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
- public string district { get; set;}
|
|
|
+ public string district { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
|
/// ç¼–ç �
|
|
|
/// </summary>
|
|
|
- public string adcode { get; set;}
|
|
|
+ public string adcode { get; set; }
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -380,8 +426,8 @@ namespace WingCloudServer.Plugin
|
|
|
class ServerInfoExtend : ServerInfoDTO
|
|
|
{
|
|
|
/// <summary>
|
|
|
- /// 最çŸè·�离
|
|
|
+ /// 最çŸè·�ç¦
|
|
|
/// </summary>
|
|
|
- public double BeelineDistance { get; set;}
|
|
|
+ public double BeelineDistance { get; set; }
|
|
|
}
|
|
|
}
|