private void ConnectVpn() { // Get the rest of the configuration data from the Manifest //first create the VPN RasPhoneBook rasPhoneBook = new RasPhoneBook(); rasPhoneBook.Open(RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User)); if (!rasPhoneBook.Entries.Contains(_vpnConfiguration.Name)) { ExecutionServices.SystemTrace.LogInfo($"Creating VPN {_vpnConfiguration.Name} {_vpnConfiguration.ServerIp}"); var currentVpn = CreateVpn(_vpnConfiguration.Name, _vpnConfiguration.ServerIp); rasPhoneBook.Entries.Add(currentVpn); } rasPhoneBook.Dispose(); RasDialer rasDialer = new RasDialer { EntryName = _vpnConfiguration.Name, PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User), Credentials = new System.Net.NetworkCredential() { UserName = _vpnConfiguration.UserName, Password = _vpnConfiguration.Password, Domain = _vpnConfiguration.Domain } }; try { if (rasDialer.IsBusy) { rasDialer.DialAsyncCancel(); } rasDialer.Dial(); } catch (InvalidOperationException ioExcep) { ExecutionServices.SystemTrace.LogDebug(ioExcep.Message); log_textBox.Text += @"Unable to connect to VPN" + Environment.NewLine; throw; } catch (RasException rasException) { ExecutionServices.SystemTrace.LogDebug(rasException.Message); log_textBox.Text += @"Unable to connect to VPN" + Environment.NewLine; throw; } vpnname_textBox.Text = _vpnConfiguration.Name; status_textBox.Text = @"Connected"; log_textBox.Text += @"Connected to VPN" + Environment.NewLine; }
/// <summary> /// 创建或更新一个VPN连接(指定VPN名称,及IP) /// </summary> public void CreateOrUpdateVPN(string updateVPNname, string updateVPNip) { try { switch (TunnelType.ToLower()) { case "pptp": VpnStrategy = RasVpnStrategy.PptpOnly; break; case "l2tp": VpnStrategy = RasVpnStrategy.L2tpOnly; break; case "sstp": VpnStrategy = RasVpnStrategy.SstpOnly; break; case "ikev2": VpnStrategy = RasVpnStrategy.IkeV2Only; break; case "automatic": VpnStrategy = RasVpnStrategy.Default; break; default: VpnStrategy = RasVpnStrategy.PptpOnly; break; } switch (EncryptionLevel.ToLower()) { case "optional": EncryptionType = RasEncryptionType.Optional; break; case "noencryption": EncryptionType = RasEncryptionType.None; break; case "required": EncryptionType = RasEncryptionType.Require; break; case "maximum": EncryptionType = RasEncryptionType.RequireMax; break; default: EncryptionType = RasEncryptionType.None; break; } RasPhoneBook allUsersPhoneBook = new RasPhoneBook(); allUsersPhoneBook.Open(RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers)); // 如果已经该名称的VPN已经存在,则更新这个VPN服务器地址 if (allUsersPhoneBook.Entries.Contains(updateVPNname)) { allUsersPhoneBook.Entries[updateVPNname].PhoneNumber = updateVPNip; allUsersPhoneBook.Entries[updateVPNname].EncryptionType = EncryptionType; allUsersPhoneBook.Entries[updateVPNname].VpnStrategy = VpnStrategy; allUsersPhoneBook.Entries[updateVPNname].Options.RemoteDefaultGateway = SplitTunneling; allUsersPhoneBook.Entries[updateVPNname].Options.IPv6RemoteDefaultGateway = SplitTunneling; switch (AuthenticationMethod.ToLower()) { case "pap": allUsersPhoneBook.Entries[updateVPNname].Options.RequirePap = true; allUsersPhoneBook.Entries[updateVPNname].Options.RequireEap = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireChap = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireMSChap2 = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireEncryptedPassword = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireDataEncryption = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireMSEncryptedPassword = false; break; case "eap": allUsersPhoneBook.Entries[updateVPNname].Options.RequireEap = true; allUsersPhoneBook.Entries[updateVPNname].Options.RequireChap = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequirePap = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireMSChap2 = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireEncryptedPassword = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireDataEncryption = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireMSEncryptedPassword = false; break; case "chap": allUsersPhoneBook.Entries[updateVPNname].Options.RequireChap = true; allUsersPhoneBook.Entries[updateVPNname].Options.RequirePap = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireEap = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireMSChap2 = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireEncryptedPassword = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireDataEncryption = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireMSEncryptedPassword = false; break; case "mschapv2": allUsersPhoneBook.Entries[updateVPNname].Options.RequireMSChap2 = true; allUsersPhoneBook.Entries[updateVPNname].Options.RequirePap = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireEap = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireChap = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireEncryptedPassword = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireDataEncryption = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireMSEncryptedPassword = false; break; default: allUsersPhoneBook.Entries[updateVPNname].Options.RequirePap = true; allUsersPhoneBook.Entries[updateVPNname].Options.RequireEap = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireChap = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireMSChap2 = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireEncryptedPassword = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireDataEncryption = false; allUsersPhoneBook.Entries[updateVPNname].Options.RequireMSEncryptedPassword = false; break; } } // 创建一个新VPN else { RasEntry entry = RasEntry.CreateVpnEntry(updateVPNname, updateVPNip, VpnStrategy, RasDevice.GetDevices().First(o => o.DeviceType == RasDeviceType.Vpn)); entry.EncryptionType = EncryptionType; entry.Options.RemoteDefaultGateway = SplitTunneling; entry.Options.IPv6RemoteDefaultGateway = SplitTunneling; switch (AuthenticationMethod.ToLower()) { case "pap": entry.Options.RequirePap = true; entry.Options.RequireEap = false; entry.Options.RequireChap = false; entry.Options.RequireMSChap2 = false; entry.Options.RequireEncryptedPassword = false; entry.Options.RequireDataEncryption = false; entry.Options.RequireMSEncryptedPassword = false; break; case "eap": entry.Options.RequireEap = true; entry.Options.RequireChap = false; entry.Options.RequirePap = false; entry.Options.RequireMSChap2 = false; entry.Options.RequireEncryptedPassword = false; entry.Options.RequireDataEncryption = false; entry.Options.RequireMSEncryptedPassword = false; break; case "chap": entry.Options.RequireChap = true; entry.Options.RequirePap = false; entry.Options.RequireEap = false; entry.Options.RequireMSChap2 = false; entry.Options.RequireEncryptedPassword = false; entry.Options.RequireDataEncryption = false; entry.Options.RequireMSEncryptedPassword = false; break; case "mschapv2": entry.Options.RequireMSChap2 = true; entry.Options.RequirePap = false; entry.Options.RequireEap = false; entry.Options.RequireChap = false; entry.Options.RequireEncryptedPassword = false; entry.Options.RequireDataEncryption = false; entry.Options.RequireMSEncryptedPassword = false; break; default: entry.Options.RequirePap = true; entry.Options.RequireEap = false; entry.Options.RequireChap = false; entry.Options.RequireMSChap2 = false; entry.Options.RequireEncryptedPassword = false; entry.Options.RequireDataEncryption = false; entry.Options.RequireMSEncryptedPassword = false; break; } allUsersPhoneBook.Entries.Add(entry); } if (VpnStrategy == RasVpnStrategy.L2tpOnly) { allUsersPhoneBook.Entries[updateVPNname].Options.UsePreSharedKey = true; allUsersPhoneBook.Entries[updateVPNname].UpdateCredentials(RasPreSharedKey.Client, L2tpPsk); } // 不管当前VPN是否连接,服务器地址的更新总能成功,如果正在连接,则需要VPN重启后才能起作用 allUsersPhoneBook.Entries[updateVPNname].Update(); allUsersPhoneBook.Dispose(); } catch (Exception ex) { //LogUtil.Write("CreateOrUpdateVPN Error:" + ex); } }