实例介绍
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Ro4ters;
using System.Net;
using System.IO;
using System.Threading;
using DotRas;
using VpnSec.Ro4ters;
namespace VpnSec
{
public partial class Form1 : Form
{
/// <summary>
/// 更新状态栏委托
/// </summary>
/// <param name="tsl">某个标签</param>
/// <param name="Msg">更新的信息</param>
public delegate void UpdateTsl(ToolStripStatusLabel tsl, string Msg);
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
/// <summary>
/// 更新ToolStripStatusLabel委托变量
/// </summary>
UpdateTsl uptls;
VpnClient vclient = new VpnClient();
List<MyUser> ulist = new List<MyUser>();
List<vpnServer> vpnlist = new List<vpnServer>();
public string vpnoneclickurl = "http://www.vpnoneclick.com";
public string kryptotelurl = "http://ssl.kryptotel.net";
public string kryptotelurlbackup = "http://ssl7.kryptotel.net";
/// <summary>
/// 更新状态栏方法
/// </summary>
/// <param name="tsl">某个标签</param>
/// <param name="Msg">更新的信息</param>
private void updateTsl(ToolStripStatusLabel tsl, string Msg)
{
tsl.Text = Msg;
tsl.ForeColor = Color.Red;
}
private void Form1_Load(object sender, EventArgs e)
{
vclient.ClientException = client_ClientException;
vclient.ClientMassage = client_ClientMassage;
vclient.ConnectionStatusChanged = client_ConnectionStatusChanged;
vclient.ClientDialCompleted = new VpnClient.ClientDialCompletedHandler(client_ClientDialCompleted);
initData();
uptls = new UpdateTsl(updateTsl);
GetIp();
StartTime();
}
/// <summary>
/// 获取当前IP
/// </summary>
private void GetIp()
{
ThreadPool.QueueUserWorkItem(o =>
{
if (vclient.updateIpaddress())
{
uptls(this.tlsMyIpaddress, vclient.publicIpAddress);
}
});
}
void AppendText(string message)
{
if (lvi != null)
{
lvi.SubItems[8].Text = message;
}
}
void client_ClientDialCompleted(object sender, DialCompletedEventArgs e)
{
if (e.Cancelled)
{
SetNi("操作已取消");
AppendText("操作已取消");
}
else if (e.TimedOut)
{
SetNi("操作超时");
StopTime();
AppendText("操作超时");
}
else if (e.Error != null)
{
AppendText("操作时遇到错误:" e.Error.Message);
SetNi("操作时遇到错误:" e.Error.Message);
}
else if (e.Connected)
{
SetNi("[" DateTime.Now.ToShortTimeString() "] " "VPN已连接");
StartTime();
AppendText("VPN已连接");
GetIp();
}
if (!e.Connected)
{
SetNi("[" DateTime.Now.ToShortTimeString() "] " "VPN已断开");
AppendText("VPN已断开");
StopTime();
}
}
private void client_ConnectionStatusChanged(DotRas.RasConnectionState connectionState)
{
SetNi(connectionState.ToString());
AppendText(connectionState.ToString());
}
private void client_ClientException(Exception ex)
{
SetNi(ex.ToString());
AppendText(ex.ToString());
}
private void client_ClientMassage(string message)
{
SetNi(message);
AppendText(message);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//直接释放托管资源
this.notifyIcon1.Visible = false;
base.Dispose(true);
}
private void 关于软件ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("\r I Hope Free.!\n" "\r By : Ro4ters \n\r Mail:Ro4ters@foxmail.com", "And Your ?", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
}
private void 退出程序ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.notifyIcon1.Visible = false;
base.Dispose(true);
}
//最小化事件
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
this.notifyIcon1.Visible = true;
SetNi("Hi~ 我在这里...");
}
}
/// <summary>
/// 设置气泡
/// </summary>
/// <param name="msg"></param>
private void SetNi(string msg)
{
notifyIcon1.ShowBalloonTip(1000, "Info", msg, ToolTipIcon.Info);
}
//单击显示软件事件
private void 显示软件ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Visible = true;
this.notifyIcon1.Visible = false;
this.WindowState = FormWindowState.Normal;
}
//双单击任务栏图标事件
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
}
/// <summary>
/// 初始化数据
/// </summary>
private void initData()
{
vpnlist.Add(new vpnServer("澳大利亚", "VPN-Australian", "ip-au.vpnoneclick.com", vpnServer.VpnServerType.OneClick));
vpnlist.Add(new vpnServer("加拿大", "VPN-CA", "ip-ca.vpnoneclick.com", vpnServer.VpnServerType.OneClick));
vpnlist.Add(new vpnServer("埃及", "VPN-EG", "ip-eg.vpnoneclick.com", vpnServer.VpnServerType.OneClick));
vpnlist.Add(new vpnServer("法国", "VPN-FR", "ip-fr.vpnoneclick.com", vpnServer.VpnServerType.OneClick));
vpnlist.Add(new vpnServer("德国", "VPN-DE", "ip-de.vpnoneclick.com", vpnServer.VpnServerType.OneClick));
vpnlist.Add(new vpnServer("香港", "VPN-HK", "ip-hk.vpnoneclick.com", vpnServer.VpnServerType.OneClick));
vpnlist.Add(new vpnServer("印度", "VPN-IN", "ip-in.vpnoneclick.com", vpnServer.VpnServerType.OneClick));
vpnlist.Add(new vpnServer("意大利", "VPN-IT", "ip-it.vpnoneclick.com", vpnServer.VpnServerType.OneClick));
vpnlist.Add(new vpnServer("荷兰", "VPN-NL", "ip-nl.vpnoneclick.com", vpnServer.VpnServerType.OneClick));
vpnlist.Add(new vpnServer("俄罗斯", "VPN-RU", "ip-ru.vpnoneclick.com", vpnServer.VpnServerType.OneClick));
vpnlist.Add(new vpnServer("新加坡", "VPN-SG", "ip-sg.vpnoneclick.com", vpnServer.VpnServerType.OneClick));
vpnlist.Add(new vpnServer("西班牙", "VPN-SP", "ip-sp.vpnoneclick.com", vpnServer.VpnServerType.OneClick));
vpnlist.Add(new vpnServer("瑞典", "VPN-SE", "ip-se.vpnoneclick.com", vpnServer.VpnServerType.OneClick));
vpnlist.Add(new vpnServer("瑞士", "VPN-CH", "ip-ch.vpnoneclick.com", vpnServer.VpnServerType.OneClick));
vpnlist.Add(new vpnServer("土耳其", "VPN-TK", "ip-tk.vpnoneclick.com", vpnServer.VpnServerType.OneClick));
vpnlist.Add(new vpnServer("英国", "VPN-UK", "ip-uk.vpnoneclick.com", vpnServer.VpnServerType.OneClick));
vpnlist.Add(new vpnServer("美国", "VPN-US", "ip-us.vpnoneclick.com", vpnServer.VpnServerType.OneClick));
vpnlist.Add(new vpnServer("日本1", "VPN-Jp1", "106.186.30.51", vpnServer.VpnServerType.Japan));
vpnlist.Add(new vpnServer("日本2", "VPN-Jp2", "106.187.97.112", vpnServer.VpnServerType.Japan));
for (int i = 0; i < vpnlist.Count; i )
{
ListViewItem lvi = new ListViewItem();
lvi.Text = i.ToString();
vpnServer vs = vpnlist[i];
lvi.SubItems.Add(vs.VpnCreateName "[ " vs.VpnCountryName " ]");
lvi.SubItems.Add("未获取");
lvi.SubItems.Add("未获取");
lvi.SubItems.Add("NA");
lvi.SubItems.Add("未接入");
lvi.SubItems.Add("0.0 byte");
lvi.SubItems.Add("0.0 byte");
lvi.SubItems.Add("未连接");
VpnLineList.Items.Add(lvi);
}
}
private void 申请账号ToolStripMenuItem_Click(object sender, EventArgs e)
{
//MessageBox.Show("鉴于OneClick账户观看视频比较卡所以暂时关闭此接口如要使用请使用1.0版本.");
//return;
uptls(this.tlsStatus, 申请账号ToolStripMenuItem.Text "中...");
ThreadPool.QueueUserWorkItem(o =>
{
//VpnClient.IsJapan = true;
MyUser user = vclient.Initialize();
if (!string.IsNullOrEmpty(user.UserName))
{
uptls(this.tslUser, user.UserName);
MessageBox.Show("账号" "[ " user.UserName " ]" "申请成功.~", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
});
}
MyUser mu = new MyUser();
private void 申请Japan账号ToolStripMenuItem_Click(object sender, EventArgs e)
{
uptls(this.tlsStatus, 申请Japan账号ToolStripMenuItem.Text "中...");
ThreadPool.QueueUserWorkItem(o =>
{
string name = StringUtility.GetRandomUserName();
HttpResult hr = GetJapanUser(name);
if (hr.Html.Contains("您的账户已经创建"))
{
uptls(this.tslUser, name);
MessageBox.Show("账号" "[ " name " ]" "申请成功.~", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
//ulist.Add(new MyUser(name,"ro4ters", vpnServer.VpnServerType.Japan));
mu.UserName = name;
mu.UserType = vpnServer.VpnServerType.Japan;
}
else
{
申请Japan账号ToolStripMenuItem_Click(sender, e); //申请失败一直申请.
}
});
}
/// <summary>
/// 获取日本线路账号
/// </summary>
/// <returns></returns>
private HttpResult GetJapanUser(string name)
{
string PostUrl = "http://user.tover.net/reg.php?cont=store_user";
string PostData = "lang=Chinese&username=" name.Trim() "&password1=ro4ters&password2=ro4ters&mobile=&email=&srvid=1&textarea=%E6%8F%90%E7%A4%BA%EF%BC%9A%E8%AF%B7%E4%BB%94%E7%BB%86%E9%98%85%E8%AF%BB%E4%BB%A5%E4%B8%8B%E6%9D%A1%E6%AC%BE%EF%BC%8C%E5%86%8D%E6%B3%A8%E5%86%8C%E5%B8%90%E5%8F%B7%E3%80%82%E4%BD%BF%E7%94%A8%E6%9C%AC%E7%BD%91%E7%AB%99%E8%A1%A8%E7%A4%BA%E6%82%A8%E5%90%8C%E6%84%8F%E9%81%B5%E5%AE%88%E8%BF%99%E4%BA%9B%E6%9D%A1%E6%AC%BE%E5%92%8C%E6%9D%A1%E4%BB%B6%E3%80%82%E5%A6%82%E6%9E%9C%E6%82%A8%E4%B8%8D%E6%8E%A5%E5%8F%97%E8%BF%99%E4%BA%9B%E6%9D%A1%E6%AC%BE%EF%BC%88%E2%80%9C%E6%9D%A1%E6%AC%BE%E2%80%9D%EF%BC%89%EF%BC%8C%E8%AF%B7%E5%8B%BF%E6%B3%A8%E5%86%8C%E3%80%82&acceptterms=1&adduser=%E5%88%9B%E5%BB%BA%E8%B4%A6%E6%88%B7";
string Referer = "http://user.tover.net/reg.php";
return StringUtility.PostHtml(PostUrl, Referer, PostData, false);
}
ListViewItem lvi;
private void 连接选中L2TPToolStripMenuItem_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(o =>
{
VpnClient.ConnType = 0;
StartVpn();
GetIp();
uptls(this.tlsStatus, "Succeed");
});
}
/// <summary>
/// 开启vpn
/// </summary>
/// <param name="VpnName">显示名</param>
/// <param name="VpnIp">连接地址</param>
private void StartVpn()
{
try
{
lvi = VpnLineList.SelectedItems[0];
string name = lvi.SubItems[1].Text.Split('[')[0];
if (name.Contains("Jp"))
{
VpnClient.IsJapan = true;
}
else
{
VpnClient.IsJapan = false;
}
lvi.SubItems[2].Text = mu.UserName;
lvi.SubItems[3].Text = "ro4ters";
lvi.SubItems[5].Text = DateTime.Now.ToShortTimeString();
string nType = string.Empty;
nType = VpnClient.ConnType == 0 ? "L2TP" : "PPTP";
SetNi(nType " 连接:" name);
vpnServer vs = vpnlist.Find(delegate(vpnServer v) { return v.VpnCreateName == name; });
vclient.Init();
if (string.IsNullOrEmpty(mu.UserName))
{
MessageBox.Show("请先申请账户.!");
return;
}
vclient.userName = mu.UserName;
vclient.passWord = mu.PassWord;
AppendText("连接" lvi.SubItems[1].Text "中...");
uptls(this.tlsStatus, "连接" lvi.SubItems[1].Text "中...");
vclient.StartVpn(lvi, "Ro4ters" vs.VpnCreateName, vs.VpnIpAddress);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void 连接选中PPTPToolStripMenuItem_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(o =>
{
VpnClient.ConnType = 1;
StartVpn();
});
}
/// <summary>
/// 关闭时钟
/// </summary>
private void StopTime()
{
if (timer1.Enabled == true)
{
timer1.Enabled = false;
timer1.Stop();
}
}
/// <summary>
/// 启动时钟
/// </summary>
private void StartTime()
{
if (timer1.Enabled == false)
{
timer1.Tick = new EventHandler(timer1_Tick);
timer1.Interval = 1000;
timer1.Enabled = true;//启动流量时钟
timer1.Start();
}
}
/// <summary>
/// 断开
/// </summary>
private void DisVpn()
{
if (lvi != null)
{
lvi = VpnLineList.SelectedItems[0];
string name = lvi.SubItems[1].Text.Split('[')[0];
if (string.IsNullOrEmpty(mu.UserName))
{
MessageBox.Show("暂无连接.!");
return;
}
SetNi("断开中,请稍后..");
vclient.DisconnectVpn(name);
}
else
{
MessageBox.Show("暂无连接..");
return;
}
}
private void 断开选中ToolStripMenuItem_Click(object sender, EventArgs e)
{
DisVpn();
}
private void 测试选中速度ToolStripMenuItem_Click(object sender, EventArgs e)
{
SetNi("开始测试速度...");
Ping();
}
/// <summary>
/// 测试所有
/// </summary>
/// <param name="lvitem"></param>
private void Ping(ListView lv)
{
Pings p = new Pings();
for (int i = 0; i < lv.Items.Count; i )
{
SetNi(string.Format("测试第{0}个..", i.ToString()));
ListViewItem litem = lv.Items[i];
ThreadPool.QueueUserWorkItem(o =>
{
string name = litem.SubItems[1].Text.Split('[')[0];
vpnServer vs = vpnlist.Find(delegate(vpnServer v) { return v.VpnCreateName == name; });
string Speed = string.Empty;
if (!name.Contains("Jp"))
{
try
{
Speed = p.PingHost(vclient.getipaddressbyname(vs.VpnIpAddress));
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
Speed = p.PingHost(vs.VpnIpAddress);
}
litem.SubItems[4].Text = Speed "ms";
});
}
}
private void Ping()
{
ThreadPool.QueueUserWorkItem(o =>
{
if (lvi != null)
{
lvi = VpnLineList.SelectedItems[0];
Pings p = new Pings();
string name = lvi.SubItems[1].Text.Split('[')[0];
vpnServer vs = vpnlist.Find(delegate(vpnServer v) { return v.VpnCreateName == name; });
string Speed = string.Empty;
if (!name.Contains("Jp"))
{
try
{
Speed = p.PingHost(vclient.getipaddressbyname(vs.VpnIpAddress));
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
Speed = p.PingHost(vs.VpnIpAddress);
}
lvi.SubItems[4].Text = Speed "ms";
}
});
}
private void timer1_Tick(object sender, EventArgs e)
{
RASDisplay display = new RASDisplay();
if (lvi != null)
{
string str = display.ConnectionName;
lvi.SubItems[8].Text = display.Duration;
lvi.SubItems[7].Text = display.BytesReceived.ToString();//接收
lvi.SubItems[6].Text = display.BytesTransmitted.ToString();//发送
}
}
private void VpnLineList_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
}
private void VpnLineList_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void 查看选中信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
ListViewItem item = VpnLineList.SelectedItems[0];
if (item != null)
{
string name = item.SubItems[1].Text.Split('[')[0];
vpnServer vs = vpnlist.Find(delegate(vpnServer v) { return v.VpnCreateName == name; });
if (vs.VpnType == vpnServer.VpnServerType.OneClick)
{
uptls(this.tslSelectedIp, vclient.getipaddressbyname(vs.VpnIpAddress));
}
else
{
uptls(this.tslSelectedIp, vs.VpnIpAddress);
}
}
}
private void 测试所有速度ToolStripMenuItem_Click(object sender, EventArgs e)
{
SetNi("开始测试速度...");
Ping(VpnLineList);
}
private void tslUser_MouseHover(object sender, EventArgs e)
{
Clipboard.SetDataObject(tslUser.Text);
MessageBox.Show("已复制.");
}
}
}
标签: VPN
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论