diff --git a/generator.go b/generator.go index 1d20e0b..85968d3 100644 --- a/generator.go +++ b/generator.go @@ -1,4 +1,4 @@ -package gotsclient +package rpc import ( _ "embed" diff --git a/go.mod b/go.mod index d12e291..052188a 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module clientGen +module RpcTsClient go 1.26.1 diff --git a/handler.go b/handler.go index 4790d31..63018fc 100644 --- a/handler.go +++ b/handler.go @@ -1,4 +1,4 @@ -package gotsclient +package rpc import ( "context" @@ -10,9 +10,9 @@ import ( "golang.org/x/net/websocket" ) -func WsHandler(api any) func(*websocket.Conn) { +func WsHandler(apiStruct any, isTokenValid func(token string, method string, params []any) bool) func(*websocket.Conn) { return func(ws *websocket.Conn) { - apiVal := reflect.ValueOf(api) + apiVal := reflect.ValueOf(apiStruct) for { var msg []byte @@ -34,7 +34,13 @@ func WsHandler(api any) func(*websocket.Conn) { continue } - log.Printf("%s, %v", req.Method, req.Params) + if !isTokenValid(req.Token, req.Method, req.Params) { + _ = websocket.JSON.Send(ws, map[string]any{ + "id": req.ID, + "error": "token not valid", + }) + return + } method := apiVal.MethodByName(req.Method) if !method.IsValid() {