From 805073c0c47f66bf70652b43582ca79a3a52ee4b Mon Sep 17 00:00:00 2001 From: Manatsawin Hanmongkolchai Date: Sat, 8 Feb 2014 21:44:03 +0700 Subject: [PATCH] Login now works --- .gitignore | 1 + kuwind.js | 27 +++++++++++++ kuwintools.js | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++ main.js | 36 +++++++++-------- package.json | 8 ++-- 5 files changed, 157 insertions(+), 21 deletions(-) create mode 100644 .gitignore create mode 100644 kuwind.js create mode 100644 kuwintools.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/kuwind.js b/kuwind.js new file mode 100644 index 0000000..ad2b00e --- /dev/null +++ b/kuwind.js @@ -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(); \ No newline at end of file diff --git a/kuwintools.js b/kuwintools.js new file mode 100644 index 0000000..c683846 --- /dev/null +++ b/kuwintools.js @@ -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; +} \ No newline at end of file diff --git a/main.js b/main.js index c26a985..b17974e 100644 --- a/main.js +++ b/main.js @@ -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(); \ No newline at end of file +}else{ + console.log("Usage: "+process.argv[1]+" user pass zone"); + console.log("zone is one of "+kuwintools.zones.join(", ")); +} \ No newline at end of file diff --git a/package.json b/package.json index 789df84..3f2d6f7 100644 --- a/package.json +++ b/package.json @@ -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" + } }