public static bool LinkVpnAsync(string serverIp, string userId, string password, int times = 30) { if (string.IsNullOrEmpty(serverIp) || string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(password)) { return(false); } var vpnModel = new VpnModel() { ServerIp = serverIp, UserName = userId, PassWord = StringHelper.Base64Decode(password), VpnProtocol = "L2TP" }; do { VpnService.Connect(ref vpnModel); //Global.Linked = await TestInternetAsync(); //if (Global.Linked) //{ // return true; //} Thread.Sleep(1000); if (TestInternet(5)) { return(true); } times--; } while (times > 0); return(false); }
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(); }
public VpnPresenter(IVpnView view) : base(view) { _model = new VpnModel(ViewInfo); }