示例#1
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);
            }
        }
示例#2
0
        private void createVpnEntry(string sVpnIp)
        {
            AllUsersPhoneBook.Open(Dialer.PhoneBookPath);

            if (this.AllUsersPhoneBook.Entries.Contains(sEntryName))
            {
                //todo
                this.AllUsersPhoneBook.Entries[sEntryName].PhoneNumber = sVpnIp;
            }
            else
            {
                try
                {
                    RasEntry entry = RasEntry.CreateVpnEntry(sEntryName, sVpnIp, RasVpnStrategy.L2tpOnly,
#pragma warning disable CS0618 // 类型或成员已过时
                                                             device: RasDevice.GetDeviceByName("(L2TP)", RasDeviceType.Vpn));
#pragma warning restore CS0618 // 类型或成员已过时
                    entry.EncryptionType = RasEncryptionType.Optional;

                    this.AllUsersPhoneBook.Entries.Add(entry);
                }
                catch (Exception ex)
                {
                    tMessage.AppendText(ex.ToString());
                }
            }
        }
示例#3
0
        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);
            }
        }
示例#4
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;
                }
            }
        }
示例#5
0
        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);
        }
示例#6
0
        public void IpSecConnectionCreate(string ipSecConnectionName, string presharedKey)
        {
            if (ipSecConnectionName == null)
            {
                throw new ArgumentNullException(nameof(ipSecConnectionName));
            }

            if (presharedKey == null)
            {
                throw new ArgumentNullException(nameof(presharedKey));
            }

            if (ipSecConnectionName.Length == 0)
            {
                throw new ArgumentException($"nameof(ipSecConnectionName) is empty string");
            }

            if (presharedKey.Length == 0)
            {
                throw new ArgumentException($"nameof(presharedKey) is empty string");
            }

            using (var rasPhoneBook = new RasPhoneBook())
            {
                rasPhoneBook.Open(IpSecClient._phoneBookPath);

                var ipSecRasEntry =
                    RasEntry.CreateVpnEntry(
                        ipSecConnectionName,
                        "0.0.0.0",
                        RasVpnStrategy.L2tpOnly,
                        RasDevice.Create(ipSecConnectionName, RasDeviceType.Vpn)
                        );

                //ipSecRasEntry.DnsAddress = System.Net.IPAddress.Parse("0.0.0.0");
                //ipSecRasEntry.DnsAddressAlt = System.Net.IPAddress.Parse("0.0.0.0");
                ipSecRasEntry.EncryptionType = RasEncryptionType.Require;
                ipSecRasEntry.EntryType      = RasEntryType.Vpn;
                ipSecRasEntry.Options.RequireDataEncryption = true;
                ipSecRasEntry.Options.UsePreSharedKey       = true; // used only for IPSec - L2TP/IPsec VPN
                ipSecRasEntry.Options.UseLogOnCredentials   = false;
                ipSecRasEntry.Options.RequireMSChap2        = true;
                //rasEntry.Options.RemoteDefaultGateway = true;
                ipSecRasEntry.Options.SecureFileAndPrint   = true;
                ipSecRasEntry.Options.SecureClientForMSNet = true;
                ipSecRasEntry.Options.ReconnectIfDropped   = false;

                // If the entry with the same name has already been added, Add throws ArgumentException
                // with message: '<Entry name>' already exists in the phone book.\r\nParameter name: item"
                rasPhoneBook.Entries.Add(ipSecRasEntry);

                // RasEntry.UpdateCredentials() has to be executed after RasEntry has been added to the Phone Book.
                // Otherwise InvalidOperationException is thrown  with message:
                // "The entry is not associated with a phone book."
                ipSecRasEntry.UpdateCredentials(RasPreSharedKey.Client, presharedKey);
                //ipSecRasEntry.UpdateCredentials(new System.Net.NetworkCredential("username", "password"));

                Console.WriteLine($"VPN connection {ipSecConnectionName} created successfully.");
            }
        }
        /// <summary>
        /// Creates a VPN connection in the Windows list of connections.
        /// </summary>
        /// <param name="vpnConnectionName">The name of the VPN connection to create.</param>
        /// <param name="strategy">The VPN strategy to use. If null the default strategy is used.</param>
        /// <param name="deviceType">The RAS (Remote Access Serice) device type. If null the default is used i.e.RasDeviceType.Vpn</param>
        /// <param name="deviceName">The name of the RAS device. If null the default is used i.e. "(PPTP)".</param>
        public void CreateVPNConnectionEntry(
            string vpnConnectionName,
            Nullable <RasVpnStrategy> strategy,
            Nullable <RasDeviceType> deviceType,
            string deviceName)
        {
            _rasPhoneBook.Open();
            if (string.IsNullOrEmpty(vpnConnectionName))
            {
                throw new NullReferenceException("vpnConnectionName may not be null or empty when requesting for a VPN connection to be created.");
            }
            if (!strategy.HasValue)
            {
                strategy = RasVpnStrategy.Default;
            }
            if (!deviceType.HasValue)
            {
                deviceType = RasDeviceType.Vpn;
            }
            if (string.IsNullOrEmpty(deviceName))
            {
                deviceName = "(PPTP)";
            }
            RasEntry entry = RasEntry.CreateVpnEntry(
                vpnConnectionName,
                IPAddress.Loopback.ToString(),
                strategy.Value,
                RasDevice.GetDeviceByName(deviceName, deviceType.Value));

            _rasPhoneBook.Entries.Add(entry);
        }
示例#8
0
文件: VPNHelper.cs 项目: tuyndv/vpn
 /// <summary>
 /// create/update vpn entry
 /// </summary>
 /// <param name="name">name of the connection</param>
 /// <param name="ip">ip address of connection</param>
 public void SetEntry(string name, string ip)
 {
     // This opens the phonebook so it can be used. Different overloads here will determine where the phonebook is opened/created.
     this.AllUsersPhoneBook.Open(true);
     // check if this entry has already exist
     if (!this.AllUsersPhoneBook.Entries.Contains(name))
     {
         // create entry
         if (ip == "")
         {
             ip = IPAddress.Loopback.ToString();
         }
         // 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.
         RasEntry entry = RasEntry.CreateVpnEntry(name, ip, RasVpnStrategy.PptpFirst,
                                                  RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn));
         // Add the new entry to the phone book.
         this.AllUsersPhoneBook.Entries.Add(entry);
     }
     else if (ip != "")
     {
         // update entry
         this.AllUsersPhoneBook.Entries[name].PhoneNumber = ip;
         this.AllUsersPhoneBook.Entries[name].Device      = RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn);
         this.AllUsersPhoneBook.Entries[name].Update();
     }
 }
示例#9
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;
            }
        }
示例#10
0
        private bool CreateVpn()
        {
            if (null == this.VPNItem)
            {
                return(false);
            }
            // This opens the phonebook so it can be used. Different overloads here will determine where the phonebook is opened/created.
            this.rasPhoneBook.Open();
            if (this.rasPhoneBook.Entries.Count > 0)
            {
                foreach (RasEntry rasEntry in this.rasPhoneBook.Entries)
                {
                    if (rasEntry.Name.Equals(this.VPNItem.EntryName, StringComparison.CurrentCultureIgnoreCase))
                    {
                        LogManager.InfoWithCallback(string.Format("-> VPN 拨号新IP:{0}", this.VPNItem.IP));
                        rasEntry.PhoneNumber = this.VPNItem.IP;
                        return(false);
                    }
                }
            }

            // 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.
            RasEntry entry = RasEntry.CreateVpnEntry(this.VPNItem.EntryName, this.VPNItem.IP, RasVpnStrategy.Default,
                                                     RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn));

            //Must set to None
            entry.EncryptionType = RasEncryptionType.None;
            // entry.PrerequisiteEntryName
            // Add the new entry to the phone book.
            this.rasPhoneBook.Entries.Add(entry);
            return(true);
        }
示例#11
0
 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());
     }
 }
示例#12
0
        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);
                }
            }
        }
示例#13
0
        private void createVpnEntry(string sVpnIp)
        {
            this.AllUsersPhoneBook.Open();

            if (this.AllUsersPhoneBook.Entries.Contains(sEntryName))
            {
                //todo
                this.AllUsersPhoneBook.Entries[sEntryName].PhoneNumber = sVpnIp;
            }
            else
            {
                try
                {
                    RasEntry entry = RasEntry.CreateVpnEntry(sEntryName, sVpnIp, RasVpnStrategy.L2tpOnly,
                                                             RasDevice.GetDeviceByName("(L2TP)", RasDeviceType.Vpn));
                    entry.EncryptionType = RasEncryptionType.Optional;

                    this.AllUsersPhoneBook.Entries.Add(entry);
                }
                catch (Exception ex)
                {
                    tMessage.AppendText(ex.ToString());
                }
            }
        }
示例#14
0
        /// <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");
            }
        }
示例#15
0
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnLogin_Click(object sender, EventArgs e)
        {
            string user = this.txtUser.Text;
            string pwd  = this.txtPwd.Text;

            if (string.IsNullOrEmpty(user) || string.IsNullOrEmpty(pwd))
            {
                return;
            }

            object selectItem = this.cmbPhoneNumber.SelectedItem;

            if (selectItem == null)
            {
                if (!string.IsNullOrEmpty(this.cmbPhoneNumber.Text))
                {
                    RasEntry newEntry = RasEntry.CreateVpnEntry(EntryName,
                                                                this.cmbPhoneNumber.Text, RasVpnStrategy.PptpOnly,
                                                                RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn), false);
                    AddRasEntry(newEntry);
                    AddEntry(newEntry);
                    selectItem = new ComboBoxItem {
                        Text = this.cmbPhoneNumber.Text, Val = EntryName
                    };
                }
                else
                {
                    return;
                }
            }
            ComboBoxItem item  = (ComboBoxItem)selectItem;
            RasEntry     entry = entryList.Single(s => s.Name.Equals(item.Val));

            if (entry == null)
            {
                return;
            }
            this.Dialer.EntryName     = entry.Name;
            this.Dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
            try
            {
                // Set the credentials the dialer should use.
                this.Dialer.Credentials = new NetworkCredential(user, pwd);

                // 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.btnLogout.Enabled = true;
            }
            catch (Exception ex)
            {
                this.AppendText(ex.ToString());
            }
        }
示例#16
0
        /// <summary>
        /// 创建或更新一个VPN连接(指定VPN名称,及IP)
        /// </summary>
        /// <param name="updateVPNname">更新vpn名称</param>
        /// <param name="updateVPNip">更新VPN地址</param>
        /// <returns>操作成功:true,操作失败:false</returns>
        public bool CreateOrUpdateVPN(string updateVPNname, string updateVPNip)
        {
            bool     result    = true;
            DateTime startTime = DateTime.Now;
            string   msg       = string.Empty;

            try
            {
                RasDialer    dialer            = new RasDialer();
                RasPhoneBook allUsersPhoneBook = new RasPhoneBook();
                allUsersPhoneBook.Open();

                // 如果已经该名称的VPN已经存在,则更新这个VPN服务器地址
                if (allUsersPhoneBook.Entries.Contains(updateVPNname))
                {
                    allUsersPhoneBook.Entries[updateVPNname].PhoneNumber = updateVPNip;
                    ////不管当前VPN是否连接,服务器地址的更新总能成功,如果正在连接,则需要VPN重启后才能起作用
                    bool updateResult = allUsersPhoneBook.Entries[updateVPNname].Update();
                    msg = "更新VPN服务器通道为:" + updateVPNip;
                    if (updateResult)
                    {
                        MessagePipe.ExcuteWriteMessageEvent(msg + "成功", Color.Green, false);
                    }
                    else
                    {
                        MessagePipe.ExcuteWriteMessageEvent(msg + "失败", Color.Green, false);
                    }
                }
                else
                {
                    ////创建一个新VPN
                    RasEntry entry = RasEntry.CreateVpnEntry(updateVPNname, updateVPNip, RasVpnStrategy.PptpFirst, RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn));

                    allUsersPhoneBook.Entries.Add(entry);
                    dialer.EntryName = updateVPNname;

                    dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
                    msg = "新建VPN通道:" + updateVPNip + "成功";
                    MessagePipe.ExcuteWriteMessageEvent(msg, Color.Green, false);
                }
            }
            catch (Exception ex)
            {
                result = false;
                MessagePipe.ExcuteWriteMessageEvent("更新或创建VPN通道异常:" + ex.Message.ToString(), Color.Green, true);
            }
            finally
            {
                ////记录交互日志
                string request = "VPN通道名称:" + updateVPNname + ";VPN账号:" + this.userName + ";VPN地址:" + updateVPNip;
                RecordLog.RecordInteractionLog(updateVPNname, "创建VPN通道", request, msg, DateTime.Now.Subtract(startTime), updateVPNip);
            }

            return(result);
        }
        // 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);
        }
示例#18
0
 public void AddL2TPVpnEntity(String l2tpVPNName, String address, String presharedKey)
 {
     if (!allUsersPhoneBook.Entries.Contains(l2tpVPNName))
     {
         RasEntry entry = RasEntry.CreateVpnEntry(l2tpVPNName, address, RasVpnStrategy.L2tpOnly, RasDevice.Create(l2tpVPNName, RasDeviceType.Vpn));
         allUsersPhoneBook.Entries.Add(entry);
     }
     allUsersPhoneBook.Entries[l2tpVPNName].PhoneNumber             = address;
     allUsersPhoneBook.Entries[l2tpVPNName].Options.UsePreSharedKey = true;
     allUsersPhoneBook.Entries[l2tpVPNName].UpdateCredentials(RasPreSharedKey.Client, presharedKey);
     allUsersPhoneBook.Entries[l2tpVPNName].Update();
 }
示例#19
0
        public void AddPPTPVpnEntity(String pptpVPNName, String address)
        {
            if (!allUsersPhoneBook.Entries.Contains(pptpVPNName))
            {
                RasEntry entry = RasEntry.CreateVpnEntry(pptpVPNName, address, RasVpnStrategy.PptpOnly, RasDevice.Create(pptpVPNName, RasDeviceType.Vpn));

                allUsersPhoneBook.Entries.Add(entry);
            }

            allUsersPhoneBook.Entries[pptpVPNName].PhoneNumber = address;
            allUsersPhoneBook.Entries[pptpVPNName].Update();
        }
示例#20
0
      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();
      }
示例#21
0
        public void Connect()
        {
            BackgroundWorker backgroundWorker = new BackgroundWorker();

            backgroundWorker.DoWork +=
                delegate
            {
                try
                {
                    System.Net.NetworkCredential creds;
                    RasEntry     pptpEntry;
                    RasPhoneBook RasPhoneBook1 = new RasPhoneBook();
                    string       path          = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);
                    RasPhoneBook1.Open(path);
                    creds = new NetworkCredential(User, Pass);
                    Console.WriteLine(creds.Password);
                    bool flag = true;
                    foreach (RasEntry ras in RasPhoneBook1.Entries.ToList())
                    {
                        if (ras.Name == creds.Password)
                        {
                            dialVPN(creds.Password, RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User), creds);
                            flag = false;
                        }
                    }
                    if (flag)
                    {
                        pptpEntry = RasEntry.CreateVpnEntry(creds.Password, host, RasVpnStrategy.PptpOnly, RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn, false));

                        RasPhoneBook1.Entries.Add(pptpEntry);

                        dialVPN(creds.Password, RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User), creds);
                    }
                    mainWindow.Dispatcher.BeginInvoke
                        (new Action(() =>
                    {
                        mainWindow.RessiveOutput(0);
                    }));
                }
                catch
                (Exception ex)
                {
                    mainWindow.Dispatcher.BeginInvoke
                        (new Action(() =>
                    {
                        mainWindow.status = 0;
                        mainWindow.setStatus(0);
                    }));
                }
            };
            backgroundWorker.RunWorkerAsync();
        }
示例#22
0
 public void CreateConnection()
 {
     try
     {
         RPB.Open();
         Entry = RasEntry.CreateVpnEntry(_VpnName, _ServerIP, RasVpnStrategy.PptpOnly,
                                         RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn, false));
         RPB.Entries.Add(Entry);
     }
     catch (Exception ex)
     {
     }
 }
示例#23
0
        private void OnCreateEntry()
        {
            if (this.phonebook.Entries.Contains(ConnectionName))
            {
                this.IsCreateButtonEnabled = false;
            }
            else
            {
                RasEntry entry = RasEntry.CreateVpnEntry(ConnectionName, IPAddress.Loopback.ToString(), RasVpnStrategy.Default, RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn));

                this.phonebook.Entries.Add(entry);
            }
        }
示例#24
0
        /// <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)
        {
            // This opens the phonebook so it can be used. Different overloads here will determine where the phonebook is opened/created.
            this.AllUsersPhoneBook.Open();

            // 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.
            RasEntry entry = RasEntry.CreateVpnEntry(EntryName, IPAddress.Loopback.ToString(), RasVpnStrategy.Default,
                                                     RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn));

            // Add the new entry to the phone book.
            this.AllUsersPhoneBook.Entries.Add(entry);
        }
示例#25
0
        public void CreateVPN(string name, string destination)
        {
            RasPhoneBook PhoneBook = new RasPhoneBook();

            PhoneBook.Open();

            RasVpnStrategy strategy = RasVpnStrategy.L2tpOnly;  // Set your strategy here

            RasEntry VPNEntry = RasEntry.CreateVpnEntry(name, destination, strategy, DotRas.RasDevice.Create(name, DotRas.RasDeviceType.Vpn));

            PhoneBook.Entries.Add(VPNEntry);

            Console.WriteLine("Create VPN Success!");
        }
        //---------------------------------------------------------------------------------------
        int CreateDevice(int i)
        {
            PLog(phoneBook.Path, false);
            if (RasEntry.Exists(Globals.vpnIsim, phoneBook.Path))
            {
                PLog("Önceki Vpn Siliniyor...");
                DisconnectDevice();
                RemoveDevice();
            }

            RasEntry entry;

            if (Vpnlist[i].Protocol == "pptp")
            {
                entry = RasEntry.CreateVpnEntry(
                    Globals.vpnIsim,
                    Vpnlist[i].Ip,
                    RasVpnStrategy.PptpFirst,
#pragma warning disable CS0618 // 'RasDevice.GetDeviceByName(string, RasDeviceType)' is obsolete: 'This method will be removed in a future version, please use the GetDevices method to find the devices.'
                    RasDevice.GetDeviceByName(Vpnlist[i].Protocol, RasDeviceType.Vpn));
#pragma warning restore CS0618 // 'RasDevice.GetDeviceByName(string, RasDeviceType)' is obsolete: 'This method will be removed in a future version, please use the GetDevices method to find the devices.'
                this.phoneBook.Entries.Add(entry);
            }
            else
            {
                entry = RasEntry.CreateVpnEntry(
                    Globals.vpnIsim,
                    Vpnlist[i].Ip,
                    RasVpnStrategy.L2tpFirst,
#pragma warning disable CS0618 // 'RasDevice.GetDeviceByName(string, RasDeviceType)' is obsolete: 'This method will be removed in a future version, please use the GetDevices method to find the devices.'
                    RasDevice.GetDeviceByName(Vpnlist[i].Protocol, RasDeviceType.Vpn));
#pragma warning restore CS0618 // 'RasDevice.GetDeviceByName(string, RasDeviceType)' is obsolete: 'This method will be removed in a future version, please use the GetDevices method to find the devices.'
                entry.Options.UsePreSharedKey = true;
                this.phoneBook.Entries.Add(entry);

                foreach (RasEntry e in this.phoneBook.Entries)
                {
                    if (e.PhoneNumber == Vpnlist[i].Ip)
                    {
                        e.UpdateCredentials(RasPreSharedKey.Client, Vpnlist[i].xl2tp);
                    }
                }
            }


            PLog(this.phoneBook.Entries.Count + ". # IP:" + this.phoneBook.Entries[this.phoneBook.Entries.Count - 1].PhoneNumber);
            PLog("Vpn Created: " + Vpnlist[i].Protocol);
            return(1);
        }
示例#27
0
        /// <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)
        {
            Guid entryId = Guid.Empty;

            RasEntry entry = RasEntry.CreateVpnEntry(entryName, serverAddress, RasVpnStrategy.PptpOnly, RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn));

            if (entry != null)
            {
                phonebook.Entries.Add(entry);

                entryId = entry.Id;
            }

            return(entryId);
        }
        // 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 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();
        }
示例#30
0
        /// <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);
        }