优化ETL过滤模块
大石头 编写于 2017-08-29 17:11:46
X
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using NewLife.Log;
using NewLife.Web;

namespace NewLife.IP
{
    /// <summary>IP搜索</summary>
    public static class Ip
    {
        private static Object lockHelper = new Object();
        private static Zip zip;

        /// <summary>数据文件</summary>
        public static String DbFile { get; set; }

        static Ip()
        {
            var dir = Runtime.IsWeb ? "..\\Data" : ".";
            var ip = dir.CombinePath("ip.gz");
            if (File.Exists(ip)) DbFile = ip;

            // 删除旧版本
            ip = "App_Data\\ip.gz".GetFullPath();
            if (File.Exists(ip))
            {
                File.Delete(ip);
                var di = "App_Data".AsDirectory();
                if (di.GetFiles().Length == 0) di.Delete(true);
            }

            // 如果本地没有IP数据库,则从网络下载
            if (DbFile.IsNullOrWhiteSpace())
            {
                Task.Factory.StartNew(() =>
                {
                    var url = Setting.Current.PluginServer;
                    XTrace.WriteLine("没有找到IP数据库,准备联网获取 {0}", url);

                    var client = new WebClientX();
                    client.Log = XTrace.Log;
                    var file = client.DownloadLink(url, "ip.gz", dir.GetFullPath());

                    if (File.Exists(file))
                    {
                        DbFile = file.GetFullPath();
                        zip = null;
                        // 让它重新初始化
                        _inited = null;
                    }
                }, TaskCreationOptions.LongRunning).LogException();
            }
        }

        static Boolean? _inited;
        static Boolean Init()
        {
            if (_inited != null) return _inited.Value;
            lock (typeof(Ip))
            {
                if (_inited != null) return _inited.Value;
                _inited = false;

                var z = new Zip();

                if (!File.Exists(DbFile))
                {
                    //throw new InvalidOperationException("无法找到IP数据库" + DbFile + "!");
                    XTrace.WriteLine("无法找到IP数据库{0}", DbFile);
                    return false;
                }
                XTrace.WriteLine("使用IP数据库{0}", DbFile);
                using (var fs = File.OpenRead(DbFile))
                {
                    try
                    {
                        z.SetStream(fs);
                    }
                    catch (Exception ex)
                    {
                        XTrace.WriteException(ex);

                        return false;
                    }
                }
                zip = z;
            }

            if (zip.Stream == null) throw new InvalidOperationException("无法打开IP数据库" + DbFile + "!");

            _inited = true;
            return true;
        }

        /// <summary>获取IP地址</summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static String GetAddress(String ip)
        {
            if (String.IsNullOrEmpty(ip)) return "";

            if (!Init()) return "";

            var ip2 = IPToUInt32(ip.Trim());
            lock (lockHelper)
            {
                return zip.GetAddress(ip2) + "";
            }
        }

        /// <summary>获取IP地址</summary>
        /// <param name="addr"></param>
        /// <returns></returns>
        public static String GetAddress(IPAddress addr)
        {
            if (addr == null) return "";

            if (!Init()) return "";

            var ip2 = (UInt32)addr.GetAddressBytes().Reverse().ToInt();
            lock (lockHelper)
            {
                return zip.GetAddress(ip2) + "";
            }
        }

        static UInt32 IPToUInt32(String IpValue)
        {
            var ss = IpValue.Split('.');
            var buf = new Byte[4];
            for (var i = 0; i < 4; i++)
            {
                var n = 0;
                if (i < ss.Length && Int32.TryParse(ss[i], out n))
                {
                    buf[3 - i] = (Byte)n;
                }
            }
            return BitConverter.ToUInt32(buf, 0);
        }
    }

    class MyIpProvider : NetHelper.IpProvider
    {
        public String GetAddress(IPAddress addr)
        {
            return Ip.GetAddress(addr);
        }
    }
}