Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
2025-06-08 04:37:18 +03:00
parent 594465269a
commit f7579670cc

28
main.go
View File

@@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"golang.org/x/time/rate"
"io" "io"
"math/rand" "math/rand"
"mime" "mime"
@@ -11,9 +12,8 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"sync" "sync"
"golang.org/x/time/rate" "time"
) )
var ( var (
@@ -22,9 +22,9 @@ var (
staticDir string staticDir string
expireDur time.Duration expireDur time.Duration
expireOnView bool expireOnView bool
limiters = make(map[string]*rate.Limiter) limiters = make(map[string]*rate.Limiter)
limMu sync.Mutex limMu sync.Mutex
useHTTPS bool useHTTPS bool
) )
type meta struct { type meta struct {
@@ -37,7 +37,7 @@ func getLimiter(ip string) *rate.Limiter {
defer limMu.Unlock() defer limMu.Unlock()
lim, ok := limiters[ip] lim, ok := limiters[ip]
if !ok { if !ok {
lim = rate.NewLimiter(1, 5) // 1 req/sec, burst of 5 lim = rate.NewLimiter(rate.Every(5*time.Second), 1)
limiters[ip] = lim limiters[ip] = lim
} }
return lim return lim
@@ -105,7 +105,7 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
} }
var reader io.Reader var reader io.Reader
var ext string var ext string
contentType := r.Header.Get("Content-Type") contentType := r.Header.Get("Content-Type")
if strings.HasPrefix(contentType, "multipart/form-data") { if strings.HasPrefix(contentType, "multipart/form-data") {
@@ -127,7 +127,7 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
ext = ".txt" ext = ".txt"
} }
id := randomID(6) id := randomID(6)
filename := id + ext filename := id + ext
if err := os.MkdirAll(staticDir, 0755); err != nil { if err := os.MkdirAll(staticDir, 0755); err != nil {
@@ -184,7 +184,7 @@ func viewHandler(w http.ResponseWriter, r *http.Request) {
indexHandler(w, r) indexHandler(w, r)
return return
} }
path := filepath.Join(staticDir, id) path := filepath.Join(staticDir, id)
metaPath := path + ".json" metaPath := path + ".json"
if data, err := os.ReadFile(metaPath); err == nil { if data, err := os.ReadFile(metaPath); err == nil {
@@ -222,11 +222,11 @@ func viewHandler(w http.ResponseWriter, r *http.Request) {
} }
func main() { func main() {
flag.StringVar(&domain, "domain", "localhost:8080", "domain name for URLs") flag.StringVar(&domain, "domain", "localhost:8080", "domain name for URLs")
flag.StringVar(&listenAddr, "listen", "0.0.0.0:8080", "listen address") flag.StringVar(&listenAddr, "listen", "0.0.0.0:8080", "listen address")
flag.StringVar(&staticDir, "static", "static", "directory to save pastes") flag.StringVar(&staticDir, "static", "static", "directory to save pastes")
flag.DurationVar(&expireDur, "expire", 0, "time after which paste expires (e.g. 5m, 1h)") flag.DurationVar(&expireDur, "expire", 0, "time after which paste expires (e.g. 5m, 1h)")
flag.BoolVar(&expireOnView, "expire-on-view", false, "delete paste after it's viewed once") flag.BoolVar(&expireOnView, "expire-on-view", false, "delete paste after it's viewed once")
flag.BoolVar(&useHTTPS, "https", false, "use https:// in generated URLs") flag.BoolVar(&useHTTPS, "https", false, "use https:// in generated URLs")
flag.Parse() flag.Parse()