DoRas一点使用心得
为了对抗伟大的墙,又续了一年半的vpn。可是最近遇到了点小麻烦,由于这家小型的vpn服务提供商无法在windows10兼容使用预共享秘钥作为身份验证的类l2pt连接方式。在windows下的设置方式如下:
后来只好自己研究做一个简单的vpn连接器。
主要使用的是DotRas的连接库。官网连接https://dotras.codeplex.com/
这里要说官网下载的1.3版SDK,好像功能有点单一无法满足需求。所以推荐大家使用下载的1.2版的SDK,http://dotras.codeplex.com/releases/view/32289
用C#做了一个调用DotRas.dll的类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | 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. } } } } |
主要的核心问题是
1 2 3 4 | this .AllUsersPhoneBook.Entries[EntryName].UpdateCredentials(RasPreSharedKey.Client, "prekey" ); //设置预共享秘钥 this .AllUsersPhoneBook.Entries[EntryName].Update(); //更新连接信息 |
UpdateCredentials方法在官网下载要V1.2版的dll才有,如果是1.3我估计需要自己编译才能有这个方法出来。
您好,请问一下您的测速功能是怎么实现的呀?也想自己做个VPN客户端来试试~~谢谢啦。。。
测是功能就是ping功能啊,百度很多源码的
测是功能就是ping功能啊,百度很多源码的
给我邮箱来个源码不? 研究好几个版本,貌似都不行啊