/// <summary> /// 创建或更新一个VPN连接(指定VPN名称,及IP) /// </summary> public void CreateOrUpdateVPN(string updateVPNname, string updateVPNip) { Log.debug(TAG, "CreateOrUpdateVPN"); RasDialer dialer = new RasDialer(); RasPhoneBook allUsersPhoneBook = new RasPhoneBook(); allUsersPhoneBook.Open(); RasEntry entry = null; if (allUsersPhoneBook.Entries.Contains(updateVPNname)) { entry = allUsersPhoneBook.Entries[updateVPNname]; entry.EncryptionType = RasEncryptionType.Optional; entry.PhoneNumber = updateVPNip; IPAddress _ip; IPAddress.TryParse(updateVPNip, out _ip); entry.IPAddress = _ip; entry.Options.UsePreSharedKey = true; entry.UpdateCredentials(RasPreSharedKey.Client, "123456"); entry.Update(); } else { entry = RasEntry.CreateVpnEntry(updateVPNname, updateVPNip, RasVpnStrategy.L2tpOnly, RasDevice.GetDeviceByName("(L2TP)", RasDeviceType.Vpn)); entry.EncryptionType = RasEncryptionType.Optional; entry.Options.UsePreSharedKey = true; allUsersPhoneBook.Entries.Add(entry); entry.UpdateCredentials(RasPreSharedKey.Client, "123456"); } }
private void CreateOrUpdateVPNEntry(string ip) { Log.debug(TAG, "CreateOrUpdateVPNEntry ip:" + ip); try { RasEntry entry = null; if (allUsersPhoneBook.Entries.Contains(VPNNAME)) { entry = allUsersPhoneBook.Entries[VPNNAME]; entry.EncryptionType = RasEncryptionType.Optional; entry.PhoneNumber = ip; IPAddress _ip; IPAddress.TryParse(ip, out _ip); entry.IPAddress = _ip; entry.Options.UsePreSharedKey = true; entry.UpdateCredentials(RasPreSharedKey.Client, "123456"); entry.Update(); } else { entry = RasEntry.CreateVpnEntry(VPNNAME, ip, RasVpnStrategy.L2tpOnly, RasDevice.GetDeviceByName("(L2TP)", RasDeviceType.Vpn)); entry.EncryptionType = RasEncryptionType.Optional; entry.Options.UsePreSharedKey = true; allUsersPhoneBook.Entries.Add(entry); entry.UpdateCredentials(RasPreSharedKey.Client, "123456"); } } catch (Exception ex) { MessageBox.Show("创建VPN失败!error msg:" + ex.ToString()); Log.debug(TAG, "CreateOrUpdateVPNEntry error:" + ex.ToString()); } }
private bool VPN_Create() { //https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/aa377274(v=vs.85) string preSharedKey = "kit2020!^)!$)%^";//l2tp 공유기 키 RasVpnStrategy strategy = RasVpnStrategy.L2tpOnly; RasEntry vpnEntry = RasEntry.CreateVpnEntry(VPN_Name, VPN_Connection_IP, strategy, RasDevice.Create(VPN_Name, RasDeviceType.Vpn), false); // vpnEntry.Options.RequireDataEncryption = true; //데이터 암호화 vpnEntry.Options.UsePreSharedKey = true; //l2tp/ipsec vpnEntry.Options.UseLogOnCredentials = false; // 로그인 기록 저장 vpnEntry.Options.RequireMSChap = false; //Microsoft CHAP Version vpnEntry.Options.RequireMSChap2 = true; //Microsoft CHAP Version 2 (MS-CHAP v2) vpnEntry.DnsAddress = System.Net.IPAddress.Parse(VPN_Create_VirtualIP); vpnEntry.Options.RemoteDefaultGateway = false; //게이트웨이 0.0.0.0으로 RasPhoneBook phoneBook = new RasPhoneBook(); try { phoneBook.Open(); phoneBook.Entries.Add(vpnEntry); //vpn 생성 vpnEntry.UpdateCredentials(RasPreSharedKey.Client, preSharedKey); //l2tp 공유키 설정 return(true); } catch (Exception ex) { Exception FailText = ex; MessageBox.Show(string.Concat(ex.ToString(), "\n")); return(false); } }
// 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); }
private void btn_CreateRAS_Click(object sender, EventArgs e) { try { Userinfo userinfo = new Userinfo(tb_user.Text, tb_pwd.Text); this.AllUsersPhoneBook.Open(); RasEntry entry = RasEntry.CreateVpnEntry(EntryName, IP, RasVpnStrategy.L2tpOnly, RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn)); entry.Options.DoNotNegotiateMultilink = false; //多链路 entry.EncryptionType = RasEncryptionType.None; //允许未加密密码 entry.Options.RequireEncryptedPassword = false; entry.NetworkProtocols.IPv6 = false; //取消IPV6服务 entry.Options.CacheCredentials = true; //win8以上记住密码 DialogResult result = MessageBox.Show("你确定要创建账号为 " + userinfo.User + " 密码为 " + userinfo.Pwd + " 的拨号程序吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (result == DialogResult.No) { return; } foreach (RasEntry temp in this.AllUsersPhoneBook.Entries) { if (temp.Name == entry.Name) { MessageBox.Show("创建失败,已经存在拨号连接 " + entry.Name, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } this.status.Text = "创建中..."; this.AllUsersPhoneBook.Entries.Add(entry); if (tb_user.Text != "" && tb_pwd.Text != "") { NetworkCredential credential = new NetworkCredential(tb_user.Text, tb_pwd.Text); entry.UpdateCredentials(credential); } MessageBox.Show("创建成功!请点击右下角网络连接处的 " + entry.Name + " \n为您添加路由ing...", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); btn_hzly_Click(this, new EventArgs()); this.status.Text = "创建成功!"; } catch { MessageBox.Show("创建失败,请设定防火墙允许!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); Process.GetCurrentProcess().Kill(); } }
private void getPhoneEntries() { List <vpn_Entry> RemoteVpns = getRemoteVpn(); this.AllUsersPhoneBook.Open(); foreach (vpn_Entry vpn in RemoteVpns) { if (!this.AllUsersPhoneBook.Entries.Contains(vpn.Name)) { if (string.IsNullOrEmpty(vpn.vpnIP)) { continue; } RasEntry re = RasEntry.CreateVpnEntry(vpn.Name, vpn.vpnIP, RasVpnStrategy.PptpOnly, RasDevice.Create("VPN_PPTP", RasDeviceType.Vpn)); re.EncryptionType = RasEncryptionType.Optional; this.AllUsersPhoneBook.Entries.Add(re); re.UpdateCredentials(new NetworkCredential(vpn.vpnID, vpn.vpnPW)); re.Update(); } else { RasEntry re = this.AllUsersPhoneBook.Entries[vpn.Name]; IPAddress tryP = null; if (!string.IsNullOrEmpty(vpn.vpnIP)) { IPAddress.TryParse(vpn.vpnIP, out tryP); } re.IPAddress = tryP; re.PhoneNumber = re.IPAddress == null?string.Empty:re.IPAddress.ToString(); NetworkCredential nc = re.GetCredentials(); nc.UserName = vpn.vpnID; nc.Password = vpn.vpnPW; re.UpdateCredentials(nc); re.Update(); } } }
static public void CreateConnectionEntry(BaseProxyServer ps, ProxyProtocolTypeEnum protocolType) { if (!ps.IsProtocolAvailable(protocolType)) { throw new ArgumentException("Protocol " + protocolType.ToString() + " not avilable"); } //http://stackoverflow.com/questions/36213393/get-connection-status-vpn-using-dotras // File.WriteAllText("your rasphone.pbk path","")//Add RasPhoneBook rasPhoneBook1 = new RasPhoneBook(); string rasPhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);//alt RasPhoneBookType.AllUsers rasPhoneBook1.Open(rasPhoneBookPath); string deviceTypeStr = "(" + protocolType.ToString() + ")"; RasVpnStrategy strategy = (protocolType == ProxyProtocolTypeEnum.L2TP) ? RasVpnStrategy.L2tpOnly : RasVpnStrategy.PptpOnly; //alt //RasVpnStrategy strategy = RasVpnStrategy.Default; RasEntry entry = RasEntry.CreateVpnEntry(ps.GetConnectionName(), ps.Url, strategy, RasDevice.GetDeviceByName(deviceTypeStr, RasDeviceType.Vpn, false)); entry.EncryptionType = ps.EncryptionType.ToEnum <RasEncryptionType>(); if (protocolType == ProxyProtocolTypeEnum.L2TP && !string.IsNullOrEmpty(ps.GetProxyProvider().UserPresharedKey)) { entry.Options.UsePreSharedKey = true; } rasPhoneBook1.Entries.Add(entry); if (protocolType == ProxyProtocolTypeEnum.L2TP && !string.IsNullOrEmpty(ps.GetProxyProvider().UserPresharedKey)) { entry.UpdateCredentials(RasPreSharedKey.Client, ps.GetProxyProvider().UserPresharedKey); } if (!string.IsNullOrEmpty(ps.GetProxyProvider().VPNLogin)) { //entry.UpdateCredentials(new System.Net.NetworkCredential(ps.JProxyProvider.VPNLogin, ps.JProxyProvider.VPNPassword), false); } }
public void setPreSharedKey(RasEntry entry, string key) //We updaten de presharedkey. { entry.UpdateCredentials(RasPreSharedKey.Client, key); return; }
public void setCredentials(RasEntry entry, System.Net.NetworkCredential credentials) //We updaten de credentials. { entry.UpdateCredentials(credentials); return; }