Introduction
VPN is great for security and general anonymity, but if your VPN goes down during use, you generally may not even notice and continue browsing / downloading, etc. thinking it is still up and active. With this program, if your VPN drops, it will immediately (within 5 seconds) kill all of your network interfaces and warn you of the problem, keeping you safe and secure.
Using the Code
The code is easy to use / understand and commented. If I make any changes, I will update this article and the code.
On the Windows Form itself, you will need to enter the IP Address your VPN has assigned you, the code will check that IP Address against your active IPs to ensure your VPN is connected. If that IP Address is not found (or if you type it incorrectly!) it will disable your Network adapters. Working up to Windows 8.1 (not tested on WinXP or below).
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace InternetKillSwitch
{
public partial class Form1 : Form
{
IPHostEntry host;
string localIP = "?";
bool vpnON = false;
int timerCount = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
button2.Enabled = true;
button1.Enabled = false;
textBox1.Enabled = false;
timer1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
button2.Enabled = false;
button1.Enabled = true;
textBox1.Enabled = true;
}
private void button3_Click(object sender, EventArgs e)
{
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
string value = nic.Name;
Process.Start("cmd.exe", "/c netsh interface set interface
\"" + value + "\" ENABLED"); }
MessageBox.Show("All network adapters have been re-enabled. InternetKillSwitch is OFF.");
}
private void checkVPN()
{
vpnON = false;
if (textBox1.Text != "")
{
IPHostEntry host;
string localIP = "?";
host = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily.ToString() == "InterNetwork")
{
localIP = ip.ToString();
if (ip.ToString() == textBox1.Text) {
vpnON = true;
timer1.Start(); }
}
}
if (vpnON == false)
{
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
string value = nic.Name;
Process.Start("cmd.exe", "/c netsh interface
set interface \"" + value + "\" DISABLED");
}
MessageBox.Show("WARNING: VPN Address was NOT found.
All Network Adapters have been disabled!");
timer1.Stop();
button2.Enabled = false;
button1.Enabled = true;
textBox1.Enabled = true;
}
}
}
private void timer1_Tick(object sender, EventArgs e) {
timerCount = timerCount + 1;
if (timerCount > 5)
{
timer1.Stop();
checkVPN();
timerCount = 0;
}
}
}
}
Future Thoughts
This should definitely be made to run as a tray application, but rather than add all of that into code, I wanted it to be very clear and concise and only do exactly what it was intended to do so that anyone who wanted could see the code.
A couple of the functions used can be rewritten to make the entirety of the code a lot cleaner.
History
- 10/29/2014 - Added initial source code and .exe for this project