aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: c03a0d75c5ad1bf5401cfdd9c6b52e98429adf51 (plain) (blame)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
package main

import (
	"bytes"
	"crypto/rand"
	"embed"
	"encoding/base64"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"io"
	"io/fs"
	"log"
	"net/http"
	"net/http/httputil"
	"net/url"
	"os"
	"path/filepath"
	"sort"
	"strconv"
	"strings"
	"time"

	jwt "github.com/golang-jwt/jwt/v4"
	"golang.org/x/crypto/bcrypt"
	"golang.org/x/term"
)

// Default to the system umask
const OPEN_MODE = 0777

var (
	Version  = "git or development"
	logger   = log.New(os.Stderr, "", 0)
	ID_BYTES = 8

	embeddedPrefix = "ui/build" // Keep in sync with staticEmbedded below
	//go:embed all:ui/build/*
	staticEmbedded embed.FS
)

type App struct {
	static        fs.FS
	users         map[string]string
	jwtKey        string
	sessionHours  int
	storage       string // path to where the paste files are actually stored
	staticHandler http.Handler
}

// EnvFlagString is a convent way to set value from environment variable,
// and allow override when a command line flag is set. It's assumed `p` is
// not nil.
func EnvFlagString(fl *flag.FlagSet, p *string, name, envvar, usage string) {
	if v := os.Getenv(envvar); v != "" {
		*p = v
	}
	fl.StringVar(p, name, *p, fmt.Sprintf("%s (Environ: '%s')", usage, envvar))
}

// Response encapsulates all the JSON responses from this server, Code
// is a copy of the HTTP status code, Msg is a human-readable statement
// about the particular response. Data is optional
type Response struct {
	w    http.ResponseWriter
	Code int
	Msg  string
	Data any `json:",omitempty"`
}

func main() {
	var (
		listen        = ":6130"
		idBytes       = "8"
		debugF        = "false"
		debug         bool
		genhash       = false
		storage       = ""
		fsdir         = ""
		proxyURL      = ""
		jwtKey        = ""
		sessionHours  = "12"
		rSessionHours = 12
		static        fs.FS
		err           error
	)

	static, err = fs.Sub(staticEmbedded, embeddedPrefix)
	if err != nil {
		logger.Fatal("Embedding failed no static directory")
	}

	fl := flag.NewFlagSet("Simple pastebin", flag.ExitOnError)
	EnvFlagString(fl, &listen, "listen", "LISTEN_ADDR",
		"Address to bind to")
	EnvFlagString(fl, &idBytes, "b", "ID_BYTES",
		"How many bytes long should the IDs be")
	EnvFlagString(fl, &debugF, "d", "DEBUG",
		"Additional debugging if set to true")
	EnvFlagString(fl, &storage, "s", "STORAGE_DIR",
		"Path of directory to serve")
	EnvFlagString(fl, &fsdir, "fs", "FS_DIR",
		"Path which static assets are located, empty to use embedded")
	EnvFlagString(fl, &proxyURL, "sp", "STATIC_PROXY",
		"What server do we proxy to for static content?")
	EnvFlagString(fl, &jwtKey, "jwt", "JWT_KEY",
		"If supplied use the value as the JWT key instead of a random value")
	EnvFlagString(fl, &sessionHours, "hours", "SESSION_HOURS",
		"How many hours should login sessions last?")
	fl.BoolVar(&genhash, "genhash", genhash,
		"Interactively prompt for a password and spit out a hash\n")
	version := fl.Bool("v", false, "Print version then exit")

	_ = fl.Parse(os.Args[1:])

	if *version {
		log.Println(Version)
		os.Exit(0)
	}

	if genhash {
		interactiveHashGen()
		os.Exit(0)
	}

	debug, err = strconv.ParseBool(debugF)
	if err != nil {
		logger.Println("Warning invalid value for debug: ", debugF)
	}

	if debug {
		logger.SetFlags(log.LstdFlags | log.Llongfile)
	}

	if fsdir != "" {
		logger.Println("Using filesystem directory for assets: ", fsdir)
		static = os.DirFS(fsdir)
	}

	if b, err := strconv.Atoi(idBytes); err == nil && b > 4 {
		logger.Printf("Setting ID_BYTES: %d\n", b)
		ID_BYTES = b
	}

	if storage == "" {
		logger.Fatal("Cannot continue without storage directory, set " +
			"`-s` flag or STORAGE_DIR environment variable")
	}

	if jwtKey == "" {
		jwtKey = genTokenKey()
	}

	if h, err := strconv.Atoi(sessionHours); err == nil && h > 0 {
		logger.Printf("Setting SESSION_HOURS: %d\n", h)
		rSessionHours = h
	}

	err = os.MkdirAll(storage, 0777)
	if err != nil {
		logger.Fatal("Failed to create storage directory: ", err)
	}

	if debug {
		dumpFStree(static)
	}

	app := &App{
		static:       static,
		storage:      storage,
		jwtKey:       jwtKey,
		sessionHours: rSessionHours,
		users:        getUsersFromEnviron(),
	}

	rp, err := getProxyHandler(proxyURL)
	if proxyURL != "" && err == nil {
		app.staticHandler = rp
		logger.Println("Proxying static requests to: ", proxyURL)
	} else if err != nil {
		logger.Printf("Warning, invalid url: '%s': %s", proxyURL, err)
	}

	logger.Println("listening on: ", listen)

	srv := &http.Server{
		Handler:      logRequests(app.Handler()),
		Addr:         listen,
		WriteTimeout: 15 * time.Second,
		ReadTimeout:  15 * time.Second,
	}
	logger.Fatal(srv.ListenAndServe())
}

func dumpFStree(f fs.FS) {
	logger.Println("dumping fs tree....")
	_ = fs.WalkDir(f, ".", func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}

		if d.IsDir() {
			return nil
		}

		logger.Println(path)
		return nil
	})
}

// getProxyHandler returns a httputil.NewSingleHostReverseProxy for the
// url provided, and errors parsing the URL, if any.
func getProxyHandler(proxyURL string) (http.Handler, error) {
	pu, err := url.Parse(proxyURL)
	if err != nil {
		return nil, err
	}
	rp := httputil.NewSingleHostReverseProxy(pu)
	return rp, nil
}

func logRequests(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		logger.Printf("%s %s %s \"%s\" \"%s\"\n",
			r.RemoteAddr, r.Method, r.URL.Path, r.UserAgent(), r.Referer())
		next.ServeHTTP(w, r)
	})
}

func interactiveHashGen() {
	fmt.Print("Enter password: ")
	passwd, err := term.ReadPassword(0)
	if err != nil {
		logger.Fatal("\nFailed: ", err)
	}

	fmt.Printf("\nAgain: ")
	passwd2, err := term.ReadPassword(0)
	if err != nil {
		logger.Fatal("\nFailed: ", err)
	}

	fmt.Println("")
	if !bytes.Equal(passwd, passwd2) {
		logger.Fatal("Passwords do not match")
	}

	passwd, err = bcrypt.GenerateFromPassword(passwd, bcrypt.DefaultCost)
	if err != nil {
		logger.Fatal("Failed: ", err)
	}

	fmt.Printf("hash: %s\n", string(passwd))
}

func (a *App) Handler() http.Handler {
	mux := http.NewServeMux()

	secHandlers := map[string]http.Handler{
		"/api/v0/new":  a.HandleNew(),
		"/api/v0/list": a.HandleList(),
		"/api/v0/del/": http.StripPrefix(
			"/api/v0/del/",
			a.HandleDel()),

		"/api/v1/new":  a.HandleNewJSON(),
		"/api/v1/list": a.HandleListJSON(),
		"/api/v1/del/": http.StripPrefix(
			"/api/v1/del/",
			a.HandleDelJSON()),
	}

	handlers := map[string]http.Handler{
		"/api/v1/view/": http.StripPrefix(
			"/api/v1/view/",
			a.HandleViewJSON()),

		"/api/v0/view/": http.StripPrefix(
			"/api/v0/view/",
			a.HandleViewPlain()),

		"/api/v1/login":  loginHandler(a.users, a.jwtKey, a.sessionHours),
		"/api/v1/logout": logoutHandler(),

		"/": http.FileServer(http.FS(a.static)),
	}

	if a.staticHandler != nil {
		handlers["/"] = a.staticHandler
	}

	if len(a.users) > 0 {
		for user := range a.users {
			logger.Println("Found user:", user)
		}

		for pth, handler := range secHandlers {
			mux.Handle(pth, authHandler(
				handler,
				a.users,
				a.jwtKey,
			))
		}
	} else {
		_, _ = fmt.Fprintf(os.Stderr,
			"\033[1;31mWARNING: RUNNING WITH NO AUTHENTICATION\033[0m\n")

		for pth, handler := range secHandlers {
			mux.Handle(pth, handler)
		}
	}

	for pth, handler := range handlers {
		mux.Handle(pth, handler)
	}

	return mux
}

func getUsersFromEnviron() map[string]string {
	users := map[string]string{}
	for _, entry := range os.Environ() {
		if !strings.HasPrefix(entry, "USER_") {
			continue
		}

		e := strings.SplitN(entry, "=", 2)
		key := e[0]
		val := e[1]

		username := strings.TrimPrefix(key, "USER_")

		users[username] = val
	}
	return users
}

func genTokenKey() string {
	r := make([]byte, 16) // 128 bits
	_, err := rand.Read(r)

	if err != nil {
		panic(fmt.Errorf("reading random bytes: %w", err))
	}

	return base64.RawURLEncoding.EncodeToString(r)
}

func sendJSON(er Response) {
	er.w.WriteHeader(er.Code)
	er.w.Header().Add("Content-type", "application/json")
	enc := json.NewEncoder(er.w)
	_ = enc.Encode(er)
}

// sendPlain takes in a response struct, and writes the msg string to the
// output verbatim unless the rdr is not nil, in which case the rdr
// is used instead
func sendPlain(r Response, rdr io.Reader) {
	r.w.WriteHeader(r.Code)
	r.w.Header().Add("Content-type", "application/json")
	if rdr != nil {
		_, _ = io.Copy(r.w, rdr)
	} else {
		_, _ = r.w.Write([]byte(r.Msg))
	}
}

func GenId() string {
	r := make([]byte, ID_BYTES)
	_, err := rand.Read(r)
	if err != nil {
		logger.Fatal(err)
	}

	return base64.RawURLEncoding.EncodeToString(r)
}

func hasValidJWT(users map[string]string, tokenString, jwtKey string) bool {
	token, err := jwt.ParseWithClaims(tokenString,
		&jwt.RegisteredClaims{},
		func(token *jwt.Token) (interface{}, error) {
			return []byte(jwtKey), nil
		})
	if err != nil {
		logger.Printf("failed parsing token: %s", err)
		return false
	}

	if !token.Valid {
		return false
	}

	// logger.Printf("Type of token.Claims: %s\n", reflect.TypeOf(token.Claims))
	claims, ok := token.Claims.(*jwt.RegisteredClaims)
	if !ok {
		logger.Println("Invalid claims for token")
		return false
	}

	_, ok = users[claims.ID]
	if !ok {
		return false
	}

	return err == nil
}

func isValidLogin(users map[string]string, username, password string) bool {
	if _, haveUser := users[username]; !haveUser {
		return false
	}

	err := bcrypt.CompareHashAndPassword(
		[]byte(users[username]),
		[]byte(password),
	)

	if errors.Is(err, bcrypt.ErrHashTooShort) {
		logger.Println("Hash too short for username: ",
			username)
	}

	return err == nil
}

func hasValidPlainAuth(r *http.Request, users map[string]string) bool {
	username, passwd, ok := r.BasicAuth()
	if !ok {
		return false
	}

	return isValidLogin(users, username, passwd)
}

// getCookie simply returns a cookie value if any, or an empty string if
// it's invalid or there are any errors.
func getCookie(r *http.Request, name string) string {
	c, err := r.Cookie(name)
	// Normally I'd also check c.Valid()... Expires is optional, but
	// apparently makes it invalid?
	if err != nil {
		return ""
	}
	fmt.Printf("Cookie value: '%s'\n", c.Value)
	return c.Value
}

// respAskForBasicLogin simply adds a header telling web browsers we need
// authentication
func respAskForBasicLogin(w http.ResponseWriter) {
	w.Header().Add("WWW-Authenticate",
		"Basic realm=\"Login\", charset=\"UTF-8\"")
}

func authHandler(next http.Handler, users map[string]string, jwtKey string,
) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		respUnAuth := func() {
			// respAskForBasicLogin(w)
			sendJSON(Response{w, http.StatusUnauthorized, "Unauthorized", nil})
		}

		if !hasValidPlainAuth(r, users) &&
			!hasValidJWT(users, getCookie(r, "Auth"), jwtKey) {

			respUnAuth()
			return
		}

		next.ServeHTTP(w, r)
	})
}

func loginHandler(users map[string]string, jwtKey string, sessionHours int,
) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "POST" {
			sendJSON(Response{w, http.StatusBadRequest,
				"Invalid type. POST only", nil})
			return
		}

		err := r.ParseForm()
		if err != nil {
			sendJSON(Response{w, http.StatusBadRequest, "Invalid form", nil})
			return
		}

		username := r.PostFormValue("username")
		password := r.PostFormValue("password")

		if !isValidLogin(users, username, password) {
			sendJSON(Response{w, http.StatusUnauthorized,
				"Invalid username or password", nil})
			return
		}
		expires := time.Now().Add(time.Hour * time.Duration(sessionHours))

		claims := &jwt.RegisteredClaims{
			ExpiresAt: jwt.NewNumericDate(expires),
			ID:        username,
		}

		token := jwt.NewWithClaims(jwt.SigningMethodHS512, claims)
		ss, err := token.SignedString([]byte(jwtKey))
		if err != nil {
			sendJSON(Response{w, http.StatusInternalServerError,
				"Invalid username or password", nil})
			return
		}

		http.SetCookie(w, &http.Cookie{
			Name:     "Auth",
			HttpOnly: true,
			SameSite: http.SameSiteStrictMode,
			Value:    ss,
		})

		http.Redirect(w, r, "/", http.StatusFound)
	})
}

func logoutHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.SetCookie(w, &http.Cookie{
			Name:     "Auth",
			HttpOnly: true,
			SameSite: http.SameSiteStrictMode,
			Value:    "logout",
			Expires:  time.Now().Add(time.Second), //nolint
		})

		http.Redirect(w, r, "/", http.StatusFound)
	})
}

func OpenW(filename string) (*os.File, error) {
	return os.OpenFile(filename, os.O_CREATE|os.O_EXCL|os.O_RDWR, OPEN_MODE)
}

func (a *App) HandleNew() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		id := GenId()

		fh, err := OpenW(filepath.Join(a.storage, id))
		if err != nil {
			sendPlain(Response{w, http.StatusInternalServerError,
				"Internal server error", nil}, nil)
			return
		}

		_, err = io.Copy(fh, r.Body)
		if err != nil {
			sendPlain(Response{w, http.StatusInternalServerError,
				"Internal server error", nil}, nil)
			return
		}

		sendPlain(Response{w, http.StatusOK, id + "\n", nil}, nil)
	})
}

type NewPost struct {
	Content string `json:"content"`
}

func (a *App) HandleNewJSON() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		id := GenId()

		p := &NewPost{}

		dec := json.NewDecoder(r.Body)

		err := dec.Decode(p)
		if err != nil || p.Content == "" {
			sendJSON(Response{w, http.StatusBadRequest,
				"Malformed JSON", nil})
			return
		}

		fh, err := OpenW(filepath.Join(a.storage, id))
		if err != nil {
			sendJSON(Response{w, http.StatusInternalServerError,
				"Internal server error", nil})
			return
		}

		_, err = fh.Write([]byte(p.Content))
		if err != nil {
			sendJSON(Response{w, http.StatusInternalServerError,
				"Internal server error", nil})
			return
		}

		sendJSON(Response{w, http.StatusOK, "OK",
			struct{ Id string }{id}})
	})
}

func getPastePathFromRawURL(storage, u string) string {
	return filepath.Join(storage, strings.ReplaceAll(
		filepath.Clean(u), "/", ""))
}

func (a *App) HandleViewJSON() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		pth := getPastePathFromRawURL(a.storage, r.URL.Path)
		fh, err := os.Open(pth)
		if err != nil {
			sendJSON(Response{w, http.StatusNotFound,
				"ID not found", nil})
			return
		}

		b, err := io.ReadAll(fh)
		if err != nil {
			sendJSON(Response{w, http.StatusInternalServerError,
				"Internal server error", nil})
			return
		}

		w.WriteHeader(http.StatusOK)
		w.Header().Add("Content-type", "application/json")
		enc := json.NewEncoder(w)
		_ = enc.Encode(struct {
			Content string
		}{string(b)})
	})
}

func (a *App) HandleViewPlain() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		pth := getPastePathFromRawURL(a.storage, r.URL.Path)
		fh, err := os.Open(pth)
		if err != nil {
			sendPlain(Response{w, http.StatusNotFound,
				"ID not found", nil}, nil)
			return
		}
		defer fh.Close()

		sendPlain(Response{w, http.StatusOK, "", nil}, fh)
	})
}

func (a *App) HandleDel() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		pth := getPastePathFromRawURL(a.storage, r.URL.Path)

		err := os.Remove(pth)
		if err != nil {
			if os.IsNotExist(err) {
				sendPlain(
					Response{w, http.StatusNotFound, "Not found", nil},
					nil)
				return
			}
			sendPlain(Response{w, http.StatusInternalServerError,
				"Internal server error", nil}, nil)
			return
		}

		sendPlain(Response{w, http.StatusOK, "Ok", nil}, nil)
	})
}

func (a *App) HandleDelJSON() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		logger.Println("Made it to delete...")
		pth := getPastePathFromRawURL(a.storage, r.URL.Path)
		logger.Println("Deleting path: ", pth)
		err := os.Remove(pth)
		if err != nil {
			if os.IsNotExist(err) {
				sendJSON(Response{w, http.StatusNotFound, "Not found", nil})
				return
			}
			sendJSON(Response{w, http.StatusInternalServerError,
				"Internal server error", nil})
			return
		}

		sendJSON(Response{w, http.StatusOK, "Ok", nil})
	})
}

func (a *App) HandleList() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		pl, err := LoadPastes(a.storage)
		if err != nil {
			sendPlain(Response{w, http.StatusInternalServerError,
				"Internal server error", nil}, nil)
			return
		}

		pl = handleSkipLimitSort(pl, r.URL)

		for _, e := range pl {
			_, _ = w.Write([]byte(fmt.Sprintf("%d\t%s\t%s\n",
				e.Size,
				e.Created.Format("2006-01-02 15:04 MST"),
				e.Id)))
		}
	})
}

func (a *App) HandleListJSON() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		pl, err := LoadPastes(a.storage)
		if err != nil {
			sendJSON(Response{w, http.StatusInternalServerError,
				"Internal server error", nil})
			return
		}

		pl = handleSkipLimitSort(pl, r.URL)

		enc := json.NewEncoder(w)
		enc.SetIndent("", "  ")
		_ = enc.Encode(pl)
	})
}

type Paste struct {
	Id      string    `json:"id"`
	Created time.Time `json:"created"`
	Size    int64     `json:"size"`
}

type PasteListing []*Paste

func LoadPastes(pth string) (PasteListing, error) {
	out := PasteListing{}

	de, err := os.ReadDir(pth)
	if err != nil {
		return nil, err
	}

	for _, e := range de {
		if e.IsDir() {
			continue
		}

		info, err := e.Info()
		if err != nil {
			return nil, err
		}

		out = append(out, &Paste{
			Id:      e.Name(),
			Created: info.ModTime(),
			Size:    info.Size(),
		})
	}

	out.SortDate()
	return out, nil
}

func (pl PasteListing) SortDateReverse() {
	sort.Slice(pl, func(i, j int) bool {
		return pl[i].Created.Before(pl[j].Created)
	})
}

func (pl PasteListing) SortDate() {
	sort.Slice(pl, func(i, j int) bool {
		return pl[i].Created.After(pl[j].Created)
	})
}

func getInt(u *url.URL, param string) (int, error) {
	i := u.Query().Get(param)
	if i == "" {
		return 0, fmt.Errorf("no param '%s' supplied", param)
	}

	n, err := strconv.Atoi(i)
	if err != nil {
		return 0, err
	}

	return n, nil
}

func limitSlice[T any](s []T, nElem int) []T {
	if nElem > len(s) {
		return s
	}
	return s[:nElem]
}

func skipSlice[T any](s []T, skip int) []T {
	if skip > len(s) {
		return []T{}
	}

	return s[skip:]
}

func handleSkipLimitSort(pl PasteListing, URL *url.URL) PasteListing {
	if _, ok := URL.Query()["reverse"]; ok {
		pl.SortDateReverse()
	}

	skip, err := getInt(URL, "skip")
	if err == nil {
		pl = skipSlice(pl, skip)
	}

	limit, err := getInt(URL, "limit")
	if err == nil {
		pl = limitSlice(pl, limit)
	}

	return pl
}