Skip to content

Instantly share code, notes, and snippets.

@citrus-lemon
Created April 20, 2018 01:40
Show Gist options
  • Save citrus-lemon/6f58d23579ef0ad44be3836cf740f980 to your computer and use it in GitHub Desktop.
Save citrus-lemon/6f58d23579ef0ad44be3836cf740f980 to your computer and use it in GitHub Desktop.
auto login XJTU student network
#!/usr/bin/env lua
-- login.lua auto login XJTU student network
-- Prerequisite: luasocket, luaposix
info = {
username = 'THIS IS USERNAME';
password = 'THIS IS PASSWORD';
}
string.split = function (pString, pPattern)
local Table = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pPattern
local last_end = 1
local s, e, cap = pString:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(Table,cap)
end
last_end = e+1
s, e, cap = pString:find(fpat, last_end)
end
if last_end <= #pString then
cap = pString:sub(last_end)
table.insert(Table, cap)
end
return Table
end
NETWORK_ERROR = 0
STATUS_NOT_ONLINE = 1
AUTHENTICATION_FAILED = 2
PASSWORD_ERROR = 3
UNKNOWN_ERROR = 8
SUCCESS = 16
LOGIN_SUCCESS = 17
LOGOUT_SUCCESS = 18
LOGOUT_INVAILD = 19
statustable = {
[LOGIN_SUCCESS] = 'Login Success';
[NETWORK_ERROR] = 'Network error';
[AUTHENTICATION_FAILED] = 'Authentication failed';
[PASSWORD_ERROR] = 'Password Error';
[STATUS_NOT_ONLINE] = 'Status not Online';
}
status = function()
local http = require("socket.http")
local ltn12 = require("ltn12")
local response_body = {}
local r, c, h = http.request {
url = 'http://10.6.8.2/include/auth_action.php?action=get_online_info';
method = 'POST';
sink = ltn12.sink.table(response_body)
}
if not r then
return false, NETWORK_ERROR
elseif response_body[1] == "not_online" then
return false, STATUS_NOT_ONLINE
else
local result = string.split(response_body[1],',')
return true, result
end
end
login = function(user, pass)
local http = require("socket.http")
local ltn12 = require("ltn12")
local request_body = 'action=login&username='..user..'&password='..pass..'&ac_id=1&user_ip=&nas_ip=&user_mac=&save_me=0&ajax=1'
local response_body = {}
local res, code, headers = http.request {
url = 'http://10.6.8.2/include/auth_action.php?action=get_online_info';
method = 'POST';
headers = {
["Content-Type"] = "application/x-www-form-urlencoded";
["Content-Length"] = #request_body;
};
source = ltn12.source.string(request_body);
sink = ltn12.sink.table(response_body);
}
-- "E2901: (Third party -1)Authentication failed, but no error message is returned.()"
if not res then
return NETWORK_ERROR
elseif not code == 200 then
return UNKNOWN_ERROR
elseif string.find(response_body[1], "E2901") then
return AUTHENTICATION_FAILED
elseif string.find(response_body[1], "login_ok") then
return LOGIN_SUCCESS
else
return response_body[1]
end
end
logout = function(user, pass)
local http = require("socket.http")
local ltn12 = require("ltn12")
local request_body = 'action=logout&username='..user..'&password='..pass..'&ajax=1'
local response_body = {}
local res, code, headers = http.request {
url = 'http://10.6.8.2/include/auth_action.php?action=get_online_info';
method = 'POST';
headers = {
["Content-Type"] = "application/x-www-form-urlencoded";
["Content-Length"] = #request_body;
};
source = ltn12.source.string(request_body);
sink = ltn12.sink.table(response_body);
}
-- "您似乎未曾连接到网络..."
-- "网络已断开"
-- "注销失败:Password is error."
if not res then
return NETWORK_ERROR
elseif not code == 200 then
return UNKNOWN_ERROR
elseif string.find(response_body[1], "E2901") then
return AUTHENTICATION_FAILED
elseif response_body[1] == "您似乎未曾连接到网络..." then
return LOGOUT_INVAILD
elseif response_body[1] == "注销失败:Password is error." then
return PASSWORD_ERROR
elseif response_body[1] == "网络已断开" then
return LOGOUT_SUCCESS
else
return response_body[1]
end
end
loop = function()
local socket = require('socket')
while true do
local c, e = status()
if c then
socket.select(nil, nil, 1000)
else
if e == NETWORK_ERROR then
socket.select(nil, nil, 1)
else
local s = login(info.username, info.password)
if not s == LOGIN_SUCCESS then
socket.select(nil, nil, 15)
else
socket.select(nil, nil, 2000)
end
end
end
end
end
savepid = function()
local posix = require('posix')
file = io.open("/var/run/login-lua.pid", "w")
if not file then
file = io.open("/tmp/login-lua.pid", "w")
end
if not file then
print('daemon start error...')
os.exit()
end
io.output(file)
io.write(tostring(type(posix.getpid()) == 'table' and posix.getpid().pid or posix.getpid()))
io.close(file)
end
if arg[1] == 'daemon' then
local posix = require("posix")
pid = posix.fork()
if pid == 0 then
print("PID: " .. tostring(type(posix.getpid()) == 'table' and posix.getpid().pid or posix.getpid()))
savepid()
loop()
else
os.exit()
end
else
local s = login(info.username, info.password)
print(statustable[s] or s)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment