[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 instead of 0/1)

VPN ON output in Mac

Destination        Gateway            Flags        Refs      Use   Netif Expire
0/1               UGSc            5        0   utun1

VPN ON output in Linux

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface       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          UGSc           66        0     en0
127                UCS             2        4     lo0

VPN OFF in Linux

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface         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, if you find it, VPN is on.

[CODE/JAVA] Scanner – read a full line

I was solving problems at HackerRank to teach a friend how to code in Java and to process the input data it’s convenient to use Java’s built in Scanner class.

However, in one of the problems we wanted to read a whole line at once and we noticed that the Scanner’s API is geared towards reading the next token, and it doesn’t have a method that reads the entire line. It has one method that skips a line and reads from the last point which is very weird to use, so we created a static method to just read the next entire line, keeping it simple.

public static String readLine(Scanner scanner) {
            Pattern oldDelimiter = scanner.delimiter();
            String r = scanner.next();
            return r;

The Scanner uses as its default token delimiter a whitespace pattern, so what we do in this method is to temporarily replace the token delimiter for a newline delimiter. Once set to newline all we need to do is to read the next token, which will be the entirety of the line, and then we put the previous delimiter set back in place.

In Java 8 there’s a new Linebreak matcher expression \R, however, if we want our method to be backwards compatible (as we don’t know on which JRE it could be running) we set the equivalent delimiter manually in our function

\R  Any Unicode linebreak sequence, is equivalent to \u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029]

If you don’t like the static function approach you can always repurpose this code and extend Scanner into a custom MyScanner class , and add a method nextLine that does the same thing

[CODE] Arduino 4 BIT LED binary counter

Here’s my “Hello World” on Arduino.
A 4-bit binary counter for arduino.
If you add another LED on the 5th pin, then it can count from 0 to 31 (32 values)

Circuit wired by @KataySantos

[pastacode lang=”c” manual=”int%20counter%20%3D%20-1%3B%0Aint%20MAX_VALUE%20%3D%2016%3B%0A%0Avoid%20updateLED(int%20pin%2C%20int%20bit%2C%20int%20ctr)%20%7B%0A%20%20digitalWrite(pin%2C%20(ctr%20%26%20(1%3C%3Cbit))%20%3D%3D%20(1%3C%3Cbit)%20%3F%20HIGH%20%3A%20LOW)%3B%0A%7D%0Avoid%20setup()%20%7B%0A%20%20%2F%2F%20this%20runs%20once%0A%20%20pinMode(1%2C%20OUTPUT)%3B%0A%20%20pinMode(2%2C%20OUTPUT)%3B%0A%20%20pinMode(3%2C%20OUTPUT)%3B%0A%20%20pinMode(4%2C%20OUTPUT)%3B%0A%7D%0A%0Avoid%20loop()%20%7B%0A%20%20%2F%2F%20this%20runs%20repeatedly%0A%20%20counter%20%3D%20(counter%20%2B%201)%20%25%20MAX_VALUE%3B%0A%20%20updateLED(1%2C%200%2C%20counter)%3B%0A%20%20updateLED(2%2C%201%2C%20counter)%3B%0A%20%20updateLED(3%2C%202%2C%20counter)%3B%0A%20%20updateLED(4%2C%203%2C%20counter)%3B%0A%20%20delay(500)%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

[CODE/PHP] JpGraph: How to output your graph as a base64 encoded image

Some times you just want to output the image created by your $graph object without having to create a separate .php script that would need to receive a bunch of parameters.

Here’s a function you can pass your $graph object right before the $graph->Stroke(); call

[pastacode lang=”php” manual=”function%20graphInSrc(%24graph%2C%20%24width%2C%20%24height)%20%7B%0A%20%20%24img%20%3D%20%24graph-%3EStroke(_IMG_HANDLER)%3B%0A%20%20ob_start()%3B%0A%20%20imagepng(%24img)%3B%0A%20%20%24img_data%20%3D%20ob_get_contents()%3B%0A%20%20ob_end_clean()%3B%0A%0A%20%20echo%20’%3Cimg%20width%3D%22′.%24width.’%22%20height%3D%22′.%24height.’%22%20src%3D%22data%3Aimage%2Fpng%3Bbase64%2C’.base64_encode(%24img_data).’%22%2F%3E’%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

[GIT] How to replace the master branch with another branch.

git checkout master
git reset --hard fixed-master-branch
git push origin master -f

Sometimes someone may pollute the ‘master’ branch and you may have noticed this only after you’ve fetched, rebased and pushed your commits.
So your history may end up like this (I’ll use decimal numbers on the commit IDs for illustrative purposes only):

commit: 87
Author: me
A good commit

commit: 86
Author: other guy
Merge: that brought back a lot of crap because they probably used some GUI based git client and they did a git pull without your permission

commit: 85
Author: other guy
Another commit that shouldn't be here

commit: 84
Author: me
Another the last good commit

Now, your master is all effed up and you’re about to have an anxiety attack. What you want is to have this in your git log.

commit: 87
Author: me
A good commit

commit: 84
Author: me
Another the last good commit

To get back to this state is pretty easy:

  1. Check out the last good commit

git checkout 84

Now you will be detached from master, let’s name this temporary branch ‘fixed-master-branch’

git checkout -b fixed-master-branch

Now the last commit you should see should be commit 84.

Let’s now bring our commit, and skip the 2 sucky commits

git cherry-pick 87

Now your ‘fixed-master-branch’ branch looks exactly how you want the master to look like.

But now, you want to replace the old master with the new one.

Go back to the broked master

git checkout master

And fix it, and fuck all that merge –strategy ours BS advice you will find on StackOverflow as it will leave the git log history really weird.

git reset --hard fixed-master-branch

Now when you look at your history, master will be the same as ‘fixed-master’, and to fix your remote repo, you will have to make a push –force and let everyone know that you did that because you needed to clean your repository’s history

git push origin master -f

You’re done.