|
@@ -11,6 +11,9 @@ using WingServerCommon.Log;
|
|
|
using WingInterfaceLibrary.Interface.DBInterface;
|
|
|
using WingInterfaceLibrary.Request.DBRequest;
|
|
|
using System.Linq;
|
|
|
+using WingServerCommon.Config;
|
|
|
+using WingServerCommon.Config.Parameters;
|
|
|
+using System.Net;
|
|
|
|
|
|
namespace WingCloudServer.InteractionCenter
|
|
|
{
|
|
@@ -23,6 +26,14 @@ namespace WingCloudServer.InteractionCenter
|
|
|
|
|
|
private IDistributedServerInfoDBService _distributedServerInfoDBService;
|
|
|
|
|
|
+ private string _fastServerHost = ConfigurationManager.GetParammeter<StringParameter>("FastServer", "ServerHost").Value;
|
|
|
+
|
|
|
+ private HttpListener _listener = new HttpListener();
|
|
|
+
|
|
|
+ private IAuthenticationService _authenticationService;
|
|
|
+
|
|
|
+ private ITokenDBService _tokenDBService;
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 默认构造
|
|
|
/// </summary>
|
|
@@ -65,11 +76,237 @@ namespace WingCloudServer.InteractionCenter
|
|
|
public override void Load(JsonRpcClientPool jsonRpcClientPool)
|
|
|
{
|
|
|
base.Load(jsonRpcClientPool);
|
|
|
- _distributedServerInfoDBService = GetProxy<IDistributedServerInfoDBService>();
|
|
|
- LoadDBServerInfo();
|
|
|
+ _distributedServerInfoDBService = GetProxy<IDistributedServerInfoDBService>();
|
|
|
+ _authenticationService = GetProxy<IAuthenticationService>();
|
|
|
+ _tokenDBService = GetProxy<ITokenDBService>();
|
|
|
+ LoadDBServerInfo();
|
|
|
+ InitVinnoCloudStorageService(_fastServerHost);
|
|
|
+ }
|
|
|
+
|
|
|
+ #region 监听服务
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 初始化监听
|
|
|
+ /// </summary>
|
|
|
+ private void InitVinnoCloudStorageService(string fastServerHost = "http://*:9304/")
|
|
|
+ {
|
|
|
+ //添加已经挂在的静态服务的监听
|
|
|
+ _listener.Prefixes.Add(fastServerHost);
|
|
|
+ _listener.Start();
|
|
|
+ Task.Run(async () =>
|
|
|
+ {
|
|
|
+ while (true)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var context = await _listener.GetContextAsync().ConfigureAwait(false);
|
|
|
+ HandleHttpContext(context);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ Logger.WriteLineError($"ListenFastServerError:{ex}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 最快服务监听
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="context">监听到的上下问对象</param>
|
|
|
+ /// <returns>输出文件流</returns>
|
|
|
+ private async void HandleHttpContext(HttpListenerContext context)
|
|
|
+ {
|
|
|
+ context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
|
|
|
+ if (context.Request.HttpMethod == "OPTIONS")
|
|
|
+ {
|
|
|
+ JsonPResponse(context);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var requestContent = new
|
|
|
+ {
|
|
|
+ RequestUrl = context.Request?.Url?.AbsoluteUri,
|
|
|
+ Authorization = context.Request?.Headers?["Authorization"],
|
|
|
+ HttpMethod = context.Request?.HttpMethod
|
|
|
+ };
|
|
|
+ if (context.Request.HttpMethod == "GET")
|
|
|
+ {
|
|
|
+ GetMethodResponse(context);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var msg = "{\"Result\":\"Not Support Current Request Error!\"}";
|
|
|
+ ResponseContent(context, false, msg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Get请求
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="context">监听到的上下问对象</param>
|
|
|
+ /// <returns>输出文件流</returns>
|
|
|
+ private async void GetMethodResponse(HttpListenerContext context)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ //读地址
|
|
|
+ var uri = context.Request.Url;
|
|
|
+ if (uri != null)
|
|
|
+ {
|
|
|
+ var absPath = uri.AbsolutePath;
|
|
|
+ //拦截,处理doc文档
|
|
|
+ if (absPath.Contains("GetConnectSpeed"))
|
|
|
+ {
|
|
|
+ var requestAcceptTime = await ToTimestamp(DateTime.Now, true);
|
|
|
+ var msgContent = "{\"Result\":\"Success\",\"RequestAcceptTime\":\"" + requestAcceptTime + "\"}";
|
|
|
+ ResponseContent(context, true, msgContent);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ else if (absPath.Contains("SendConnectMessage"))
|
|
|
+ {
|
|
|
+ var requestToken = context.Request.QueryString["Token"];;
|
|
|
+ string requestIp = null;
|
|
|
+ if (context.Request.RemoteEndPoint != null)
|
|
|
+ {
|
|
|
+ requestIp = context.Request.RemoteEndPoint.Address.ToString();
|
|
|
+ }
|
|
|
+ var tokenInfo = await ValidateTokenAsync(requestToken);
|
|
|
+ if (tokenInfo != null)
|
|
|
+ {
|
|
|
+ tokenInfo.IpValue = IpToLong(requestIp);
|
|
|
+ var res = await _tokenDBService.UpdateTokenAsync(tokenInfo.Code, tokenInfo);
|
|
|
+ if (res) {
|
|
|
+ _fastestServerManager.Remove(tokenInfo.Code);
|
|
|
+ var msgContent = "{\"Result\":\"Success\"}";
|
|
|
+ ResponseContent(context, true, msgContent);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (absPath.Contains("GetServerConfig"))
|
|
|
+ {
|
|
|
+ List<ServerInfoDTO> serverList = await GetServerInfoFromCacheAsync(new QueryServerInfoRequest());
|
|
|
+ var jsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(serverList);
|
|
|
+ var msgContent = "{\"Result\":\"Success\",\"Data\":" + jsonStr + "}";
|
|
|
+ ResponseContent(context, true, msgContent);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ResponseContent(context, false, "{\"Result\":\"Fail\"}");
|
|
|
+ }
|
|
|
+ catch(Exception ex)
|
|
|
+ {
|
|
|
+ Logger.WriteLineError($"GetError:{ex}");
|
|
|
+ var msg = "{\"Result\":\"Fail\"}";
|
|
|
+ ResponseContent(context, false, msg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// ip地址转long
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="ipAddress"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private long IpToLong(string ipAddress)
|
|
|
+ {
|
|
|
+ byte[] byts = IPAddress.Parse(ipAddress).GetAddressBytes();
|
|
|
+ Array.Reverse(byts); // 需要倒置一次字节序
|
|
|
+ long ipLong = BitConverter.ToUInt32(byts, 0);
|
|
|
+ return ipLong;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 时间转长整形的时间戳
|
|
|
+ /// </summary>
|
|
|
+ /// <returns>""</returns>
|
|
|
+ private async Task<TokenDTO> ValidateTokenAsync(string token)
|
|
|
+ {
|
|
|
+ var request = new WingInterfaceLibrary.Request.Authentication.ValidateTokenRequest() { Token = token };
|
|
|
+ var result = await _authenticationService.ValidateTokenAsync(request);
|
|
|
+ //Check 权限
|
|
|
+ if (result != null && result.Code != WingInterfaceLibrary.Enum.CustomerRpcCode.Ok)
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return result.Token; //return User code
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 时间转长整形的时间戳
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="time">当前时间</param>
|
|
|
+ /// <param name="isUtc">是否utc格式时间戳</param>
|
|
|
+ /// <returns>true</returns>
|
|
|
+ private async Task<long> ToTimestamp(DateTime time, bool isUtc = false)
|
|
|
+ {
|
|
|
+ DateTime refTime = TimeZoneInfo.ConvertTimeFromUtc(new DateTime(1970, 1, 1), TimeZoneInfo.Utc);
|
|
|
+ var diffSpan = (isUtc ? time : time.ToUniversalTime()) - refTime;
|
|
|
+ long unixTime = (long)Math.Round(diffSpan.TotalMilliseconds, MidpointRounding.AwayFromZero);
|
|
|
+ return unixTime;
|
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
+ /// <summary>
|
|
|
+ /// 跨域访问
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="context">监听到的上下问对象</param>
|
|
|
+ /// <returns>输出文件流</returns>
|
|
|
+ private async void JsonPResponse(HttpListenerContext context)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ //后台跨域请求,必须配置
|
|
|
+ context.Response.AppendHeader("Access-Control-Allow-Headers", "*");
|
|
|
+ context.Response.AddHeader("Access-Control-Allow-Credentials", "true");
|
|
|
+ context.Response.AddHeader("Access-Control-Max-Age", "1800");
|
|
|
+ context.Response.AppendHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
|
|
|
+ ResponseContent(context, true);
|
|
|
+ }
|
|
|
+ catch(Exception ex)
|
|
|
+ {
|
|
|
+ Logger.WriteLineError($"OPTIONSError:{ex}");
|
|
|
+ var msg = "{\"Result\":\"OPTIONSError:{" + ex + "}\"}";
|
|
|
+ ResponseContent(context, false, msg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 输出结果
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="context">监听到的上下问对象</param>
|
|
|
+ /// <param name="isSuccess">是否成功:true </param>
|
|
|
+ /// <param name="code">错误码</param>
|
|
|
+ /// <param name="msg">内容</param>
|
|
|
+ /// <returns>输出结果</returns>
|
|
|
+ private void ResponseContent(HttpListenerContext context, bool isSuccess = false, string data = "")
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ context.Response.ContentType = "application/json";
|
|
|
+ if (isSuccess) {
|
|
|
+ context.Response.StatusCode = (int)HttpStatusCode.OK;
|
|
|
+ context.Response.StatusDescription = "OK";
|
|
|
+ }
|
|
|
+ context.Response.AddHeader("Access-Control-Allow-Origin", "*");
|
|
|
+ context.Response.AddHeader("Access-Control-Allow-Credentials", "true");
|
|
|
+ context.Response.AddHeader("Access-Control-Max-Age", "1800");
|
|
|
+ byte[] bytesR = System.Text.Encoding.UTF8.GetBytes(data);
|
|
|
+ context.Response.ContentLength64 = bytesR.Length;
|
|
|
+ context.Response.OutputStream.Write(bytesR, 0, bytesR.Length);
|
|
|
+ //输出结束并关闭流
|
|
|
+ context.Response.OutputStream.Flush();
|
|
|
+ context.Response.OutputStream.Close();
|
|
|
+ }
|
|
|
+ catch(Exception ex)
|
|
|
+ {
|
|
|
+ Logger.WriteLineError($"ResponseContent Error:{ex}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
/// 加载ServerInfo到内存
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
@@ -96,7 +333,7 @@ namespace WingCloudServer.InteractionCenter
|
|
|
private async Task<IList<ServerInformation>> GetServerInformationList(IList<string> ids)
|
|
|
{
|
|
|
IList<ServerInformation> serverInformationList = new List<ServerInformation>();
|
|
|
- if (ids?.Count > 0) {
|
|
|
+ if (ids == null || ids.Count <= 0) {
|
|
|
var request = new GetDistributedServerInfoDBPagesRequest(){
|
|
|
CurrentPage = 1,
|
|
|
PageSize = 1000
|
|
@@ -104,6 +341,7 @@ namespace WingCloudServer.InteractionCenter
|
|
|
var serverPageInfo = await _distributedServerInfoDBService.GetDistributedServerInfoPagesAsync(request);
|
|
|
if (serverPageInfo != null && serverPageInfo.TotalCount > 0 && serverPageInfo.PageData != null && serverPageInfo.PageData.Any()) {
|
|
|
serverInformationList = serverPageInfo.PageData.Select(d => new ServerInformation{
|
|
|
+ Code = d.ServerCode,
|
|
|
Name = d.Name,
|
|
|
Host = d.ServerUrl
|
|
|
}).ToList();
|
|
@@ -117,6 +355,7 @@ namespace WingCloudServer.InteractionCenter
|
|
|
var serverPageInfo = await _distributedServerInfoDBService.GetDistributedServerInfoDBByCodesAsync(request);
|
|
|
if (serverPageInfo != null && serverPageInfo.Any()) {
|
|
|
serverInformationList = serverPageInfo.Select(d => new ServerInformation{
|
|
|
+ Code = d.ServerCode,
|
|
|
Name = d.Name,
|
|
|
Host = d.ServerUrl
|
|
|
}).ToList();
|
|
@@ -135,13 +374,6 @@ namespace WingCloudServer.InteractionCenter
|
|
|
try
|
|
|
{
|
|
|
_fastestServerManager.Remove(serverInformation.Code);
|
|
|
- // var tokenDB = await _tokenDBService.FindTokenByValueAsync(token.Code);
|
|
|
- // if (tokenDB != null && !string.IsNullOrWhiteSpace(tokenDB.Code))
|
|
|
- // {
|
|
|
- // tokenDB.Expiration = token.Expiration;
|
|
|
- // await _tokenDBService.UpdateTokenAsync(token.Code, tokenDB);
|
|
|
- // _serverTokenManager.Remove(tokenDB.Code);
|
|
|
- // }
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|