-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua.go
More file actions
155 lines (136 loc) · 3.14 KB
/
lua.go
File metadata and controls
155 lines (136 loc) · 3.14 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
lua "github.com/yuin/gopher-lua"
)
var (
luaCatchAll = make(map[int]lua.LValue)
luaHostMatch = make(map[string]lua.LValue)
L = lua.NewState()
)
func luaInit() {
pkg := L.GetGlobal("package").(*lua.LTable)
currentPath := L.GetField(pkg, "path").String()
newPath := currentPath + ";./lualibs/?.lua;./scripts/?.lua"
L.SetField(pkg, "path", lua.LString(newPath))
err := filepath.Walk("scripts", func(path string, info os.FileInfo, err error) error {
if strings.Contains(strings.TrimPrefix(path, "scripts/"), "/") {
if *debug {
log.Printf("Not loading: %s", path)
}
} else {
if !info.IsDir() {
top := L.GetTop()
if err := L.DoFile(path); err != nil {
panic(err)
}
c := L.GetTop() - top
catchall := L.Get(1)
var hostmatch *lua.LTable
if c >= 2 {
hostmatch = L.CheckTable(2)
}
log.Printf("%s %s", catchall, hostmatch)
if catchall != lua.LNil {
L.Pop(1)
luaCatchAll[len(luaCatchAll)] = catchall
}
if hostmatch != nil {
L.Pop(1)
hostmatch.ForEach(func(key lua.LValue, value lua.LValue) {
s := key.String()
if luaHostMatch[s] != nil {
panic(fmt.Errorf("%s already exists from another file but is referenced in %s", s, path))
}
luaHostMatch[s] = value
})
}
}
}
return nil
})
if err != nil {
panic(err)
}
}
func luaProc(w http.ResponseWriter, r *http.Request) bool {
req := L.NewTable()
L.SetField(req, "path", lua.LString(r.URL.Path))
L.SetField(req, "host", lua.LString(r.Header.Get("Host")))
body, err := io.ReadAll(r.Body)
log.Printf("awawwaawa %s %s", body, err)
if err == nil {
L.SetField(req, "body", lua.LString(body))
}
headers := L.NewTable()
for k, v := range r.Header {
L.SetField(headers, k, lua.LString(v[0]))
}
L.SetField(req, "headers", headers)
res := L.NewTable()
resHeaders := L.NewTable()
L.SetField(res, "headers", resHeaders)
process := L.NewFunction(func(L *lua.LState) int {
fmt.Println("Go function called")
client := &http.Client{}
resp, err := client.Do(r)
if err != nil {
panic(err)
}
// Read response body
body, err := io.ReadAll(resp.Body)
if err != nil {
L.Push(lua.LString(fmt.Sprintf("%s", err)))
return 1
}
L.SetField(res, "body", lua.LString(body))
resp.Body.Close()
return 0
})
top := L.GetTop()
suc := false
for _, v := range luaCatchAll {
if err := L.CallByParam(lua.P{
Fn: v,
NRet: lua.MultRet,
Protect: true,
}, req, res, process); err != nil {
panic(err)
}
if L.GetTop()-top >= 1 {
suc = L.CheckBool(1)
if suc {
break
}
}
}
if !suc && luaHostMatch[r.Host] != nil {
if err := L.CallByParam(lua.P{
Fn: luaHostMatch[r.Host],
NRet: lua.MultRet,
Protect: true,
}, req, res, process); err != nil {
panic(err)
}
if L.GetTop()-top >= 1 {
suc = L.CheckBool(-1)
}
}
if suc {
resHeaders.ForEach(func(k lua.LValue, v lua.LValue) {
w.Header().Add(k.String(), v.String())
})
b := L.GetField(res, "body")
if b != nil {
log.Printf("%s", b)
w.Write([]byte(b.String()))
}
}
return suc
}