/// <summary> /// IP位置查询 /// </summary> /// <param name="searcher"><see cref="IIpSearcher"/></param> /// <param name="address">ipv4地址</param> /// <returns> /// IP位置信息 /// </returns> public static IpLocation Search(this IIpSearcher searcher, string address) { if (IPAddress.TryParse(address, out IPAddress ip)) { var octets = ip.GetAddressBytes(); if (octets.Length == 4) { return(searcher.Search(IPv4ToInteger(ip.GetAddressBytes()))); } } return(null); }
static IpHelper() { try { if (IpSettings.DefalutSearcherType == IpSearcherType.China) { searcher = new IpSimpleSearcher(); } if (IpSettings.DefalutSearcherType == IpSearcherType.International) { searcher = new IpComplexSearcher(); } } catch (System.Exception e) { throw new Exception("IPTools initialize failed.", e); } }
static IpTool() { Searcher = new IpLightweightSearcher(); }
static IpTool() { try { #if NETSTANDARD2_0 if (File.Exists(Path.Combine(AppContext.BaseDirectory, IpCnAsmName + ".dll"))) { var ipCnAsm = Assembly.Load(IpCnAsmName); IpChinaSearcher = (IIpSearcher)ipCnAsm.CreateInstance("IPTools.China.IpLightweightSearcher"); } if (File.Exists(Path.Combine(AppContext.BaseDirectory, IpAllAsmName + ".dll"))) { var ipAllAsm = Assembly.Load(IpAllAsmName); IpAllSearcher = (IIpSearcher)ipAllAsm.CreateInstance("IPTools.International.IpHeavyweightSearcher"); } #else #region China if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, IpCnAsmName + ".dll"))) //netfx console { var ipCnAsm = Assembly.Load(IpCnAsmName); IpChinaSearcher = (IIpSearcher)ipCnAsm.CreateInstance("IPTools.China.IpLightweightSearcher"); } else if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin", IpCnAsmName + ".dll"))) // netfx asp.net { var ipCnAsm = Assembly.Load(IpCnAsmName); IpChinaSearcher = (IIpSearcher)ipCnAsm.CreateInstance("IPTools.China.IpLightweightSearcher"); } #endregion #region All if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, IpAllAsmName + ".dll"))) //netfx console { var ipAllAsm = Assembly.Load(IpAllAsmName); IpAllSearcher = (IIpSearcher)ipAllAsm.CreateInstance("IPTools.International.IpHeavyweightSearcher"); } else if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin", IpAllAsmName + ".dll"))) // netfx asp.net { var ipAllAsm = Assembly.Load(IpAllAsmName); IpAllSearcher = (IIpSearcher)ipAllAsm.CreateInstance("IPTools.International.IpHeavyweightSearcher"); } #endregion #endif if (IpChinaSearcher == null && IpAllSearcher == null) { throw new IpToolException("Can not load any IpSearcher."); } else if (IpChinaSearcher != null && IpAllSearcher != null) { DefaultSearcher = IpToolSettings.DefalutSearcherType == IpSearcherType.International ? IpAllSearcher : IpChinaSearcher; } else if (IpChinaSearcher != null) { DefaultSearcher = IpChinaSearcher; } else { DefaultSearcher = IpAllSearcher; } } catch (System.Exception e) { throw new IpToolException("IPTools initialize failed.", e); } }
static IpTool() { try { #if NETSTANDARD2_0 var depJsonName = Path.Combine(AppContext.BaseDirectory, $"{Assembly.GetEntryAssembly().GetName().Name}.deps.json"); var depJsonStr = File.ReadAllText(depJsonName); if (File.Exists(Path.Combine(AppContext.BaseDirectory, IpCnAsmName + ".dll")) || depJsonStr.Contains(IpCnAsmName)) { var ipCnAsm = Assembly.Load(IpCnAsmName); IpChinaSearcher = (IIpSearcher)ipCnAsm.CreateInstance("IPTools.China.IpSimpleSearcher"); } if (File.Exists(Path.Combine(AppContext.BaseDirectory, IpAllAsmName + ".dll")) || depJsonStr.Contains(IpAllAsmName)) { var ipAllAsm = Assembly.Load(IpAllAsmName); IpAllSearcher = (IIpSearcher)ipAllAsm.CreateInstance("IPTools.International.IpComplexSearcher"); } #else #region China if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, IpCnAsmName + ".dll"))) //netfx console { var ipCnAsm = Assembly.Load(IpCnAsmName); IpChinaSearcher = (IIpSearcher)ipCnAsm.CreateInstance("IPTools.China.IpSimpleSearcher"); } else if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin", IpCnAsmName + ".dll"))) // netfx asp.net { var ipCnAsm = Assembly.Load(IpCnAsmName); IpChinaSearcher = (IIpSearcher)ipCnAsm.CreateInstance("IPTools.China.IpSimpleSearcher"); } #endregion #region All if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, IpAllAsmName + ".dll"))) //netfx console { var ipAllAsm = Assembly.Load(IpAllAsmName); IpAllSearcher = (IIpSearcher)ipAllAsm.CreateInstance("IPTools.International.IpComplexSearcher"); } else if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin", IpAllAsmName + ".dll"))) // netfx asp.net { var ipAllAsm = Assembly.Load(IpAllAsmName); IpAllSearcher = (IIpSearcher)ipAllAsm.CreateInstance("IPTools.International.IpComplexSearcher"); } #endregion #endif if (IpChinaSearcher == null && IpAllSearcher == null) { throw new IpToolException("Can not load any IpSearcher."); } else if (IpChinaSearcher != null && IpAllSearcher != null) { DefaultSearcher = IpToolSettings.DefalutSearcherType == IpSearcherType.International ? IpAllSearcher : IpChinaSearcher; } else if (IpChinaSearcher != null) { DefaultSearcher = IpChinaSearcher; } else { DefaultSearcher = IpAllSearcher; } } catch (System.Exception e) { throw new IpToolException("IPTools initialize failed.", e); } }
static IpTool() { Searcher = new IpHeavyweightSearcher(); }