-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
329 lines (272 loc) · 9.13 KB
/
http.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/golang-jwt/jwt"
memory "github.com/pbnjay/memory"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/recover"
)
var sleepTest = time.Duration(3) * time.Second
func errorResponse(c *fiber.Ctx, message string, code int) error {
c.Status(code)
return c.JSON(message)
}
func successResponse(c *fiber.Ctx, message string) error {
c.Status(200)
return c.JSON(message)
}
func successResponseInterface(c *fiber.Ctx, data interface{}) error {
c.Status(200)
return c.JSON(data)
}
func restoreLastSuccess() {
executeMany(
"rm -rf addon/",
"tar -xf addon.success.tgz",
)
}
func backupLastSuccess() {
executeMany(
"rm -rf addon.success.tgz",
"tar -czf addon.success.tgz addon",
)
}
func healthCheck(dockerPath string) (bool, string) {
_, testError := execute("docker", "run", "--rm", "--name", "test", "-v", dockerPath+"/addon:/usr/local/openresty/nginx/addon", "ghcr.io/aasaam/web-server", "openresty", "-t")
if testError != nil {
return false, testError.Error()
}
execute("docker-compose", "down", "-d")
_, composeUpErr := execute("docker-compose", "up", "-d")
if composeUpErr != nil {
return false, composeUpErr.Error()
}
out1, err1 := execute("docker", "inspect", "aasaam-web-server", "--format", "{{.State.Running}}")
if err1 != nil {
return false, "Web server container not found"
}
var stateRunning bool
err2 := json.Unmarshal(out1, &stateRunning)
if err2 != nil || !stateRunning {
return false, "Web server container not running"
}
out3, err3 := execute("docker", "exec", "-t", "aasaam-web-server", "openresty", "-t")
if err3 != nil {
return false, err3.Error()
}
return true, "htm-node: healthy\n" + normalizeStd(out3)
}
func newHTTPServer(config *nodeConfig) *fiber.App {
cwd, cwdErr := os.Getwd()
if cwdErr != nil {
panic(cwdErr)
} else if cwd != config.dockerPath {
panic(errors.New("invlaid path for htm: " + cwd))
}
app := fiber.New(fiber.Config{
BodyLimit: 1024 * 1024 * 64,
DisableStartupMessage: true,
Prefork: false,
ErrorHandler: func(c *fiber.Ctx, err error) error {
code := fiber.StatusInternalServerError
if e, ok := err.(*fiber.Error); ok {
code = e.Code
}
defer config.getLogger().
Error().
Str("error", err.Error()).
Str("ip", c.IP()).
Str("method", c.Method()).
Str("url", c.Path()).
Int("status_code", code).
Send()
return errorResponse(c, "Internal Server Error", code)
},
})
app.Use(recover.New())
// middle ware
app.Use(func(c *fiber.Ctx) error {
tokenString := c.Get(tokenHeader, "")
token, tokenErr := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
// Don't forget to validate the alg is what you expect:
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return []byte(config.token), nil
})
validationError := "invalid token"
if tokenErr != nil {
validationError = tokenErr.Error()
} else {
_, validationOK := token.Claims.(jwt.MapClaims)
if validationOK && token.Valid {
defer config.getLogger().
Info().
Str("ip", c.IP()).
Str("method", c.Method()).
Str("url", c.Path()).
Msg("Access granted")
return c.Next()
}
}
defer config.getLogger().
Error().
Str("ip", c.IP()).
Str("error", validationError).
Str("method", c.Method()).
Str("url", c.Path()).
Msg("Forbidden")
return errorResponse(c, "Forbidden: "+validationError, 403)
})
app.Get("/ping", func(c *fiber.Ctx) error {
return c.JSON("pong")
})
app.Get("/health", func(c *fiber.Ctx) error {
isHealthy, isHealthyMessage := healthCheck(config.dockerPath)
if !isHealthy {
return errorResponse(c, isHealthyMessage, 500)
}
return c.JSON(isHealthyMessage)
})
app.Get("/info", func(c *fiber.Ctx) error {
memoryInfoValue := memoryInfo{
Total: totalMemory,
Free: memory.FreeMemory(),
}
info := nodeInfo{
CPU: cpuInfoValue,
Memory: memoryInfoValue,
}
uname, unameErr := executeString("uname", "-r")
lsb, lsbErr := executeString("lsb_release", "-sd")
if unameErr != nil || lsbErr != nil {
return errorResponse(c, "cannot get os information", 500)
}
diWebServer, diWebServerErr := execute("docker", "image", "inspect", "ghcr.io/aasaam/web-server:latest", "--format", "{{json .}}")
if diWebServerErr != nil {
return errorResponse(c, "cannot image 'web-server' information", 500)
}
var imageWebServer dockerImage
diWebServerJSONErr := json.Unmarshal(diWebServer, &imageWebServer)
if diWebServerJSONErr != nil {
return errorResponse(c, "invalid image data for 'web-server'", 500)
}
nginxProtection, nginxProtectionErr := execute("docker", "image", "inspect", "ghcr.io/aasaam/nginx-protection:latest", "--format", "{{json .}}")
if nginxProtectionErr != nil {
return errorResponse(c, "cannot image 'nginx-protection' information", 500)
}
var imageNginxProtection dockerImage
nginxProtectionJSONErr := json.Unmarshal(nginxProtection, &imageNginxProtection)
if nginxProtectionJSONErr != nil {
return errorResponse(c, "invalid image data for 'nginx-protection'", 500)
}
restCaptcha, restCaptchaErr := execute("docker", "image", "inspect", "ghcr.io/aasaam/rest-captcha:latest", "--format", "{{json .}}")
if restCaptchaErr != nil {
return errorResponse(c, "cannot image 'rest-captcha' information", 500)
}
var imageRESTCaptcha dockerImage
restCaptchaJSONErr := json.Unmarshal(restCaptcha, &imageRESTCaptcha)
if restCaptchaJSONErr != nil {
return errorResponse(c, "invalid image data for 'rest-captcha'", 500)
}
nginxErrorLogParser, nginxErrorLogParserErr := execute("docker", "image", "inspect", "ghcr.io/aasaam/nginx-error-log-parser:latest", "--format", "{{json .}}")
if nginxErrorLogParserErr != nil {
return errorResponse(c, "cannot image 'nginx-error-log-parser' information", 500)
}
var imageNginxErrorLogParser dockerImage
nginxErrorLogParserJSONErr := json.Unmarshal(nginxErrorLogParser, &imageNginxErrorLogParser)
if nginxErrorLogParserJSONErr != nil {
return errorResponse(c, "invalid image data for 'nginx-error-log-parser'", 500)
}
info.Kernel = uname
info.Distribution = lsb
info.ImageWebServer = imageWebServer
info.ImageNginxProtection = imageNginxProtection
info.ImageRESTCaptcha = imageRESTCaptcha
info.ImageNginxErrorLogParser = imageNginxErrorLogParser
return successResponseInterface(c, info)
})
app.Post("/firewall", func(c *fiber.Ctx) error {
var firewallRequest firewallRequest
if jsonError := c.BodyParser(firewallRequest); jsonError != nil {
return errorResponse(c, "invalid firewall data", 422)
}
result := []string{}
for _, ipString := range firewallRequest.IPs {
if config.canBlockIP(ipString) {
result = append(result, ipString)
}
}
for _, cidrString := range firewallRequest.CIDRs {
if config.canBlockCIDR(cidrString) {
result = append(result, cidrString)
}
}
writeFileErr := ioutil.WriteFile(config.dockerPath+"/var/ufw_block", []byte(strings.Join(result, "\n")), 0644)
if writeFileErr != nil {
return errorResponse(c, "failed to write to file: "+writeFileErr.Error(), 500)
}
_, runFireWallErr := execute("/usr/local/bin/firewall")
if runFireWallErr != nil {
return errorResponse(c, "failed to run firewall: "+runFireWallErr.Error(), 500)
}
ufw, ufwErr := execute("ufw", "status", "numbered")
if ufwErr != nil {
return errorResponse(c, "failed to check ufw status: "+ufwErr.Error(), 500)
}
return successResponse(c, string(ufw))
})
app.Post("/restart", func(c *fiber.Ctx) error {
_, composeDownErr := execute("docker-compose", "down")
if composeDownErr != nil {
restoreLastSuccess()
return errorResponse(c, composeDownErr.Error(), 500)
}
_, composeUpErr := execute("docker-compose", "up", "-d")
if composeUpErr != nil {
restoreLastSuccess()
return errorResponse(c, composeUpErr.Error(), 500)
}
isHealthy, isHealthyMessage := healthCheck(config.dockerPath)
if !isHealthy {
restoreLastSuccess()
return errorResponse(c, isHealthyMessage, 500)
}
backupLastSuccess()
return c.JSON(isHealthyMessage)
})
app.Post("/update", func(c *fiber.Ctx) error {
file, fileErr := c.FormFile("addon.tgz")
if fileErr != nil {
return errorResponse(c, "file 'addon.tgz' not present: "+fileErr.Error(), fiber.StatusFailedDependency)
}
executeMany("rm -rf /tmp/addon.tgz")
saveErr := c.SaveFile(file, "/tmp/addon.tgz")
if saveErr != nil {
return errorResponse(c, "cannot save 'addon.tgz': "+saveErr.Error(), fiber.StatusLoopDetected)
}
executeMany("/usr/local/bin/htm-addon-backup")
extractErr := executeMany(
"cd "+config.dockerPath,
"cp /tmp/addon.tgz ./addon.tgz",
"rm -rf addon/",
"tar -xf addon.tgz",
"rm -rf addon.tgz",
)
if extractErr != nil {
return errorResponse(c, "cannot extract 'addon.tgz': "+extractErr.Error(), fiber.StatusInternalServerError)
}
isHealthy, isHealthyMessage := healthCheck(config.dockerPath)
if !isHealthy {
return errorResponse(c, isHealthyMessage, 500)
}
return c.JSON(isHealthyMessage)
})
return app
}