[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

int counter = -1;
int MAX_VALUE = 16;

void updateLED(int pin, int bit, int ctr) {
  digitalWrite(pin, (ctr & (1<<bit)) == (1<<bit) ? HIGH : LOW);
void setup() {
  // this runs once
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);

void loop() {
  // this runs repeatedly
  counter = (counter + 1) % MAX_VALUE;
  updateLED(1, 0, counter);
  updateLED(2, 1, counter);
  updateLED(3, 2, counter);
  updateLED(4, 3, counter);

[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

function graphInSrc($graph, $width, $height) {
  $img = $graph->Stroke(_IMG_HANDLER);
  $img_data = ob_get_contents();

  echo '<img width="'.$width.'" height="'.$height.'" src="data:image/png;base64,'.base64_encode($img_data).'"/>';

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

[CODE/JAVA] ARGB_8888 Pixel Abstraction

This is one of the ways you can decode Pixel colors out of the integers you get from Android Pixels.

ARGB_8888, stands for Alpha, Reg, Green, Blue. The 8’s stand for the number of bits per channel.

In Android, signed int‘s are used to represent pixel’s alpha/color information.
Since Android’s language of choice is java, these ints are 32-bit integers, each int takes 4 bytes.

4 bytes = 32 bits = 8bits + 8bits + 8bits + 8bits.

If you had an int like 0xFFAABBCC, each pair of letters on that hexadecimal would mean the following from left to right


If you’ve done web programming and played with rgb colors and you didn’t know about this, now it all should click on how your web browser represents colors, except in HTML you don’t deal with the alpha value in the front. On Android’s XML you do.

In bits (binary), the 0xFFAABBCC value would look like this:

alpha red green blue
0xFF 0xAA 0xBB 0xCC
255 170 187 204

If you wanted to look at the entire number in binary/bits, it’d be something like this (leaving spaces for visual help):

0b11111111 10101010 10111011 11001100 == 0xFFAABBCC == 4289379276

So if you wanted to get the red channel (0xAA = 170 = 0b10101010) , you’d have to move the whole thing towards the right 16 places, and then compare it with only the rightmost 8 bits.

So, we shift 16 places to the right, we’d get rid of the 2 bytes on the right side and end up only with the left half

0b11111111 10101010

Since we only care about those 8 bits on the right, we do an “&” (bitwise “and”) against 0xFF=255=0b111111111 (all 8 rightmost bits set to 1)

0b11111111 10101010 &
0b00000000 11111111
0b00000000 10101010

So with simple right bit shifting and “& 0xff” of the shifted value we can extract the values per channel.

This class also features a “multiplyByFloat()” function, which I was using to multiply to each channel as I was operating with convolution kernels while playing with image filters.

/** ARGB_8888 Pixel abstraction */
public static class PixelARGB_8888 {
 public final byte a;
 public final byte r;
 public final byte g;
 public final byte b;
 public final int intVal;

 public PixelARGB_8888(final int argb32bitInt) {
  a = (byte)((argb32bitInt >> 24) & 0xff);
  r = (byte)((argb32bitInt >> 16) & 0xff);
  g = (byte)((argb32bitInt >> 8) & 0xff);
  b = (byte)(argb32bitInt & 0xff);
  intVal = argb32bitInt;

 public PixelARGB_8888(byte a, byte r, byte g, byte b) {
  this.a = a;
  this.r = r;
  this.g = g;
  this.b = b;
  intVal = (a << 24) + (r << 16) + (g << 8) + b;

 public static int multiplyByFloat(float factor, int arg32bitInt) {
  return multiplyByFloat(factor, arg32bitInt, false);

 public static int multiplyByFloat(float factor, int argb32bitInt, boolean multiplyAlphaChannel) {
  PixelARGB_8888 original = new PixelARGB_8888(argb32bitInt);
  byte alpha = original.a;
  if (multiplyAlphaChannel) {
   alpha = (byte)(original.a * factor);
  PixelARGB_8888 multiplied = new PixelARGB_8888(alpha, (byte)(factor * original.r), (byte)(factor * original.g), (byte)(factor * original.b));
  return multiplied.intVal;