Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Login now works
Browse files Browse the repository at this point in the history
  • Loading branch information
whs committed Feb 8, 2014
1 parent 856ca78 commit 805073c
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
27 changes: 27 additions & 0 deletions kuwind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var os = require("os");
var kuwintools = require("./kuwintools");

var Kuwind = function(){};

Kuwind.prototype.isInKu = function(){
var self = this;

var ifaces = os.networkInterfaces();
return Object.keys().some(function(iface){
return ifaces[iface].some(function(ip){
return self.isKuIp(ip.address);
});
});
};

Kuwind.prototype.isKuIp = function(ip){
return ip.indexOf("158.108.") === 0;
};

Kuwind.prototype.login = function(opt, cb){
kuwintools.login(opt.username, opt.password, opt.zone, function(data){
cb(!data.success, data);
});
};

module.exports = new Kuwind();
106 changes: 106 additions & 0 deletions kuwintools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* KUWINTools
* Originally from Chrome Extension
*
*
*/
"use strict";

var request = require("request");

var KUWINTools = {};
KUWINTools.utils = {};

KUWINTools.utils.xhr = function(type, url, data, cb){
request({
"url": url,
"form": data,
"method": type,
"headers": {
"User-Agent": ""
}
}, function (error, response, body) {
if(!cb){
return;
}
cb({
"success": !error,
"resp": body,
"status": response ? response.statusCode : null
});
});
};
KUWINTools.utils.get = function(url, cb){
return KUWINTools.utils.xhr("GET", url, null, cb);
};
KUWINTools.utils.post = function(url, postdata, cb){
return KUWINTools.utils.xhr("POST", url, postdata, cb);
};

// Don't change these
KUWINTools.endpoint = "https://login!.ku.ac.th/mobile.php";
KUWINTools.zones = ["bkn", "kps", "src", "csc", "Guest"];

KUWINTools.utils.get_server = function(){
// min = 1, max = 12
return Math.floor(Math.random() * 12)+1;
};
KUWINTools.get_endpoint = function(){
return KUWINTools.endpoint.replace("!", KUWINTools.utils.get_server());
};
KUWINTools.login = function(user, pass, zone, cb){
if(typeof zone != "number"){
zone = KUWINTools.zones.indexOf(zone);
if(zone == -1){
return cb({
"success": false,
"message": "Invalid zone. Valid zones are: "+KUWINTools.zones.join(", ")
});
}
}
KUWINTools.utils.post(KUWINTools.get_endpoint() + "?action=login", {
"username": user,
"password": pass,
"v": 4,
"trackme": "n",
"zone": zone
}, function(resp){
if(!resp.success){
return cb({
"success": false,
"message": "HTTP Error: "+resp.status+" "+resp.resp
});
}
// parse it
resp = resp.resp.split("\n");
if(resp[0] !== "OK"){
return cb({
"success": false,
"message": resp[0]
});
}
var user = resp[3];
var sessions = resp.slice(4);
var outSessions = [];
sessions.forEach(function(sess){
var out = {};
sess = sess.split("\t");
if(sess.length != 3){
return;
}
out.ip = sess[0];
out.time = new Date(sess[1].replace("ICT", "+0700"));
out.idle = sess[2];
outSessions.push(out);
});
cb({
"success": true,
"user": user,
"sessions": outSessions
});
});
};

if(module){
module.exports = KUWINTools;
}
36 changes: 19 additions & 17 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
var os = require("os");
#!/usr/bin/env node

var Kuwind = function(){};
var kuwind = require("./kuwind");
var kuwintools = require("./kuwintools");

Kuwind.prototype.isInKu = function(){
var self = this;

var ifaces = os.networkInterfaces();
return Object.keys().some(function(iface){
return ifaces[iface].some(function(ip){
return self.isKuIp(ip.address);
});
if(process.argv.length == 5){
kuwind.login({
username: process.argv[2],
password: process.argv[3],
zone: process.argv[4]
}, function(error, data){
if(error){
console.log("Cannot login: "+data.message);
process.exit(1);
}else{
console.log("Logged in as "+data.user);
}
});
};

Kuwind.prototype.isKuIp = function(ip){
return ip.indexOf("158.108.") === 0;
};

module.exports = new Kuwind();
}else{
console.log("Usage: "+process.argv[1]+" user pass zone");
console.log("zone is one of "+kuwintools.zones.join(", "));
}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"version": "1.0.0",
"description": "Log you in to Kasetsart University WIreless Network",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"ku"
],
"author": "Manatsawin Hanmongkolchai",
"license": "GPLv3"
"license": "GPLv3",
"dependencies": {
"request": "~2.33.0"
}
}

0 comments on commit 805073c

Please sign in to comment.