public static ClientIpSource GetAllIpAddress(this HttpRequestMessage request) { var ips = new ClientIpSource(); // 获取反向代理过来的IP IEnumerable <string> values; if (request.Headers.TryGetValues("X-Real-IP", out values)) { ips.HttpClientIp = values.FirstOrDefault(); } if (request.Headers.TryGetValues("request_port", out values)) { ips.IsHttps = values.FirstOrDefault() == "443"; } if (request.Properties.ContainsKey("MS_HttpContext")) { object ctxObj = request.Properties["MS_HttpContext"]; if (ctxObj is HttpContextBase) { var ctx = (HttpContextBase)ctxObj; ips.NginxClientIp = ctx.Request.UserHostAddress; } } return(ips); }
public static ClientIpSource GetAllIpAddress(this HttpRequestBase request) { var ips = new ClientIpSource(); // 获取反向代理过来的IP // 获取IP,有从TcpIP层获取的IP和http header获取的IP ips.NginxClientIp = request.UserHostAddress; ips.HttpClientIp = request.Headers["X-Real-IP"]; ips.IsHttps = request.Headers["request_port"] == "443"; return(ips); }
public static RequestInfo BuildRequestInfo(HttpRequestMessage request) { RequestImeiDto dto; IEnumerable <string> enumerable; RequestInfo info = new RequestInfo { RequestStartTime = DateTime.Now }; KeyValuePair <string, string> pair = request.GetQueryNameValuePairs().FirstOrDefault <KeyValuePair <string, string> >(d => d.Key == "__h"); string encryptedImei = ""; string regionCode = ""; if (string.IsNullOrEmpty(pair.Key)) { info.Lng = request.GetHeaderValue <double>("m-lng"); info.Lat = request.GetHeaderValue <double>("m-lat"); int headerValue = request.GetHeaderValue <int>("m-lt"); info.LocationType = (headerValue != 1) ? LocationType.MapLocation : LocationType.GpsLocation; info.ClientNetType = request.GetHeaderValue("m-nw"); info.InterfaceVersion = request.GetHeaderValue("m-iv"); info.ClientVersion = request.GetHeaderValue("m-cv"); info.ClientType = request.GetHeaderValue <DevicePlatform>("m-ct"); info.ClientWidth = request.GetHeaderValue <int>("m-cw"); info.ClientHeight = request.GetHeaderValue <int>("m-ch"); encryptedImei = request.GetHeaderValue("m-ii"); info.ClientSourceId = request.GetHeaderValue <byte>("m-sr"); regionCode = request.GetHeaderValue("m-lc"); } else { NameValueCollection collection = HttpUtility.ParseQueryString(HttpUtility.UrlDecode(pair.Value)); info.Lng = collection.GetValue <double>("m-lng"); info.Lat = collection.GetValue <double>("m-lat"); info.LocationType = collection.GetValue <LocationType>("m-lt"); info.ClientNetType = collection.GetValue("m-nw"); info.InterfaceVersion = collection.GetValue("m-iv"); info.ClientVersion = collection.GetValue("m-cv"); info.ClientType = collection.GetValue <DevicePlatform>("m-ct"); info.ClientWidth = collection.GetValue <int>("m-cw"); info.ClientHeight = collection.GetValue <int>("m-ch"); encryptedImei = collection.GetValue("m-ii"); string cookies = collection.GetValue("_cookies"); if (!string.IsNullOrEmpty(cookies)) { cookies = HttpUtility.UrlDecode(cookies); request.Headers.Add("Cookie", cookies); } info.ClientSourceId = collection.GetValue <byte>("m-sr"); info.Token = collection.GetValue("token"); if (!string.IsNullOrEmpty(info.Token)) { request.Headers.Add("Cookie", string.Format("{0}={1}", "token", info.Token)); } regionCode = collection.GetValue("m-lc"); } if (!string.IsNullOrEmpty(regionCode)) { try { //TODO:解析区域位置 } catch (Exception exception) { log4Logger.Error("区域位置解析错误", exception); } } string userAgent = request.Headers.UserAgent.ToString(); info.UserAgent = userAgent; if (userAgent.IndexOf("MicroMessenger") > 0) { info.ClientSourceId = 100; info.ClientType = DevicePlatform.Weixin; info.InterfaceVersion = ConfigurationManager.AppSettings["interface_version"]; } if (info.ClientSourceId == 0) { if (info.ClientType == DevicePlatform.Android) { info.ClientSourceId = 1; } else if (info.ClientType == DevicePlatform.Ios) { info.ClientSourceId = 2; } } info.OtherHeader = JsonConvert.SerializeObject(new { ClientSourceId = info.ClientSourceId }); if (info.ClientSourceId != 100) { dto = ImeiSecurity.Decrypt(encryptedImei, request.RequestUri.PathAndQuery, info.UserAgent, info.Lng, info.Lat); } else { dto = new RequestImeiDto(encryptedImei); } info.ImeiInfo = dto; if (request.Headers.TryGetValues("Cookie", out enumerable)) { foreach (string str10 in enumerable) { info.Cookie = info.Cookie + str10 + "~"; } if (info.Cookie.Length > 0) { info.Cookie = info.Cookie.TrimEnd(new char[] { '~' }); } } ClientIpSource clientIpAddress = request.GetClientIpAddress(); info.ClientIP = clientIpAddress.ClientIpFromTcpIp; info.ClientIpHttp = clientIpAddress.ClientIpFromHttp; info.IsHttps = clientIpAddress.IsHttps; info.HttpMethod = request.Method; return(info); }