forked from rramiachraf/dumb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
119 lines (98 loc) · 2.52 KB
/
utils.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
package main
import (
"encoding/json"
"fmt"
"net"
"net/http"
"net/url"
"path"
"text/template"
"time"
"github.com/allegro/bigcache/v3"
"github.com/caffix/cloudflare-roundtripper/cfrt"
)
var cache *bigcache.BigCache
func setCache(key string, entry interface{}) error {
data, err := json.Marshal(&entry)
if err != nil {
return err
}
return cache.Set(key, data)
}
func getCache(key string) (interface{}, error) {
data, err := cache.Get(key)
if err != nil {
return nil, err
}
var decoded interface{}
if err = json.Unmarshal(data, &decoded); err != nil {
return nil, err
}
return decoded, nil
}
func write(w http.ResponseWriter, status int, data []byte) {
w.WriteHeader(status)
w.Write(data)
}
func securityHeaders(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
csp := "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self'; object-src 'none'"
w.Header().Add("content-security-policy", csp)
w.Header().Add("referrer-policy", "no-referrer")
w.Header().Add("x-content-type-options", "nosniff")
next.ServeHTTP(w, r)
})
}
func getTemplates(templates ...string) []string {
var pths []string
for _, t := range templates {
tmpl := path.Join("views", fmt.Sprintf("%s.tmpl", t))
pths = append(pths, tmpl)
}
return pths
}
func render(n string, w http.ResponseWriter, data interface{}) {
w.Header().Set("content-type", "text/html")
t := template.New(n + ".tmpl").Funcs(template.FuncMap{"extractURL": extractURL})
t, err := t.ParseFiles(getTemplates(n, "navbar", "footer")...)
if err != nil {
logger.Errorln(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if err = t.Execute(w, data); err != nil {
logger.Errorln(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
const UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
var client = &http.Client{
Timeout: 20 * time.Second,
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 15 * time.Second,
KeepAlive: 15 * time.Second,
DualStack: true,
}).DialContext,
},
}
func sendRequest(u string) (*http.Response, error) {
url, err := url.Parse(u)
if err != nil {
return nil, err
}
client.Transport, err = cfrt.New(client.Transport)
if err != nil {
return nil, err
}
req := &http.Request{
Method: http.MethodGet,
URL: url,
Header: map[string][]string{
"Accept-Language": {"en-US"},
"User-Agent": {UA},
},
}
return client.Do(req)
}