mirror of
https://github.com/akyaiy/GoSally-mvp.git
synced 2026-01-03 20:12:25 +00:00
Compare commits
4 Commits
5b32698ec5
...
c734779b69
| Author | SHA1 | Date | |
|---|---|---|---|
| c734779b69 | |||
| 0923f32b46 | |||
| 1c2c4c1356 | |||
| d3eb483461 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -6,6 +6,7 @@ tmp/
|
|||||||
db/
|
db/
|
||||||
|
|
||||||
com/test.lua
|
com/test.lua
|
||||||
|
com/_config.lua
|
||||||
|
|
||||||
.vscode
|
.vscode
|
||||||
Taskfile.yml
|
Taskfile.yml
|
||||||
|
|||||||
96
com/Auth/DeleteUnit.lua
Normal file
96
com/Auth/DeleteUnit.lua
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
-- com/DeleteUnit.lua
|
||||||
|
|
||||||
|
---@diagnostic disable: redefined-local
|
||||||
|
local db = require("internal.database-sqlite").connect("db/user-database.db", {log = true})
|
||||||
|
local log = require("internal.log")
|
||||||
|
local session = require("internal.session")
|
||||||
|
local crypt = require("internal.crypt.bcrypt")
|
||||||
|
|
||||||
|
local function close_db()
|
||||||
|
if db then
|
||||||
|
db:close()
|
||||||
|
db = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function error_response(message, code, data)
|
||||||
|
session.response.error = {
|
||||||
|
code = code or nil,
|
||||||
|
message = message,
|
||||||
|
data = data or nil
|
||||||
|
}
|
||||||
|
close_db()
|
||||||
|
end
|
||||||
|
|
||||||
|
if not session.request.params then
|
||||||
|
return error_response("no params provided")
|
||||||
|
end
|
||||||
|
|
||||||
|
if not session.request.params.token then
|
||||||
|
return error_response("access denied")
|
||||||
|
end
|
||||||
|
|
||||||
|
if session.request.params.token ~= require("_config").token() then
|
||||||
|
return error_response("access denied")
|
||||||
|
end
|
||||||
|
|
||||||
|
local params = session.request.params
|
||||||
|
|
||||||
|
if not (params.username and params.email and params.password) then
|
||||||
|
return error_response("no username/email/password provided")
|
||||||
|
end
|
||||||
|
|
||||||
|
local existing, err = db:query(
|
||||||
|
"SELECT password FROM users WHERE email = ? AND username = ? AND deleted = 0 LIMIT 1",
|
||||||
|
{
|
||||||
|
params.email,
|
||||||
|
params.username
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
if err ~= nil then
|
||||||
|
log.error("Password fetch failed: " .. tostring(err))
|
||||||
|
return error_response("Database query failed: " .. tostring(err))
|
||||||
|
end
|
||||||
|
|
||||||
|
if not existing or #existing == 0 then
|
||||||
|
return error_response("Unit not found")
|
||||||
|
end
|
||||||
|
|
||||||
|
local hashed_password = existing[1].password
|
||||||
|
|
||||||
|
local ok = crypt.compare(hashed_password, params.password)
|
||||||
|
if not ok then
|
||||||
|
log.warn("Wrong password attempt for: " .. params.username)
|
||||||
|
return error_response("Invalid password")
|
||||||
|
end
|
||||||
|
|
||||||
|
local ctx, err = db:exec(
|
||||||
|
[[
|
||||||
|
UPDATE users
|
||||||
|
SET deleted = 1,
|
||||||
|
deleted_at = CURRENT_TIMESTAMP
|
||||||
|
WHERE email = ? AND username = ? AND deleted = 0
|
||||||
|
]],
|
||||||
|
{ params.email, params.username }
|
||||||
|
)
|
||||||
|
|
||||||
|
if err ~= nil then
|
||||||
|
log.error("Soft delete failed: " .. tostring(err))
|
||||||
|
return error_response("Soft delete failed: " .. tostring(err))
|
||||||
|
end
|
||||||
|
|
||||||
|
local res, err = ctx:wait()
|
||||||
|
if err ~= nil then
|
||||||
|
log.error("Soft delete confirmation failed: " .. tostring(err))
|
||||||
|
return error_response("Soft delete confirmation failed: " .. tostring(err))
|
||||||
|
end
|
||||||
|
|
||||||
|
session.response.result = {
|
||||||
|
rows_affected = res,
|
||||||
|
message = "Unit soft-deleted successfully"
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("user " .. params.username .. " soft-deleted successfully")
|
||||||
|
|
||||||
|
close_db()
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
-- com/GetAccess
|
||||||
|
|
||||||
---@diagnostic disable: redefined-local
|
---@diagnostic disable: redefined-local
|
||||||
local db = require("internal.database-sqlite").connect("db/user-database.db", {log = true})
|
local db = require("internal.database-sqlite").connect("db/user-database.db", {log = true})
|
||||||
local log = require("internal.log")
|
local log = require("internal.log")
|
||||||
@@ -13,7 +15,7 @@ end
|
|||||||
|
|
||||||
local function error_response(message, code, data)
|
local function error_response(message, code, data)
|
||||||
session.response.error = {
|
session.response.error = {
|
||||||
code = code or -32600,
|
code = code or nil,
|
||||||
message = message,
|
message = message,
|
||||||
data = data or nil
|
data = data or nil
|
||||||
}
|
}
|
||||||
@@ -25,13 +27,24 @@ if not params then
|
|||||||
return error_response("No params provided")
|
return error_response("No params provided")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if not session.request.params.token then
|
||||||
|
return error_response("access denied")
|
||||||
|
end
|
||||||
|
|
||||||
|
if session.request.params.token ~= require("_config").token() then
|
||||||
|
return error_response("access denied")
|
||||||
|
end
|
||||||
|
|
||||||
if not (params.username and params.email and params.password) then
|
if not (params.username and params.email and params.password) then
|
||||||
return error_response("Missing username, email or password", -32602)
|
return error_response("Missing username, email or password")
|
||||||
end
|
end
|
||||||
|
|
||||||
local unit, err = db:query(
|
local unit, err = db:query(
|
||||||
"SELECT id, username, email, password, created_at FROM users WHERE email = ? AND username = ? LIMIT 1",
|
"SELECT id, username, email, password, created_at FROM users WHERE email = ? AND username = ? AND deleted = 0 LIMIT 1",
|
||||||
{params.email, params.username}
|
{
|
||||||
|
params.email,
|
||||||
|
params.username
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
if err then
|
if err then
|
||||||
@@ -40,7 +53,7 @@ if err then
|
|||||||
end
|
end
|
||||||
|
|
||||||
if not unit or #unit == 0 then
|
if not unit or #unit == 0 then
|
||||||
return error_response("Unit not found", -32604)
|
return error_response("Unit not found")
|
||||||
end
|
end
|
||||||
|
|
||||||
unit = unit[1]
|
unit = unit[1]
|
||||||
@@ -48,7 +61,7 @@ unit = unit[1]
|
|||||||
local ok = crypt.compare(unit.password, params.password)
|
local ok = crypt.compare(unit.password, params.password)
|
||||||
if not ok then
|
if not ok then
|
||||||
log.warn("Login failed: wrong password for " .. params.username)
|
log.warn("Login failed: wrong password for " .. params.username)
|
||||||
return error_response("Invalid password", -32605)
|
return error_response("Invalid password")
|
||||||
end
|
end
|
||||||
|
|
||||||
session.response.result = {
|
session.response.result = {
|
||||||
|
|||||||
@@ -1,77 +0,0 @@
|
|||||||
---@diagnostic disable: redefined-local
|
|
||||||
local db = require("internal.database-sqlite").connect("db/user-database.db", {log = true})
|
|
||||||
local log = require("internal.log")
|
|
||||||
local session = require("internal.session")
|
|
||||||
local crypt = require("internal.crypt.bcrypt")
|
|
||||||
|
|
||||||
if not session.request.params then
|
|
||||||
session.response.error = {
|
|
||||||
message = "no params provided"
|
|
||||||
}
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local params = session.request.params
|
|
||||||
|
|
||||||
if not (params.username and params.email and params.password) then
|
|
||||||
session.response.error = {
|
|
||||||
message = "no username/email/password provided"
|
|
||||||
}
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local hashPass = crypt.generate(params.password, crypt.DefaultCost)
|
|
||||||
|
|
||||||
local existing, err = db:query("SELECT 1 FROM users WHERE email = ? OR username = ? LIMIT 1", {
|
|
||||||
params.email,
|
|
||||||
params.username
|
|
||||||
})
|
|
||||||
if err ~= nil then
|
|
||||||
session.response.error = {
|
|
||||||
message = "Database check failed: "..tostring(err)
|
|
||||||
}
|
|
||||||
log.error("Email check failed: "..tostring(err))
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if existing and #existing > 0 then
|
|
||||||
session.response.error = {
|
|
||||||
code = -32604,
|
|
||||||
message = "Unit already exists"
|
|
||||||
}
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local ctx, err = db:exec(
|
|
||||||
"INSERT INTO users (username, email, password) VALUES (?, ?, ?)",
|
|
||||||
{
|
|
||||||
params.username,
|
|
||||||
params.email,
|
|
||||||
hashPass
|
|
||||||
}
|
|
||||||
)
|
|
||||||
if err ~= nil then
|
|
||||||
session.response.error = {
|
|
||||||
code = -32605,
|
|
||||||
message = "Insert failed: "..tostring(err)
|
|
||||||
}
|
|
||||||
log.error("Insert failed: "..tostring(err))
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local res, err = ctx:wait()
|
|
||||||
if err ~= nil then
|
|
||||||
session.response.error = {
|
|
||||||
code = -32606,
|
|
||||||
message = "Insert confirmation failed: "..tostring(err)
|
|
||||||
}
|
|
||||||
log.error("Insert confirmation failed: "..tostring(err))
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
session.response.result = {
|
|
||||||
rows_affected = res,
|
|
||||||
message = "Unit created successfully"
|
|
||||||
}
|
|
||||||
|
|
||||||
db:close()
|
|
||||||
86
com/Auth/PutNewUnit.lua
Normal file
86
com/Auth/PutNewUnit.lua
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
-- com/PutNewUnit.lua
|
||||||
|
|
||||||
|
---@diagnostic disable: redefined-local
|
||||||
|
local db = require("internal.database-sqlite").connect("db/user-database.db", {log = true})
|
||||||
|
local log = require("internal.log")
|
||||||
|
local session = require("internal.session")
|
||||||
|
local crypt = require("internal.crypt.bcrypt")
|
||||||
|
|
||||||
|
local function close_db()
|
||||||
|
if db then
|
||||||
|
db:close()
|
||||||
|
db = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function error_response(message, code, data)
|
||||||
|
session.response.error = {
|
||||||
|
code = code or nil,
|
||||||
|
message = message,
|
||||||
|
data = data or nil
|
||||||
|
}
|
||||||
|
close_db()
|
||||||
|
end
|
||||||
|
|
||||||
|
if not session.request.params then
|
||||||
|
return error_response("no params provided")
|
||||||
|
end
|
||||||
|
|
||||||
|
if not session.request.params.token then
|
||||||
|
return error_response("access denied")
|
||||||
|
end
|
||||||
|
|
||||||
|
if session.request.params.token ~= require("_config").token() then
|
||||||
|
return error_response("access denied")
|
||||||
|
end
|
||||||
|
|
||||||
|
local params = session.request.params
|
||||||
|
if not (params.username and params.email and params.password) then
|
||||||
|
return error_response("no username/email/password provided")
|
||||||
|
end
|
||||||
|
|
||||||
|
local hashPass = crypt.generate(params.password, crypt.DefaultCost)
|
||||||
|
|
||||||
|
local existing, err = db:query("SELECT 1 FROM users WHERE deleted = 0 AND (email = ? OR username = ? OR phone_number = ?) LIMIT 1", {
|
||||||
|
params.email,
|
||||||
|
params.username,
|
||||||
|
params.phone_number
|
||||||
|
})
|
||||||
|
|
||||||
|
if err ~= nil then
|
||||||
|
log.error("Email check failed: "..tostring(err))
|
||||||
|
return error_response("Database check failed: "..tostring(err))
|
||||||
|
end
|
||||||
|
|
||||||
|
if existing and #existing > 0 then
|
||||||
|
return error_response("Unit already exists")
|
||||||
|
end
|
||||||
|
|
||||||
|
local ctx, err = db:exec(
|
||||||
|
"INSERT INTO users (username, email, password, first_name, last_name, phone_number) VALUES (?, ?, ?, ?, ?, ?)",
|
||||||
|
{
|
||||||
|
params.username,
|
||||||
|
params.email,
|
||||||
|
hashPass,
|
||||||
|
params.first_name or "",
|
||||||
|
params.last_name or "",
|
||||||
|
params.phone_number or ""
|
||||||
|
}
|
||||||
|
)
|
||||||
|
if err ~= nil then
|
||||||
|
log.error("Insert failed: "..tostring(err))
|
||||||
|
return error_response("Insert failed: "..tostring(err))
|
||||||
|
end
|
||||||
|
|
||||||
|
local res, err = ctx:wait()
|
||||||
|
if err ~= nil then
|
||||||
|
log.error("Insert confirmation failed: "..tostring(err))
|
||||||
|
return error_response("Insert confirmation failed: "..tostring(err))
|
||||||
|
end
|
||||||
|
|
||||||
|
session.response.result = {
|
||||||
|
rows_affected = res,
|
||||||
|
message = "Unit created successfully"
|
||||||
|
}
|
||||||
|
|
||||||
|
close_db()
|
||||||
@@ -162,7 +162,7 @@ func dbExec(L *lua.LState) int {
|
|||||||
var result lua.LValue = lua.LNil
|
var result lua.LValue = lua.LNil
|
||||||
var errorMsg lua.LValue = lua.LNil
|
var errorMsg lua.LValue = lua.LNil
|
||||||
|
|
||||||
L.SetField(ctx, "wait", L.NewFunction(func(lL *lua.LState) int {
|
L.SetField(ctx, "wait", L.NewFunction(func(L *lua.LState) int {
|
||||||
res := <-resCh
|
res := <-resCh
|
||||||
L.SetField(ctx, "done", lua.LBool(true))
|
L.SetField(ctx, "done", lua.LBool(true))
|
||||||
|
|
||||||
@@ -175,35 +175,35 @@ func dbExec(L *lua.LState) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if res.err != nil {
|
if res.err != nil {
|
||||||
lL.Push(lua.LNil)
|
L.Push(lua.LNil)
|
||||||
lL.Push(lua.LString(res.err.Error()))
|
L.Push(lua.LString(res.err.Error()))
|
||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
lL.Push(lua.LNumber(res.rowsAffected))
|
L.Push(lua.LNumber(res.rowsAffected))
|
||||||
lL.Push(lua.LNil)
|
L.Push(lua.LNil)
|
||||||
return 2
|
return 2
|
||||||
}))
|
}))
|
||||||
|
|
||||||
L.SetField(ctx, "check", L.NewFunction(func(lL *lua.LState) int {
|
L.SetField(ctx, "check", L.NewFunction(func(L *lua.LState) int {
|
||||||
select {
|
select {
|
||||||
case res := <-resCh:
|
case res := <-resCh:
|
||||||
lL.SetField(ctx, "done", lua.LBool(true))
|
L.SetField(ctx, "done", lua.LBool(true))
|
||||||
if res.err != nil {
|
if res.err != nil {
|
||||||
errorMsg = lua.LString(res.err.Error())
|
errorMsg = lua.LString(res.err.Error())
|
||||||
result = lua.LNil
|
result = lua.LNil
|
||||||
lL.Push(lua.LNil)
|
L.Push(lua.LNil)
|
||||||
lL.Push(lua.LString(res.err.Error()))
|
L.Push(lua.LString(res.err.Error()))
|
||||||
return 2
|
return 2
|
||||||
} else {
|
} else {
|
||||||
result = lua.LNumber(res.rowsAffected)
|
result = lua.LNumber(res.rowsAffected)
|
||||||
errorMsg = lua.LNil
|
errorMsg = lua.LNil
|
||||||
lL.Push(lua.LNumber(res.rowsAffected))
|
L.Push(lua.LNumber(res.rowsAffected))
|
||||||
lL.Push(lua.LNil)
|
L.Push(lua.LNil)
|
||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
lL.Push(result)
|
L.Push(result)
|
||||||
lL.Push(errorMsg)
|
L.Push(errorMsg)
|
||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|||||||
@@ -48,36 +48,74 @@ func (h *HandlerV1) handleLUA(sid string, r *http.Request, req *rpc.RPCRequest,
|
|||||||
|
|
||||||
seed := rand.Int()
|
seed := rand.Int()
|
||||||
|
|
||||||
loadSessionMod := func(lL *lua.LState) int {
|
loadSessionMod := func(L *lua.LState) int {
|
||||||
llog.Debug("import module session", slog.String("script", path))
|
llog.Debug("import module session", slog.String("script", path))
|
||||||
sessionMod := lL.NewTable()
|
sessionMod := L.NewTable()
|
||||||
inTable := lL.NewTable()
|
inTable := L.NewTable()
|
||||||
paramsTable := lL.NewTable()
|
paramsTable := L.NewTable()
|
||||||
|
|
||||||
|
fetchedParamsTable := L.NewTable()
|
||||||
if fetchedParams, ok := req.Params.(map[string]any); ok {
|
if fetchedParams, ok := req.Params.(map[string]any); ok {
|
||||||
for k, v := range fetchedParams {
|
for k, v := range fetchedParams {
|
||||||
lL.SetField(paramsTable, k, ConvertGolangTypesToLua(lL, v))
|
L.SetField(fetchedParamsTable, k, ConvertGolangTypesToLua(L, v))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lL.SetField(inTable, "params", paramsTable)
|
|
||||||
|
getter := L.NewFunction(func(L *lua.LState) int {
|
||||||
|
path := L.OptString(1, "")
|
||||||
|
def := L.Get(2)
|
||||||
|
|
||||||
outTable := lL.NewTable()
|
get := func(tbl *lua.LTable, path string) lua.LValue {
|
||||||
resultTable := lL.NewTable()
|
if path == "" {
|
||||||
lL.SetField(outTable, "result", resultTable)
|
return tbl
|
||||||
|
}
|
||||||
|
current := tbl
|
||||||
|
parts := strings.Split(path, ".")
|
||||||
|
size := len(parts)
|
||||||
|
for index, key := range parts {
|
||||||
|
val := current.RawGetString(key)
|
||||||
|
if tblVal, ok := val.(*lua.LTable); ok {
|
||||||
|
current = tblVal
|
||||||
|
} else {
|
||||||
|
if index == size - 1 {
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
return lua.LNil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lua.LNil
|
||||||
|
}
|
||||||
|
|
||||||
|
val := get(fetchedParamsTable, path)
|
||||||
|
if val == lua.LNil && def != lua.LNil {
|
||||||
|
L.Push(def)
|
||||||
|
} else {
|
||||||
|
L.Push(val)
|
||||||
|
}
|
||||||
|
return 1
|
||||||
|
})
|
||||||
|
|
||||||
lL.SetField(inTable, "address", lua.LString(r.RemoteAddr))
|
L.SetField(paramsTable, "get", getter)
|
||||||
lL.SetField(sessionMod, "request", inTable)
|
L.SetField(inTable, "params", paramsTable)
|
||||||
lL.SetField(sessionMod, "response", outTable)
|
|
||||||
|
|
||||||
lL.SetField(sessionMod, "id", lua.LString(sid))
|
outTable := L.NewTable()
|
||||||
|
resultTable := L.NewTable()
|
||||||
|
L.SetField(outTable, "result", resultTable)
|
||||||
|
|
||||||
lL.SetField(sessionMod, "__gosally_internal", lua.LString(fmt.Sprint(seed)))
|
L.SetField(inTable, "address", lua.LString(r.RemoteAddr))
|
||||||
lL.Push(sessionMod)
|
L.SetField(sessionMod, "request", inTable)
|
||||||
|
L.SetField(sessionMod, "response", outTable)
|
||||||
|
|
||||||
|
L.SetField(sessionMod, "id", lua.LString(sid))
|
||||||
|
|
||||||
|
L.SetField(sessionMod, "__gosally_internal", lua.LString(fmt.Sprint(seed)))
|
||||||
|
L.Push(sessionMod)
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
loadLogMod := func(lL *lua.LState) int {
|
loadLogMod := func(L *lua.LState) int {
|
||||||
llog.Debug("import module log", slog.String("script", path))
|
llog.Debug("import module log", slog.String("script", path))
|
||||||
logMod := lL.NewTable()
|
logMod := L.NewTable()
|
||||||
|
|
||||||
logFuncs := map[string]func(string, ...any){
|
logFuncs := map[string]func(string, ...any){
|
||||||
"info": llog.Info,
|
"info": llog.Info,
|
||||||
@@ -88,8 +126,8 @@ func (h *HandlerV1) handleLUA(sid string, r *http.Request, req *rpc.RPCRequest,
|
|||||||
|
|
||||||
for name, logFunc := range logFuncs {
|
for name, logFunc := range logFuncs {
|
||||||
fun := logFunc
|
fun := logFunc
|
||||||
lL.SetField(logMod, name, lL.NewFunction(func(lL *lua.LState) int {
|
L.SetField(logMod, name, L.NewFunction(func(L *lua.LState) int {
|
||||||
msg := lL.Get(1)
|
msg := L.Get(1)
|
||||||
converted := ConvertLuaTypesToGolang(msg)
|
converted := ConvertLuaTypesToGolang(msg)
|
||||||
fun(fmt.Sprintf("the script says: %s", converted), slog.String("script", path))
|
fun(fmt.Sprintf("the script says: %s", converted), slog.String("script", path))
|
||||||
return 0
|
return 0
|
||||||
@@ -105,8 +143,8 @@ func (h *HandlerV1) handleLUA(sid string, r *http.Request, req *rpc.RPCRequest,
|
|||||||
{"event_error", h.x.Log.Printf, colors.PrintError},
|
{"event_error", h.x.Log.Printf, colors.PrintError},
|
||||||
{"event_warn", h.x.Log.Printf, colors.PrintWarn},
|
{"event_warn", h.x.Log.Printf, colors.PrintWarn},
|
||||||
} {
|
} {
|
||||||
lL.SetField(logMod, fn.field, lL.NewFunction(func(lL *lua.LState) int {
|
L.SetField(logMod, fn.field, L.NewFunction(func(L *lua.LState) int {
|
||||||
msg := lL.Get(1)
|
msg := L.Get(1)
|
||||||
converted := ConvertLuaTypesToGolang(msg)
|
converted := ConvertLuaTypesToGolang(msg)
|
||||||
if fn.color != nil {
|
if fn.color != nil {
|
||||||
h.x.Log.Printf("%s: %s: %s", fn.color(), path, converted)
|
h.x.Log.Printf("%s: %s: %s", fn.color(), path, converted)
|
||||||
@@ -117,24 +155,24 @@ func (h *HandlerV1) handleLUA(sid string, r *http.Request, req *rpc.RPCRequest,
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
lL.SetField(logMod, "__gosally_internal", lua.LString(fmt.Sprint(seed)))
|
L.SetField(logMod, "__gosally_internal", lua.LString(fmt.Sprint(seed)))
|
||||||
lL.Push(logMod)
|
L.Push(logMod)
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
loadNetMod := func(lL *lua.LState) int {
|
loadNetMod := func(L *lua.LState) int {
|
||||||
llog.Debug("import module net", slog.String("script", path))
|
llog.Debug("import module net", slog.String("script", path))
|
||||||
netMod := lL.NewTable()
|
netMod := L.NewTable()
|
||||||
netModhttp := lL.NewTable()
|
netModhttp := L.NewTable()
|
||||||
|
|
||||||
lL.SetField(netModhttp, "get_request", lL.NewFunction(func(lL *lua.LState) int {
|
L.SetField(netModhttp, "get_request", L.NewFunction(func(L *lua.LState) int {
|
||||||
logRequest := lL.ToBool(1)
|
logRequest := L.ToBool(1)
|
||||||
url := lL.ToString(2)
|
url := L.ToString(2)
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lL.Push(lua.LNil)
|
L.Push(lua.LNil)
|
||||||
lL.Push(lua.LString(err.Error()))
|
L.Push(lua.LString(err.Error()))
|
||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,16 +181,16 @@ func (h *HandlerV1) handleLUA(sid string, r *http.Request, req *rpc.RPCRequest,
|
|||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lL.Push(lua.LNil)
|
L.Push(lua.LNil)
|
||||||
lL.Push(lua.LString(err.Error()))
|
L.Push(lua.LString(err.Error()))
|
||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lL.Push(lua.LNil)
|
L.Push(lua.LNil)
|
||||||
lL.Push(lua.LString(err.Error()))
|
L.Push(lua.LString(err.Error()))
|
||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,34 +204,34 @@ func (h *HandlerV1) handleLUA(sid string, r *http.Request, req *rpc.RPCRequest,
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
result := lL.NewTable()
|
result := L.NewTable()
|
||||||
lL.SetField(result, "status", lua.LNumber(resp.StatusCode))
|
L.SetField(result, "status", lua.LNumber(resp.StatusCode))
|
||||||
lL.SetField(result, "status_text", lua.LString(resp.Status))
|
L.SetField(result, "status_text", lua.LString(resp.Status))
|
||||||
lL.SetField(result, "body", lua.LString(body))
|
L.SetField(result, "body", lua.LString(body))
|
||||||
lL.SetField(result, "content_length", lua.LNumber(resp.ContentLength))
|
L.SetField(result, "content_length", lua.LNumber(resp.ContentLength))
|
||||||
|
|
||||||
headers := lL.NewTable()
|
headers := L.NewTable()
|
||||||
for k, v := range resp.Header {
|
for k, v := range resp.Header {
|
||||||
lL.SetField(headers, k, ConvertGolangTypesToLua(lL, v))
|
L.SetField(headers, k, ConvertGolangTypesToLua(L, v))
|
||||||
}
|
}
|
||||||
lL.SetField(result, "headers", headers)
|
L.SetField(result, "headers", headers)
|
||||||
|
|
||||||
lL.Push(result)
|
L.Push(result)
|
||||||
return 1
|
return 1
|
||||||
}))
|
}))
|
||||||
|
|
||||||
lL.SetField(netModhttp, "post_request", lL.NewFunction(func(lL *lua.LState) int {
|
L.SetField(netModhttp, "post_request", L.NewFunction(func(L *lua.LState) int {
|
||||||
logRequest := lL.ToBool(1)
|
logRequest := L.ToBool(1)
|
||||||
url := lL.ToString(2)
|
url := L.ToString(2)
|
||||||
contentType := lL.ToString(3)
|
contentType := L.ToString(3)
|
||||||
payload := lL.ToString(4)
|
payload := L.ToString(4)
|
||||||
|
|
||||||
body := strings.NewReader(payload)
|
body := strings.NewReader(payload)
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", url, body)
|
req, err := http.NewRequest("POST", url, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lL.Push(lua.LNil)
|
L.Push(lua.LNil)
|
||||||
lL.Push(lua.LString(err.Error()))
|
L.Push(lua.LString(err.Error()))
|
||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,16 +242,16 @@ func (h *HandlerV1) handleLUA(sid string, r *http.Request, req *rpc.RPCRequest,
|
|||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lL.Push(lua.LNil)
|
L.Push(lua.LNil)
|
||||||
lL.Push(lua.LString(err.Error()))
|
L.Push(lua.LString(err.Error()))
|
||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
respBody, err := io.ReadAll(resp.Body)
|
respBody, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lL.Push(lua.LNil)
|
L.Push(lua.LNil)
|
||||||
lL.Push(lua.LString(err.Error()))
|
L.Push(lua.LString(err.Error()))
|
||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -228,47 +266,47 @@ func (h *HandlerV1) handleLUA(sid string, r *http.Request, req *rpc.RPCRequest,
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
result := lL.NewTable()
|
result := L.NewTable()
|
||||||
lL.SetField(result, "status", lua.LNumber(resp.StatusCode))
|
L.SetField(result, "status", lua.LNumber(resp.StatusCode))
|
||||||
lL.SetField(result, "status_text", lua.LString(resp.Status))
|
L.SetField(result, "status_text", lua.LString(resp.Status))
|
||||||
lL.SetField(result, "body", lua.LString(respBody))
|
L.SetField(result, "body", lua.LString(respBody))
|
||||||
lL.SetField(result, "content_length", lua.LNumber(resp.ContentLength))
|
L.SetField(result, "content_length", lua.LNumber(resp.ContentLength))
|
||||||
|
|
||||||
headers := lL.NewTable()
|
headers := L.NewTable()
|
||||||
for k, v := range resp.Header {
|
for k, v := range resp.Header {
|
||||||
lL.SetField(headers, k, ConvertGolangTypesToLua(lL, v))
|
L.SetField(headers, k, ConvertGolangTypesToLua(L, v))
|
||||||
}
|
}
|
||||||
lL.SetField(result, "headers", headers)
|
L.SetField(result, "headers", headers)
|
||||||
|
|
||||||
lL.Push(result)
|
L.Push(result)
|
||||||
return 1
|
return 1
|
||||||
}))
|
}))
|
||||||
|
|
||||||
lL.SetField(netMod, "http", netModhttp)
|
L.SetField(netMod, "http", netModhttp)
|
||||||
|
|
||||||
lL.SetField(netMod, "__gosally_internal", lua.LString(fmt.Sprint(seed)))
|
L.SetField(netMod, "__gosally_internal", lua.LString(fmt.Sprint(seed)))
|
||||||
lL.Push(netMod)
|
L.Push(netMod)
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
loadCryptbcryptMod := func(lL *lua.LState) int {
|
loadCryptbcryptMod := func(L *lua.LState) int {
|
||||||
llog.Debug("import module crypt.bcrypt", slog.String("script", path))
|
llog.Debug("import module crypt.bcrypt", slog.String("script", path))
|
||||||
bcryptMod := lL.NewTable()
|
bcryptMod := L.NewTable()
|
||||||
|
|
||||||
lL.SetField(bcryptMod, "MinCost", lua.LNumber(bcrypt.MinCost))
|
L.SetField(bcryptMod, "MinCost", lua.LNumber(bcrypt.MinCost))
|
||||||
lL.SetField(bcryptMod, "MaxCost", lua.LNumber(bcrypt.MaxCost))
|
L.SetField(bcryptMod, "MaxCost", lua.LNumber(bcrypt.MaxCost))
|
||||||
lL.SetField(bcryptMod, "DefaultCost", lua.LNumber(bcrypt.DefaultCost))
|
L.SetField(bcryptMod, "DefaultCost", lua.LNumber(bcrypt.DefaultCost))
|
||||||
|
|
||||||
lL.SetField(bcryptMod, "generate", lL.NewFunction(func(l *lua.LState) int {
|
L.SetField(bcryptMod, "generate", L.NewFunction(func(l *lua.LState) int {
|
||||||
password := ConvertLuaTypesToGolang(lL.Get(1))
|
password := ConvertLuaTypesToGolang(L.Get(1))
|
||||||
passwordStr, ok := password.(string)
|
passwordStr, ok := password.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
lL.Push(lua.LNil)
|
L.Push(lua.LNil)
|
||||||
lL.Push(lua.LString("error: password must be a string"))
|
L.Push(lua.LString("error: password must be a string"))
|
||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
|
|
||||||
cost := ConvertLuaTypesToGolang(lL.Get(2))
|
cost := ConvertLuaTypesToGolang(L.Get(2))
|
||||||
costInt := bcrypt.DefaultCost
|
costInt := bcrypt.DefaultCost
|
||||||
switch v := cost.(type) {
|
switch v := cost.(type) {
|
||||||
case int:
|
case int:
|
||||||
@@ -278,48 +316,48 @@ func (h *HandlerV1) handleLUA(sid string, r *http.Request, req *rpc.RPCRequest,
|
|||||||
case nil:
|
case nil:
|
||||||
// ok, use DefaultCost
|
// ok, use DefaultCost
|
||||||
default:
|
default:
|
||||||
lL.Push(lua.LNil)
|
L.Push(lua.LNil)
|
||||||
lL.Push(lua.LString("error: cost must be an integer"))
|
L.Push(lua.LString("error: cost must be an integer"))
|
||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
|
|
||||||
hashBytes, err := bcrypt.GenerateFromPassword([]byte(passwordStr), costInt)
|
hashBytes, err := bcrypt.GenerateFromPassword([]byte(passwordStr), costInt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lL.Push(lua.LNil)
|
L.Push(lua.LNil)
|
||||||
lL.Push(lua.LString("error: " + err.Error()))
|
L.Push(lua.LString("error: " + err.Error()))
|
||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
|
|
||||||
lL.Push(lua.LString(string(hashBytes)))
|
L.Push(lua.LString(string(hashBytes)))
|
||||||
lL.Push(lua.LNil)
|
L.Push(lua.LNil)
|
||||||
return 2
|
return 2
|
||||||
}))
|
}))
|
||||||
|
|
||||||
lL.SetField(bcryptMod, "compare", lL.NewFunction(func(l *lua.LState) int {
|
L.SetField(bcryptMod, "compare", L.NewFunction(func(l *lua.LState) int {
|
||||||
hash := ConvertLuaTypesToGolang(lL.Get(1))
|
hash := ConvertLuaTypesToGolang(L.Get(1))
|
||||||
hashStr, ok := hash.(string)
|
hashStr, ok := hash.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
lL.Push(lua.LString("error: hash must be a string"))
|
L.Push(lua.LString("error: hash must be a string"))
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
password := ConvertLuaTypesToGolang(lL.Get(2))
|
password := ConvertLuaTypesToGolang(L.Get(2))
|
||||||
passwordStr, ok := password.(string)
|
passwordStr, ok := password.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
lL.Push(lua.LString("error: password must be a string"))
|
L.Push(lua.LString("error: password must be a string"))
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
err := bcrypt.CompareHashAndPassword([]byte(hashStr), []byte(passwordStr))
|
err := bcrypt.CompareHashAndPassword([]byte(hashStr), []byte(passwordStr))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lL.Push(lua.LFalse)
|
L.Push(lua.LFalse)
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
lL.Push(lua.LTrue)
|
L.Push(lua.LTrue)
|
||||||
return 1
|
return 1
|
||||||
}))
|
}))
|
||||||
|
|
||||||
lL.SetField(bcryptMod, "__gosally_internal", lua.LString(fmt.Sprint(seed)))
|
L.SetField(bcryptMod, "__gosally_internal", lua.LString(fmt.Sprint(seed)))
|
||||||
lL.Push(bcryptMod)
|
L.Push(bcryptMod)
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user