This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
157 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(", ")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters