Пример #1
0
        /// <summary>
        /// 是否连接上VPN服务器
        /// </summary>
        /// <param name="vpnAccount">VPN服务器供应账号信息</param>
        /// <param name="vpnServer">VPN服务器信息</param>
        /// <returns>返回连接结果</returns>
        public bool IslinkVpnServer(MVPNAccount vpnAccount, MVPNServer vpnServer)
        {
            bool linkResult = false;

            VpnHelper = new VPNHelper(vpnServer.Address, vpnServer.Area + vpnServer.Line, VpnAccount.Account, VpnAccount.Password);
            bool createOrUpdateResult = VpnHelper.CreateOrUpdateVPN();

            if (createOrUpdateResult)
            {
                ////连接VPN
                linkResult = VpnHelper.TryConnectVPN();
                if (linkResult)
                {
                    DateTime startTime    = DateTime.Now;
                    string   testLinkInfo = new TestLink().GetPingStr("www.baidu.com", 32, 200, 3);
                    if (testLinkInfo.Contains("100% 丢失"))
                    {
                        ////访问超时,也认为IP不可用
                        ////记录交互日志
                        string request = "VPN通道名称:" + vpnServer.Area + vpnServer.Line + ";VPN账号:" + vpnAccount.Account;
                        RecordLog.RecordInteractionLog(vpnAccount.Account, "连接VPN成功后,测试请求外网", request, testLinkInfo, DateTime.Now.Subtract(startTime), vpnServer.Address);
                        linkResult = false;
                    }

                    MessagePipe.ExcuteWriteMessageEvent(testLinkInfo, Color.Green, false);
                }
            }

            return(linkResult);
        }
Пример #2
0
        /// <summary>
        /// 创建或更新一个VPN连接(指定VPN名称,及IP)
        /// </summary>
        /// <param name="updateVPNname">更新vpn名称</param>
        /// <param name="updateVPNip">更新VPN地址</param>
        /// <returns>操作成功:true,操作失败:false</returns>
        public bool CreateOrUpdateVPN(string updateVPNname, string updateVPNip)
        {
            bool     result    = true;
            DateTime startTime = DateTime.Now;
            string   msg       = string.Empty;

            try
            {
                RasDialer    dialer            = new RasDialer();
                RasPhoneBook allUsersPhoneBook = new RasPhoneBook();
                allUsersPhoneBook.Open();

                // 如果已经该名称的VPN已经存在,则更新这个VPN服务器地址
                if (allUsersPhoneBook.Entries.Contains(updateVPNname))
                {
                    allUsersPhoneBook.Entries[updateVPNname].PhoneNumber = updateVPNip;
                    ////不管当前VPN是否连接,服务器地址的更新总能成功,如果正在连接,则需要VPN重启后才能起作用
                    bool updateResult = allUsersPhoneBook.Entries[updateVPNname].Update();
                    msg = "更新VPN服务器通道为:" + updateVPNip;
                    if (updateResult)
                    {
                        MessagePipe.ExcuteWriteMessageEvent(msg + "成功", Color.Green, false);
                    }
                    else
                    {
                        MessagePipe.ExcuteWriteMessageEvent(msg + "失败", Color.Green, false);
                    }
                }
                else
                {
                    ////创建一个新VPN
                    RasEntry entry = RasEntry.CreateVpnEntry(updateVPNname, updateVPNip, RasVpnStrategy.PptpFirst, RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn));

                    allUsersPhoneBook.Entries.Add(entry);
                    dialer.EntryName = updateVPNname;

                    dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
                    msg = "新建VPN通道:" + updateVPNip + "成功";
                    MessagePipe.ExcuteWriteMessageEvent(msg, Color.Green, false);
                }
            }
            catch (Exception ex)
            {
                result = false;
                MessagePipe.ExcuteWriteMessageEvent("更新或创建VPN通道异常:" + ex.Message.ToString(), Color.Green, true);
            }
            finally
            {
                ////记录交互日志
                string request = "VPN通道名称:" + updateVPNname + ";VPN账号:" + this.userName + ";VPN地址:" + updateVPNip;
                RecordLog.RecordInteractionLog(updateVPNname, "创建VPN通道", request, msg, DateTime.Now.Subtract(startTime), updateVPNip);
            }

            return(result);
        }
Пример #3
0
        /// <summary>
        /// 尝试连接VPN(指定VPN名称,用户名,密码)
        /// </summary>
        /// <param name="connVpnName">vpn名称</param>
        /// <param name="connUserName">连接的用户名称</param>
        /// <param name="connPassWord">连接的密码</param>
        /// <returns>连接成功true,连接失败false</returns>
        public bool TryConnectVPN(string connVpnName, string connUserName, string connPassWord)
        {
            bool     result    = false;
            DateTime startTime = DateTime.Now;
            string   linkInfo  = string.Empty;

            try
            {
                string args = string.Format("{0} {1} {2}", connVpnName, connUserName, SecurityUtility.DecryptString(connPassWord));

                ProcessStartInfo myProcess = new ProcessStartInfo(this.vpnProcess, args);

                myProcess.CreateNoWindow = true;

                myProcess.UseShellExecute        = false;
                myProcess.RedirectStandardOutput = true;
                var excuteResult = Process.Start(myProcess);
                linkInfo = excuteResult.StandardOutput.ReadToEnd();
                linkInfo = linkInfo.Replace("\n", string.Empty);
                if (linkInfo.Contains("已连接"))
                {
                    result = true;
                }
                else
                {
                    MessagePipe.ExcuteWriteMessageEvent("登陆失败,账号:" + connUserName + "密码:" + SecurityUtility.EncryptDES(connPassWord) + "VPN服务器:" + this.vpnIP, Color.Green, true);
                }

                MessagePipe.ExcuteWriteMessageEvent(connVpnName + "登陆验证结果:" + linkInfo, Color.Green, false);
            }
            catch (Exception ex)
            {
                string msg = "登陆异常,账号:" + connUserName + "密码:" + SecurityUtility.EncryptDES(connPassWord) + "VPN服务器:" + this.vpnIP;
                LogManager.Log.WriteException(new AppException(msg, ex, ExceptionLevel.Info));
            }
            finally
            {
                ////记录交互日志
                string request = "VPN通道名称:" + connVpnName + ";VPN账号:" + connUserName;
                RecordLog.RecordInteractionLog(connUserName, "通过用账号和密码登陆", request, linkInfo, DateTime.Now.Subtract(startTime), this.vpnIP);
            }

            return(result);
        }
Пример #4
0
        /// <summary>
        /// 尝试断开VPN(指定VPN名称)
        /// </summary>
        /// <param name="disConnVpnName">连接的vpn名称</param>
        /// <returns>返回执行结果,释放成功true,释放失败false</returns>
        public bool TryDisConnectVPN(string disConnVpnName)
        {
            bool     result    = false;
            DateTime startTime = DateTime.Now;
            string   linkInfo  = string.Empty;

            try
            {
                string args = string.Format(@"{0} /d", disConnVpnName);

                ProcessStartInfo myProcess = new ProcessStartInfo(this.vpnProcess, args);

                myProcess.CreateNoWindow = true;

                myProcess.UseShellExecute        = false;
                myProcess.RedirectStandardOutput = true;
                var excuteResult = Process.Start(myProcess);
                linkInfo = excuteResult.StandardOutput.ReadToEnd();
                linkInfo = linkInfo.Replace("\n", string.Empty);
                if (!linkInfo.Contains("没有连接") && linkInfo.Contains("命令已完成"))
                {
                    result = true;
                }

                MessagePipe.ExcuteWriteMessageEvent("释放结果:" + linkInfo, Color.Green, true);
            }
            catch (Exception ex)
            {
                string msg = disConnVpnName + "断开VPN异常" + ex;
                MessagePipe.ExcuteWriteMessageEvent(msg, Color.Red, true);
            }
            finally
            {
                ////记录交互日志
                string request = "VPN通道名称" + disConnVpnName + ";VPN地址:" + this.vpnIP + ";VPN账号:" + this.userName;
                RecordLog.RecordInteractionLog(this.userName, "断开VPN连接", request, linkInfo, DateTime.Now.Subtract(startTime), this.vpnIP);
            }

            return(result);
        }