-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
311 lines (256 loc) · 5.94 KB
/
main.go
File metadata and controls
311 lines (256 loc) · 5.94 KB
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
package main
import (
"crypto/rand"
"embed"
"encoding/hex"
"fmt"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
//go:embed templates/*
var templateFiles embed.FS
func generateID() string {
bytes := make([]byte, 8)
rand.Read(bytes)
return hex.EncodeToString(bytes)
}
type Paste struct {
ID string
Title string
Body []byte
TTL string
}
var TTLHours = map[string]int{
"1h": 1,
"3h": 3,
"6h": 6,
"12h": 12,
"24h": 24,
"3d": 72,
"7d": 168,
}
func (p *Paste) save() error {
// Create subdirectory using first 2 chars of ID (256 buckets)
subdir := fmt.Sprintf("pastes/%s", p.ID[:2])
os.MkdirAll(subdir, 0755)
// Save content as plain text
content := p.Title + "\n" + string(p.Body)
filename := fmt.Sprintf("%s/%s_%s.txt", subdir, p.ID, p.TTL)
file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer file.Close()
_, err = file.Write([]byte(content))
if err != nil {
return err
}
// Force sync to disk
err = file.Sync()
if err != nil {
return err
}
return nil
}
var cleanupOffset int
func cleanupExpired() {
now := time.Now().Unix()
// Process 16 subdirs per cycle (full scan in ~8 hours)
start := cleanupOffset
end := cleanupOffset + 16
for i := start; i < end; i++ {
subdir := fmt.Sprintf("pastes/%02x", i)
entries, err := os.ReadDir(subdir)
if err != nil {
continue
}
for _, entry := range entries {
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".txt") {
continue
}
// Parse filename: id_ttl.txt
name := strings.TrimSuffix(entry.Name(), ".txt")
parts := strings.Split(name, "_")
if len(parts) != 2 {
continue
}
// Get file modification time
filePath := filepath.Join(subdir, entry.Name())
info, err := os.Stat(filePath)
if err != nil {
continue
}
createdAt := info.ModTime().Unix()
// Calculate expiration using TTL
ttlHours, exists := TTLHours[parts[1]]
if !exists {
continue
}
expiresAt := createdAt + int64(ttlHours*3600)
if now > expiresAt {
os.Remove(filePath)
}
}
}
cleanupOffset = (cleanupOffset + 16) % 256
}
func loadPaste(id string) (*Paste, error) {
// Find file by scanning subdirectory for matching ID
subdir := fmt.Sprintf("pastes/%s", id[:2])
files, err := filepath.Glob(subdir + "/" + id + "_*.txt")
if err != nil || len(files) == 0 {
return nil, fmt.Errorf("paste not found")
}
filename := files[0]
// Use file mtime as creation time
info, err := os.Stat(filename)
if err != nil {
return nil, err
}
createdAt := info.ModTime().Unix()
// Parse TTL from filename
basename := filepath.Base(filename)
parts := strings.Split(strings.TrimSuffix(basename, ".txt"), "_")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid paste file format")
}
ttl := parts[1]
ttlHours, exists := TTLHours[ttl]
if !exists {
return nil, fmt.Errorf("invalid TTL")
}
expiresAt := createdAt + int64(ttlHours*3600)
// Check if expired
if time.Now().Unix() > expiresAt {
os.Remove(filename) // Clean up expired paste
return nil, fmt.Errorf("paste expired")
}
content, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
lines := strings.SplitN(string(content), "\n", 2)
if len(lines) < 2 {
return nil, fmt.Errorf("invalid paste content")
}
return &Paste{
ID: id,
Title: lines[0],
Body: []byte(lines[1]),
TTL: ttl,
}, nil
}
func saveHandler(w http.ResponseWriter, r *http.Request) {
// Only allow POST requests
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
title := r.FormValue("title")
body := r.FormValue("body")
ttl := r.FormValue("ttl")
// Basic size limits
if len(title) > 200 {
http.Error(w, "Title too long (max 200 chars)", http.StatusBadRequest)
return
}
if len(body) > 1024*1024 { // 1MB limit
http.Error(w, "Content too large (max 1MB)", http.StatusBadRequest)
return
}
if title == "" || body == "" {
http.Error(w, "Title and content required", http.StatusBadRequest)
return
}
// Default to 6h if no TTL specified
if ttl == "" {
ttl = "6h"
}
// Validate TTL
_, exists := TTLHours[ttl]
if !exists {
http.Error(w, "Invalid TTL", http.StatusBadRequest)
return
}
id := generateID()
p := &Paste{
ID: id,
Title: title,
Body: []byte(body),
TTL: ttl,
}
err := p.save()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/"+id, http.StatusFound)
}
var templates = template.Must(template.ParseFS(templateFiles, "templates/*.html"))
func renderTemplate(w http.ResponseWriter, tmpl string, p *Paste) {
err := templates.ExecuteTemplate(w, tmpl+".html", p)
if err != nil {
log.Printf("Template error: %v", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
}
func isValidID(id string) bool {
// Only allow hex characters, 16 chars long (8 bytes * 2)
if len(id) != 16 {
return false
}
for _, c := range id {
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) {
return false
}
}
return true
}
func mainHandler(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
switch path {
case "/":
renderTemplate(w, "index", nil)
return
case "/about":
renderTemplate(w, "about", nil)
return
case "/legal":
renderTemplate(w, "legal", nil)
return
}
id := strings.TrimPrefix(path, "/")
// Validate ID format
if !isValidID(id) {
http.NotFound(w, r)
return
}
p, err := loadPaste(id)
if err != nil {
http.NotFound(w, r)
return
}
renderTemplate(w, "view", p)
}
func main() {
// Cleanup job runs every 30min
go func() {
for {
time.Sleep(30 * time.Minute)
cleanupExpired()
}
}()
http.HandleFunc("/", mainHandler)
http.HandleFunc("/save", saveHandler)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Printf("Starting server on port %s", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}