Example #1
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);
            }
        }
Example #2
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!");
        }
Example #3
0
        private void Master(string _server)
        {
            // Connection Parameters
            string         ServerAddress           = _server;
            RasVpnStrategy strategy                = RasVpnStrategy.SstpOnly;
            RasDevice      device                  = RasDevice.Create("SSTP", RasDeviceType.Vpn);
            bool           useRemoteDefaultGateway = true;

            // Create entry
            RasEntry entry = RasEntry.CreateVpnEntry(ConnectionName, ServerAddress, strategy, device, false);

            entry.DnsAddress = IPAddress.Parse("8.8.8.8");
            //entry.IPAddress = IPAddress.Parse("219.100.37.219");


            entry.EncryptionType = RasEncryptionType.Require;
            entry.EntryType      = RasEntryType.Vpn;
            entry.Options.RequireDataEncryption = false;
            entry.Options.UseLogOnCredentials   = false;
            entry.Options.RequireMSChap2        = false;
            entry.Options.RemoteDefaultGateway  = useRemoteDefaultGateway;
            entry.Options.SecureFileAndPrint    = true;
            entry.Options.SecureClientForMSNet  = true;
            entry.Options.ReconnectIfDropped    = false;
            //entry.Options.RegisterIPWithDns = true;


            // Get phone book (list of connetions) path
            string path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);

            // Load
            RasPhoneBook rpb = new RasPhoneBook();

            rpb.Open(path);

            // Check for existance of the same connection
            if (!rpb.Entries.Contains(entry.Name))
            {
                rpb.Entries.Remove(entry.Name);
            }
            rpb.Entries.Add(entry);
            // Set user and password
            entry.ClearCredentials();
        }
Example #4
0
        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);
            }
        }
Example #5
0
        /// <summary>
        /// Creates a new virtual private network (VPN) entry.
        /// </summary>
        /// <param name="name">The name of the entry.</param>
        /// <param name="serverAddress">The server address to connect to.</param>
        /// <param name="strategy">The virtual private network (VPN) strategy of the connection.</param>
        /// <param name="device">Required. An <see cref="DotRas.RasDevice"/> to use for connecting.</param>
        /// <param name="useRemoteDefaultGateway"><b>true</b> if the connection should use the remote default gateway, otherwise <b>false</b>.</param>
        /// <returns>A new <see cref="DotRas.RasEntry"/> object.</returns>
        /// <remarks>The device for this connection is typically a WAN Miniport (L2TP) or WAN Miniport (PPTP).</remarks>
        /// <exception cref="System.ArgumentException"><paramref name="name"/> or <paramref name="serverAddress"/> is an empty string or null reference (<b>Nothing</b> in Visual Basic).</exception>
        /// <exception cref="System.ArgumentNullException"><paramref name="device"/> is a null reference (<b>Nothing</b> in Visual Basic).</exception>
        public static RasEntry CreateVpnEntry(string name, string serverAddress, RasVpnStrategy strategy, RasDevice device, bool useRemoteDefaultGateway)
        {
            if (string.IsNullOrEmpty(name))
            {
                ThrowHelper.ThrowArgumentException("name", Resources.Argument_StringCannotBeNullOrEmpty);
            }

            if (string.IsNullOrEmpty(serverAddress))
            {
                ThrowHelper.ThrowArgumentException("serverAddress", Resources.Argument_StringCannotBeNullOrEmpty);
            }

            if (device == null)
            {
                ThrowHelper.ThrowArgumentNullException("device");
            }

            RasEntry entry = new RasEntry(name);

            entry.Device = device;
            entry.EncryptionType = RasEncryptionType.Require;
            entry.EntryType = RasEntryType.Vpn;
            entry.FramingProtocol = RasFramingProtocol.Ppp;
            entry.NetworkProtocols.IP = true;
            entry.Options.ModemLights = true;

#if (WIN7 || WIN8)
            if (strategy == RasVpnStrategy.IkeV2First || strategy == RasVpnStrategy.IkeV2Only)
            {
                entry.Options.RequireDataEncryption = true;
                entry.Options.RequireEap = true;
                entry.Options.RequireEncryptedPassword = false;
            }
            else
            {
                entry.Options.RequireEncryptedPassword = true;
            }
#else
            entry.Options.RequireEncryptedPassword = true;
#endif

            entry.Options.PreviewUserPassword = true;
            entry.Options.PreviewDomain = true;
            entry.Options.ShowDialingProgress = true;

            if (useRemoteDefaultGateway)
            {
                entry.Options.RemoteDefaultGateway = true;

#if (WIN2K8 || WIN7 || WIN8)
                entry.Options.IPv6RemoteDefaultGateway = true;
#endif
            }

#if (WINXP || WIN2K8 || WIN7 || WIN8)
            entry.RedialCount = 3;
            entry.RedialPause = 60;

            entry.Options.DoNotNegotiateMultilink = true;
            entry.Options.ReconnectIfDropped = true;
#endif
#if (WIN2K8 || WIN7 || WIN8)
            entry.Options.UseTypicalSettings = true;
            entry.NetworkProtocols.IPv6 = true;
#endif
            entry.PhoneNumber = serverAddress;
            entry.VpnStrategy = strategy;

            return entry;
        }
Example #6
0
 /// <summary>
 /// Creates a new virtual private network (VPN) entry.
 /// </summary>
 /// <param name="name">The name of the entry.</param>
 /// <param name="serverAddress">The server address to connect to.</param>
 /// <param name="strategy">The virtual private network (VPN) strategy of the connection.</param>
 /// <param name="device">Required. An <see cref="DotRas.RasDevice"/> to use for connecting.</param>
 /// <returns>A new <see cref="DotRas.RasEntry"/> object.</returns>
 /// <remarks>The device for this connection is typically a WAN Miniport (L2TP) or WAN Miniport (PPTP).</remarks>
 /// <exception cref="System.ArgumentException"><paramref name="name"/> or <paramref name="serverAddress"/> is an empty string or null reference (<b>Nothing</b> in Visual Basic).</exception>
 /// <exception cref="System.ArgumentNullException"><paramref name="device"/> is a null reference (<b>Nothing</b> in Visual Basic).</exception>
 public static RasEntry CreateVpnEntry(string name, string serverAddress, RasVpnStrategy strategy, RasDevice device)
 {
     return CreateVpnEntry(name, serverAddress, strategy, device, true);
 }
Example #7
0
        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);
            }
        }