-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
template.go
194 lines (174 loc) · 4.54 KB
/
template.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package templates
import (
"context"
"encoding/json"
"github.com/reddec/trusted-cgi/types"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"
)
func Read(filename string) (*Template, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
var t = &Template{}
return t, json.NewDecoder(f).Decode(t)
}
type Template struct {
Description string `json:"description" yaml:"description"`
Manifest types.Manifest `json:"manifest" yaml:"manifest"` // manifest to copy
Check []string `json:"check,omitempty" yaml:"check,omitempty"` // check availability
Files map[string]string `json:"files" yaml:"files,omitempty"` //only for embedded
}
func (t *Template) IsAvailable(ctx context.Context) bool {
if len(t.Check) == 0 {
return true
}
cmd := exec.CommandContext(ctx, t.Check[0], t.Check[1:]...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
Pdeathsig: syscall.SIGINT,
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run() == nil
}
// List embedded and external templates
func List(templatesDir string) (map[string]*Template, error) {
merged := ListEmbedded()
ext, err := ListDir(templatesDir)
if err != nil {
return nil, err
}
for name, t := range ext {
merged[name] = t
}
return merged, nil
}
func ListDir(dir string) (map[string]*Template, error) {
const suffix = ".json"
items, err := ioutil.ReadDir(dir)
if os.IsNotExist(err) {
return map[string]*Template{}, nil
} else if err != nil {
return nil, err
}
var ans = make(map[string]*Template)
for _, item := range items {
name := item.Name()
if item.IsDir() || !strings.HasSuffix(name, suffix) {
continue
}
name = name[:len(name)-len(suffix)]
t, err := Read(filepath.Join(dir, item.Name()))
if err != nil {
return nil, err
}
ans[name] = t
}
return ans, nil
}
func ListEmbedded() map[string]*Template {
return map[string]*Template{
"Python": {
Description: "Python basic function",
Check: []string{"which", "python3"},
Files: map[string]string{
"app.py": pythonScript,
},
Manifest: types.Manifest{
Name: "Example Python Function",
Description: `### Usage
curl --data-binary '{"name": "reddec"}' -H 'Content-Type: application/json' "http://example.com/a/xyz"
Replace url to the real
`,
Run: []string{"python3", "app.py"},
TimeLimit: types.JsonDuration(time.Second),
Public: true,
MaximumPayload: 8192,
OutputHeaders: map[string]string{
"Content-Type": "application/json",
},
},
},
"Node JS": {
Description: "Node JS basic function",
Check: []string{"which", "node"},
Files: map[string]string{
"app.js": nodeJsScript,
},
Manifest: types.Manifest{
Name: "Example NodeJS Function",
Description: `### Usage
curl --data-binary '{"name": "reddec"}' -H 'Content-Type: application/json' "http://example.com/a/xyz"
Replace url to the real
`,
Run: []string{"node", "app.js"},
TimeLimit: types.JsonDuration(time.Second),
Public: true,
MaximumPayload: 8192,
OutputHeaders: map[string]string{
"Content-Type": "application/json",
},
},
},
"PHP": {
Description: "PHP basic function",
Manifest: types.Manifest{
Name: "Example PHP Function",
Description: `### Usage
curl --data-binary '{"name": "reddec"}' -H 'Content-Type: application/json' "http://example.com/a/xyz"
Replace url to the real
`,
Run: []string{"php", "app.php"},
TimeLimit: types.JsonDuration(time.Second),
Public: true,
MaximumPayload: 8192,
OutputHeaders: map[string]string{
"Content-Type": "application/json",
},
},
Check: []string{"which", "php"},
Files: map[string]string{
"app.php": phpScript,
},
},
}
}
const pythonScript = `
import sys
import json
request = json.load(sys.stdin)
response = ['hello', 'world']
json.dump(response, sys.stdout)
`
const nodeJsScript = `
async function run(request) {
return ["hello", "world"];
}
let input = '';
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (chunk) {
input += chunk;
});
process.stdin.on('end', function () {
run(JSON.parse(input)).catch((e)=> {
return {"error": e + ''};
}).then((response)=> {
process.stdout.write(JSON.stringify(response));
})
});
`
const phpScript = `
<?php
$request = json_decode(stream_get_contents(STDIN));
$response = array("hello", "world");
echo json_encode($response, JSON_PRETTY_PRINT);
?>`