-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandler.go
165 lines (144 loc) · 3.06 KB
/
handler.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
package main
import (
"net/http"
"strconv"
"sync"
"time"
)
type handler struct {
lhm *LinkedHashMap
lock sync.Mutex
whitelist map[string]struct{}
}
type paste struct {
text string
exp int64
}
func NewHandler(length int) *handler {
return &handler{
lhm: NewLHM(length),
lock: sync.Mutex{},
whitelist: make(map[string]struct{}),
}
}
func (h *handler) add(w http.ResponseWriter, r *http.Request) {
ok := h.checkOrigin(w, r)
if r.Method != "POST" || !ok {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
if r.ContentLength > int64(conf.MaxLength)*1024+64 {
w.WriteHeader(http.StatusRequestEntityTooLarge)
return
}
tmp := r.PostFormValue("exp")
if tmp == "" {
tmp = "1440"
}
exp, err := strconv.Atoi(tmp)
text := r.PostFormValue("text")
if text == "" || err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
data := &paste{
text: text,
exp: time.Now().Unix() + int64(exp*60),
}
key := hash(*data)
h.lock.Lock()
h.lhm.Add(key, data)
h.lock.Unlock()
http.SetCookie(w, &http.Cookie{
Name: "token_" + key,
SameSite: http.SameSiteNoneMode,
Secure: true,
Value: key,
MaxAge: exp * 60,
})
w.WriteHeader(http.StatusOK)
w.Write([]byte(key))
}
func (h *handler) get(w http.ResponseWriter, r *http.Request) {
key, ok0 := r.URL.Query()["k"]
ok1 := h.checkOrigin(w, r)
if !ok1 || !ok0 {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
h.lock.Lock()
tmp, ok := h.lhm.Get(key[0])
h.lock.Unlock()
if !ok {
w.WriteHeader(http.StatusNotFound)
return
}
text := tmp.text
w.WriteHeader(http.StatusOK)
w.Write([]byte(text))
}
func (h *handler) del(w http.ResponseWriter, r *http.Request) {
key, ok0 := r.URL.Query()["k"]
ok1 := h.checkOrigin(w, r)
cookie, err := r.Cookie("token_" + key[0])
if !ok0 || !ok1 || err != nil {
w.WriteHeader(http.StatusForbidden)
return
}
h.lock.Lock()
ok0 = h.lhm.Delete(key[0])
h.lock.Unlock()
if !ok0 {
http.SetCookie(w, &http.Cookie{
Name: cookie.Name,
SameSite: http.SameSiteNoneMode,
Secure: true,
Expires: time.Unix(1, 0),
})
w.WriteHeader(http.StatusInternalServerError)
return
}
http.SetCookie(w, &http.Cookie{
Name: cookie.Name,
Expires: time.Unix(1, 0),
})
w.WriteHeader(http.StatusOK)
}
func (h *handler) checkOrigin(w http.ResponseWriter, r *http.Request) bool {
origin := r.Header.Get("Origin")
if origin == "" {
return true
}
_, ok := h.whitelist[origin]
if !ok {
return false
}
w.Header().Add("Access-Control-Allow-Origin", origin)
w.Header().Add("Access-Control-Allow-Credentials", "true")
w.Header().Add("Content-Type", "text/plain; charset=UTF-8")
return true
}
func (h *handler) cleanUp() {
var data *paste
tmp := h.lhm.head
for {
if tmp == nil {
return
}
data = tmp.data
if data.exp <= time.Now().Unix() {
h.lock.Lock()
h.lhm.Delete(tmp.key)
h.lock.Unlock()
}
tmp = tmp.next
}
}
func (h *handler) timeToCleanUp(dur int) {
timer := time.NewTimer(time.Second * time.Duration(dur))
for {
<-timer.C
h.cleanUp()
timer.Reset(time.Second * time.Duration(dur))
}
}