DoRas一点使用心得

为了对抗伟大的墙,又续了一年半的vpn。可是最近遇到了点小麻烦,由于这家小型的vpn服务提供商无法在windows10兼容使用预共享秘钥作为身份验证的类l2pt连接方式。在windows下的设置方式如下:

微信截图_20150928214304.png

后来只好自己研究做一个简单的vpn连接器。

微信截图_20150928211151.png

主要使用的是DotRas的连接库。官网连接https://dotras.codeplex.com/

这里要说官网下载的1.3版SDK,好像功能有点单一无法满足需求。所以推荐大家使用下载的1.2版的SDK,http://dotras.codeplex.com/releases/view/32289

用C#做了一个调用DotRas.dll的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using DotRas;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace MyVPN
{
    class RasapiHelper
    {
        System.Windows.Forms.ToolStripLabel stateLable;
        public const string EntryName = "VPN_Connection";
        private RasHandle handle = null;
        private DotRas.RasPhoneBook AllUsersPhoneBook;
        private DotRas.RasDialer Dialer;
        public RasapiHelper(System.ComponentModel.IContainer component)
        {
            this.AllUsersPhoneBook = new DotRas.RasPhoneBook(component);
            this.Dialer = new DotRas.RasDialer(component);
            this.Dialer.StateChanged += new System.EventHandler<DotRas.StateChangedEventArgs>(this.Dialer_StateChanged);
            this.Dialer.DialCompleted += new System.EventHandler<DotRas.DialCompletedEventArgs>(this.Dialer_DialCompleted);
        }
        public void CreateOrUpdataEntry()
        {
            this.AllUsersPhoneBook.Open(true);
            if (!this.AllUsersPhoneBook.Entries.Contains(EntryName))
            {
                RasEntry entry = RasEntry.CreateVpnEntry(EntryName, "127.0.0.1", RasVpnStrategy.L2tpOnly, RasDevice.GetDeviceByName("(L2TP)", RasDeviceType.Vpn));
                entry.Options.UsePreSharedKey = true;
                this.AllUsersPhoneBook.Entries.Add(entry);
            }
            this.AllUsersPhoneBook.Entries[EntryName].UpdateCredentials(RasPreSharedKey.Client, "prekey");
            this.AllUsersPhoneBook.Entries[EntryName].Update();
        }
        public void UpdataIPAddress(string IP)
        {
            this.AllUsersPhoneBook.Entries[EntryName].PhoneNumber = IP;
            this.AllUsersPhoneBook.Entries[EntryName].Update();

            }
        public void Connect()
        {
            this.Dialer.EntryName = EntryName;
            this.Dialer.PhoneBookPath = this.AllUsersPhoneBook.Path;
            try
            {
                this.Dialer.Credentials = new NetworkCredential("username", "password");
                this.handle = this.Dialer.DialAsync();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
            }
        }
        public void Disconnect()
        {
            if (this.Dialer.IsBusy)
            {
                this.Dialer.DialAsyncCancel();
            }
            else
            {
                RasConnection connection = RasConnection.GetActiveConnectionByHandle(this.handle);
                if (connection != null)
                {
                    connection.HangUp();
                }
            }
            this.stateLable.Text = ("Disconnection!");
        }
        private void Dialer_StateChanged(object sender, StateChangedEventArgs e)
        {
            this.stateLable.Text = e.State.ToString();
            //this.StatusTextBox.AppendText(e.State.ToString() + "\r\n");
        }
        public void toolStripShowState(System.Windows.Forms.ToolStripLabel toolSpLabe)
        {
            this.stateLable = toolSpLabe;

        }
        private void Dialer_DialCompleted(object sender, DialCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                this.stateLable.Text = "Cancelled!";
            }
            else if (e.TimedOut)
            {
                this.stateLable.Text = ("Connection attempt timed out!");
            }
            else if (e.Error != null)
            {
                this.stateLable.Text = (e.Error.ToString());
            }
            else if (e.Connected)
            {
                this.stateLable.Text = ("Connection successful!");
            }

            if (!e.Connected)
            {
                this.stateLable.Text = ("Disconnection!");
                // The connection was not connected, disable the disconnect button.

            }
        }
    }
}

主要的核心问题是

this.AllUsersPhoneBook.Entries[EntryName].UpdateCredentials(RasPreSharedKey.Client, "prekey"); 
//设置预共享秘钥
this.AllUsersPhoneBook.Entries[EntryName].Update();
//更新连接信息

UpdateCredentials方法在官网下载要V1.2版的dll才有,如果是1.3我估计需要自己编译才能有这个方法出来。

标签: C#  

有4条评论

添加新评论