Skip to content

Commit

Permalink
first blood:smile:
Browse files Browse the repository at this point in the history
  • Loading branch information
lizheming committed Jun 23, 2015
1 parent 3d2fb9a commit a10f3d3
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
9 changes: 9 additions & 0 deletions bin/srun-config-get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var program = require("commander");

program
.action(function(name) {
var config = require("../config.json");
console.log( config[name] || "未设置" );
})
.parse(process.argv);

15 changes: 15 additions & 0 deletions bin/srun-config-set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var program = require("commander"),
fs = require("fs")

program
.action(function(name, value) {
if( !name || !value ) return false;

var config = require("../config.json");
config[name] = value;
fs.writeFile("../config.json", JSON.stringify(config, null, "\t"), function(err) {
console.log( err ? err : "设置成功" )
})
})
.parse(process.argv);

8 changes: 8 additions & 0 deletions bin/srun-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var program = require("commander");

program
.usage("[command] [name] [value]")
.command("set <name> <value>", "set config")
.command("get <name>", "get config")
.parse(process.argv);

28 changes: 28 additions & 0 deletions bin/srun-login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var program = require("commander");
srun = require("../lib/srun");

program
.option("-u, --username <value>", "student number")
.option("-p, --password [value]", "password")
.parse(process.argv);

if( !program.hasOwnProperty("username") || !program.hasOwnProperty("password") ) {
return program.help();
}

if( program.password === true ) {
var prompt = require("prompt");
prompt.message = "";
prompt.delimiter = "";
prompt.colors = false;

prompt.start();
prompt.get([{
name: "password",
description: "password:",
hidden: true
}], function(err, res) {
if( err ) return false;
srun.login(program.username, res.password);
})
} else srun.login(program.username, program.password);
35 changes: 35 additions & 0 deletions bin/srun-logout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var program = require("commander"),
srun = require("../lib/srun");

program
.option("-u, --username <value>", "student number")
.option("-p, --password [value]", "password")
.option("-f, --force", "force logout account login in the current network")
.parse(process.argv);

if( !program.hasOwnProperty("force") ||
(!program.hasOwnProperty("username") || !program.hasOwnProperty("password"))
){
return program.help();
}

if( program.force ) srun.forceLogout();
else {
if(!program.username || !program.password) return false;
if( program.password === true ) {
var prompt = require("prompt");
prompt.message = "";
prompt.delimiter = "";
prompt.colors = false;

prompt.start();
prompt.get([{
name: "password",
description: "password:",
hidden: true
}], function(err, res) {
if( err ) return false;
srun.logout(program.username, res.password);
})
} else srun.logout(program.username, program.password);
}
1 change: 1 addition & 0 deletions bin/srun-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Building...");
2 changes: 2 additions & 0 deletions bin/srun-who.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var srun = require("../lib/srun");
srun.who();
12 changes: 12 additions & 0 deletions bin/srun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#! /usr/bin/env node

var program = require("commander");
program
.version( require("../package.json").version )
.usage("[command] [options]")
.command("login", "login by username and password")
.command("logout", "logout by using username and password or force logout")
.command("who", "look over which one login now")
.command("status", "output network status now")
.command("config", "get/set config")
.parse(process.argv);
3 changes: 3 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"server": "202.204.105.195:3333",
}
135 changes: 135 additions & 0 deletions lib/srun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
var querystring = require('querystring'),
parse = require('url').parse,
http =require('http');

var server = require("../config.json").server;

function post(url, data, callback) {
data = querystring.stringify( data );
url = parse(url);
var postOpt = {
host: url.hostname,
port: url.port,
path: url.pathname,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': data.length
}
};

var postReq = http.request( postOpt, function(res) {
res.setEncoding("utf-8");
res.on('data', function(data) {
callback = callback || function() {};
callback(data)
});
});

postReq.write(data);
postReq.end();
}

function get(url, callback) {
http.get(url, function(res) {
var content = "";
res.on("data", function(d) { content += d });
res.on("end", function() {
callback(content)
})
})
}

function info(text) {
if( !/[a-z_0-9@]+/g.test(text) ) return console.log(text);
var arr = {};
arr['user_tab_error'] = '认证程序未启动';
arr['username_error'] = '用户名错误';
arr['non_auth_error'] = '您无须认证,可直接上网';
arr['password_error'] = '密码错误';
arr['status_error'] = '用户已欠费,请尽快充值。';
arr['available_error'] = '用户已禁用';
arr['ip_exist_error'] = '您的IP尚未下线,请等待2分钟再试。';
arr['usernum_error'] = '用户数已达上限';
arr['online_num_error'] = '该帐号的登录人数已超过限额\n如果怀疑帐号被盗用,请联系管理员。';
arr['mode_error'] = '系统已禁止WEB方式登录,请使用客户端';
arr['time_policy_error'] = '当前时段不允许连接';
arr['flux_error'] = '您的流量已超支';
arr['minutes_error'] = '您的时长已超支';
arr['ip_error'] = '您的IP地址不合法';
arr['mac_error'] = '您的MAC地址不合法';
arr['sync_error'] = '您的资料已修改,正在等待同步,请2分钟后再试。';
arr['logout_ok'] = '注销成功,请等1分钟后登录。';
arr['logout_error'] = '您不在线上';

if(text.indexOf("password_error") !== -1) return console.log( arr.password_error );
return console.log( arr[text] );
}

function login( username, password ) {
function encrypt( password, time ) {
var key = ''+ Math.floor(time / 60);
return password.substring(0, 16).split("").map(function(p, i) {
var k = key[ key.length - i%key.length - 1].charCodeAt() ^ p.charCodeAt(),
l = String.fromCharCode( (k&0x0f) + 0x36 ),
h = String.fromCharCode( ((k>>4)&0x0f) + 0x63 );
return i%2 ? h+l : l+h;
}).join("");
}

var serverUrl = 'http://'+server+'/cgi-bin/do_login',
postData = {
username: username,
password: encrypt( password, 12321321),
drop: 0,
type: 10,
n: 117,
pop: 0,
ac_type: "h3c",
mac: ""
};

post( serverUrl, postData, function(data) {
if( data.indexOf("password_error") !== -1 ) {
postData.password = encrypt( password, data.split("@")[1] )
post( serverUrl, postData, info)
}
})

}

function forceLogout() {
var url = "http://"+server+"/cgi-bin/do_logout";
get(url, info)
}

function logout(username, password) {
var serverUrl = 'http://'+server+'/cgi-bin/force_logout',
postData = {
username: username,
password: password,
drop: 0,
type: 10,
n: 117,
pop: 0,
ac_type: "h3c",
mac: ""
};

post( serverUrl, postData, info )
}

function who() {
var url = "http://"+server+"/cgi-bin/keeplive";
get(url, function(data) {
if(data.indexOf("error") !== -1) return console.log("当前无账号登陆");
return console.log( data.split(",").pop() );
})
}

module.exports = {
login: login,
forceLogout: forceLogout,
logout: logout,
who: who
}
33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "srun-cli",
"version": "0.0.1",
"description": "srun cli client",
"main": "./bin/srun.js",
"bin": {
"srun": "./bin/srun.js"
},
"scripts": {
"test": "srun --help"
},
"repository": {
"type": "git",
"url": "git+https://github.com/lizheming/srun-cli.git"
},
"keywords": [
"srun",
"login",
"cli",
"logout",
"network"
],
"author": "lizheming <[email protected]>",
"license": "GPL",
"bugs": {
"url": "https://github.com/lizheming/srun-cli/issues"
},
"homepage": "https://github.com/lizheming/srun-cli#readme",
"dependencies": {
"commander": "^2.8.1",
"prompt": "^0.2.14"
}
}

0 comments on commit a10f3d3

Please sign in to comment.