Session.lua
random_hash
返回 base64 编码的哈希值,使用 _- 作为额外字符,因为它们在 URL 中使用是安全的。
输入
- 大小
- 哈希大小,以位为单位
输出
- 一个至少为size长度的 base64 编码哈希值
- 来自读取 /dev/urandom
代码示例
-- Set variable/Call for this library bobo = require "session" print(bobo.random_hash(100))
将输出一个来自 /dev/urandom 的哈希值,长度为 17 个字符
hash_ip_addr
接受一个 IP 地址字符串并返回一个 HEX 编码的版本。
输入
- 包含 IP 地址的字符串
输出
- HEX 编码的 IP 地址
代码示例
-- Set variable/Call for this library bobo = require "session" print(bobo.hash_ip_addr("192.168.10.1))
输出可能是
c0a80a01
ip_addr_from_hash
接受一个 HEX 编码的 IP 地址并返回一个 IP 地址字符串。
输入
- HEX 编码的 IP 地址
输出
- 包含 IP 地址的字符串
代码示例
-- Set variable/Call for this library bobo = require "session" print(bobo.ip_addr_from_hash("c0a80a01")
输出可能是
192.168.10.1
serialize
接受一个输入变量和名称,并返回一个包含 LUA 代码的字符串,用于生成该变量。
输入
- 名称
- 要序列化的变量/表的名称
- 值
- 要序列化的变量/表的值
- 已保存
- 由递归函数内部使用,以跟踪进度。
输出
- 序列化后的表格字符串
代码示例
-- Set variable/Call for this library bobo = require "session" t = {foo={1,2,3,4}, "one", "two", "bar", "baz"} stuff = bobo.serialize("t",t) print(stuff)
输出
t = {} t[1] = "one" t[2] = "two" t[3] = "bar" t[4] = "baz" t["foo"] = {} t["foo"][1] = 1 t["foo"][2] = 2 t["foo"][3] = 3 t["foo"][4] = 4
save_session
将序列化的用户会话保存到文件。
输入
- sessionpath
- 会话文件要保存的路径
- sessiontable
- 用户会话
输出
- 成功返回 true,错误返回 false
代码示例
-- Set variable/Call for this library bobo = require "session" print(bobo.save_session("tmp", session, sessiontable)
如果成功将打印 true,如果失败将打印 false
load_session
从文件加载序列化的用户会话。
输入
- sessionpath
- 会话文件保存的路径
- session
- 会话 ID
输出
- ts
- 会话保存时的时间戳
- sessiontable
- 用户会话
代码示例
-- Set variable/Call for this library bobo = require "session" ts, sess = bobo.load_session("tmp", "OPRfhPH3rq2p8tpG978oiql8vy58tr9q3ghiUIH")
如果可用,将会话加载到 sess 中。
unlink_session
删除已保存的会话文件。
输入
- sessionpath
- 会话文件保存的路径
- session
- 会话 ID
输出
- 失败返回 nil
代码示例
-- Set variable/Call for this library bobo = require "session" result = bobo.unlink_session("tmp", "OPRfhPH3rq2p8tpG978oiql8vy58tr9q3ghiUIH")
如果会话存在,将删除它。
record_event
通过创建一个零长度文件来记录无效的登录尝试。
输入
- sessionpath
- 事件文件要保存的路径
- id_u
- 第一个 ID 字符串,通常是用户名
- id_ip
- 第二个 ID 字符串,通常是哈希 IP 地址
输出
- 无
代码示例
-- Set variable/Call for this library bobo = require "session" result = bobo.record_event("tmp", username, hash_ip_addr(ipaddr))
将为此用户和 IP 地址创建一个事件文件。
count_events
统计在过去 30 分钟内,此 ID 发生了多少次无效用户尝试。
输入
- sessionpath
- 事件文件保存的路径
- id_u
- 第一个 ID 字符串,通常是用户名
- id_ip
- 第二个 ID 字符串,通常是哈希 IP 地址
输出
- blockaccess
- 如果超过 10 个事件,则为 True。
代码示例
-- Set variable/Call for this library bobo = require "session" print(bobo.count_events("tmp", username, hash_ip_addr(ipaddr)))
如果超过 10 个锁定事件,则输出 true。
expired_events
清除超过 30 分钟的无效用户尝试和已保存的用户会话。
输入
- sessionpath
- 事件文件保存的路径
代码示例
-- Set variable/Call for this library bobo = require "session" bobo.expired_events("tmp")
删除超过 30 分钟的事件和会话。