fix error logs, add timestamp to http request logs, add back password funcs to sandbox

This commit is contained in:
Sky Johnson 2025-06-03 20:24:05 -05:00
parent 22c340648b
commit ff01a1f0b1
3 changed files with 60 additions and 4 deletions

View File

@ -17,6 +17,7 @@ import (
"Moonshark/utils/logger"
"Moonshark/utils/metadata"
luajit "git.sharkk.net/Sky/LuaJIT-to-Go"
"github.com/valyala/fasthttp"
)
@ -292,9 +293,22 @@ func (s *Server) sendError(ctx *fasthttp.RequestCtx, status int, pathBytes []byt
s.errorCacheMu.RLock()
ctx.SetBody(s.cached500)
s.errorCacheMu.RUnlock()
} else {
ctx.SetBody([]byte(utils.InternalErrorPage(s.errorConfig, string(pathBytes), err.Error())))
return
}
var errorMessage string
if luaErr, ok := err.(*luajit.LuaError); ok {
// Use just the message if stack trace is empty
if luaErr.StackTrace == "" {
errorMessage = luaErr.Message
} else {
errorMessage = err.Error() // Full error with stack trace
}
} else {
errorMessage = err.Error()
}
ctx.SetBody([]byte(utils.InternalErrorPage(s.errorConfig, string(pathBytes), errorMessage)))
}
func (s *Server) handleDebugStats(ctx *fasthttp.RequestCtx) {

View File

@ -4,6 +4,7 @@ function __execute(script_func, ctx, response)
-- Store context and response globally for function access
__ctx = ctx
__response = response
_G.ctx = ctx
-- Execute script in global environment
local ok, result = pcall(script_func)
@ -82,7 +83,7 @@ function cookie_get(name)
return __ctx._request_cookies and __ctx._request_cookies[name]
end
function cookie_remove(name, path, domain)
function cookie_delete(name, path, domain)
return cookie_set(name, "", {expires = -1, path = path or "/", domain = domain})
end
@ -388,3 +389,35 @@ function iparse(template_str, values)
return table.concat(output)
end
-- ======================================================================
-- PASSWORD FUNCTIONS
-- ======================================================================
-- Hash a password using Argon2id
-- Options:
-- memory: Amount of memory to use in KB (default: 128MB)
-- iterations: Number of iterations (default: 4)
-- parallelism: Number of threads (default: 4)
-- salt_length: Length of salt in bytes (default: 16)
-- key_length: Length of the derived key in bytes (default: 32)
function password_hash(plain_password, options)
if type(plain_password) ~= "string" then
error("password_hash: expected string password", 2)
end
return __password_hash(plain_password, options)
end
-- Verify a password against a hash
function password_verify(plain_password, hash_string)
if type(plain_password) ~= "string" then
error("password_verify: expected string password", 2)
end
if type(hash_string) ~= "string" then
error("password_verify: expected string hash", 2)
end
return __password_verify(plain_password, hash_string)
end

View File

@ -162,13 +162,22 @@ func Request(status int, method, path string, duration time.Duration) {
dur = fmt.Sprintf("%.2fs", duration.Seconds())
}
msg := fmt.Sprintf("%s %s %s %s",
var parts []string
if global.timestamp.Load() {
ts := applyColor(time.Now().Format("3:04PM"), color.Gray)
parts = append(parts, ts)
}
parts = append(parts,
applyColor("["+method+"]", color.Gray),
applyColor(fmt.Sprintf("%d", status), statusColor),
applyColor(path, color.Gray),
applyColor(dur, color.Gray),
)
msg := strings.Join(parts, " ")
global.mu.Lock()
fmt.Fprint(global.out, msg+"\n")
global.mu.Unlock()