fogbound.net




Sat, 2 Nov 2013

Hallowe’en

— SjG @ 10:43 am

We had a Hack o’ Lantern this year, who responded to tweets, tweeted jokes, and “accidentally” revealed it’s plans for world domination.

Pure pumpkin cyber evil

Pure pumpkin cyber evil

For the World Domination Plan, I needed some artwork, which I hereby inflict upon the world.

They’re remixes. Tux is courtesy of Larry Ewing, Simon Budig, and Anja Gerwinski.

broadcast2

vamp-tux


Fri, 24 May 2013

Javascript approximation for Pi

— SjG @ 11:01 am

Based on this tweet, I now have the ultimate JavaScript approximation for Pi (Π), which I think we can all agree is preferable in every way to the outmoded Math.PI:

var pi=((++[+[]][+[]]+[]+ ++[+[]][+[]]+[])* ++[+[]][+[]])*(++[+[]][+[]]+ ++[+[]][+[]])/((+[+[]]+'x'+(![]+[])[[+!+[]+!+[]]*[+!+[]+!+[]]])/(++[+[]][+[]]+ ++[+[]][+[]]));


Sun, 21 Apr 2013

Measuring network traffic between two hosts

— SjG @ 10:28 am

For a project that communicates over an expensive network connection (i.e., one that charges by the kilobyte), I needed to find out exactly how many bytes a specific process was going to transfer between my source host and a destination machine. For my own nefarious purposes, I need to know how many bytes of payload data I’m sending/receiving, but I also need to know the true total data transfer, including TCP/IP headers, etc.

Over the years, I have accumulated a few tricks for measuring this sort of thing. Usually, though, I’ve had to measure one particular kind of traffic (specifically, HTTP) — in which case, it’s not hard to set up a proxy using nc. In this latest case, however, the process not only uses HTTP/HTTPS, but ssh to issue remote commands, so I need to monitor all TCP/IP traffic between the machines.

There are other tools that are sometimes helpful. For example, to see what’s using up bandwidth at a given moment, a tool like iftop is great. Unfortunately, I need to know the aggregates, and iftop doesn’t log to a file in a way that I can use.

If I were on a pure Linux environment, it looks like IPTraf would do what I want, but I’m using a Mac.

I don’t doubt that there are much better approaches out there1, but here’s what I used (pretending that the remote host was at IP 192.168.1.100):

sudo tcpdump -e host 192.168.1.100 > net_process_log.txt
perl -p -i.bak -e 's/(.*?)length (\d+):(.*)length (\d+)/$2,$4/g' net_process_log.txt
cut -d , -f 1 net_process_log.txt > actual_size.txt
cut -d , -f 2 net_process_log.txt > data_size.txt
awk '{s+=$1} END {print s}' actual_size.txt
138099
awk '{s+=$1} END {print s}' data_size.txt
102412

So, in my example, I’m using tcpdump to output all traffic between my machine and the host 192.168.1.100. Typical records output from tcpdump looks like:

11:13:23.834080 xx:xx:xx:xx:xx:xx (oui Unknown) > xx:xx:xx:xx:xx:xx (oui Unknown), ethertype IPv4 (0x0800), length 292: dvr.home.http > apotheosis.home.64602: Flags [P.], seq 1:227, ack 468, win 1354, options [nop,nop,TS val 132300159 ecr 1750033024], length 226
11:13:23.834081 xx:xx:xx:xx:xx:xx (oui Unknown) > xx:xx:xx:xx:xx:xx (oui Unknown), ethertype IPv4 (0x0800), length 66: dvr.home.http > apotheosis.home.64602: Flags [.], ack 469, win 1354, options [nop,nop,TS val 132300159 ecr 1750033026], length 0

There are two lengths specified: the first is the actual packet size, and the second is the payload of the packet. As you can see in the second packet, the payload is zero bytes, but the packet length is 66 bytes.

In any case, I use perl to extract the two lengths into a comma-delimited file, cut to split out the columns, and awk to add them up. It’d be trivial to do all these steps together in a short perl program, but I like keeping around tons of obscure text files from forgotten procedures on my hard drive. Well, actually, I did it this way so I could sanity-check the intermediate steps.

So, this taught me that my process transferred 102,412 bytes of payload, and with the TCP/IP packet overhead transmitted a total of 138,099 bytes.

1 I didn’t discover bwm-ng until after I did my measurement. It looks like it might be a good solution as well. I probably could have used Wireshark too.


Mon, 8 Apr 2013

Why I need an extension on my taxes

— SjG @ 9:49 pm

… because I wanted to play with HTML 5 Canvas.
example1

example2
This should run on any reasonably sane browser. Play with it yourself here.


Wed, 13 Feb 2013

PHPUnit/Selenium and iframes

— SjG @ 10:20 am

I ran into an interesting issue with a functional test, and wasn’t able to Google a specific solution out there. So here’s my solution.

The Problem:
When testing a web form that contains a Javascript “wysiwyg” editor (in this case xhEditor, since it’s a site built on Yii, and there’s a great extension), the Selenium IDE was not logging anything typed into that field. When I tried using the field ID, the data was also silently vanishing.

The Explanation:
xhEditor converts the TextArea into an iFrame, and captures keystrokes to populate the HTML body of the dependent frame. The Selenium plugin for Firefox doesn’t grok this, and the data goes missing.

The Solution:
Manually switch frames in your PHPUnit/Selenium code.

$this->selectFrame("//*[@id='xhEdt0_iframe']"); // switch to the iframe
$this->type("//body","This text shows up in the xhEdit WYSIWYG!"); // type into the body
$this->selectFrame("relative=top"); // switch back to the surrounding frame