private static void Main(string[] args) { try { QQWryLocator qqWry = new QQWryLocator(@"D:\QQWry.Dat"); //初始化数据库文件,并获得IP记录数,通过Count可以获得 IPLocation ip = qqWry.Query("222.71.108.200"); //查询一个IP地址 Console.WriteLine("{0} {1} {2}", ip.IP, ip.Country, ip.Local); Stopwatch stopwatch = new Stopwatch(); List <string> ips = new List <string> { "218.5.3.128", "120.67.217.7", "125.78.67.175", "220.250.64.23", "218.5.3.128", "120.67.217.7", "125.78.67.175", "220.250.64.23" }; stopwatch.Start(); for (int i = 0; i < 100; i++) { foreach (string item in ips) { ip = qqWry.Query(item); Console.WriteLine("{0} {1} {2}", ip.IP, ip.Country, ip.Local); } } stopwatch.Stop(); Console.WriteLine("查询了800次IP,QQWryLocator 花了{0} ms", stopwatch.ElapsedMilliseconds); } catch (Exception e) { Console.WriteLine(e.Message); } Console.Read(); }
[AsyncTimeout(30000)]// 超时时间30秒钟 public void IndexAsync(long?JournalID) { IAccessLogService logService = ServiceContainer.Instance.Container.Resolve <IAccessLogService>(); HttpBrowserCapabilitiesBase bc = HttpContext.Request.Browser; AccessLog stat = new AccessLog(); stat.JournalID = JournalID == null ? 0 : JournalID.Value; stat.Browser = bc.Browser; stat.BrowserType = bc.Type; stat.Version = bc.Version; stat.Platform = bc.Platform; stat.UrlReferrer = HttpContext.Request.UrlReferrer == null ? "" : HttpContext.Request.UrlReferrer.ToString(); stat.UserHostAddress = Utils.GetRealIP(); stat.HttpMethod = HttpContext.Request.HttpMethod; stat.IsAuthenticated = HttpContext.Request.IsAuthenticated; stat.LogDateTime = DateTime.Now.ToLocalTime(); try { QQWryLocator ipLocator = new QQWryLocator(); IPLocation ipInfo = ipLocator.Query(stat.UserHostAddress); stat.Country = ipInfo.Country; stat.City = ipInfo.Local; logService.AddAccessLog(stat); //参数要放在这个字典里面实现向Completed action传递 //AsyncManager.Parameters["ExecResult"] = "<script> var result = 'success';</script>"; } catch (Exception ex) { LogProvider.Instance.Error("访问日志统计:" + ex.Message); //AsyncManager.Parameters["ExecResult"] = "error:" + ex.Message; } AsyncManager.OutstandingOperations.Decrement(); }
public void QueryTest() { QQWryLocator _qqWry = new QQWryLocator(@"D:\OneDrive\软件\开发\qqwry\qqwry.dat"); IPLocation _ip = _qqWry.Query("116.226.81.32"); Assert.AreEqual("上海市 电信", string.Format("{0} {1}", _ip.Country, _ip.Local)); }
/// <summary> /// /// </summary> /// <param name="ip"></param> public static string GetIpCity(string dbPath, string strIp) { QQWryLocator qqWry = new QQWryLocator(dbPath); //初始化数据库文件,并获得IP记录数,通过Count可以获得 IPLocation ip = qqWry.Query(strIp); //查询一个IP地址 return(ip.Country.Trim()); //Stopwatch stopwatch = new Stopwatch(); //stopwatch.Start(); //stopwatch.Stop(); //Console.WriteLine("一共花了{0} ms的时间", stopwatch.ElapsedMilliseconds); }
//解析扩展字段 private void ParseExtendField(LogRecord record) { if (qqWry != null) { string ipLocation = qqWry.Query(record.ClientIP).Country; record.ClientIPLocation = ipLocation; } record.UriStemAlias = ""; foreach (KeyValuePair <string, string> kvp in dicUriStemAliasMapping) { if (record.UriStem.Trim().ToLower() == kvp.Key.Trim().ToLower()) { record.UriStemAlias = kvp.Value; break; } } record.RefererAlias = ""; foreach (KeyValuePair <string, string> kvp in dicRefererAliasMapping) { if (record.Referer.ToLower().StartsWith(kvp.Key.ToLower())) { record.RefererAlias = kvp.Value; break; } } record.UserAgentAliasList = new List <string>(); foreach (KeyValuePair <string, string> kvp in dicUserAgentAliasMapping) { //多次匹配统计,一条日志记录的用户代理字段可以匹配多个别名 if (record.UserAgent.ToLower().Contains(kvp.Key.ToLower())) { record.UserAgentAliasList.Add(kvp.Value); if (dicUserAgentAlias.ContainsKey(kvp.Value)) { int val = dicUserAgentAlias[kvp.Value]; dicUserAgentAlias[kvp.Value] = val + 1; } else { dicUserAgentAlias.Add(kvp.Value, 1); } } } }
private static void Tracer(object source, ElapsedEventArgs e) { for (int i = 0; i < MaxHop; i++) { // Must store value in here, if not will cause incorrect value int Num = i; int Hop = Num + 1; new Thread(() => { // Slow down First Hop Speed // BECAUSE: some router block fast icmp requestion. if (Hop == 1 && NTR_1HOPCOUNT++ % 2 != 0) { return; } string NewHost = Trace.GetRouterIpByHop(Hop); NtrResultList[Num].Hop = Hop; // If new Hop Hostname is empty means no route get if (NewHost != string.Empty) { NtrResultList[Num].Host = NewHost; } // Packets Rtt count long LastPing = Trace.GetDestRttByHop(Hop); long BestPing = NtrResultList[Num].Best; long BestPingCbrt = BestPing; long WrstPing = NtrResultList[Num].Wrst; NtrResultList[Num].Last = LastPing; if (BestPing == NTR_TIMEDOUT) // Calibrate when -1 means no value { BestPingCbrt = Timeout; } if (LastPing < BestPingCbrt && LastPing != NTR_TIMEDOUT) // Best (Compare with Calibrated value) { NtrResultList[Num].Best = LastPing; } if (LastPing > WrstPing) // Worst { NtrResultList[Num].Wrst = LastPing; } // Average Rtt count if (LastPing > NTR_TIMEDOUT) { if (NtrResultList[Num].Rtts.Count < 128) { NtrResultList[Num].Rtts.Add(LastPing); } else { NtrResultList[Num].Rtts.RemoveAt(0); // remove first NtrResultList[Num].Rtts.Add(LastPing); } } if (NtrResultList[Num].Rtts.Count > 0) { NtrResultList[Num].Avg = Convert.ToInt32(NtrResultList[Num].Rtts.Average()); } // Packets sent and loss count if (LastPing == NTR_TIMEDOUT) // Count Lost Ping { NtrResultList[Num].LoCn++; } long TotalSent = ++NtrResultList[Num].Sent; float LossCount = NtrResultList[Num].LoCn; NtrResultList[Num].Loss = Convert.ToInt32((LossCount / TotalSent) * 100); // Try to Get Geo Location if (IPAddress.TryParse(NtrResultList[Num].Host, out _)) { try { if (ipv6) { if (NtrResultList[Num].Geo == String.Empty) { NtrResultList[Num].Geo = "-"; NtrResultList[Num].Geo = Ip2Api.Parse(NtrResultList[Num].Host); } } if (UseIPIPGeo) { if (NtrResultList[Num].Geo == String.Empty) { NtrResultList[Num].Geo = "-"; NtrResultList[Num].Geo = IPIPNet.Parse(NtrResultList[Num].Host); } } else { var Location = QQWry.Query(NtrResultList[Num].Host); NtrResultList[Num].Geo = Location.Country + " " + Location.Local; } } catch { } } // Try to Get BGP AS Number if (EnableASN) { NtrResultList[Num].ASN = AsnHelper.Parse(NtrResultList[Num].Host); } else { NtrResultList[Num].ASN = "- -"; } }).Start(); } }
/// <summary> /// QQ 根据ip获取地理位置 /// </summary> /// <param name="ip">ip</param> /// <returns></returns> public static IPLocation QQGetAddress(string ip) { QQWryLocator qqwry = new QQWryLocator(Converter.GetMapPath("qqwry.dat")); return(qqwry.Query(ip)); }