Projects/KPN Hotspots Autologin

From Hackerspace Amersfoort
Revision as of 18:03, 31 May 2012 by AK47 (talk | contribs)
Jump to navigation Jump to search


Project KPN Hotspots Autologin
Name KPN Hotspots Autologin
Start
End
Contact User:AK47
Website
Information KPN Hotspots Autologin
Status Productie"Productie" is not in the list (Alpha, Beta, Production, Finished, Abandoned) of allowed values for the "Project status" property.


Introduction

If you are ever in the desperate situation of needing to build a KPN Hotspots "loadbalancer" for your event, use the script below for inspiration.

What do you need

  • A location with KPN hotspots
  • Preferably a wired connection to the KPN hotspots VLAN (wireless bridge would work as well, but allows only 1 MAC address behind it)
  • A XS4ALL account
  • A box with lot of virtual machines
  • Two (virtual) network interfaces on each machine
  • NAT on the interface facing the KPN hotspots VLAN
  • A router with ECMP routing support and ICMP "up" checking (like RouterOS/Routerboard etc)

Introduction part 2

When you have a XS4ALL account each mailbox counts as a valid "unlimited" KPN Hotspots account. Unlimited means you have an online-time of 8 hours and a bandwidth limit of 2Mbit. After the 8 hours has expired you can login again and have another 8 hours of online-time.

Currently the script below has a timeout of 460 minutes, so the script will logout after 460 minutes (20 minutes before time) and then login again.

During the login period the script will disable ICMP facing the router so the router knows it shouldn't send any traffic. Whenever the script has verified the "line" is up it will re-enable ICMP.

Autologin script

<?php
/**
 * CONFIG
 */
$username = "user@xs4all.nl";       // with @domain.name
$password = "password";
$timeout = 460;         // in minutes
$checkurl = "http://82.197.206.41/hotspotcheck";
$checkvalue = "wooyeah!";
$testno = 4;
$version = "0.11";
$sessionfile = "/root/sessionid";

/**
 * functions
 */
function gettime() {
        return "[" . date("Y-m-d H:i:s") . "] ";
}

function _log($line) {
        echo gettime() . $line . "\n";
}

function toggleICMP($enable = true) {
        if ($enable) {
                _log("enabling ICMP...");
                passthru("iptables -F");
        }
        else {
                _log("disabling ICMP...");
                passthru("iptables -A INPUT -p icmp -j DROP");
        }
}

function getsessionid() {
	global $sessionfile;

        $data = file_get_contents("https://portal.hotspotsvankpn.com/templates/dispatcher.asp?page_id=home_hs_out_nl");

        for ($i = 0; $i < count($http_response_header); $i++) {
                $tmp = explode (" ",$http_response_header[$i]);
                if ($tmp[0] == "Set-Cookie:") {
                        $tmp2 = explode (";",$tmp[1]);
                        $tmp3 = explode ("=", $tmp2[0]);

                        if ($tmp3[0] == "CMSSESSID") {
                                $value = $tmp3[1];
                                break;
                        }
                }
        }

        file_put_contents($sessionfile, $value);

        return $value;
}

function login($username, $password, $sessionid) {
        _log("Logging in...");

        $data = array ('name' => $username, 'pass' => $password, 'page_id' => 1620, 'ws_action' => 'login_user_hotspot', 'voorwaarden' => 'Y');
        $data = http_build_query($data);

        $context_options = array (
                'http' => array (
                    'method' => 'POST',
                    'header'=> "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.1.249.1045 Safari/532.5
\r\n" .
                                "Content-type: application/x-www-form-urlencoded\r\n" .
                                "Content-Length: " . strlen($data) . "\r\n",
                    'content' => $data
                    )
                );

        $context = stream_context_create($context_options);

        $data = file_get_contents("https://portal.hotspotsvankpn.com/templates/dispatcher.asp?page_id=home_hs_out_nl&CMSSESSID=" . $sessionid, false, $context);

}

function logout($sessionid) {
        _log("Logging out...");
        file_get_contents("https://portal.hotspotsvankpn.com/templates/logout2.asp?CMSSESSID=" . $sessionid);
}

function checkup($checkurl, $checkvalue) {
        return (trim(file_get_contents($checkurl)) == $checkvalue);
}

/**
 * init
 */
_log("KPN Hotspots auto-login version {$version}");
toggleICMP(false);
logout(file_get_contents($sessionfile));

$last_login = 0;
$first = true;
$retry = false;
$nosleep = false;
$cnt = 0;

while (true) {
        if ($retry == true || $last_login == 0 || ( ((time() - $last_login) / 60) > $timeout )) {
                if (!$first) {
                        toggleICMP(false);
                        sleep(10);
                        logout($sessionid);
                        sleep(2);
                }
                $sessionid = getsessionid();
                login($username, $password, $sessionid);

                sleep(2);
                if (checkup($checkurl, $checkvalue)) {
                        _log("Line is up!");
                        $last_login = time();
                        toggleICMP(true);
                        $first = false;
                        $retry = false;
                        $nosleep = false;
                }
                else {
                        _log("Hmm. that did not seem to work. Maybe check your username+password and re-run the script? Retrying...");
                        $retry = true;
                        $nosleep = true;
                        sleep(1);
                }
        }

        if (!$nosleep) {

                $good = 0;
                // check if line is still up
                for ($i = 1; $i <= $testno; $i++) {
                        if (checkup($checkurl, $checkvalue)) {
                                $good++;
                        }
                        sleep(1);
                }

                if ($good == 0) {
                        $retry = true;
                        _log("Line seems down, retrying...");
                        toggleICMP(false);
                }
                else {
                        if ($cnt == 0) _log("Ping!");
                        elseif ($cnt > 20) $cnt = 0;
                        $cnt++;
                        sleep(60);
                }
        }
}