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