In the Year 2013

As always, first post of a year is reserved for a bit of statistics.

This year I have slowed-down posting a bit with only 74 posts since January. As usual 40% of posts is programming related, followed by 25% of Windows posts. Rest is such a mishmash of everything that I wont even go into analyzing it.

Traffic-wise it was a fantastic year - another 25% increase is greatly appreciated. Unfortunately huge 66% of traffic comes from places unknown so it is really doubtful whether it is even worth tracking this. Assuming same distribution of unknown locations as ones that were recognized, USA leads with 60% of visits. After that there is 20% visits from Germany and 15% from India. Other 216 countries cover the rest.

Google Chrome leads the pack with 40% visitors. Next one is Internet Explorer with 25% followed closely by Firefox. All other browsers are in traces. Mostly users came with desktop browsers (more than 95%).

Vast majority of search traffic to blog came in search for Windows 8.1 product key, probably because of my Installing Windows 8.1 (or 8) without a product key post. Another huge chunk were ones searching for VHD Attach and MagiWOL.

Pingdom claims 99.93% uptime (6h down) which is definitely helped by CloudFlare. While free CloudFlare account is not a perfect solution, it is definitely helping with site’s speed. For next year I will need to think of some other improvements.

And that’s all folks!

Variable Parsing Au Bash

Every command-line processor has a way to allow for embedded variables. Those familiar with DOS will recognize percent encoded variables, e.g. “echo I am %USERNAME%”. Those more familiar with Unixoids will be more relaxed around dollar variables, e.g.: “echo I am $USERNAME”. Either way gives possibility to combine fixed text with some external value. I personally prefer bash-style so I will continue with it.

If we try to parse “User $USERNAME is working on $OS using $NUMBER_OF_PROCESSORS processors.”, this is really easy for human to tackle. Even on first glance, we know that result should be “Joe is working on Windows NT using 1024 processors.”. Making computer recognize that is a bit more involved.

Well, generic solution is really simple. Just make an function that will extract all variables from string and ask us what values might those be:

string ParseVariables(string text) {
    var res = ParseVariablesStateMachine(text, **delegate(string variable)** {
        **return Environment.GetEnvironmentVariable(variable);**
    });
    return res;
}

As you can see from above code, we call into ParseVariablesStateMachine method giving it whole input text alongside with a callback delegate. Function will take care of parsing variables out and ask us what each value should be via delegate function. In our example we do simple environment variable lookup but this can be modified to return almost anything.

This delegate will return value back into calling method and that method will continue string processing until next variable comes along. At that time it will repeat call to our delegate; rinse and repeat. At the end it will return value composed of all these variables in one nice string.

One way to tackle this problem is by using a simple state machine (yes, name was kinda giving it away). We definitely have starting state, we have state where we found inner variable, and state where variable is being processed. Code may look something like this (heavily redacted):

string ParseVariablesStateMachine(string text, Func<string, string> variableCallback) {
    var state = ParsingState.Default;
    for (int i = 0; i < text.Length; i++) {
        switch (state) {
            case ParsingState.Default:
                if (text[i] == '$') { state = ParsingState.VariableStart; }
                break;

            case ParsingState.VariableStart:
                state = ParsingState.NormalVariable;
                break;

            case ParsingState.NormalVariable:
                if (!char.isLetterOrDigit(text[i])) {
                    variableCallback.Invoke(text.substring(variableStart, variableLength));
                    state = ParsingState.Default; //search for next variable
                }
                break;
        }
    }
    return parsedString;
}

Full example source is available for download.

Modifying ParseVariablesStateMachine mathod to parse DOS-style variables is exercise left to a reader. It is actually simpler than bash-style parsing because we know when variable ends based on closing percent sign.

PS: Full example also covers extended variable style (e.g. ${USERNAME}.).

Asus RT-AC56U

Illustration

As I was getting tired of crowded 2.4 GHz band in my neighborhood, I started looking into newest 802.11ac routers. I was after something small, dual band, and with support for Tomato (or DD-WRT) firmware (history taught me that manufacturer’s firmware sucks).

One device that fitted description was Asus RT-AC56U. Having owned Asus’s WL-500g, WL-300g, and still owning WL-330gE I was quite confident that hardware will be good, LEDs will be blue, and original firmware will be rubish. I also expected to get unbrickable device with a lot of community support. [2014-04-19: Of course I have managed to brick this one. I should learn to keep my mouth shut.]

Hardware-wise this is AC1200 category, dual-band (2.4 GHz and 5 GHz), dual-stream router with 4 gigabit ethernet ports and two USB ports (one is 3.0). All is driven by BCM4708, dual core 1 GHz CPU (down-clocked to 800 MHz), 256 MB of RAM, and 128 MB of flash. As home routers go, this is a beast. Not the fastest beast, but beast nevertheless.

As I plugged it in, I was enchanted. This is my first home router where I didn’t need to open a case and modify LED brightness myself. It is nice subdued blue light that won’t light-up your house like a Christmas tree. It is just perfect intensity. And yes, I am easily amused. :)

I haven’t been checking Asus firmwares for a while so it came as a surprise how open it has become. Yes, it has always been an open source but best thing you could count on was to get raw sources every few months. Asus now bases firmware on AsusWRT source code that is obtainable few days after firmware gets published. It even contains (accurate) setup instructions for compiling that code yourself.

I am pleasantly surprised with this development and it shows results. While usually only firmwares worth having on router were DD-WRT and Tomato, now we must take Merlin into consideration too. Probably due to all that liveliness, Asus own firmware actually doesn’t suck. Mind you it is not as good as any of these alternatives but it is actually possible to use it without swearing.

Setup of a device is quite simple and practically leads you by hand toward getting your Internet connection. Beginners will probably leave it there and be happy. Advanced users have many more things to configure, whether it is IPv6, disk sharing, or VPN. Most settings can even be applied without losing connection. Really nice.

Not all things are perfect unfortunately. For one, 2.4 GHz stability and range is between bearable and just awful. Irony would have it that, in my household, Asus’ own N56VJ laptop (Atheros AR9485WB) is almost unusable even just two meters away. For a minute it would have low speed connection (i.e. 2-3 Mbps), then it would just stall for a few seconds, recovering to 2-3 Mbps in ten seconds or so. Then it would stall again, repeating the cycle. And it is not because of location; kiddie WL-330gE router handles the very same location without any issue…

Asus team is looking at it and there are promises of a new firmware that might make things better but I wouldn’t hold my breath here. I have tried their newest beta firmware and results are just marginally better. I have tried completely alternative firmwares and everything is as bad as with original one. 2.4 GHz devices I have with Intel and Broadcom chipset are behaving much better but nowhere near how they should. I would say that this is more probably a design issue that might or might not be dealt with in a newer hardware revisions. Those who already bought one are probably out of luck.

All taken in consideration, I am very satisfied with purchase. Most devices I have are 5 GHz capable and they work wonderfully through my whole apartment. In furthest location I still have download speed exceeding 50 Mbps. Maybe it would go faster but my Internet pipe cannot handle it. Whether it will be as good when 5 GHz band gets congested as 2.4 GHz one well see in future, but I will enjoy it while it lasts.

Would I recommend it to someone else strictly depends on how bad do they need 2.4 GHz. If you are shooting toward 5 GHz this device is probably best bang-for-buck. For $120 you get a really capable CPU, lots of memory, and a variety of firmwares that can give you whatever functionality you desire. For somebody who likes tinkering with network this is god-given.

Alternative Boolean Syntax

I got into discussion with a friend on what is the best way to express boolean-like value in a C# initializer. Whole discussion was spurred by following piece of code (ok, not exactly this one, but similar enough):

var x = new FieldCollection(**true**);

This perfectly valid line has a magic boolean argument whose function is not immediately clear. Syntax-wise code is fine but readability does suffer. Neither one liked how this looked and we went into discussion about refactoring. First one was introducing an enumeration class:

var x = new FieldCollection(**FieldDuplicate.Allow**);

This is an usual approach to readability argument. Create an enumumeration that has (more or less) reasonable names and then use that as an argument. It allows for better readability at cost of a bit more coding.

But C# has something I find better. You can use named arguments to have greater visibility without any extra coding:

var x = new FieldCollection(**allowDuplicates: true**);

Maybe it is not as clean as a dedicated enumeration but I find it works for me. It doesn’t require any code changes inside of a class. It doesn’t require an extra enumeration declaration. And it works even when you don’t have access to code of a class. All that is needed is a bit of discipline upon calling code.

And programmers always have abundance of discipline… :)

Setting Up Private Internet Access on Mint

Illustration

When I published post about OpenVPN on CentOS most common comment was "How to do that on some more user-friendly distribution. Well, you don’t get much more friendlier than Mint 16 (Petra).

Before starting with anything we need to install OpenVPN package. This is done via Menu, Administration, Software Manager. Just type OpenVPN and install first thing you get back (yep, this is great piece of security advice ;)).

First we can do the easy stuff. Download PIA’s OpenVPN configuration files and extract it to directory of your choice. I kept them in /home/MyUserName/pia.

Next easy step is setting up DNS resolving. For that we go to Menu, Preferences, Network Connections. Just click edit on connection you are using and go to IPv4 Settings tab. Change Method to Automatic (DHCP addresses only). Under DNS servers enter 209.222.18.222 209.222.18.218 (PIA’s DNS).

All other commands are to be executed in terminal and most of them require root privileges. It might be best if you just become root for a while:

su - root

Next step is getting configuration in place (replace username and password with yours):

cp /home/MyUserName/pia/ca.crt /etc/openvpn/ca.crt
cp /home/MyUserName/pia/crl.pem /etc/openvpn/crl.pem
cp /home/MyUserName/pia/US\ Midwest.ovpn /etc/openvpn/client.conf

sed -i "s*ca ca.crt*ca /etc/openvpn/ca.crt*" /etc/openvpn/client.conf
sed -i "s*crl-verify crl.pem*crl-verify /etc/openvpn/crl.pem*" /etc/openvpn/client.conf

echo "auth-user-pass /etc/openvpn/login.pia" >> /etc/openvpn/client.conf
echo "mssfix 1400" >> /etc/openvpn/client.conf

echo "^^username^^" > /etc/openvpn/login.pia
echo "^^password^^" >> /etc/openvpn/login.pia

chmod 500 /etc/openvpn/login.pia

Now we can test our connection (after we restart network in order to activate DNS changes):

/etc/init.d/networking restart
openvpn --config /etc/openvpn/client.conf

Assuming that this last step ended with Initialization Sequence Completed, we just need to verify whether this connection is actually used. I found whatismyipaddress.com quite helpful here. If you see some mid-west town on map, you are golden (assuming that you don’t actually live in US mid-west :)).

Now you can stop test connection via Ctrl+C in order to properly start it. In addition, you can specify it should start on each system startup:

service openvpn start
echo "AUTOSTART=all" >> /etc/default/openvpn

Lastly you can think about firewall and disabling default interface when VPN is not active. Of course, in order for things to work, we still need to allow port 1194 toward our VPN server and DNS:

ufw default deny incoming
ifw default deny outgoing
ufw allow out on tun0
ufw allow out 1194/udp
ufw allow out on eth0 to 209.222.18.222 port 53 proto udp
ufw allow out on eth0 to 209.222.18.218 port 53 proto udp
ufw enable

And that is all.

PS: It is not a déjà vu, article is really close to how it was set on CentOS.

[2016-10-01: Removed auth-nocache directive as it didn’t play nice with auth-user-pass.]