mirror of
https://github.com/akyaiy/GoSally-mvp.git
synced 2026-01-03 21:12:25 +00:00
Compare commits
2 Commits
e594d519a7
...
c6da55ad65
| Author | SHA1 | Date | |
|---|---|---|---|
| c6da55ad65 | |||
| 20a1e3e7bb |
@@ -52,6 +52,6 @@ end
|
|||||||
local basePath = "com"
|
local basePath = "com"
|
||||||
local layer = params.layer and params.layer:gsub(">", "/") or nil
|
local layer = params.layer and params.layer:gsub(">", "/") or nil
|
||||||
|
|
||||||
session.response.result = {
|
session.response.send({
|
||||||
answer = layer and scanDirectory(basePath, layer) or scanDirectory(basePath, "")
|
answer = layer and scanDirectory(basePath, layer) or scanDirectory(basePath, "")
|
||||||
}
|
})
|
||||||
@@ -80,6 +80,7 @@ func InitUUUDHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) {
|
|||||||
x.Log.Fatalf("uuid load error: %s", err)
|
x.Log.Fatalf("uuid load error: %s", err)
|
||||||
}
|
}
|
||||||
cs.UUID32 = uuid32
|
cs.UUID32 = uuid32
|
||||||
|
corestate.NODE_UUID = uuid32
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitRuntimeHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) {
|
func InitRuntimeHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package corestate
|
package corestate
|
||||||
|
|
||||||
|
var NODE_UUID string
|
||||||
|
|
||||||
type Stage string
|
type Stage string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|||||||
@@ -20,18 +20,14 @@ func (gs *GatewayServer) Handle(w http.ResponseWriter, r *http.Request) {
|
|||||||
sessionUUID := r.Header.Get("X-Session-UUID")
|
sessionUUID := r.Header.Get("X-Session-UUID")
|
||||||
if sessionUUID == "" {
|
if sessionUUID == "" {
|
||||||
sessionUUID = uuid.New().String()
|
sessionUUID = uuid.New().String()
|
||||||
|
|
||||||
}
|
}
|
||||||
gs.x.SLog.Debug("new request", slog.String("session-uuid", sessionUUID), slog.Group("connection", slog.String("ip", r.RemoteAddr)))
|
gs.x.SLog.Debug("new request", slog.String("session-uuid", sessionUUID), slog.Group("connection", slog.String("ip", r.RemoteAddr)))
|
||||||
|
|
||||||
w.Header().Set("X-Session-UUID", sessionUUID)
|
w.Header().Set("X-Session-UUID", sessionUUID)
|
||||||
if !gs.sm.Add(sessionUUID) {
|
if !gs.sm.Add(sessionUUID) {
|
||||||
gs.x.SLog.Debug("session is busy", slog.String("session-uuid", sessionUUID))
|
gs.x.SLog.Debug("session is busy", slog.String("session-uuid", sessionUUID))
|
||||||
rpc.WriteError(gs.cs.UUID32, w, &rpc.RPCResponse{
|
rpc.WriteError(w, rpc.NewError(rpc.ErrSessionIsBusy, rpc.ErrSessionIsBusyS, nil, nil))
|
||||||
Error: map[string]any{
|
|
||||||
"code": rpc.ErrSessionIsBusy,
|
|
||||||
"message": rpc.ErrSessionIsBusyS,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer gs.sm.Delete(sessionUUID)
|
defer gs.sm.Delete(sessionUUID)
|
||||||
@@ -40,14 +36,7 @@ func (gs *GatewayServer) Handle(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
gs.x.SLog.Debug("failed to read body", slog.String("err", err.Error()))
|
gs.x.SLog.Debug("failed to read body", slog.String("err", err.Error()))
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
rpc.WriteError(gs.cs.UUID32, w, &rpc.RPCResponse{
|
rpc.WriteError(w, rpc.NewError(rpc.ErrInternalError, rpc.ErrInternalErrorS, nil, nil))
|
||||||
JSONRPC: rpc.JSONRPCVersion,
|
|
||||||
ID: nil,
|
|
||||||
Error: map[string]any{
|
|
||||||
"code": rpc.ErrInternalError,
|
|
||||||
"message": rpc.ErrInternalErrorS,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
gs.x.SLog.Info("invalid request received", slog.String("issue", rpc.ErrInternalErrorS))
|
gs.x.SLog.Info("invalid request received", slog.String("issue", rpc.ErrInternalErrorS))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -60,14 +49,7 @@ func (gs *GatewayServer) Handle(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err := json.Unmarshal(body, &single); err != nil {
|
if err := json.Unmarshal(body, &single); err != nil {
|
||||||
gs.x.SLog.Debug("failed to parse json", slog.String("err", err.Error()))
|
gs.x.SLog.Debug("failed to parse json", slog.String("err", err.Error()))
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
rpc.WriteError(gs.cs.UUID32, w, &rpc.RPCResponse{
|
rpc.WriteError(w, rpc.NewError(rpc.ErrParseError, rpc.ErrParseErrorS, nil, nil))
|
||||||
JSONRPC: rpc.JSONRPCVersion,
|
|
||||||
ID: nil,
|
|
||||||
Error: map[string]any{
|
|
||||||
"code": rpc.ErrParseError,
|
|
||||||
"message": rpc.ErrParseErrorS,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
gs.x.SLog.Info("invalid request received", slog.String("issue", rpc.ErrParseErrorS))
|
gs.x.SLog.Info("invalid request received", slog.String("issue", rpc.ErrParseErrorS))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -76,7 +58,7 @@ func (gs *GatewayServer) Handle(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Write([]byte(""))
|
w.Write([]byte(""))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
rpc.WriteResponse(gs.cs.UUID32, w, resp)
|
rpc.WriteResponse(w, resp)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,13 +11,18 @@ type RPCRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type RPCResponse struct {
|
type RPCResponse struct {
|
||||||
JSONRPC string `json:"jsonrpc"`
|
JSONRPC string `json:"jsonrpc"`
|
||||||
ID *json.RawMessage `json:"id"`
|
ID *json.RawMessage `json:"id"`
|
||||||
Result any `json:"result,omitempty"`
|
Result any `json:"result,omitzero"`
|
||||||
Error any `json:"error,omitempty"`
|
Error any `json:"error,omitzero"`
|
||||||
ResponsibleNode string `json:"responsible-node,omitempty"`
|
Data *RPCData `json:"data,omitzero"`
|
||||||
Salt string `json:"salt,omitempty"`
|
}
|
||||||
Checksum string `json:"checksum-md5,omitempty"`
|
|
||||||
|
type RPCData struct {
|
||||||
|
ResponsibleNode string `json:"responsible-node,omitempty"`
|
||||||
|
Salt string `json:"salt,omitempty"`
|
||||||
|
Checksum string `json:"checksum-md5,omitempty"`
|
||||||
|
NewSessionUUID string `json:"new-session-uuid,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|||||||
@@ -1,39 +1,58 @@
|
|||||||
package rpc
|
package rpc
|
||||||
|
|
||||||
import "encoding/json"
|
import (
|
||||||
|
"crypto/md5"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/akyaiy/GoSally-mvp/internal/core/corestate"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
func generateChecksum(result any) string {
|
||||||
|
if result == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
data, err := json.Marshal(result)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%x", md5.Sum(data))
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateSalt() string {
|
||||||
|
return uuid.NewString()
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetData(data any) *RPCData {
|
||||||
|
return &RPCData{
|
||||||
|
Salt: generateSalt(),
|
||||||
|
ResponsibleNode: corestate.NODE_UUID,
|
||||||
|
Checksum: generateChecksum(data),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func NewError(code int, message string, data any, id *json.RawMessage) *RPCResponse {
|
func NewError(code int, message string, data any, id *json.RawMessage) *RPCResponse {
|
||||||
if data != nil {
|
Error := make(map[string]any)
|
||||||
return &RPCResponse{
|
Error = map[string]any{
|
||||||
JSONRPC: JSONRPCVersion,
|
"code": code,
|
||||||
ID: id,
|
"message": message,
|
||||||
Error: map[string]any{
|
"data": data,
|
||||||
"code": code,
|
|
||||||
"message": message,
|
|
||||||
"data": data,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &RPCResponse{
|
return &RPCResponse{
|
||||||
JSONRPC: JSONRPCVersion,
|
JSONRPC: JSONRPCVersion,
|
||||||
ID: id,
|
ID: id,
|
||||||
Error: map[string]any{
|
Error: Error,
|
||||||
"code": code,
|
Data: GetData(Error),
|
||||||
"message": message,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewResponse(result any, id *json.RawMessage) *RPCResponse {
|
func NewResponse(result any, id *json.RawMessage) *RPCResponse {
|
||||||
if result == nil {
|
|
||||||
return &RPCResponse{
|
|
||||||
JSONRPC: JSONRPCVersion,
|
|
||||||
ID: id,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return &RPCResponse{
|
return &RPCResponse{
|
||||||
JSONRPC: JSONRPCVersion,
|
JSONRPC: JSONRPCVersion,
|
||||||
ID: id,
|
ID: id,
|
||||||
Result: result,
|
Result: result,
|
||||||
|
Data: GetData(result),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,40 +1,11 @@
|
|||||||
package rpc
|
package rpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func generateChecksum(result any) string {
|
func write(w http.ResponseWriter, msg *RPCResponse) error {
|
||||||
if result == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
data, err := json.Marshal(result)
|
|
||||||
if err != nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("%x", md5.Sum(data))
|
|
||||||
}
|
|
||||||
|
|
||||||
func generateSalt() string {
|
|
||||||
return uuid.NewString()
|
|
||||||
}
|
|
||||||
|
|
||||||
func write(nid string, w http.ResponseWriter, msg *RPCResponse) error {
|
|
||||||
msg.Salt = generateSalt()
|
|
||||||
if msg.Result != nil {
|
|
||||||
msg.Checksum = generateChecksum(msg.Result)
|
|
||||||
} else if msg.Error != nil {
|
|
||||||
msg.Checksum = generateChecksum(msg.Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
if nid != "" {
|
|
||||||
msg.ResponsibleNode = nid
|
|
||||||
}
|
|
||||||
data, err := json.Marshal(msg)
|
data, err := json.Marshal(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -43,10 +14,10 @@ func write(nid string, w http.ResponseWriter, msg *RPCResponse) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteError(nid string, w http.ResponseWriter, errm *RPCResponse) error {
|
func WriteError(w http.ResponseWriter, errm *RPCResponse) error {
|
||||||
return write(nid, w, errm)
|
return write(w, errm)
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteResponse(nid string, w http.ResponseWriter, response *RPCResponse) error {
|
func WriteResponse(w http.ResponseWriter, response *RPCResponse) error {
|
||||||
return write(nid, w, response)
|
return write(w, response)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -200,7 +200,7 @@ func (h *HandlerV1) handleLUA(sid string, r *http.Request, req *rpc.RPCRequest,
|
|||||||
} else {
|
} else {
|
||||||
L.SetField(scriptDataTable, "result", res)
|
L.SetField(scriptDataTable, "result", res)
|
||||||
}
|
}
|
||||||
|
|
||||||
__exit = 0
|
__exit = 0
|
||||||
L.RaiseError("__successfull")
|
L.RaiseError("__successfull")
|
||||||
return 0
|
return 0
|
||||||
@@ -584,8 +584,7 @@ func (h *HandlerV1) handleLUA(sid string, r *http.Request, req *rpc.RPCRequest,
|
|||||||
if err != nil && __exit != 0 && __exit != 1 {
|
if err != nil && __exit != 0 && __exit != 1 {
|
||||||
llog.Error("script error", slog.String("script", path), slog.String("error", err.Error()))
|
llog.Error("script error", slog.String("script", path), slog.String("error", err.Error()))
|
||||||
return rpc.NewError(rpc.ErrInternalError, rpc.ErrInternalErrorS, nil, req.ID)
|
return rpc.NewError(rpc.ErrInternalError, rpc.ErrInternalErrorS, nil, req.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pkg := L.GetGlobal("package")
|
pkg := L.GetGlobal("package")
|
||||||
pkgTbl, ok := pkg.(*lua.LTable)
|
pkgTbl, ok := pkg.(*lua.LTable)
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ func ConvertLuaTypesToGolang(value lua.LValue) any {
|
|||||||
}
|
}
|
||||||
isNumeric = true
|
isNumeric = true
|
||||||
}
|
}
|
||||||
|
|
||||||
num, err := strconv.Atoi(numKey.String())
|
num, err := strconv.Atoi(numKey.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
isArray = false
|
isArray = false
|
||||||
|
|||||||
Reference in New Issue
Block a user