[CODE] Detecting if a VPN connection is active programmatically (Jan/2017)

This method of programatic detection works as of January 24, 2017 with the latest versions of Express VPN and PIA (Private Internet Access)

On Mac OSX/
This works for Mac OSX 10.2.2.

The trick is to request your ip routing table and examine through which network interface your default traffic is going through.
(To do this programmatically you will have to parse the output with your favorite programming language)

This is how it looks for both ExpressVPN and PIA when the VPN is active:

To request your routing table you can do this on the command line:
netstat -nr

Notice the line starting with “0/1”, it’s going through that tunnel interface. (In Linux it would show 0.0.0.0 instead of 0/1)

VPN ON output in Mac

Internet:
Destination        Gateway            Flags        Refs      Use   Netif Expire
0/1                10.81.10.5         UGSc            5        0   utun1
...

VPN ON output in Linux

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         10.31.10.5      128.0.0.0       UG        0 0          0 tun0
...

When you turn VPN off this is how it looks:

VPN OFF in Mac

Destination        Gateway            Flags        Refs      Use   Netif Expire
default            192.168.1.1        UGSc           66        0     en0
127                127.0.0.1          UCS             2        4     lo0

VPN OFF in Linux

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         172.16.245.2    0.0.0.0         UG        0 0          0 eth0

So a quick way to determine if the VPN is on or off in Mac or Linux, is to filter-out what you care for using grep.
If you have any output it’s on, if not it’s off

netstat -nr | egrep "^0" | grep "tun"

(we filter for “tun” and not utun1, as in linux vpn network interfaces start with “tun”)

Parse the output of that command and you will have your VPN status. No output means VPN is disconnected. Some output means the VPN is connected.

On Windows
Do a nestat -nr and look for 128.0.0.1, if you find it, VPN is on.

How to actually build bitcoin on Mac OSX 10.9.1

First of all, if you have Macports, do yourself a favor and get rid of it.

Then make sure you have Homebrew installed and all the packages installed by it up to date.

1. Let’s install all the dependencies for Bitcoin hacking.

brew install autoconf automake berkeley-db4 boost miniupnpc openssl pkg-config protobuf qt libtool

2. Make sure you have the right OpenSSL version installed. Type the following on your terminal:

openssl version

you should see “OpenSSL 1.0.1f 6 Jan 2014.”

if you see an older version, do

brew update
brew upgrade

OpenSSL should be upgraded, you may or may not have to issue a “brew link openssl” or even a “brew link --overwrite openssl” if it’s giving you trouble.

3. Now, let’s configure, and make. I strongly suggest you add the boost library path when configuring, otherwise you may get nasty “Undefined symbols for architecture x86_64” compilation errors. During the time I wrote this, homebrew had installed boost 1.55 in my system, and the boost lib path was /usr/local/Cellar/boost/1.55.0/lib so I invoked the following:

./configure --with-boost-libdir=/usr/local/Cellar/boost/1.55.0/lib

After that I just issued a

make

And I was done.

If you want to hack the bitcoin-qt client like me, head to src/qt/, there should be a bitcoin-qt executable there now.

Enjoy

How to reset Mac USB

Your Mac won’t recognize your USB device until you reset, what a pain in the ass right?

Open Activity Monitor

Search for “usbmuxd”

Terminate Process (You will need an Adminstrator password)

Re-Insert your USB device, it should be recognized now.

Worked everytime for Bose USB Audio.

How to resize a VirtualBox fixed size virtual drive (.vdi) on Mac

So you created a disk for your Windows or Linux VirtualBox VM and you made the mistake of not creating the drive as a dynamically expanding storage drive, you chose fixed size.

Now you’re running out of space and all your Google searches point you to stupid posts on the virtualbox.org forums that lead to nowhere, specially if you are a MacOSX user, look no further, you’ve found the solution to your problem on this post.

VirtualBox for Mac comes with a tool called VBoxManage, this tool is all you need to resize your virtual disk.

When you’re using the VBoxManage tool make sure your VM is not running, stop it completely to be safe.

Step 0. Backup your current drive (optional)

I recommend that before you do anything you clone your existing drive, just in case…

You can clone your drive using guess what? yes, the VBoxManage tool. No need to download any external tools.

[bash]$ VboxManage clonehd <path to your original drive> <path of the copy>[/bash]

in my computer it looked exactly like this:

[bash]$ VBoxManage clonehd /Users/gubatron/VirtualBox VMs/windows7-64bit/windows7-64bit.vdi /Users/gubatron/windows7-64bit.cloned.vdi
0%…10%…20%…30%…40%…50%…60%…70%…80%…90%…100%[/bash]

If you don’t know where your .vdi file is, it’s very simple, right Click your VM > Settings > Storage, and mouse over the .vdi to see it’s physical location on your Mac hardrive.


Mouse over your virtual drive if you don’t know its location

Step 1. Resize!

The command to resize is:
[bash]$ VBoxManage modifyhd <path to your vdi> –resize <new size in megabytes>[/bash]

In my case I had a 20GB drive that I wanted to double in size (40GB), a quick Google search for “40GB to megabytes” yields the number 40960, so that’s what I put on my –resize parameter.

This is how it looked for me:
[bash]$ VBoxManage modifyhd /Users/gubatron/VirtualBox VMs/windows7-64bit/windows7-64bit.vdi –resize 40960
0%…10%…20%…30%…40%…50%…60%…70%…80%…90%…100%[/bash]

The ‘resizing’ is almost immediate.

Step 2. (Re)Start VirtualBox

After you’ve physically resized your drive, both VirtualBox and your OS should know about the changes.

At this point, clicking on your VM in the VirtualBox window will show that it still has the old size:

Before: (Note the size of the drive at the bottom of the image)

After doing the resize, shutdown completely your VirtualBox, and launch it again. When you see the information about the storage drives attached to your VM you should see the new disk size.

After: (Note the size of the drive at the bottom of the image)

Step 3. Let your OS know the new logical size of the drive


Your OS is still not aware of the changes, you need to let it know about them

When you start your VM, it won’t know right away what the new size of the disk is, you’ll have let the operating system know that there is additional space it can use.

If your VM is running Windows 7, the way to let the operating system know that it can use the additional space on your drive is very straight forward.

Click on the Windows (Start) Icon on the Bar > Right Click on “Computer” > Manage (You’ll need to be an Administrator)

The “Computer Management” window will open, in it go to:

Storage > Disk Management > Right Click on the partition you want to extend.

Select the option that says “Extend”, a Wizard will open, just hit Next until it ends if you want to use the whole space.

After you’re done, if you refresh your “Computer” on the File explorer the resized drive should show right away

That’s it, enjoy and let me know how it went.