Example #1
0
        /// <summary>
        /// 获取账号
        /// </summary>
        /// <param name="isneedLock">是否需要锁</param>
        private void GetVpnAccount(ref bool isneedLock)
        {
            ////程序允许就马上去锁定一个账号
            VpnInfo vpnInfo = new VpnInfo();

            ////随机取账号之前,把原来的账号状态还原
            if (VpnAccount != null)
            {
                vpnInfo.ReleaseVpnAccountState(VpnAccount);
            }

            ////先用本机IP取获取一个账号,因为有可能前面本机锁定了一个账号
            ////后来本机出了异常,程序启动需要重新去取一次这个账号
            MVPNAccount temp = vpnInfo.GetVpnAccountByIP();
            if (temp != null && !string.IsNullOrEmpty(temp.KeyID))
            {
                isneedLock = false;
            }
            else
            {
                ////通过IP没有拿到账号,那就随机取一个账号
                temp = vpnInfo.GetVpnAccount();
            }

            if (temp != null && VpnAccount != null && VpnAccount.KeyID == temp.KeyID)
            {
                ////说明又拿到同一个账号了,看能否重新获取一个
                VpnAccount = vpnInfo.GetNewVpnAccount(temp.Provider);
            }
            else
            {
                VpnAccount = temp;
            }
        }
Example #2
0
        /// <summary>
        /// 重写方法
        /// </summary>
        protected override void ExcuteMethod()
        {
            while (true)
            {
                try
                {
                    bool isneedLock = true;
                    MessagePipe.ExcuteWriteMessageEvent("517VPN客户端启动成功", Color.Green, false);
                    VpnInfo vpnInfo = new VpnInfo();

                    ////获取账号
                    this.GetVpnAccount(ref isneedLock);
                    if (VpnAccount != null && !string.IsNullOrEmpty(VpnAccount.KeyID))
                    {
                        MessagePipe.ExcuteWriteMessageEvent("517VPN客户端获取到账号" + VpnAccount.Account, Color.Green, false);
                        ////马上锁定账号,锁定成功过,才允许使用这个账号
                        bool lockAccountResult = vpnInfo.UpdateVpnAccountState(VpnAccount);
                        if (lockAccountResult || !isneedLock)
                        {
                            MessagePipe.ExcuteShowAccountInfoEvent(VpnAccount.Account);
                            MessagePipe.ExcuteWriteMessageEvent("517VPN客户端锁定账号:" + VpnAccount.Account + "成功", Color.Green, false);

                            ////清空一下加载的数据
                            MessagePipe.ExcuteDelVpnServerInfoEvent("clear");

                            ////加载出所有可用的VPN服务器信息,先拿使用服务器为本机的IP
                            List<MVPNServer> tempExceptionVpn = vpnInfo.GetLocalUsedVpnServer(VpnAccount.Provider);
                            ProductVpnServers.AddRange(tempExceptionVpn);

                            List<MVPNServer> tempValidVpn = this.GetAllValidVpnServer(VpnAccount.Provider);
                            ProductVpnServers.AddRange(tempValidVpn);

                            ////因为有可能因为异常退出,导致IP一直被占用,所以首先取使用IP为自己服务器的IP
                            if (ProductVpnServers != null && ProductVpnServers.Count > 0)
                            {
                                MessagePipe.ExcuteBindVpnServerInfoEvent(ProductVpnServers);

                                ////锁定成功,取一个可用的VPN服务器建立连接(人工切换IP会有影响,)
                                foreach (var tempVpn in ProductVpnServers)
                                {
                                    TrackID.GetInstance("517VPN");

                                    ////////接收到暂停指令
                                    ////if (SignalControl.Issuspend)
                                    ////{
                                    ////    ////等待解锁指令
                                    ////    SignalControl.WaitSignal.WaitOne();
                                    ////}

                                    VpnServer = tempVpn;
                                    MessagePipe.ExcuteShowIpInfoEvent(tempVpn.Address);
                                    MessagePipe.ExcuteShowLinkInfoEvent("连接中...");

                                    ////IP独占成功,方可连接
                                    if (vpnInfo.UpdateVpnUseStatu(tempVpn) || tempVpn.UsingServer == new GetIP().GetLocalIp("本地连接"))
                                    {
                                        string msg = string.Empty;
                                        bool connectResult = this.IslinkVpnServer(VpnAccount, tempVpn);
                                        if (connectResult)
                                        {
                                            MessagePipe.ExcuteWriteMessageEvent("517客户端连接VPN:" + tempVpn.Address + "成功", Color.Green, false);
                                            MessagePipe.ExcuteShowLinkInfoEvent("连接成功");

                                            ////建立连接成功过后,调用业务组件,如果业务组件需要更换VPN服务器,则删除现有连接,重新建立新连接
                                            bool result = this.InvokeIpMonitor();
                                            if (result)
                                            {
                                                ////需要替换IP,还原数据状态
                                                vpnInfo.UpdateVpnIsValid(tempVpn);
                                                MessagePipe.ExcuteWriteMessageEvent("检测到" + tempVpn.Address + "被封,需要替换IP", Color.Green, false);
                                            }
                                        }
                                        else
                                        {
                                            msg = tempVpn.Provider + "|" + tempVpn.Address;
                                            vpnInfo.UpdateVpnLinkStatu(tempVpn);
                                            MessagePipe.ExcuteWriteMessageEvent(msg + "连接失败", Color.Red, false);
                                        }

                                        msg = tempVpn.Provider + "|" + tempVpn.Address;
                                        MessagePipe.ExcuteDelVpnServerInfoEvent(msg);

                                        ////连接失败,删除连接
                                        if (VpnControlThread.VpnHelper != null)
                                        {
                                            ////退出之前关闭VPN
                                            bool disConnectResult = VpnControlThread.VpnHelper.TryDisConnectVPN();
                                            VpnControlThread.VpnHelper.TryDeleteVPN();
                                            MessagePipe.ExcuteShowLinkInfoEvent("未连接");
                                        }
                                    }
                                }

                                ////表明这一轮可用IP已经使用完了,所以清空一下
                                ProductVpnServers.Clear();
                            }
                        }
                        else
                        {
                            MessagePipe.ExcuteWriteMessageEvent("517VPN客户端没有锁定到账号,休眠40秒", Color.Red, true);
                            ////锁定失败,休眠一分钟
                            Thread.Sleep(TimeSpan.FromSeconds(40));
                        }
                    }
                    else
                    {
                        MessagePipe.ExcuteWriteMessageEvent("517VPN客户端没有获取到账号,休眠40秒", Color.Red, true);
                        ////锁定失败,休眠一分钟
                        Thread.Sleep(TimeSpan.FromSeconds(40));
                    }
                }
                catch (Exception ex)
                {
                    LogManager.Log.WriteException(new AppException("517VPN客户端主流程异常", ex, ExceptionLevel.Error));
                    MessagePipe.ExcuteWriteMessageEvent("517VPN主流程异常" + ex.Message.ToString(), Color.Red, false);
                }
                finally
                {
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            }
        }
Example #3
0
        /// <summary>
        /// 获取指定供应商的所有可用VPN服务器
        /// </summary>
        /// <param name="provider">VPN服务器提供商</param>
        /// <returns>返回VPN服务器信息</returns>
        private List<MVPNServer> GetAllValidVpnServer(string provider)
        {
            List<MVPNServer> allVpnServers = new List<MVPNServer>();
            VpnInfo vpnInfo = new VpnInfo();
            string keyid = "0";
            while (true)
            {
                List<MVPNServer> temp = vpnInfo.GetValidVpn(provider, keyid, 50);
                if (temp != null && temp.Count > 0)
                {
                    keyid = temp.OrderByDescending(p => p.KeyID).ToList()[0].KeyID;
                    allVpnServers.AddRange(temp);
                }

                if (temp == null || temp.Count < 50)
                {
                    break;
                }
            }

            return allVpnServers;
        }
Example #4
0
        /// <summary>
        /// 退出操作
        /// </summary>
        private void ExitOperate()
        {
            if (VpnControlThread.VpnHelper != null)
            {
                ////退出之前关闭VPN
                bool disConnectResult = VpnControlThread.VpnHelper.TryDisConnectVPN();
                VpnControlThread.VpnHelper.TryDeleteVPN();
            }

            if (VpnControlThread.VpnAccount != null)
            {
                ////解锁状态
                VpnInfo vpnInfo = new VpnInfo();
                vpnInfo.ReleaseVpnAccountState(VpnControlThread.VpnAccount);
                if (VpnControlThread.VpnServer != null)
                {
                    vpnInfo.ExitUpdateVpnIsValid(VpnControlThread.VpnServer);
                }
            }
        }