This repository was archived by the owner on Oct 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmisc.go
380 lines (350 loc) · 9.28 KB
/
misc.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"github.com/MakeNowJust/heredoc"
"golang.org/x/crypto/bcrypt"
)
func btoi(a uint32) bool {
return a != 0
}
//lint:ignore U1000 for performance
func measureHandlerTimings(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
s := time.Now()
h(w, r)
log.Printf("Timings: %v", time.Since(s))
}
}
func parseFormString(r *http.Request, field string, re *regexp.Regexp) *string {
f := r.FormValue(field)
if f == "" {
return nil
}
if re != nil {
if !re.MatchString(f) {
return nil
}
}
return &f
}
func parseFormInt(r *http.Request, field string) *int {
f := r.FormValue(field)
if f == "" {
return nil
}
ret, err := strconv.Atoi(f)
if err != nil {
return nil
}
return &ret
}
func parseFormIntWhitelist(r *http.Request, field string, whitelist ...int) *int {
i := parseFormInt(r, field)
if i == nil {
return nil
}
for _, v := range whitelist {
if v == *i {
return i
}
}
return nil
}
func parseFormBool(r *http.Request, field string) bool {
f := r.FormValue(field)
return f == "on" || f == "true" || f == "checked"
}
func parseQueryInt(r *http.Request, field string, d int) int {
if val, ok := r.URL.Query()[field]; ok && len(val) > 0 {
val2, err := strconv.Atoi(val[0])
if err == nil {
return val2
}
}
return d
}
func parseQueryString(r *http.Request, field string, d string) string {
if val, ok := r.URL.Query()[field]; ok && len(val) > 0 {
return val[0]
}
return d
}
func parseQueryStringFiltered(r *http.Request, field string, d string, variants ...string) string {
if val, ok := r.URL.Query()[field]; ok && len(val) > 0 {
for _, v := range variants {
if val[0] == v {
return v
}
}
}
return d
}
func parseQueryStringMapped(r *http.Request, field string, d string, m map[string]string) string {
if val, ok := r.URL.Query()[field]; ok && len(val) > 0 {
v, ok := m[val[0]]
if ok {
return v
}
}
return d
}
func stringOneOf(a string, b ...string) bool {
for _, s := range b {
if a == s {
return true
}
}
return false
}
func respondWithCodeAndPlaintext(w http.ResponseWriter, code int, resp string) {
w.WriteHeader(code)
w.Write([]byte(resp))
}
func logRespondWithCodeAndPlaintext(w http.ResponseWriter, code int, resp string) {
log.Println(resp)
respondWithCodeAndPlaintext(w, code, resp)
}
func respondWithUnauthorized(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
basicLayoutLookupRespond(templateNotAuthorized, w, r, map[string]any{})
}
func respondWithForbidden(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden)
basicLayoutLookupRespond(templateErrorForbidden, w, r, map[string]any{})
}
func respondWithNotImplemented(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotImplemented)
basicLayoutLookupRespond(templatePlainMessage, w, r, map[string]any{"msg": "Not implemented"})
}
func checkFormParse(w http.ResponseWriter, r *http.Request) bool {
err := r.ParseForm()
if err != nil {
basicLayoutLookupRespond(templatePlainMessage, w, r, map[string]any{"msgred": true, "msg": "Form parse error: " + err.Error()})
}
return err == nil
}
//lint:ignore U1000 for future
func checkRespondDatabaseErrorAny(w http.ResponseWriter, r *http.Request, derr error) bool {
if derr != nil {
basicLayoutLookupRespond(templatePlainMessage, w, r, map[string]any{"msgred": true, "msg": "Database query error: " + derr.Error()})
}
return derr == nil
}
func checkRespondGenericErrorAny(w http.ResponseWriter, r *http.Request, derr error) bool {
if derr != nil {
basicLayoutLookupRespond(templatePlainMessage, w, r, map[string]any{"msgred": true, "msg": "Error: " + derr.Error()})
}
return derr == nil
}
func myNotFoundHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
if !strings.HasPrefix(r.URL.Path, "/api/") {
basicLayoutLookupRespond("error404", w, r, map[string]any{})
}
})
}
func hashPassword(pwd string) string {
hash, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost)
if err != nil {
log.Println(err)
return ""
}
return string(hash)
}
func comparePasswords(hashedPwd string, plainPwd string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hashedPwd), []byte(plainPwd))
if err != nil {
log.Println(err)
return false
}
return true
}
func generateRandomString(slen int) string {
s := ""
a := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
al := len(a)
for i := 0; i < slen; i++ {
s += string(a[rand.Intn(al-1)])
}
return s
}
func validateUsername(u string) bool {
if len(u) < 3 || len(u) > 25 {
return false
}
if strings.Contains(u, "@") {
return false
}
return true
}
func validatePassword(u string) bool {
if len(u) < 6 || len(u) > 25 {
return false
}
return true
}
var regexEmail = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
func validateEmail(u string) bool {
if len(u) < 3 || len(u) > 254 {
return false
}
return regexEmail.MatchString(u)
}
func sendgridConfirmcode(email string, code string) error {
sendstr := fmt.Sprintf(`{
"personalizations": [
{
"to": [
{
"email":"%s"
}
]
}
],
"from": {
"email": "[email protected]",
"name": "Account Registration"
},
"subject": "Welcome to Warzone 2100 Autohoster website",
"content": [
{
"type": "text/plain",
"value": "Welcome to Warzone 2100 Autohoster. To confirm your email address follow this link: https://wz2100-autohost.net/activate?code=%s"
},
{
"type":"text/html",
"value":"<html><h3>Welcome to Warzone 2100 Autohoster.</h3><p>To confirm your email address follow link below.</p><p><a href=\"https://wz2100-autohost.net/activate?code=%s\">Activate account</a></p></html>"
}
]
}`, email, code, code)
req, err := http.NewRequest("POST", "https://api.sendgrid.com/v3/mail/send", bytes.NewBuffer([]byte(sendstr)))
if err != nil {
return err
}
skey, ok := cfg.GetString("sendgridKey")
if !ok {
log.Println("Sendgrid key is not set!")
return errors.New("missconfigured server (let admin know)")
}
req.Header.Set("Authorization", "Bearer "+skey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
log.Println("response Status:", resp.Status)
log.Println("response Headers:", resp.Header)
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
log.Println("response Body:", string(body))
if resp.StatusCode == 200 || resp.StatusCode == 202 {
return nil
}
return nil
}
func sendgridRecoverRequest(email string, code string) bool {
sendstr := heredoc.Docf(`
{
"personalizations": [
{
"to": [
{
"email":"%s"
}
]
}
],
"from": {
"email": "[email protected]",
"name": "Autohoster"
},
"subject": "Password recovery",
"content": [
{
"type": "text/plain",
"value": "Hello, to reset your password please follow this link: https://wz2100-autohost.net/recover?code=%s\nIf this was not you and you think someone is trying to gain access to your account please contact us."
},
{
"type":"text/html",
"value":"<html><h3>Password reset</h3><p>To reset your password follow link below.</p><p><a href=\"https://wz2100-autohost.net/recover?code=%s\">Set new password</a></p><p>If this was not you and you think someone is trying to gain access to your account please contact us.</p></html>"
}
]
}`, email, code, code)
req, _ := http.NewRequest("POST", "https://api.sendgrid.com/v3/mail/send", bytes.NewBuffer([]byte(sendstr)))
skey, ok := cfg.GetString("sendgridKey")
if !ok {
log.Println("Sendgrid key is not set!")
return false
}
req.Header.Set("Authorization", "Bearer "+skey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println(err)
return false
}
defer resp.Body.Close()
log.Println("response Status:", resp.Status)
log.Println("response Headers:", resp.Header)
body, _ := io.ReadAll(resp.Body)
log.Println("response Body:", string(body))
return resp.Status == "200 Success" || resp.Status == "202 Accepted"
}
func isAprilFools() bool {
t := time.Now()
return t.Month() == 4 && ((t.Day() == 1 && t.Hour() >= 2) || (t.Day() == 2 && t.Hour() < 2))
}
func escapeBacktick(s string) string {
return strings.ReplaceAll(s, "`", "\\`")
}
func sendWebhook(url, content string) error {
if url == "" {
return errors.New("url is empty")
}
if content == "" {
return errors.New("content is empty")
}
b, err := json.Marshal(map[string]any{
"username": "Frontend",
"content": content,
})
if err != nil {
return err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(b))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
c := http.Client{Timeout: 5 * time.Second}
resp, err := c.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 && resp.StatusCode != 204 {
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf(string(responseBody))
}
return nil
}