/// <summary> /// Gets a list of RAS devices available e.g. modems, VPN connections etc. /// </summary> /// <returns></returns> public List <RasDevice> GetDevices() { List <RasDevice> result = new List <RasDevice>(); RasDevice.GetDevices().ToList().ForEach(p => result.Add(p)); return(result); }
/// <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); } }
/// <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); } }
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})"); } }
public void CreateCustomPhoneBookTest() { var tempFolder = TestUtilities.GetTempPath(true); string path = null; try { path = Path.Combine(tempFolder.FullName, string.Format("{0}.pbk", TestUtilities.StripNonAlphaNumericChars(Guid.NewGuid().ToString()))); var target = new RasPhoneBook(); target.Open(path); var device = RasDevice.GetDevices().Where(o => o.Name.Contains("(PPTP)") && o.DeviceType == RasDeviceType.Vpn).FirstOrDefault(); var entry = RasEntry.CreateVpnEntry("Test Entry", IPAddress.Loopback.ToString(), RasVpnStrategy.Default, device); if (entry != null) { target.Entries.Add(entry); } Assert.IsTrue(File.Exists(path), "The phone book file was not found at the expected location. '{0}'", path); } finally { if (Directory.Exists(tempFolder.FullName)) { // The folder was created successfully, delete it before the test completes. Directory.Delete(tempFolder.FullName, true); } } }
/// <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); } }
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连接失败"; } }
/// <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); } }
public void NewRasEntry() { ReadOnlyCollection <RasDevice> devices = RasDevice.GetDevices(); foreach (RasDevice modem in devices) { if (modem.Name.ToLower().Contains("(pptp)")) { this.rDevice = modem; } } newRas = RasEntry.CreateVpnEntry(entryName, entryAddress, RasVpnStrategy.PptpOnly, rDevice, false); newRas.Options.RemoteDefaultGateway = false; newRas.Options.IPv6RemoteDefaultGateway = false; newRas.NetworkProtocols.IPv6 = false; newRas.IdleDisconnectSeconds = 10; newRas.Options.RequireMSChap2 = true; newRas.Options.Internet = false; newRas.Options.UseLogOnCredentials = true; NetworkCredential cred = new NetworkCredential(); cred.UserName = this.entryUsername; cred.Password = this.entrySecret; Data.Pbk.Entries.Add(newRas); Data.Pbk.Entries[Data.Pbk.Entries.Last().Name].UpdateCredentials(cred); }
/// <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); } }
public void GetDevicesTest() { var expected = RasHelper.Instance.GetDevices(); ReadOnlyCollection <RasDevice> actual; actual = RasDevice.GetDevices(); CollectionAssert.AreEqual(expected, actual, new RasDeviceComparer()); }
// Token: 0x0600008C RID: 140 RVA: 0x00005B58 File Offset: 0x00003D58 public RasPhoneBook SetPhoneBookPPTP(string server, RasPhoneBook myPB, string sVPNPrefix) { myPB = this.ClearPhoneBook(myPB, sVPNPrefix); RasDevice pptpDevice = ConnectorVPN.GetPptpDevice(RasDevice.GetDevices()); if (pptpDevice != null) { RasEntry item = RasEntry.CreateVpnEntry(sVPNPrefix, server, RasVpnStrategy.PptpOnly, pptpDevice, true); myPB.Entries.Add(item); } return(myPB); }
public static void Connect(ref VpnModel vpnModel) { if (vpnModel == null) { return; } using (var phoneBook = new RasPhoneBook()) { //PhoneBook.Open(RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers)); phoneBook.Open(PhoneBookPath); RasEntry entry; if (phoneBook.Entries.Contains(AdapterName)) { phoneBook.Entries.Remove(AdapterName); } if (vpnModel.VpnProtocol.Contains("PPTP")) { entry = RasEntry.CreateVpnEntry( AdapterName, vpnModel.ServerIp, RasVpnStrategy.PptpOnly, RasDevice.GetDevices().First(o => o.DeviceType == RasDeviceType.Vpn)); } else { entry = RasEntry.CreateVpnEntry( AdapterName, vpnModel.ServerIp, RasVpnStrategy.L2tpOnly, RasDevice.GetDevices().First(o => o.DeviceType == RasDeviceType.Vpn)); } phoneBook.Entries.Add(entry); entry.Options.PreviewDomain = false; entry.Options.ShowDialingProgress = false; entry.Options.PromoteAlternates = false; entry.Options.DoNotNegotiateMultilink = false; //if (VpnProtocol.Equals("L2TP")) //{ // Entry.Options.UsePreSharedKey = true; // Entry.UpdateCredentials(RasPreSharedKey.Client, PreSharedKey); // Entry.Update(); //} Global.Dialer.EntryName = AdapterName; //Dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers); Global.Dialer.PhoneBookPath = PhoneBookPath; Global.Dialer.Credentials = new NetworkCredential(vpnModel.UserName, vpnModel.PassWord); } Global.Handle = Global.Dialer.DialAsync(); }
// Token: 0x0600008D RID: 141 RVA: 0x00005BA4 File Offset: 0x00003DA4 public RasPhoneBook SetPhoneBookL2TP(string server, RasPhoneBook myPB, string sVPNPrefix, string sSharedKey) { myPB = this.ClearPhoneBook(myPB, sVPNPrefix); RasDevice l2tpDevice = ConnectorVPN.GetL2tpDevice(RasDevice.GetDevices()); if (l2tpDevice != null) { RasEntry rasEntry = RasEntry.CreateVpnEntry(sVPNPrefix, server, RasVpnStrategy.L2tpOnly, l2tpDevice, true); rasEntry.Options.UsePreSharedKey = true; rasEntry.Options.UseLogOnCredentials = true; myPB.Entries.Add(rasEntry); rasEntry.UpdateCredentials(RasPreSharedKey.Client, sSharedKey); } return(myPB); }
public void CreateConnect(string ConnectName) { RasDialer dialer = new RasDialer(); try { 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 = " "; } catch (Exception) { System.Windows.Forms.MessageBox.Show("创建PPPoE连接失败"); } }
/// <summary> /// Connect /// </summary> /// <param name="connectName"></param> /// <param name="userName"></param> /// <param name="password"></param> public static void Connect(string connectName, string userName, string password) { //关闭现有连接 IReadOnlyCollection <RasConnection> conns = RasConnection.GetActiveConnections(); if (conns.Count != 0) { foreach (var conn in conns) { conn.HangUp(); } } RasPhoneBook rasPhoneBook = new RasPhoneBook(); string path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers); rasPhoneBook.Open(path); //创建一个新的PPPOE连接 IReadOnlyCollection <RasDevice> rasDevices = RasDevice.GetDevices(); RasDevice device = rasDevices.First(d => d.DeviceType == RasDeviceType.PPPoE); var entry = RasEntry.CreateBroadbandEntry(connectName, device); entry.PhoneNumber = userName; //如果不存在该名字的用户名和密码的连接,则Add 存在则更新 if (!rasPhoneBook.Entries.Contains(connectName)) { rasPhoneBook.Entries.Add(entry); } else { rasPhoneBook.Entries[connectName].Update(); } RasDialer dialer = new RasDialer { EntryName = connectName, PhoneNumber = userName, AllowUseStoredCredentials = true, PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers), Credentials = new NetworkCredential(userName, password), Timeout = 1000 * 10 }; RasHandle rasHandle = dialer.Dial(); }
public bool Connect(VPNServer server) { Disconnect(); current_server = server; try { using (RasPhoneBook phonebook = new RasPhoneBook()) { phonebook.Open(VPNSettings.VPNRasPhoneBook); string name = VPNSettings.ConnectionEntry; var devices = RasDevice.GetDevices(); RasDevice device = null; foreach (RasDevice modem in devices) { if (modem.Name.ToLower().Contains("(pptp)")) { device = modem; break; } } RasEntry entry = RasEntry.CreateVpnEntry(name, server.ipinfo.ip, RasVpnStrategy.Default, device); phonebook.Entries.Clear(); phonebook.Entries.Add(entry); } RasDialer dialer = new RasDialer(); dialer.EntryName = VPNSettings.ConnectionEntry; dialer.PhoneBookPath = VPNSettings.VPNRasPhoneBook; dialer.Credentials = server.credentials; dialer.DialAsync(); return(true); } catch (Exception e) { MessageBox.Show(e.ToString()); return(false); } }
/// <summary> /// Creates a VPN entry for the phonebook. /// </summary> /// <param name="phonebook">The phonebook to receive the entry.</param> /// <param name="entryName">The name of the entry.</param> /// <param name="serverAddress">The server address to connect to.</param> /// <returns>The entry id.</returns> private static Guid CreateVpnEntry(RasPhoneBook phonebook, string entryName, string serverAddress) { var entryId = Guid.Empty; var device = RasDevice.GetDevices().Where(o => o.Name.Contains("(PPTP)") && o.DeviceType == RasDeviceType.Vpn).FirstOrDefault(); var entry = RasEntry.CreateVpnEntry(entryName, serverAddress, RasVpnStrategy.PptpOnly, device); if (entry != null) { phonebook.Entries.Add(entry); entryId = entry.Id; } return(entryId); }
private void Connect() { var rasPhoneBook = new RasPhoneBook(); rasPhoneBook.Open(PhoneBookPath); if (!rasPhoneBook.Entries.Contains(VpnName)) { var vpnDevice = RasDevice.GetDevices().FirstOrDefault(d => d.DeviceType == RasDeviceType.Vpn && d.Name.ToLower(CultureInfo.CurrentCulture).Contains("(PPTP)".ToLower(CultureInfo.CurrentCulture))); var rasEntry = RasEntry.CreateVpnEntry(VpnName, VpnServer, RasVpnStrategy.PptpOnly, vpnDevice, false); rasEntry.Options.ShowDialingProgress = false; rasPhoneBook.Entries.Add(rasEntry); } _dialer.EntryName = VpnName; _dialer.PhoneBookPath = PhoneBookPath; _dialer.Credentials = _credentials; _handle = _dialer.DialAsync(); }
public void GetDevicesByTypeTest() { RasDeviceType deviceType = RasDeviceType.Vpn; Collection <RasDevice> tempDevices = new Collection <RasDevice>(); foreach (RasDevice device in RasDevice.GetDevices()) { if (device.DeviceType == deviceType) { tempDevices.Add(device); } } ReadOnlyCollection <RasDevice> expected = new ReadOnlyCollection <RasDevice>(tempDevices); ReadOnlyCollection <RasDevice> actual = RasDevice.GetDevicesByType(deviceType); CollectionAssert.AreEqual(expected, actual, new RasDeviceComparer()); }
/// <summary> /// Occurs when the user clicks the Create Entry 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 CreateEntryButton_Click(object sender, EventArgs e) { try { // This opens the phonebook so it can be used. Different overloads here will determine where the phonebook is opened/created. AllUsersPhoneBook.Open(PHONE_BOOK_PATH); // Create the entry that will be used by the dialer to dial the connection. Entries can be created manually, however the static methods on // the RasEntry class shown below contain default information matching that what is set by Windows for each platform. var rasDevice = RasDevice.GetDevices().FirstOrDefault(_ => _.Name.Contains("(PPTP)") && _.DeviceType == RasDeviceType.Vpn); var entry = RasEntry.CreateVpnEntry(EntryName, IPAddress.Loopback.ToString(), RasVpnStrategy.Default, rasDevice); // Add the new entry to the phone book. AllUsersPhoneBook.Entries.Add(entry); } catch (Exception ex) { StatusTextBox.AppendText(ex.ToString()); } }
public override void Initialize() { base.Initialize(); this._phoneBookPath = System.IO.Path.GetTempFileName(); this._entryName = Guid.NewGuid().ToString(); this._phoneBook = RasPhoneBook.Open(this._phoneBookPath); this._entry = new RasEntry(this._entryName); this._entry.Device = RasDevice.GetDevices().Where(o => o.Name.Contains("(PPTP)") && o.DeviceType == RasDeviceType.Vpn).FirstOrDefault(); this._entry.EncryptionType = RasEncryptionType.Require; this._entry.EntryType = RasEntryType.Vpn; this._entry.FramingProtocol = RasFramingProtocol.Ppp; this._entry.NetworkProtocols.IP = true; this._entry.NetworkProtocols.IPv6 = true; this._entry.PhoneNumber = IPAddress.Loopback.ToString(); this._entry.VpnStrategy = RasVpnStrategy.Default; this._phoneBook.Entries.Add(this._entry); }
/// <summary> /// Created an pptp protocol vpn entry。When it be created , it will be store in phonebook (RasPhoneBookType.User) entries collection. /// </summary> /// <param name="pkb"></param> /// <param name="entryName"></param> /// <returns>Assurmed phone book had been opened</returns> /// <exception cref="System.Exception"></exception> /// <exception cref="System.ArgumentException"/> public static RasEntry CreatePptpVpnEntry(this RasPhoneBook pbk, string entryName) { var pptpdDevice = RasDevice.GetDevices().First(dev => dev.DeviceType == RasDeviceType.Vpn && (dev.Name.Contains(@"PPTP") || dev.Name.Contains(@"pptp"))); pbk.Open(RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User)); var entry = RasEntry.CreateVpnEntry(entryName, "", RasVpnStrategy.PptpOnly, pptpdDevice); entry.IdleDisconnectSeconds = RasIdleDisconnectTimeout.Disabled; entry.EncryptionType = RasEncryptionType.Require; var option = entry.Options; option.PreviewUserPassword = false; option.RemoteDefaultGateway = true; option.RequireMSChap = false; option.RequireMSChap2 = true; option.TerminalBeforeDial = false; pbk.Entries.Add(entry); return(entry); }
private static bool CreateConnector() { RasPhoneBook book = new RasPhoneBook(); try { book.Open(RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User)); if (book.Entries.Contains(ConnectorName)) { book.Entries[ConnectorName].PhoneNumber = " "; book.Entries[ConnectorName].Update(); } else { //ReadOnlyCollection<RasDevice> readOnlyCollection = RasDevice.GetDevices(); RasDevice device = RasDevice.GetDevices().First(o => o.DeviceType == RasDeviceType.PPPoE); RasEntry entry = RasEntry.CreateBroadbandEntry(ConnectorName, device); entry.PhoneNumber = " "; book.Entries.Add(entry); } try { ReadOnlyCollection <RasConnection> conList = RasConnection.GetActiveConnections(); foreach (RasConnection con in conList) { con.HangUp(); } } catch (Exception ex) { Console.WriteLine("General - Logout abnormal(Exception): " + ex); } return(true); } catch (Exception ex) { Console.WriteLine("Create a PPPoE connection fails(Exception): " + ex); return(false); } }
private bool CreateVpnEntry(string sVpnIp) { AllUsersPhoneBook.Open(); if (this.AllUsersPhoneBook.Entries.Contains(sEntryName)) { var ipInfo = GetVpnIP(); if (ipInfo != null) { DoMessage(string.Format("{0} 已经存在到同一个服务器的连接!", DateTime.Now.ToString())); return(false); } this.AllUsersPhoneBook.Entries[sEntryName].PhoneNumber = sVpnIp; } else { try { var deviceList = RasDevice.GetDevices(); var vpnDevice = deviceList.Where(d => d.DeviceType == RasDeviceType.Vpn && d.Name.Contains("(PPTP)")).FirstOrDefault(); if (vpnDevice == null) { DoMessage(string.Format("{0} 无可用VPN端口,请尝试执行[netsh winsock reset]进行修复!", DateTime.Now.ToString())); return(false); } RasEntry entry = RasEntry.CreateVpnEntry(sEntryName, sVpnIp, RasVpnStrategy.PptpOnly, vpnDevice, false); entry.EncryptionType = RasEncryptionType.Optional; this.AllUsersPhoneBook.Entries.Add(entry); } catch (Exception ex) { DoMessage(string.Format("{0} {1}", DateTime.Now.ToString(), ex.ToString())); } } return(true); }
public static void CreateSstpVpn(string entryName, string serverName, RasPhoneBookType rasPhoneBookType = RasPhoneBookType.User, RasVpnStrategy rasVpnStrategy = RasVpnStrategy.SstpOnly) { var path = RasPhoneBook.GetPhoneBookPath(rasPhoneBookType); using (var phoneBook = new RasPhoneBook()) { phoneBook.Open(path); if (phoneBook.Entries.Contains(entryName)) { MessageBox.Show(entryName + " recreated!", "Phonebook", MessageBoxButtons.OK, MessageBoxIcon.Warning); phoneBook.Entries.Remove(entryName); } var device = RasDevice.GetDevices().FirstOrDefault(d => d.Name.Contains("SSTP")); var entry = RasEntry.CreateVpnEntry(entryName, serverName, rasVpnStrategy, device); entry.Options.RemoteDefaultGateway = false; phoneBook.Entries.Add(entry); } }
public static void CreateOrUpdatePPPOE(string updatePPPOEname) { RasDialer dialer = new RasDialer(); RasPhoneBook currentUserPhoneBook = new RasPhoneBook(); string path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User); currentUserPhoneBook.Open(path); if (currentUserPhoneBook.Entries.Contains(updatePPPOEname)) { currentUserPhoneBook.Entries[updatePPPOEname].PhoneNumber = " "; currentUserPhoneBook.Entries[updatePPPOEname].Update(); } 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.PhoneNumber = " "; currentUserPhoneBook.Entries.Add(entry); } }
/// <summary> /// 创建或更新一个Vpn连接(指定Vpn名称,及IP) /// </summary> public void CreateOrUpdateVpn(string updateVpNname, string updateVpNip) { RasDialer dialer = new RasDialer(); RasPhoneBook allUsersPhoneBook = new RasPhoneBook(); allUsersPhoneBook.Open(allUsersPhoneBook.Path); // 如果已经该名称的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.GetDevices().First(d => d.Name == "(PPTP)")); allUsersPhoneBook.Entries.Add(entry); dialer.EntryName = updateVpNname; dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers); } }
//This creates the M5 phonebook with all the necessary parameters in it private static void CreateRas(string phonebookPath) { DotRas.RasPhoneBook pb = new DotRas.RasPhoneBook(); pb.Open(phonebookPath); RasDevice rasDevice = RasDevice.GetDevices().Where(P => P.DeviceType == RasDeviceType.Modem && P.Name.ToUpper().StartsWith("M5-")).FirstOrDefault(); if (rasDevice == null) { return; } RasEntry entry = pb.Entries.Where(p => p.Name == "M5_1").FirstOrDefault(); if (entry == null) { entry = RasEntry.CreateDialUpEntry("M5_1", "0", rasDevice); } entry.FramingProtocol = RasFramingProtocol.Ppp; entry.EncryptionType = RasEncryptionType.None; entry.FrameSize = 0; entry.Options.DisableLcpExtensions = true; entry.Options.DisableNbtOverIP = true; entry.Options.DoNotNegotiateMultilink = true; entry.Options.DoNotUseRasCredentials = true; entry.Options.Internet = false; entry.Options.IPHeaderCompression = false; entry.Options.ModemLights = true; entry.Options.NetworkLogOn = false; entry.Options.PreviewDomain = false; entry.Options.PreviewPhoneNumber = false; entry.Options.PreviewUserPassword = false; entry.Options.PromoteAlternates = false; entry.Options.ReconnectIfDropped = false; entry.Options.RemoteDefaultGateway = false; entry.Options.RequireChap = false; entry.Options.RequireDataEncryption = false; entry.Options.RequireEap = false; entry.Options.RequireEncryptedPassword = false; entry.Options.RequireMSChap = false; entry.Options.RequireMSChap2 = false; entry.Options.RequireMSEncryptedPassword = false; entry.Options.RequirePap = false; entry.Options.RequireSpap = false; entry.Options.RequireWin95MSChap = false; entry.Options.SecureClientForMSNet = true; entry.Options.SecureFileAndPrint = true; entry.Options.SecureLocalFiles = true; entry.Options.SharedPhoneNumbers = false; entry.Options.SharePhoneNumbers = false; entry.Options.ShowDialingProgress = false; entry.Options.SoftwareCompression = false; entry.Options.TerminalAfterDial = false; entry.Options.TerminalBeforeDial = false; entry.Options.UseCountryAndAreaCodes = false; entry.Options.UseGlobalDeviceSettings = false; entry.Options.UseLogOnCredentials = false; entry.Options.UsePreSharedKey = false; if (pb.Entries.Contains(entry.Name)) { entry.Update(); } else { pb.Entries.Add(entry); } }
public override bool Connect() { this.connected = false; try { this.rasProperties = new RasProperties(this.ParentForm, this); DirectoryInfo directoryInfo = (new FileInfo(this.GetType().Assembly.Location)).Directory; if ((directoryInfo == null || directoryInfo.Exists == false) && string.IsNullOrEmpty(this.PhonebookPath)) { rasProperties.Error("The phonebook path hasn't been set. Aborting RAS connection."); return(this.connected = false); } this.Embed(this.rasProperties); // The 'RasDevice.GetDeviceByName([string], [RasDeviceType])' method is obsolete as of DotRas (ChangeSet 93435): RasEntry entry = RasEntry.CreateVpnEntry(this.Favorite.Name, this.Favorite.ServerName, RasVpnStrategy.Default, (from d in RasDevice.GetDevices() where d.DeviceType == RasDeviceType.Vpn && d.Name.ToUpper() .Contains("(PPTP)") select d).FirstOrDefault()); // Create the Ras phonebook or upen it under the below mentioned path. string phonebookPath = this.PhonebookPath ?? Path.Combine(directoryInfo.FullName, "rasphone.pbk"); this.rasPhoneBook = new RasPhoneBook(); this.rasPhoneBook.Open(phonebookPath); this.rasProperties.RasEntry = entry; // Check if the entry hasn't been added. if (!this.rasPhoneBook.Entries.Contains(entry.Name)) { this.rasPhoneBook.Entries.Add(entry); } this.rasDialer = new RasDialer { PhoneBookPath = phonebookPath, // Set the credentials later ... Credentials = null, // Initialize with default values (same as the designer would do) EapOptions = new RasEapOptions(false, false, false), // Initialize with default values (same as the designer would do) Options = new RasDialOptions(false, false, false, false, false, false, false, false, false, false, false), EntryName = this.Favorite.Name }; this.rasDialer.Error += this.rasDialer_Error; this.rasDialer.DialCompleted += this.rasDialer_DialCompleted; this.rasDialer.StateChanged += this.rasDialer_StateChanged; // Set the ras dialer credentials and checks if setting the credentials has been // successful; if not NULL will be set to the ras dealer credentials and in that // case we'll load the RAS connection from the phone book. if ((this.rasDialer.Credentials = this.Favorite.Credential) == null) { rasProperties.Warn("Terminals has no credentials, showing dial dialog ..."); RasDialDialog rasDialDialog = new RasDialDialog { PhoneBookPath = phonebookPath, EntryName = entry.Name }; if (rasDialDialog.ShowDialog() == DialogResult.OK) { rasProperties.Info(string.Format("{0} {1}", "Thank you for providing the credentials." + "Using the Terminals credentials, dialing ...")); return(this.connected = true); } rasProperties.Error("Terminating RAS connection, either credentials haven't been supplied or error connecting to the target."); return(this.connected = false); } rasProperties.Info("Using the Terminals credentials, dialing ..."); this.rasDialer.Dial(); return(this.connected = true); } catch (Exception ex) { rasProperties.Error(string.Format("Terminals was unable to create the {0} connection.", this.Favorite.Protocol), ex); return(this.connected = false); } }