Enhance server functionality: add versioning support, implement command handling improvements, and introduce new Lua scripts for command execution

This commit is contained in:
alex
2025-06-23 01:26:16 +03:00
parent 03195dca59
commit 241809025d
12 changed files with 369 additions and 63 deletions

View File

@@ -13,23 +13,51 @@ import (
func (h *HandlerV1) _handle() {
uuid16 := h.newUUID()
h.log.Info("Received request", slog.String("version", "v1"), slog.String("connection-uuid", uuid16), slog.String("remote", h.r.RemoteAddr), slog.String("method", h.r.Method), slog.String("url", h.r.URL.String()))
h.log.Info("Received request",
slog.String("version", "v1"),
slog.String("connection-uuid", uuid16),
slog.String("remote", h.r.RemoteAddr),
slog.String("method", h.r.Method),
slog.String("url", h.r.URL.String()))
cmd := chi.URLParam(h.r, "cmd")
var scriptPath string
if !h.allowedCmd.MatchString(string([]rune(cmd)[0])) {
h.log.Error("HTTP request error",
slog.String("connection-uuid", uuid16),
slog.String("error", "invalid command"),
slog.String("cmd", cmd),
slog.Int("status", http.StatusBadRequest))
h.writeJSONError(http.StatusBadRequest, "invalid command")
h.log.Error("HTTP request error", slog.String("connection-uuid", uuid16), slog.String("error", "invalid command"), slog.String("cmd", cmd), slog.Int("status", http.StatusBadRequest))
return
}
if !h.listAllowedCmd.MatchString(cmd) {
h.log.Error("HTTP request error",
slog.String("connection-uuid", uuid16),
slog.String("error", "invalid command"),
slog.String("cmd", cmd),
slog.Int("status", http.StatusBadRequest))
h.writeJSONError(http.StatusBadRequest, "invalid command")
h.log.Error("HTTP request error", slog.String("connection-uuid", uuid16), slog.String("error", "invalid command"), slog.String("cmd", cmd), slog.Int("status", http.StatusBadRequest))
return
}
scriptPath := filepath.Join(h.cfg.ComDir, cmd+".lua")
if _, err := os.Stat(scriptPath); err != nil {
if scriptPath = h.comMatch(chi.URLParam(h.r, "ver"), cmd); scriptPath == "" {
h.log.Error("HTTP request error",
slog.String("connection-uuid", uuid16),
slog.String("error", "command not found"),
slog.String("cmd", cmd),
slog.Int("status", http.StatusNotFound))
h.writeJSONError(http.StatusNotFound, "command not found")
return
}
scriptPath = filepath.Join(h.cfg.ComDir, scriptPath)
if _, err := os.Stat(scriptPath); err != nil {
h.log.Error("HTTP request error",
slog.String("connection-uuid", uuid16),
slog.String("error", "command not found"),
slog.String("cmd", cmd),
slog.Int("status", http.StatusNotFound))
h.writeJSONError(http.StatusNotFound, "command not found")
h.log.Error("HTTP request error", slog.String("connection-uuid", uuid16), slog.String("error", "command not found"), slog.String("cmd", cmd), slog.Int("status", http.StatusNotFound))
return
}
@@ -48,17 +76,24 @@ func (h *HandlerV1) _handle() {
L.SetGlobal("Params", tbl)
L.SetGlobal("Result", L.NewTable())
L.DoString(`
print = function() end
io.write = function(...) end
io.stdout = function() return nil end
io.stderr = function() return nil end
io.read = function(...) return nil end
`)
prepareLuaEnv := filepath.Join(h.cfg.ComDir, "_prepare"+".lua")
if _, err := os.Stat(prepareLuaEnv); err == nil {
if err := L.DoFile(prepareLuaEnv); err != nil {
h.log.Error("Failed to prepare lua environment",
slog.String("connection-uuid", uuid16),
slog.String("error", err.Error()))
h.writeJSONError(http.StatusInternalServerError, "lua error: "+err.Error())
return
}
} else {
h.log.Error("No environment preparation script found, skipping preparation", slog.String("connection-uuid", uuid16), slog.String("error", err.Error()))
}
if err := L.DoFile(scriptPath); err != nil {
h.log.Error("Failed to execute lua script",
slog.String("connection-uuid", uuid16),
slog.String("error", err.Error()))
h.writeJSONError(http.StatusInternalServerError, "lua error: "+err.Error())
h.log.Error("Failed to execute lua script", slog.String("connection-uuid", uuid16), slog.String("error", err.Error()))
return
}
@@ -82,11 +117,23 @@ func (h *HandlerV1) _handle() {
json.NewEncoder(h.w).Encode(out)
switch out["status"] {
case "error":
h.log.Info("Command executed with error", slog.String("connection-uuid", uuid16), slog.String("cmd", cmd), slog.Any("result", out))
h.log.Info("Command executed with error",
slog.String("connection-uuid", uuid16),
slog.String("cmd", cmd),
slog.Any("result", out))
case "ok":
h.log.Info("Command executed successfully", slog.String("connection-uuid", uuid16), slog.String("cmd", cmd), slog.Any("result", out))
h.log.Info("Command executed successfully",
slog.String("connection-uuid", uuid16),
slog.String("cmd", cmd), slog.Any("result", out))
default:
h.log.Info("Command executed and returned an unknown status", slog.String("connection-uuid", uuid16), slog.String("cmd", cmd), slog.Any("result", out))
h.log.Info("Command executed and returned an unknown status",
slog.String("connection-uuid", uuid16),
slog.String("cmd", cmd),
slog.Any("result", out))
}
h.log.Info("Session completed", slog.String("connection-uuid", uuid16), slog.String("remote", h.r.RemoteAddr), slog.String("method", h.r.Method), slog.String("url", h.r.URL.String()))
h.log.Info("Session completed",
slog.String("connection-uuid", uuid16),
slog.String("remote", h.r.RemoteAddr),
slog.String("method", h.r.Method),
slog.String("url", h.r.URL.String()))
}