-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
154 lines (133 loc) · 3.77 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"html/template"
"log"
"net/http"
"strings"
"time"
gz "github.com/NYTimes/gziphandler"
"github.com/catcombo/go-staticfiles"
"github.com/vsliouniaev/go-pass-cache/cache"
"github.com/vsliouniaev/go-pass-cache/util"
"github.com/vsliouniaev/go-pass-cache/www"
)
var (
c cache.Cache
ww www.Server
maxSize int64
cacheDuration time.Duration
linkProbeAgents = map[string]struct{}{
"discord": {}, "skype": {}, "whatsapp": {}, "slack": {}, "signal": {}, "telegram": {}, "zoom": {},
}
)
type Data struct {
Id string `json:"id"`
Data string `json:"data"`
}
func generic(w http.ResponseWriter, r *http.Request) {
key := r.URL.RawQuery
if key == "" {
if r.ContentLength < maxSize*1000000 {
var data Data
if err := json.NewDecoder(r.Body).Decode(&data); err == nil {
if data.Id != "" && data.Data != "" {
c.Store(data.Id, data.Data)
}
}
}
ww.RenderTemplate(w, "set.gohtml", cacheDuration)
} else {
val, ok := c.TryGet(key)
if !ok {
ww.RenderTemplate(w, "gone.gohtml", nil)
} else {
ww.RenderTemplate(w, "get.gohtml", val)
}
}
}
func filterLinkProbes(w http.ResponseWriter, r *http.Request) bool {
ua := r.Header.Get("User-Agent")
for a := range linkProbeAgents {
if strings.Contains(ua, a) {
w.WriteHeader(http.StatusNoContent)
return false
}
}
return true
}
type genericWithFilter struct{}
func (g *genericWithFilter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if filterLinkProbes(w, r) {
generic(w, r)
}
}
func main() {
var (
bind string
dev bool
keyPath string
certPath string
ignore util.ArrayFlags
)
flag.BoolVar(&dev, "dev", false, "Development mode. Default false")
flag.StringVar(&bind, "bind", ":8080", "address:port to bind to. Default :8080")
flag.StringVar(&certPath, "cert-path", "", "path to TLS certificate")
flag.StringVar(&keyPath, "key-path", "", "path to TLS key")
flag.Var(&ignore, "ignore-agents",
fmt.Sprintf("Ignore user-agent strings containing this value. Flag can be specified multiple times. Default %s)",
strings.Join(util.SortedKeys(linkProbeAgents), ", ")))
flag.DurationVar(&cacheDuration, "cache-duration", time.Minute*5, "Cache duration. Default 5m")
flag.Int64Var(&maxSize, "max-size", 10, "Max size of request in MB. Default 10MB")
flag.Parse()
if maxSize < 0 {
log.Println("max-size cannot be negative")
}
if cacheDuration < time.Second*5 {
log.Println("cache-duration < 5s is ridiculous")
}
for _, strMatch := range ignore {
linkProbeAgents[strings.ToLower(strMatch)] = struct{}{}
}
c = cache.New(cacheDuration)
staticLoc := "/www/static/"
fns, staticHandler := initStatic(staticLoc, dev)
ww = www.Init(fns)
var strat util.CacheStrategy
if dev {
strat = util.Never
} else {
strat = util.Forever
}
http.Handle(staticLoc, util.NewCacheHandler(strat, gz.GzipHandler(staticHandler)))
http.Handle("/", util.NewCacheHandler(util.Never, gz.GzipHandler(&genericWithFilter{})))
if certPath != "" && keyPath != "" {
if err := http.ListenAndServeTLS(bind, certPath, keyPath, nil); err != http.ErrServerClosed {
log.Fatal(err)
}
} else {
if err := http.ListenAndServe(bind, nil); err != http.ErrServerClosed {
log.Fatal(err)
}
}
}
func initStatic(staticLoc string, dev bool) (template.FuncMap, http.Handler) {
storage, err := staticfiles.NewStorage(".static")
if err != nil {
log.Fatal(err)
}
storage.AddInputDir(strings.Trim(staticLoc, "/"))
if err = storage.CollectStatic(); err != nil {
log.Fatal(err)
}
fns := template.FuncMap{
"static": func(relPath string) string {
return staticLoc + storage.Resolve(relPath)
},
}
storage.OutputDirList = false
storage.Enabled = !dev
return fns, http.StripPrefix(staticLoc, http.FileServer(storage))
}