Exemplo n.º 1
0
        /// <summary>
        /// 更换IP 
        /// </summary>
        /// <param name="netType">0表示路由 1表示拨号连接</param>
        /// <param name="msg">通知输出消息</param>
        /// <param name="done">完成时执行</param>
        /// <param name="index">第几次执行</param>
        /// <returns></returns>
        public static string ChangeIP(int netType, Action<string> msg = null, Pub.Class.Action done = null, int index = 0)
        {
            string name = GetNetName(netType);
            setting = SendSettingHelper.SelectByID(1);

            if (setting.IsNull()) {
                if (!msg.IsNull()) msg("请修改发送设置!");
                if (!done.IsNull()) done();
                return "";
            } else {
                if (index == setting.MaxRetryCount) {
                    if (!done.IsNull()) done();
                    return "";
                }
                if (!msg.IsNull()) msg((index + 1).ToString());

                //清空多少分钟前的历史IP
                if (Data.Pool("ConnString").DBType == "SqlServer") {
                    "delete from IpHistory where CreateTime < DateAdd(MINUTE , -{0}, getdate())".FormatWith(setting.DeleteInterval).ToSQL().ToExec();
                } else if (Data.Pool("ConnString").DBType == "SQLite" || Data.Pool("ConnString").DBType == "MonoSQLite") {
                    "delete from IpHistory where datetime(CreateTime) < datetime('now','localtime', '-{0} minute')".FormatWith(setting.DeleteInterval).ToSQL().ToExec();
                }
                if (!msg.IsNull()) msg("正在重启" + name + "......");
                IController connect;
                switch (netType) {
                    case 1: connect = new ModelController(); break;
                    case 2: connect = new TianYiController(); break;
                    default: connect = new RouteController(); break;
                }
                string error = connect.Reset();
                if (!error.IsNullEmpty()) {
                    if (!msg.IsNull()) msg("重启" + name + "失败:" + error);
                    return ChangeIP(netType, msg, done, index + 1);
                } else {
                    if (!msg.IsNull()) msg("已重启" + name + ",正在检测是否联网......");
                    bool isTrue = NetHelper.CheckNetwork(msg);
                    if (!isTrue) return ChangeIP(netType, msg, done, index + 1);

                    if (!msg.IsNull()) msg("已联接网络,正在获取IP......");
                    string ip = IPHelper.GetIpFast();

                    if (!msg.IsNull()) msg("获取到IP:" + ip);
                    if (IpHistoryHelper.IsExistByID(ip)) {
                        if (!msg.IsNull()) msg("检测到IP:" + ip + "最近已使用!");
                        return ChangeIP(netType, msg, done, index + 1);
                    } else {
                        IpHistoryHelper.Insert(new IpHistory() { IP = ip, CreateTime = DateTime.Now.ToDateTime().ToDateTime() });
                    };
                    return ip;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 更换IP
        /// </summary>
        /// <param name="netType">0表示路由 1表示拨号连接</param>
        /// <param name="msg">通知输出消息</param>
        /// <param name="done">完成时执行</param>
        /// <param name="index">第几次执行</param>
        /// <returns></returns>
        public static string ChangeIP(int netType, Action <string> msg = null, Pub.Class.Action done = null, int index = 0)
        {
            string name = GetNetName(netType);

            setting = SendSettingHelper.SelectByID(1);

            if (setting.IsNull())
            {
                if (!msg.IsNull())
                {
                    msg("请修改发送设置!");
                }
                if (!done.IsNull())
                {
                    done();
                }
                return("");
            }
            else
            {
                if (index == setting.MaxRetryCount)
                {
                    if (!done.IsNull())
                    {
                        done();
                    }
                    return("");
                }
                if (!msg.IsNull())
                {
                    msg((index + 1).ToString());
                }

                //清空多少分钟前的历史IP
                if (Data.Pool("ConnString").DBType == "SqlServer")
                {
                    "delete from IpHistory where CreateTime < DateAdd(MINUTE , -{0}, getdate())".FormatWith(setting.DeleteInterval).ToSQL().ToExec();
                }
                else if (Data.Pool("ConnString").DBType == "SQLite" || Data.Pool("ConnString").DBType == "MonoSQLite")
                {
                    "delete from IpHistory where datetime(CreateTime) < datetime('now','localtime', '-{0} minute')".FormatWith(setting.DeleteInterval).ToSQL().ToExec();
                }
                if (!msg.IsNull())
                {
                    msg("正在重启" + name + "......");
                }
                IController connect;
                switch (netType)
                {
                case 1: connect = new ModelController(); break;

                case 2: connect = new TianYiController(); break;

                default: connect = new RouteController(); break;
                }
                string error = connect.Reset();
                if (!error.IsNullEmpty())
                {
                    if (!msg.IsNull())
                    {
                        msg("重启" + name + "失败:" + error);
                    }
                    return(ChangeIP(netType, msg, done, index + 1));
                }
                else
                {
                    if (!msg.IsNull())
                    {
                        msg("已重启" + name + ",正在检测是否联网......");
                    }
                    bool isTrue = NetHelper.CheckNetwork(msg);
                    if (!isTrue)
                    {
                        return(ChangeIP(netType, msg, done, index + 1));
                    }

                    if (!msg.IsNull())
                    {
                        msg("已联接网络,正在获取IP......");
                    }
                    string ip = IPHelper.GetIpFast();

                    if (!msg.IsNull())
                    {
                        msg("获取到IP:" + ip);
                    }
                    if (IpHistoryHelper.IsExistByID(ip))
                    {
                        if (!msg.IsNull())
                        {
                            msg("检测到IP:" + ip + "最近已使用!");
                        }
                        return(ChangeIP(netType, msg, done, index + 1));
                    }
                    else
                    {
                        IpHistoryHelper.Insert(new IpHistory()
                        {
                            IP = ip, CreateTime = DateTime.Now.ToDateTime().ToDateTime()
                        });
                    };
                    return(ip);
                }
            }
        }