/// <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)); } } }