private void btnTestVpn_Click(object sender, EventArgs e) { VPNManager vpnMgr = new VPNManager(this.LogManager); VPNItem vpnItem = this.GetVpnParams(); if (null != vpnItem) { this.OnTestChanged(true); Thread t = new Thread(new ThreadStart(delegate() { LogManager.InfoWithCallback(string.Format("-> 正在开始对VPN: 名称={0},IP={1} 进行网络重连测试,请稍等......", vpnItem.EntryName, vpnItem.IP)); bool isConnected = vpnMgr.Reconnect(vpnItem); if (isConnected) { LogManager.InfoWithCallback(string.Format("-> 对VPN: 名称={0},IP={1} 网络重连成功,网络恢复正常", vpnItem.EntryName, vpnItem.IP)); } else { LogManager.InfoWithCallback(string.Format("-> 对VPN: 名称={0},IP={1} 网络重连失败,请检查IP,用户名或者密码是否正确!", vpnItem.EntryName, vpnItem.IP)); } this.OnTestChanged(false); })); t.Start(); } }
public UcVpnItem(VPNItem item, LogManagerBase logManager) { InitializeComponent(); this.vpnItem = item; this.LogManager = logManager; this.Load += UcVpnItem_Load; }
public VPNItem GetVpnParams() { #region VPN VPNItem vpnItem = new VPNItem(); StringBuilder sb = new StringBuilder(); int i = 1; string vpnName = this.txtVpnName.Text.Trim(); if (!string.IsNullOrEmpty(vpnName)) { vpnItem.EntryName = vpnName; } else { sb.AppendLine(string.Format("{0}、请录入正确的VPN连接名称!", i++)); } string vpnIP = this.txtVpnIP.Text.Trim(); if (!string.IsNullOrEmpty(vpnIP) && TextHelper.IsIP(vpnIP)) { vpnItem.IP = vpnIP; } else { sb.AppendLine(string.Format("{0}、您录入的不是有效的VPN IP地址!", i++)); } string vpnUser = this.txtVpnUser.Text.Trim(); if (!string.IsNullOrEmpty(vpnUser)) { vpnItem.User = vpnUser; } else { sb.AppendLine(string.Format("{0}、请录入正确的VPN登录用户名!", i++)); } string vpnPwd = this.txtVpnPwd.Text.Trim(); if (!string.IsNullOrEmpty(vpnPwd)) { vpnItem.Password = vpnPwd; } else { sb.AppendLine(string.Format("{0}、请录入正确的VPN登录密码!", i++)); } if (sb.Length > 0) { MessageBox.Show(sb.ToString()); return(null); } return(vpnItem); #endregion }
public VPNDialer(VPNItem vpnItem, ManualResetEvent manualReset, LogManagerBase logManager) { this.LogManager = logManager; if ((null == vpnItem) || (null == manualReset)) { LogManager.Error("Vpn dialer parameters can't be null!"); } this.VPNItem = vpnItem; this.vpnManualReset = manualReset; this.Timeout = 5 * 60 * 1000; this.rasDialer.StateChanged += rasDialer_StateChanged; this.rasDialer.DialCompleted += rasDialer_DialCompleted; }
public IList <VPNItem> GetVpnList() { StringBuilder sb = new StringBuilder(); int i = 0; if (string.IsNullOrEmpty(txtVpnName.Text.Trim())) { sb.AppendLine(string.Format("{0}、VPN名称不能为空!", ++i)); } if (string.IsNullOrEmpty(txtFile.Text) || !File.Exists(txtFile.Text) || string.IsNullOrEmpty(txtVpnName.Text.Trim())) { sb.AppendLine(string.Format("{0}、VPN数据不能为空或者不存在!", ++i)); } if (sb.Length > 0) { MessageBox.Show(sb.ToString()); return(null); } IList <VPNItem> vpnList = new List <VPNItem>(); using (StreamReader sr = new StreamReader(txtFile.Text)) { while (!sr.EndOfStream) { VPNItem item = GetVpn(sr.ReadLine()); if (null != item) { vpnList.Add(item); } } } if (vpnList.Count == 0) { MessageBox.Show("当前选择的VPN列表文件非有效格式,获取失败!"); return(null); } return(vpnList); }
public bool Reconnect(VPNItem item) { if ((null == item)) { LogManager.Error("VPN服务器参数获取出错!"); return(false); } if (this.CurrentVpnItem != item) { this.CurrentVpnItem = item; } if (string.IsNullOrEmpty(this.CurrentVpnItem.EntryName) || string.IsNullOrEmpty(this.CurrentVpnItem.IP) || string.IsNullOrEmpty(this.CurrentVpnItem.User) || string.IsNullOrEmpty(this.CurrentVpnItem.Password)) { LogManager.Error("VPN 拨号参数不能为空"); return(false); } if (!this.Reconnect()) { return(false); } ReconnectManager.Sleep(); //bool isConnected = CmdHelper.Ping(this.CurrentVpnItem.IP); //if (!isConnected) { // The connection attempt has completed, attempt to find the connection in the active connections. foreach (RasConnection connection in RasConnection.GetActiveConnections()) { if (connection.EntryName == this.CurrentVpnItem.EntryName) { return(true); } } } return(false); }