Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C# 5.0
Tip/Trick

VPN Kill Switch

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
30 Oct 2014CPOL1 min read 24.1K   1.1K   14   4
A very simple program to kill all network interfaces if your VPN goes down.

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).

C#
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");  //This should 
                        //enable the interfaces again but for some reason doesnt work.
            }
            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()); //This gets what your current IP Address is
                foreach (IPAddress ip in host.AddressList)
                {
                    if (ip.AddressFamily.ToString() == "InterNetwork")
                    {
                        localIP = ip.ToString();
                        if (ip.ToString() == textBox1.Text)  //Returns true 
                            //if the VPN Address typed matches the IP from the system
                        {
                            vpnON = true;
                            timer1.Start(); //Restart the timer since the check was good
                        }
                    }
                }

                if (vpnON == false)
                {
                    //Kill all internet adapters

                    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) //This is the function 
            //to continuously check for VPN Connectivity, default check is every 5 seconds
        {
            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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionLittle Enable/Disable Fix for that old Projekt Pin
KeRn199530-Jul-18 10:22
KeRn199530-Jul-18 10:22 
QuestionCan not enable network adapter Pin
vutrulavotann12-May-16 4:29
vutrulavotann12-May-16 4:29 
Questiondoesn't work Pin
Member 147115623-Dec-15 5:01
Member 147115623-Dec-15 5:01 
SuggestionNot having to poll for updates Pin
gnagel10-Nov-14 0:39
gnagel10-Nov-14 0:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.