github.com/sagernet/sing-box@v1.9.0-rc.20/experimental/clashapi/script.go (about) 1 package clashapi 2 3 import ( 4 "net/http" 5 6 "github.com/go-chi/chi/v5" 7 "github.com/go-chi/render" 8 ) 9 10 func scriptRouter() http.Handler { 11 r := chi.NewRouter() 12 r.Post("/", testScript) 13 r.Patch("/", patchScript) 14 return r 15 } 16 17 /*type TestScriptRequest struct { 18 Script *string `json:"script"` 19 Metadata C.Metadata `json:"metadata"` 20 }*/ 21 22 func testScript(w http.ResponseWriter, r *http.Request) { 23 /* req := TestScriptRequest{} 24 if err := render.DecodeJSON(r.Body, &req); err != nil { 25 render.Status(r, http.StatusBadRequest) 26 render.JSON(w, r, ErrBadRequest) 27 return 28 } 29 30 fn := tunnel.ScriptFn() 31 if req.Script == nil && fn == nil { 32 render.Status(r, http.StatusBadRequest) 33 render.JSON(w, r, newError("should send `script`")) 34 return 35 } 36 37 if !req.Metadata.Valid() { 38 render.Status(r, http.StatusBadRequest) 39 render.JSON(w, r, newError("metadata not valid")) 40 return 41 } 42 43 if req.Script != nil { 44 var err error 45 fn, err = script.ParseScript(*req.Script) 46 if err != nil { 47 render.Status(r, http.StatusBadRequest) 48 render.JSON(w, r, newError(err.Error())) 49 return 50 } 51 } 52 53 ctx, _ := script.MakeContext(tunnel.ProxyProviders(), tunnel.RuleProviders()) 54 55 thread := &starlark.Thread{} 56 ret, err := starlark.Call(thread, fn, starlark.Tuple{ctx, script.MakeMetadata(&req.Metadata)}, nil) 57 if err != nil { 58 render.Status(r, http.StatusBadRequest) 59 render.JSON(w, r, newError(err.Error())) 60 return 61 } 62 63 elm, ok := ret.(starlark.String) 64 if !ok { 65 render.Status(r, http.StatusBadRequest) 66 render.JSON(w, r, "script fn must return a string") 67 return 68 } 69 70 render.JSON(w, r, render.M{ 71 "result": string(elm), 72 })*/ 73 render.Status(r, http.StatusBadRequest) 74 render.JSON(w, r, newError("not implemented")) 75 } 76 77 type PatchScriptRequest struct { 78 Script string `json:"script"` 79 } 80 81 func patchScript(w http.ResponseWriter, r *http.Request) { 82 /*req := PatchScriptRequest{} 83 if err := render.DecodeJSON(r.Body, &req); err != nil { 84 render.Status(r, http.StatusBadRequest) 85 render.JSON(w, r, ErrBadRequest) 86 return 87 } 88 89 fn, err := script.ParseScript(req.Script) 90 if err != nil { 91 render.Status(r, http.StatusBadRequest) 92 render.JSON(w, r, newError(err.Error())) 93 return 94 } 95 96 tunnel.UpdateScript(fn)*/ 97 render.NoContent(w, r) 98 }