Ejemplo n.º 1
0
 /// <summary>
 /// 宽带连接,成功返回true,失败返回 false
 /// </summary>
 /// <param name="PPPOEname">宽带连接名称</param>
 /// <param name="username">宽带账号</param>
 /// <param name="password">宽带密码</param>
 /// <returns></returns>
 public bool Connect(string PPPOEname, string username, string password)
 {
     log.writeLog("正在进行本地拨号", log.msgType.info);
     try
     {
         CreateOrUpdatePPPOE(PPPOEname);
         log.writeLog("创建PPPOE接口完成", log.msgType.info);
         using (RasDialer dialer = new RasDialer())
         {
             dialer.EntryName = PPPOEname;
             dialer.AllowUseStoredCredentials = true;
             dialer.Timeout       = 1000;
             dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
             dialer.Credentials   = new NetworkCredential(username, password);
             dialer.Dial();
             log.writeLog($"本地拨号成功,账号:{username}\n密码:{password}", log.msgType.error);
             return(true);
         }
     }
     catch (RasException re)
     {
         log.writeLog("本地拨号捕获到未知异常,拨号失败!", log.msgType.error);
         return(false);
     }
 }
Ejemplo n.º 2
0
        public bool Dial()
        {
            this.HangUp();
            //this.CreateADSL();

            this.isDialing = true;
            // This button will be used to dial the connection.
            this.rasDialer.EntryName     = this.ADSLItem.EntryName;
            this.rasDialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
            //MS 5 Mins
            this.rasDialer.Timeout = this.Timeout;

            try
            {
                // Set the credentials the dialer should use.
                this.rasDialer.Credentials = new NetworkCredential(this.ADSLItem.User, this.ADSLItem.Password);
                this.rasDialer.AllowUseStoredCredentials = true;
                //this.rasDialer.
                // NOTE: The entry MUST be in the phone book before the connection can be dialed.
                // Begin dialing the connection; this will raise events from the dialer instance.
                RasHandle rasHandle = this.rasDialer.Dial();
                bool      b         = !rasHandle.IsInvalid;
                return(b);
            }
            catch (Exception ex)
            {
                LogManager.ErrorWithCallback(string.Format("-> ADSL拨号异常:{0}", ex.ToString()));
                this.isDialing = false;
            }
            return(false);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 创建或更新一个PPPOE连接(指定PPPOE名称)
        /// </summary>
        public void CreateOrUpdatePPPOE(string updatePPPOEname)
        {
            RasDialer    dialer            = new RasDialer();
            RasPhoneBook allUsersPhoneBook = new RasPhoneBook();
            string       path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);

            allUsersPhoneBook.Open(path);
            // 如果已经该名称的PPPOE已经存在,则更新这个PPPOE服务器地址
            if (allUsersPhoneBook.Entries.Contains(updatePPPOEname))
            {
                allUsersPhoneBook.Entries[updatePPPOEname].PhoneNumber = " ";
                // 不管当前PPPOE是否连接,服务器地址的更新总能成功,如果正在连接,则需要PPPOE重启后才能起作用
                allUsersPhoneBook.Entries[updatePPPOEname].Update();
            }
            // 创建一个新PPPOE
            else
            {
                string adds = string.Empty;
                ReadOnlyCollection <RasDevice> readOnlyCollection = RasDevice.GetDevices();
                //                foreach (var col in readOnlyCollection)
                //                {
                //                    adds += col.Name + ":" + col.DeviceType.ToString() + "|||";
                //                }
                //                _log.Info("Devices are : " + adds);
                // Find the device that will be used to dial the connection.
                RasDevice device = RasDevice.GetDevices().Where(o => o.DeviceType == RasDeviceType.PPPoE).First();
                RasEntry  entry  = RasEntry.CreateBroadbandEntry(updatePPPOEname, device);  //建立宽带连接Entry
                entry.PhoneNumber = " ";
                allUsersPhoneBook.Entries.Add(entry);
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Connect
 /// </summary>
 /// <param name="connection"></param>
 private void Connect(string connection)
 {
     try
     {
         CreatePPPOE(connection);
         var dialer = new RasDialer
         {
             EntryName   = connection,
             PhoneNumber = " ",
             AllowUseStoredCredentials = true,
             PhoneBookPath             = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers),
             Credentials = new NetworkCredential("057111414695", "336875"),
             Timeout     = 1000
         };
         var myras = dialer.Dial();
         while (myras.IsInvalid)
         {
             Thread.Sleep(5000);
             myras = dialer.Dial();
         }
         if (!myras.IsInvalid)
         {
             Console.WriteLine($"RasDialer Success! {DateTime.Now}");
         }
     }
     catch (Exception ex)
     {
         throw new Exception($"RasDialer error! {DateTime.Now} connection error is :: {ex.ToString()}");
         //Console.WriteLine($"RasDialer error! {DateTime.Now} connection error is :: {ex.ToString()}");
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// 创建或更新一个PPPOE连接(指定PPPOE名称)
        /// </summary>
        static void CreateOrUpdatePPPOE(string updatePPPOEname)
        {
            RasDialer    dialer            = new RasDialer();
            RasPhoneBook allUsersPhoneBook = new RasPhoneBook();
            string       path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);

            allUsersPhoneBook.Open(path);
            // 如果已经该名称的PPPOE已经存在,则更新这个PPPOE服务器地址
            if (allUsersPhoneBook.Entries.Contains(updatePPPOEname))
            {
                allUsersPhoneBook.Entries[updatePPPOEname].PhoneNumber = " ";
                // 不管当前PPPOE是否连接,服务器地址的更新总能成功,如果正在连接,则需要PPPOE重启后才能起作用
                allUsersPhoneBook.Entries[updatePPPOEname].Update();
            }
            // 创建一个新PPPOE
            else
            {
                string adds = string.Empty;
                ReadOnlyCollection <RasDevice> readOnlyCollection = RasDevice.GetDevices();
                RasDevice device = RasDevice.GetDevices().Where(o => o.DeviceType == RasDeviceType.PPPoE).First();
                RasEntry  entry  = RasEntry.CreateBroadbandEntry(updatePPPOEname, device);  //建立宽带连接Entry
                entry.PhoneNumber = " ";
                allUsersPhoneBook.Entries.Add(entry);
            }
        }
Ejemplo n.º 6
0
        void Dial()
        {
            this.Dialer.EntryName     = EntryName;
            this.Dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
            try
            {
                if (this.Dialer.IsBusy)
                {
                    return;
                }
                // Set the credentials the dialer should use.
                this.Dialer.Credentials = new NetworkCredential("14ab1", "735435");

                // NOTE: The entry MUST be in the phone book before the connection can be dialed.
                // Begin dialing the connection; this will raise events from the dialer instance.
                this.handle = this.Dialer.Dial();
            }
            catch (Exception ex)
            {
                LogUtil.Write(ex.ToString());

                if (ex.Message.Contains("已经拨了这个连接"))
                {
                    System.Diagnostics.Process.Start(AppDomain.CurrentDomain.BaseDirectory + "系统重启.bat");
                }
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 创建或更新一个PPPOE连接
        /// </summary>
        /// <param name="updatePPPOEName"></param>
        private void CreateOrUpdatePPPOE(string updatePPPOEName)
        {
            var allUsersPhoneBook = new RasPhoneBook();
            var path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);

            allUsersPhoneBook.Open(path);
            //如果该名称的PPPOE已经存在,则更新这个PPPOE服务器地址
            if (allUsersPhoneBook.Entries.Contains(updatePPPOEName))
            {
                allUsersPhoneBook.Entries[updatePPPOEName].PhoneNumber = " ";
                //不管当前PPPOE是否连接,服务器地址的更新总能成功,如果正在连接,则需要PPPOE重启后才能起作用
                allUsersPhoneBook.Entries[updatePPPOEName].Update();
            }
            //创建一个新的PPPOE
            else
            {
                var address            = string.Empty;
                var readOnlyCollection = RasDevice.GetDevices();
                var device             = RasDevice.GetDevices().First(o => o.DeviceType == RasDeviceType.PPPoE);
                //建立宽带连接
                var entry = RasEntry.CreateBroadbandEntry(updatePPPOEName, device);
                entry.PhoneNumber = " ";
                allUsersPhoneBook.Entries.Add(entry);
            }
        }
Ejemplo n.º 8
0
        private Encoding defEncoding           = Encoding.GetEncoding(1252); // ANSI

        private void CreateConnect()
        {
            RasPhoneBook book = new RasPhoneBook();

            try
            {
                book.Open(RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User));
                if (book.Entries.Contains(ConnectionName))
                {
                    book.Entries[ConnectionName].PhoneNumber = " ";
                    book.Entries[ConnectionName].Update();
                }
                else
                {
                    RasDevice device = RasDevice.GetDevices().
                                       Where(o => o.DeviceType == RasDeviceType.PPPoE).First();
                    RasEntry entry = RasEntry.CreateBroadbandEntry(ConnectionName, device);
                    entry.PhoneNumber = " ";
                    book.Entries.Add(entry);
                }
            }
            catch (Exception ex)
            {
                Logger.Log($"创建PPPoE连接失败({ex.Message})");
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 测试拨号连接
        /// </summary>
        private string  getNewPPPoe(string name = null)
        {
            try
            {
                if (name == null)
                {
                    name = txtBox_pppoeName.Text.Trim();
                    name = string.IsNullOrEmpty(name) ? dic_pppoeName.First().Key : name;
                }


                RasDialer dialer = new RasDialer();
                dialer.EntryName   = name;
                dialer.PhoneNumber = " ";
                dialer.AllowUseStoredCredentials = true;
                dialer.PhoneBookPath             = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
                dialer.Timeout = 1000;
                dialer.Dial();

                Thread.Sleep(100);
                return(LoadConnections());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            return("");
        }        /// <summary>
Ejemplo n.º 10
0
        /// <summary>
        /// 初始化PPPoE拨号器
        /// </summary>
        /// <param name="connectName">连接名称</param>
        public static void Init(string connectName)
        {
            try
            {
                RasDialer    dialer    = new RasDialer();
                RasPhoneBook phoneBook = new RasPhoneBook();
                string       path      = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);
                phoneBook.Open(path);

                if (phoneBook.Entries.Contains(connectName))
                {
                    phoneBook.Entries[connectName].PhoneNumber = " ";
                    phoneBook.Entries[connectName].Update();
                }
                else
                {
                    string adds = string.Empty;
                    System.Collections.ObjectModel.ReadOnlyCollection <RasDevice> readOnlyCollection = RasDevice.GetDevices();
                    RasDevice device = RasDevice.GetDevices().First(o => o.DeviceType == RasDeviceType.PPPoE);
                    RasEntry  entry  = RasEntry.CreateBroadbandEntry(connectName, device);
                    entry.PhoneNumber = " ";
                    phoneBook.Entries.Add(entry);
                }
            }
            catch (Exception e)
            {
                Utils.Log4Net.WriteLog(e.Message, e);
            }
        }
Ejemplo n.º 11
0
 public void Diale(string username, string key, string connectName)
 {
     try
     {
         RasDialer dialer = new RasDialer();
         dialer.EntryName   = connectName;
         dialer.PhoneNumber = " ";
         dialer.AllowUseStoredCredentials = true;
         dialer.PhoneBookPath             = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);
         dialer.Credentials = new System.Net.NetworkCredential(username, key);
         dialer.Timeout     = 500;
         RasHandle myras = dialer.Dial();
         while (myras.IsInvalid)
         {
             //lb_status.Content = "拨号失败";
             OnDialeFailure?.Invoke();
         }
         if (!myras.IsInvalid)
         {
             //lb_status.Content = "拨号成功! ";
             RasConnection conn   = RasConnection.GetActiveConnectionByHandle(myras);
             RasIPInfo     ipaddr = (RasIPInfo)conn.GetProjectionInfo(RasProjectionType.IP);
             //lb_message.Content = "获得IP: " + ipaddr.IPAddress.ToString();
             OnDialeSuccess?.Invoke("获得IP: " + ipaddr.IPAddress.ToString());
             //dial.IsEnabled = false;
             //hangup.IsEnabled = true;
         }
     }
     catch (Exception e)
     {
         //lb_status.Content = "拨号出现异常";
         OnDialeExcep?.Invoke();
     }
 }
Ejemplo n.º 12
0
        /// <summary>
        /// ADSL链接
        /// </summary>
        /// <param name="strConnName">连接名称</param>
        public bool Connect()
        {
            try
            {
                // CreateOrUpdatePPPOE(strConnName);
                RasDialer dialer = new RasDialer();
                dialer.EntryName   = ConstParameter.EntryName;
                dialer.PhoneNumber = " ";
                dialer.AllowUseStoredCredentials = true;
                dialer.PhoneBookPath             = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
                dialer.Credentials = new NetworkCredential(
                    CommonParameter.ConfigParam.adsl_info.username,
                    CommonParameter.ConfigParam.adsl_info.password);
                dialer.Timeout = 1000;
                RasHandle myras = dialer.Dial();
                if (!myras.IsInvalid)
                {
                    CommonFunction.WriteLogInFile("RasDialer Success! ", LogType.RunLog);
                    return(true);
                }
            }
            catch (Exception ex)
            {
                CommonFunction.WriteLogInFile("RasDialer error!  error is :" + ex.ToString(), LogType.RunLog);
            }

            return(false);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 创建或更新一个VPN连接(指定VPN名称,及IP)
        /// </summary>
        public void CreateOrUpdateVPN(string updateVPNname, string updateVPNip)
        {
            RasDialer    dialer            = new RasDialer();
            RasPhoneBook allUsersPhoneBook = new RasPhoneBook();

            allUsersPhoneBook.Open();
            // 如果已经该名称的VPN已经存在,则更新这个VPN服务器地址
            if (allUsersPhoneBook.Entries.Contains(updateVPNname))
            {
                allUsersPhoneBook.Entries[updateVPNname].PhoneNumber = updateVPNip;
                // 不管当前VPN是否连接,服务器地址的更新总能成功,如果正在连接,则需要VPN重启后才能起作用
                allUsersPhoneBook.Entries[updateVPNname].Update();
            }
            // 创建一个新VPN
            else
            {
                //RasEntry entry = RasEntry.CreateVpnEntry(updateVPNname, updateVPNip, RasVpnStrategy.PptpFirst, RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn));
                //allUsersPhoneBook.Entries.Add(entry);
                RasEntry entryL2TP = RasEntry.CreateVpnEntry(updateVPNname, updateVPNip, RasVpnStrategy.L2tpOnly, RasDevice.GetDeviceByName("(L2TP)", RasDeviceType.Vpn));

                allUsersPhoneBook.Entries.Add(entryL2TP);
                dialer.EntryName     = updateVPNname;
                dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
            }
        }
Ejemplo n.º 14
0
        public void Disconnect()
        {
            if (dialer.IsBusy)
            {
                dialer.DialAsyncCancel();
            }
            else
            {
                if (handle != null)
                {
                    RasConnection Connection = RasConnection.GetActiveConnectionByHandle(handle);
                    if (Connection != null)
                    {
                        Connection.HangUp();
                    }
                }
            }

            using (RasPhoneBook PhoneBook = new RasPhoneBook())
            {
                PhoneBook.Open(RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers));

                if (PhoneBook.Entries.Contains(adapterName))
                {
                    PhoneBook.Entries.Remove(adapterName);
                }
            }
        }
Ejemplo n.º 15
0
        public void Dial()
        {
            this.Disconnect();
            this.CreateVpn();

            this.isDialing = true;
            // This button will be used to dial the connection.
            this.rasDialer.EntryName     = this.VPNItem.EntryName;
            this.rasDialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
            //MS 5M
            this.rasDialer.Timeout = this.Timeout;

            try
            {
                // Set the credentials the dialer should use.
                this.rasDialer.Credentials = new NetworkCredential(this.VPNItem.User, this.VPNItem.Password);
                this.rasDialer.AllowUseStoredCredentials = true;
                this.rasDialer.PhoneBookPath             = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);

                //this.rasDialer.
                // NOTE: The entry MUST be in the phone book before the connection can be dialed.
                // Begin dialing the connection; this will raise events from the dialer instance.
                this.rasDialer.DialAsync();
            }
            catch (Exception ex)
            {
                LogManager.ErrorWithCallback(string.Format("-> VPN拨号异常:{0}", ex.ToString()));
                this.vpnManualReset.Set();
                this.isDialing = false;
            }
        }
Ejemplo n.º 16
0
        // Add a new VPN connection action.
        private void btnConnectVPN_Click(object sender, EventArgs e)
        {
            var sis = new SingleItemSelectorForm();

            sis.SetText("Connect to VPN", "VPN Name:");

            // Populate the combo box drop-down with the list of known VPNs.
            var rpbk = new RasPhoneBook();

            rpbk.Open(RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers));
            foreach (var rent in rpbk.Entries)
            {
                sis.comboBox1.Items.Add(rent.Name);
            }
            rpbk.Open(RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User));
            foreach (var rent in rpbk.Entries)
            {
                sis.comboBox1.Items.Add(rent.Name);
            }

            if (sis.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                var pcv = new PAConnectVPN();
                pcv.VPN_Name = sis.comboBox1.Text;
                listBox1.Items.Add(pcv);
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Occurs when the user clicks the Dial button.
        /// </summary>
        /// <param name="sender">The object that raised the event.</param>
        /// <param name="e">An <see cref="System.EventArgs"/> containing event data.</param>
        private void DialButton_Click(object sender, EventArgs e)
        {
            this.StatusTextBox.Clear();

            // This button will be used to dial the connection.
            this.Dialer.EntryName     = EntryName;
            this.Dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);

            try
            {
                // Set the credentials the dialer should use.
                this.Dialer.Credentials = new NetworkCredential("Test", "User");

                // NOTE: The entry MUST be in the phone book before the connection can be dialed.
                // Begin dialing the connection; this will raise events from the dialer instance.
                this.handle = this.Dialer.DialAsync();

                // Enable the disconnect button for use later.
                this.DisconnectButton.Enabled = true;
            }
            catch (Exception ex)
            {
                this.StatusTextBox.AppendText(ex.ToString());
            }
        }
Ejemplo n.º 18
0
        public static void CreateVPN()
        {
            VPNHelper vpn = new VPNHelper();

            using (DotRas.RasDialer dialer = new DotRas.RasDialer())
            {
                DotRas.RasPhoneBook allUsersPhoneBook = new DotRas.RasPhoneBook();
                allUsersPhoneBook.Open();
                if (allUsersPhoneBook.Entries.Contains(VPNConnectionName))
                {
                    return;
                }
                RasEntry entry = RasEntry.CreateVpnEntry(VPNConnectionName, IPToPing, RasVpnStrategy.PptpFirst, RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn));
                allUsersPhoneBook.Entries.Add(entry);
                dialer.EntryName     = VPNConnectionName;
                dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
                try
                {
                    dialer.Credentials = new NetworkCredential(UserName, Password);
                    dialer.DialAsync();
                }
                catch (Exception)
                {
                    return;
                }
            }
        }
Ejemplo n.º 19
0
        private void Form2_Load(object sender, EventArgs e)
        {
            this.Dialer.Timeout               = 20000;
            this.Dialer.Credentials           = null;
            this.Dialer.EapOptions            = new DotRas.RasEapOptions(false, false, false);
            this.Dialer.HangUpPollingInterval = 0;
            this.Dialer.Options               = new DotRas.RasDialOptions(false, false, false, false, false, false, false, false, false, false);
            this.Dialer.SynchronizingObject   = this;
            //this.Dialer.StateChanged += new System.EventHandler<DotRas.StateChangedEventArgs>(this.Dialer_StateChanged);
            this.Dialer.DialCompleted += new System.EventHandler <DotRas.DialCompletedEventArgs>(this.Dialer_DialCompleted);
            string path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);

            //创建VPN
            this.AllUsersPhoneBook.Open(path);

            if (!this.AllUsersPhoneBook.Entries.Contains(EntryName))
            {
                RasEntry entry = RasEntry.CreateBroadbandEntry(EntryName,
                                                               RasDevice.GetDeviceByName("(PPPoE)", RasDeviceType.PPPoE));

                this.AllUsersPhoneBook.Entries.Add(entry);
            }

            InitAction();
        }
Ejemplo n.º 20
0
        private void BeginConnect()
        {
            string sServerip = tServerIP.Text;
            string sUsername = tUsername.Text;
            string sPassword = tUserkey.Text;

            sEntryName = tVpnName.Text;

            if (!CreateVpnEntry(sServerip))
            {
                return;
            }

            this.Dialer.EntryName     = sEntryName;
            this.Dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);

            try
            {
                this.Dialer.Credentials = new NetworkCredential(sUsername, sPassword);
                var handle = this.Dialer.DialAsync();
                DoMessage(string.Format("{0} 正在尝试连接...", DateTime.Now.ToString()));

                // 连接状态监控
                connWatcher                     = new RasConnectionWatcher();
                connWatcher.Handle              = handle;
                connWatcher.Disconnected       += ConnWatcher_Disconnected;
                connWatcher.EnableRaisingEvents = true;
            }
            catch (Exception ex)
            {
                this.tMessage.AppendText(ex.ToString());
            }
        }
Ejemplo n.º 21
0
        /// <summary>
        /// 创建一个PPPOE连接
        /// </summary>
        /// <param name="updatePPPOEName"></param>
        private void CreatePPPOE(string updatePPPOEName)
        {
            var conns = RasConnection.GetActiveConnections();

            if (conns != null)
            {
                foreach (var conn in conns)
                {
                    conn.HangUp();
                }
            }

            var allUsersPhoneBook = new RasPhoneBook();
            var path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);

            allUsersPhoneBook.Open(path);

            //创建一个新的PPPOE
            var address            = string.Empty;
            var readOnlyCollection = RasDevice.GetDevices();
            var device             = RasDevice.GetDevices().First(o => o.DeviceType == RasDeviceType.PPPoE);
            //建立宽带连接
            var entry = RasEntry.CreateBroadbandEntry(updatePPPOEName, device);

            entry.PhoneNumber = " ";
            if (!allUsersPhoneBook.Entries.Contains(updatePPPOEName))
            {
                allUsersPhoneBook.Entries.Add(entry);
            }
        }
Ejemplo n.º 22
0
        private void DialButton_Click(object sender, RoutedEventArgs e)
        {
            this.StatusTextBox.Clear();

            this.dialer = new RasDialer();
            this.dialer.DialCompleted += new EventHandler <DialCompletedEventArgs>(dialer_DialCompleted);
            this.dialer.StateChanged  += new EventHandler <StateChangedEventArgs>(dialer_StateChanged);

            this.dialer.EntryName     = (string)this.ConnectionComboBox.SelectedValue;
            this.dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);

            // This allows the multi-threaded dialer to update information on the user interface without causing any threading problems.
            this.dialer.SynchronizingObject = new DispatcherSynchronizingObject(App.Current.Dispatcher);

            try
            {
                // Set the credentials the dialer should use.
                this.dialer.Credentials = new System.Net.NetworkCredential("Test", "User");

                this.handle = this.dialer.DialAsync();

                // Disable the button used to initiate dialing while the operation is in progress.
                this.DialButton.IsEnabled       = false;
                this.DisconnectButton.IsEnabled = true;
            }
            catch (Exception ex)
            {
                this.StatusTextBox.AppendText(ex.ToString());
                this.StatusTextBox.ScrollToEnd();
            }
        }
Ejemplo n.º 23
0
 void Btn_DialupClick(object sender, EventArgs e)
 {
     try {
         string    username = Username.Text.Replace("\\r", "\r").Replace("\\n", "\n");
         string    password = Password.Text.ToString();
         RasDialer dialer   = new RasDialer();
         dialer.EntryName   = "PPPoEDial";
         dialer.PhoneNumber = " ";
         dialer.AllowUseStoredCredentials = true;
         dialer.PhoneBookPath             = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);
         dialer.Credentials = new NetworkCredential(username, password);
         dialer.Timeout     = 1000;
         RasHandle myras = dialer.Dial();
         while (myras.IsInvalid)
         {
             lb_Status.Text = "拨号失败";
         }
         if (!myras.IsInvalid)
         {
             lb_Status.Text = "拨号成功! ";
             RasConnection conn   = RasConnection.GetActiveConnectionByHandle(myras);
             RasIPInfo     ipaddr = (RasIPInfo)conn.GetProjectionInfo(RasProjectionType.IP);
             lb_IPAddr.Text     = "获得IP: " + ipaddr.IPAddress.ToString();
             btn_Dialup.Enabled = false;
             btn_Hungup.Enabled = true;
         }
     } catch (Exception) {
         lb_Status.Text = "拨号出现异常";
     }
 }
Ejemplo n.º 24
0
        public bool Connect(VpnConnection selectedConnection)
        {
            _dialer.Credentials = new NetworkCredential(_loginMonitor.User.Name, _loginMonitor.SecurePassword);
            try
            {
                string path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);
                _phoneBook.Open(path);

                _entry = _phoneBook.Entries
                         .FirstOrDefault(x => x.PhoneNumber == selectedConnection.Hostname);

                if (_entry != null)
                {
                    _dialer.EntryName = _entry.Name;
                    _dialer.DialAsync();
                    return(true);
                }
            }
            catch (InvalidOperationException)
            {
                Disconnect();
            }
            catch (RasException)
            {
                Disconnect();
            }

            return(false);
        }
Ejemplo n.º 25
0
 /// <summary>
 /// 宽带连接,成功返回true,失败返回 false
 /// </summary>
 /// <param name="PPPOEname">宽带连接名称</param>
 /// <param name="username">宽带账号</param>
 /// <param name="password">宽带密码</param>
 /// <returns></returns>
 public static bool Connect(string PPPOEname, string username, string password, ref string msg, int delay = 10000)
 {
     try
     {
         CreateOrUpdatePPPOE(PPPOEname);
         using (RasDialer dialer = new RasDialer())
         {
             dialer.EntryName = PPPOEname;
             dialer.AllowUseStoredCredentials = true;
             dialer.Timeout       = 1000;
             dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
             dialer.Credentials   = new NetworkCredential(username, password);
             dialer.Dial();
             return(true);
         }
     }
     catch (RasException re)
     {
         msg = re.ErrorCode + " " + re.Message;
         return(false);
     }
     finally
     {
         Thread.Sleep(delay);
     }
 }
Ejemplo n.º 26
0
        public static void CreateVPN()
        {
            VPN vpn = new VPN();

            DotRas.RasDialer    dialer            = new DotRas.RasDialer();
            DotRas.RasPhoneBook allUsersPhoneBook = new DotRas.RasPhoneBook();
            allUsersPhoneBook.Open();
            if (allUsersPhoneBook.Entries.Contains(vpn.VPNConnectionName))
            {
                return;
            }
            RasEntry entry = RasEntry.CreateVpnEntry(vpn.VPNConnectionName, vpn.IPToPing, RasVpnStrategy.PptpFirst, RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn));

            allUsersPhoneBook.Entries.Add(entry);
            dialer.EntryName     = vpn.VPNConnectionName;
            dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
            try
            {
                dialer.DialAsync();
            }
            catch (Exception)
            {
                return;
            }
        }
Ejemplo n.º 27
0
 private void dial_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         string    username = tb_username.Text.Replace("\\r", "\r").Replace("\\n", "\n");
         string    password = pb_password.Password.ToString();
         RasDialer dialer   = new RasDialer();
         dialer.EntryName   = "PPPoEDialer";
         dialer.PhoneNumber = " ";
         dialer.AllowUseStoredCredentials = true;
         dialer.PhoneBookPath             = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);
         dialer.Credentials = new System.Net.NetworkCredential(username, password);
         dialer.Timeout     = 500;
         RasHandle myras = dialer.Dial();
         while (myras.IsInvalid)
         {
             lb_status.Content = "拨号失败";
         }
         if (!myras.IsInvalid)
         {
             lb_status.Content = "拨号成功! ";
             RasConnection conn   = RasConnection.GetActiveConnectionByHandle(myras);
             RasIPInfo     ipaddr = (RasIPInfo)conn.GetProjectionInfo(RasProjectionType.IP);
             lb_message.Content = "获得IP: " + ipaddr.IPAddress.ToString();
             dial.IsEnabled     = false;
             hangup.IsEnabled   = true;
         }
     }
     catch (Exception)
     {
         lb_status.Content = "拨号出现异常";
     }
 }
Ejemplo n.º 28
0
        /// <summary>
        /// 测试拨号连接
        /// </summary>
        public void Connect(string RasName)
        {
            try
            {
                if (OnHandle != null)
                {
                    OnHandle(string.Format("正在拨号 {0}...", RasName));
                }

                RasDialer dialer = new RasDialer();
                dialer.EntryName   = RasName;
                dialer.PhoneNumber = " ";
                dialer.AllowUseStoredCredentials = true;
                dialer.PhoneBookPath             = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
                dialer.Timeout = 1000;
                dialer.Dial();

                Thread.Sleep(100);

                if (OnHandle != null)
                {
                    OnHandle(string.Format("拨号完成"));
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogLevel.LOG_LEVEL_CRIT, ex, typeof(ADSLDialerUtil));
                throw;
            }
        }
Ejemplo n.º 29
0
        public void CreateConnect(string ConnectName)
        {
            RasDialer    dialer = new RasDialer();
            RasPhoneBook book   = new RasPhoneBook();

            try
            {
                book.Open(RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User));
                if (book.Entries.Contains(ConnectName))
                {
                    book.Entries[ConnectName].PhoneNumber = " ";
                    book.Entries[ConnectName].Update();
                }
                else
                {
                    System.Collections.ObjectModel.ReadOnlyCollection <RasDevice> readOnlyCollection = RasDevice.GetDevices();
                    RasDevice device = RasDevice.GetDevices().Where(o => o.DeviceType == RasDeviceType.PPPoE).First();
                    RasEntry  entry  = RasEntry.CreateBroadbandEntry(ConnectName, device);
                    entry.PhoneNumber = " ";
                    book.Entries.Add(entry);
                }
            }
            catch (Exception)
            {
                lb_status.Content = "创建PPPoE连接失败";
            }
        }
Ejemplo n.º 30
0
        public void Disconnect()
        {
            RasCon = RasConnection.GetActiveConnectionByName(Pass, RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User));
            if (dialer.IsBusy)
            {
                dialer.DialAsyncCancel();
            }
            else
            {
                if (RasCon != null)
                {
                    RasCon.HangUp();
                }
            }
            try
            {
                Process.GetProcessesByName("ping").First().Kill();
            }
            catch (Exception ee)
            { };



            Boolean isBusy = dialer.IsBusy;


            dialer.DialAsyncCancel();
            isBusy = dialer.IsBusy;
            if (isBusy)
            {
                dialer.DialAsyncCancel();
            }
        }