2022-09-08 15:12:06 +00:00
|
|
|
/*
|
2024-01-14 18:39:14 +00:00
|
|
|
Copyright © 2024 Seednode <seednode@seedno.de>
|
2022-09-08 15:12:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-05-31 17:43:07 +00:00
|
|
|
"errors"
|
2022-09-10 23:03:04 +00:00
|
|
|
"fmt"
|
2022-09-08 15:12:06 +00:00
|
|
|
"io"
|
2023-05-08 16:45:57 +00:00
|
|
|
"net"
|
2022-09-08 15:12:06 +00:00
|
|
|
"net/http"
|
2022-09-08 15:57:59 +00:00
|
|
|
"os"
|
2022-09-16 18:45:33 +00:00
|
|
|
"path/filepath"
|
2024-01-15 14:51:41 +00:00
|
|
|
"regexp"
|
2022-10-23 21:29:58 +00:00
|
|
|
"runtime"
|
2022-09-08 15:12:06 +00:00
|
|
|
"strconv"
|
2022-09-08 20:57:52 +00:00
|
|
|
"strings"
|
2023-01-21 04:42:44 +00:00
|
|
|
"sync"
|
2023-09-17 01:15:39 +00:00
|
|
|
"syscall"
|
2022-09-10 23:03:04 +00:00
|
|
|
"time"
|
2022-11-10 05:17:19 +00:00
|
|
|
|
2023-06-03 18:29:49 +00:00
|
|
|
"github.com/julienschmidt/httprouter"
|
2022-11-10 05:17:19 +00:00
|
|
|
"github.com/yosssi/gohtml"
|
2023-09-12 21:32:19 +00:00
|
|
|
"seedno.de/seednode/roulette/types"
|
2023-09-13 04:35:17 +00:00
|
|
|
"seedno.de/seednode/roulette/types/audio"
|
2023-09-14 22:37:22 +00:00
|
|
|
"seedno.de/seednode/roulette/types/code"
|
2023-09-13 04:35:17 +00:00
|
|
|
"seedno.de/seednode/roulette/types/flash"
|
|
|
|
"seedno.de/seednode/roulette/types/images"
|
|
|
|
"seedno.de/seednode/roulette/types/text"
|
|
|
|
"seedno.de/seednode/roulette/types/video"
|
2022-09-08 15:12:06 +00:00
|
|
|
)
|
|
|
|
|
2022-10-20 22:12:29 +00:00
|
|
|
const (
|
2023-09-13 14:26:15 +00:00
|
|
|
logDate string = `2006-01-02T15:04:05.000-07:00`
|
|
|
|
sourcePrefix string = `/source`
|
|
|
|
mediaPrefix string = `/view`
|
2023-09-28 09:42:30 +00:00
|
|
|
redirectStatusCode int = http.StatusSeeOther
|
2023-09-13 14:26:15 +00:00
|
|
|
timeout time.Duration = 10 * time.Second
|
2023-09-06 16:53:19 +00:00
|
|
|
)
|
2023-05-12 14:05:14 +00:00
|
|
|
|
2024-01-30 19:07:59 +00:00
|
|
|
func newPage(title, body, nonce string) string {
|
2024-01-06 00:50:45 +00:00
|
|
|
var htmlBody strings.Builder
|
|
|
|
|
|
|
|
htmlBody.WriteString(`<!DOCTYPE html><html lang="en"><head>`)
|
2024-01-30 19:07:59 +00:00
|
|
|
htmlBody.WriteString(getFavicon(nonce))
|
|
|
|
htmlBody.WriteString(fmt.Sprintf(`<style nonce=%q>`, nonce))
|
|
|
|
htmlBody.WriteString(`html,body,a{display:block;height:100%;width:100%;text-decoration:none;color:inherit;cursor:auto;}</style>`)
|
2024-01-06 00:50:45 +00:00
|
|
|
htmlBody.WriteString(fmt.Sprintf("<title>%s</title></head>", title))
|
|
|
|
htmlBody.WriteString(fmt.Sprintf("<body><a href=\"/\">%s</a></body></html>", body))
|
2023-09-17 01:15:39 +00:00
|
|
|
|
2024-01-06 00:50:45 +00:00
|
|
|
return htmlBody.String()
|
|
|
|
}
|
|
|
|
|
2023-09-28 15:09:45 +00:00
|
|
|
func serveStaticFile(paths []string, index *fileIndex, errorChannel chan<- error) httprouter.Handle {
|
2023-06-03 18:29:49 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
2024-01-30 19:22:42 +00:00
|
|
|
w.Header().Add("Content-Security-Policy", "default-src 'self';")
|
|
|
|
|
2023-09-14 04:24:29 +00:00
|
|
|
prefix := Prefix + sourcePrefix
|
|
|
|
|
|
|
|
path := strings.TrimPrefix(r.URL.Path, prefix)
|
2023-06-03 18:29:49 +00:00
|
|
|
|
|
|
|
prefixedFilePath, err := stripQueryParams(path)
|
2023-05-08 18:12:51 +00:00
|
|
|
if err != nil {
|
2023-09-15 19:28:21 +00:00
|
|
|
errorChannel <- err
|
2023-06-03 23:45:32 +00:00
|
|
|
|
2023-08-06 02:38:28 +00:00
|
|
|
serverError(w, r, nil)
|
2023-06-20 15:24:12 +00:00
|
|
|
|
2023-05-08 18:12:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-14 04:24:29 +00:00
|
|
|
filePath, err := filepath.EvalSymlinks(strings.TrimPrefix(prefixedFilePath, prefix))
|
2023-05-08 18:12:51 +00:00
|
|
|
if err != nil {
|
2023-09-15 19:28:21 +00:00
|
|
|
errorChannel <- err
|
2023-06-03 23:45:32 +00:00
|
|
|
|
2023-08-06 02:38:28 +00:00
|
|
|
serverError(w, r, nil)
|
2023-06-20 15:24:12 +00:00
|
|
|
|
2023-05-08 18:12:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !pathIsValid(filePath, paths) {
|
|
|
|
notFound(w, r, filePath)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := fileExists(filePath)
|
2023-01-25 01:06:15 +00:00
|
|
|
if err != nil {
|
2023-09-15 19:28:21 +00:00
|
|
|
errorChannel <- err
|
|
|
|
|
2023-08-06 02:38:28 +00:00
|
|
|
serverError(w, r, nil)
|
2023-06-20 15:24:12 +00:00
|
|
|
|
2023-05-08 18:12:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
notFound(w, r, filePath)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
buf, err := os.ReadFile(filePath)
|
|
|
|
if err != nil {
|
2023-09-15 19:28:21 +00:00
|
|
|
errorChannel <- err
|
|
|
|
|
2023-08-06 02:38:28 +00:00
|
|
|
serverError(w, r, nil)
|
2023-06-20 15:24:12 +00:00
|
|
|
|
2023-05-08 18:12:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-17 01:15:39 +00:00
|
|
|
var status string
|
|
|
|
|
|
|
|
written, err := w.Write(buf)
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, syscall.EPIPE):
|
|
|
|
status = " (incomplete)"
|
|
|
|
case err != nil:
|
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
serverError(w, r, nil)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2023-05-08 18:12:51 +00:00
|
|
|
|
2023-09-15 20:13:45 +00:00
|
|
|
refererUri, err := stripQueryParams(refererToUri(r.Referer()))
|
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
serverError(w, r, nil)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if Russian && refererUri != "" {
|
2023-09-28 15:09:45 +00:00
|
|
|
err = kill(filePath, index)
|
2023-09-10 01:57:50 +00:00
|
|
|
if err != nil {
|
2023-09-15 19:28:21 +00:00
|
|
|
errorChannel <- err
|
|
|
|
|
2023-09-10 01:57:50 +00:00
|
|
|
serverError(w, r, nil)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-12 18:06:45 +00:00
|
|
|
if Verbose {
|
2023-09-26 10:29:55 +00:00
|
|
|
fmt.Printf("%s | SERVE: %s (%s) to %s in %s%s\n",
|
2023-09-13 14:26:15 +00:00
|
|
|
startTime.Format(logDate),
|
2023-05-08 18:12:51 +00:00
|
|
|
filePath,
|
2023-09-15 20:30:52 +00:00
|
|
|
humanReadableSize(written),
|
2023-05-08 18:12:51 +00:00
|
|
|
realIP(r),
|
|
|
|
time.Since(startTime).Round(time.Microsecond),
|
2023-09-17 01:15:39 +00:00
|
|
|
status,
|
2023-05-08 18:12:51 +00:00
|
|
|
)
|
|
|
|
}
|
2023-01-20 02:31:02 +00:00
|
|
|
}
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
2023-01-20 02:31:02 +00:00
|
|
|
|
2024-02-06 16:12:55 +00:00
|
|
|
func serveRoot(paths []string, index *fileIndex, filename *regexp.Regexp, formats types.Types, errorChannel chan<- error) httprouter.Handle {
|
2023-06-03 18:29:49 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
2024-01-30 19:22:42 +00:00
|
|
|
w.Header().Add("Content-Security-Policy", "default-src 'self';")
|
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
refererUri, err := stripQueryParams(refererToUri(r.Referer()))
|
|
|
|
if err != nil {
|
2023-09-15 19:28:21 +00:00
|
|
|
errorChannel <- err
|
2023-06-03 23:37:06 +00:00
|
|
|
|
2023-08-06 02:38:28 +00:00
|
|
|
serverError(w, r, nil)
|
2023-06-20 15:24:12 +00:00
|
|
|
|
2023-02-07 13:39:04 +00:00
|
|
|
return
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
2023-01-21 04:42:44 +00:00
|
|
|
|
2023-09-14 04:24:29 +00:00
|
|
|
strippedRefererUri := strings.TrimPrefix(refererUri, Prefix+mediaPrefix)
|
2023-06-03 23:37:06 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
filters := &filters{
|
2024-01-10 19:07:24 +00:00
|
|
|
included: splitQueryParams(r.URL.Query().Get("include")),
|
|
|
|
excluded: splitQueryParams(r.URL.Query().Get("exclude")),
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
sortOrder := sortOrder(r)
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2023-09-12 18:06:45 +00:00
|
|
|
_, refreshInterval := refreshInterval(r)
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2023-09-17 01:15:39 +00:00
|
|
|
var path string
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2023-06-03 18:29:49 +00:00
|
|
|
if refererUri != "" {
|
2024-01-15 14:51:41 +00:00
|
|
|
path, err = nextFile(strippedRefererUri, sortOrder, filename, formats)
|
2023-06-03 18:29:49 +00:00
|
|
|
if err != nil {
|
2023-09-15 19:28:21 +00:00
|
|
|
errorChannel <- err
|
2023-06-03 23:37:06 +00:00
|
|
|
|
2023-08-06 02:38:28 +00:00
|
|
|
serverError(w, r, nil)
|
2023-06-20 15:24:12 +00:00
|
|
|
|
2023-06-03 18:29:49 +00:00
|
|
|
return
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
2023-06-03 18:29:49 +00:00
|
|
|
}
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2024-02-06 16:12:55 +00:00
|
|
|
list := fileList(paths, filters, sortOrder, index, formats, errorChannel)
|
2023-09-17 01:15:39 +00:00
|
|
|
|
2023-06-03 18:29:49 +00:00
|
|
|
loop:
|
2023-09-13 14:26:15 +00:00
|
|
|
for timeout := time.After(timeout); ; {
|
2023-06-03 18:29:49 +00:00
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
break loop
|
|
|
|
default:
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
2023-09-17 01:15:39 +00:00
|
|
|
if path != "" {
|
2023-06-03 18:29:49 +00:00
|
|
|
break loop
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 14:51:41 +00:00
|
|
|
path, err = newFile(list, sortOrder, filename, formats)
|
2023-06-03 18:29:49 +00:00
|
|
|
switch {
|
2024-01-06 00:50:45 +00:00
|
|
|
case path == "":
|
2024-01-30 12:25:14 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/plain;charset=UTF-8")
|
|
|
|
|
|
|
|
w.Write([]byte("No files found in the specified path(s).\n"))
|
|
|
|
|
|
|
|
if Verbose {
|
|
|
|
fmt.Printf("%s | SERVE: Empty path notification to %s\n",
|
|
|
|
startTime.Format(logDate),
|
|
|
|
r.RemoteAddr,
|
|
|
|
)
|
|
|
|
}
|
2024-01-06 00:50:45 +00:00
|
|
|
|
|
|
|
return
|
2023-09-11 01:29:11 +00:00
|
|
|
case err != nil && err == ErrNoMediaFound:
|
2023-09-17 01:15:39 +00:00
|
|
|
notFound(w, r, path)
|
2023-01-25 01:06:15 +00:00
|
|
|
|
|
|
|
return
|
2023-06-03 18:29:49 +00:00
|
|
|
case err != nil:
|
2023-09-15 19:28:21 +00:00
|
|
|
errorChannel <- err
|
2023-06-03 23:37:06 +00:00
|
|
|
|
2023-08-06 02:38:28 +00:00
|
|
|
serverError(w, r, nil)
|
2023-06-20 15:24:12 +00:00
|
|
|
|
2023-02-07 13:39:04 +00:00
|
|
|
return
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
2023-06-03 18:29:49 +00:00
|
|
|
}
|
2023-05-09 01:05:10 +00:00
|
|
|
|
2023-06-03 18:29:49 +00:00
|
|
|
queryParams := generateQueryParams(filters, sortOrder, refreshInterval)
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2023-09-14 04:24:29 +00:00
|
|
|
newUrl := fmt.Sprintf("http://%s%s%s%s",
|
2023-06-03 18:29:49 +00:00
|
|
|
r.Host,
|
2023-09-14 04:24:29 +00:00
|
|
|
Prefix,
|
2023-10-03 14:44:50 +00:00
|
|
|
preparePath(mediaPrefix, path),
|
2023-06-03 18:29:49 +00:00
|
|
|
queryParams,
|
|
|
|
)
|
2023-09-28 09:42:30 +00:00
|
|
|
http.Redirect(w, r, newUrl, redirectStatusCode)
|
2023-06-03 18:29:49 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2024-01-15 14:51:41 +00:00
|
|
|
func serveMedia(paths []string, index *fileIndex, filename *regexp.Regexp, formats types.Types, errorChannel chan<- error) httprouter.Handle {
|
2023-06-03 18:29:49 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
2024-01-08 04:43:51 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
filters := &filters{
|
2024-01-10 19:07:24 +00:00
|
|
|
included: splitQueryParams(r.URL.Query().Get("include")),
|
|
|
|
excluded: splitQueryParams(r.URL.Query().Get("exclude")),
|
2023-06-03 18:29:49 +00:00
|
|
|
}
|
2023-05-09 01:05:10 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
sortOrder := sortOrder(r)
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2023-09-14 04:24:29 +00:00
|
|
|
path := strings.TrimPrefix(strings.TrimPrefix(r.URL.Path, Prefix), mediaPrefix)
|
2023-05-08 18:12:51 +00:00
|
|
|
|
2023-06-03 18:29:49 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
2023-09-13 14:26:15 +00:00
|
|
|
path = strings.TrimPrefix(path, "/")
|
2023-06-03 18:29:49 +00:00
|
|
|
}
|
2023-05-08 18:12:51 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
exists, err := fileExists(path)
|
2023-06-03 18:29:49 +00:00
|
|
|
if err != nil {
|
2023-09-15 19:28:21 +00:00
|
|
|
errorChannel <- err
|
2023-06-03 23:37:06 +00:00
|
|
|
|
2023-08-06 02:38:28 +00:00
|
|
|
serverError(w, r, nil)
|
2023-06-20 15:24:12 +00:00
|
|
|
|
2023-06-03 18:29:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !exists {
|
2023-09-13 14:26:15 +00:00
|
|
|
notFound(w, r, path)
|
2023-06-03 18:29:49 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2023-05-08 18:12:51 +00:00
|
|
|
|
2023-09-14 22:37:22 +00:00
|
|
|
format := formats.FileType(path)
|
|
|
|
if format == nil {
|
2023-10-03 14:44:50 +00:00
|
|
|
if Fallback {
|
|
|
|
w.Header().Add("Content-Type", "application/octet-stream")
|
2023-06-20 15:24:12 +00:00
|
|
|
|
2023-10-03 14:44:50 +00:00
|
|
|
_, refreshInterval := refreshInterval(r)
|
|
|
|
|
|
|
|
// redirect to static url for file
|
|
|
|
newUrl := fmt.Sprintf("http://%s%s%s%s",
|
|
|
|
r.Host,
|
|
|
|
Prefix,
|
|
|
|
preparePath(sourcePrefix, path),
|
|
|
|
generateQueryParams(filters, sortOrder, refreshInterval),
|
|
|
|
)
|
|
|
|
|
|
|
|
http.Redirect(w, r, newUrl, redirectStatusCode)
|
|
|
|
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
notFound(w, r, path)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
2023-06-03 18:29:49 +00:00
|
|
|
}
|
2023-05-08 18:12:51 +00:00
|
|
|
|
2023-09-14 22:37:22 +00:00
|
|
|
if !format.Validate(path) {
|
2023-09-13 14:26:15 +00:00
|
|
|
notFound(w, r, path)
|
2023-05-08 18:12:51 +00:00
|
|
|
|
2023-06-03 18:29:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-09 03:17:47 +00:00
|
|
|
// Temporarily disabled until I figure out how to make things
|
|
|
|
// work properly with inline event handlers.
|
|
|
|
// nonce := format.CSP(w)
|
|
|
|
nonce := ""
|
|
|
|
|
2024-01-30 19:07:59 +00:00
|
|
|
|
2024-01-15 14:51:41 +00:00
|
|
|
mediaType := format.MediaType(filepath.Ext(path))
|
2023-09-14 22:37:22 +00:00
|
|
|
|
2023-09-14 05:01:50 +00:00
|
|
|
fileUri := Prefix + generateFileUri(path)
|
2023-06-03 18:29:49 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
fileName := filepath.Base(path)
|
2023-06-03 18:29:49 +00:00
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "text/html")
|
|
|
|
|
2023-09-12 18:06:45 +00:00
|
|
|
refreshTimer, refreshInterval := refreshInterval(r)
|
2023-06-03 18:29:49 +00:00
|
|
|
|
2023-10-20 22:18:35 +00:00
|
|
|
queryParams := generateQueryParams(filters, sortOrder, refreshInterval)
|
|
|
|
|
|
|
|
rootUrl := Prefix + "/" + queryParams
|
2023-06-03 18:29:49 +00:00
|
|
|
|
|
|
|
var htmlBody strings.Builder
|
2023-09-15 03:13:21 +00:00
|
|
|
htmlBody.WriteString(`<!DOCTYPE html><html class="bg" lang="en"><head>`)
|
2024-01-30 19:07:59 +00:00
|
|
|
htmlBody.WriteString(getFavicon(nonce))
|
|
|
|
htmlBody.WriteString(fmt.Sprintf(`<style nonce=%q>%s</style>`, nonce, format.CSS()))
|
2023-09-15 19:28:21 +00:00
|
|
|
|
2023-10-09 14:47:17 +00:00
|
|
|
title, err := format.Title(rootUrl, fileUri, path, fileName, Prefix, mediaType)
|
2023-09-15 19:28:21 +00:00
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
serverError(w, r, nil)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
htmlBody.WriteString(title)
|
2023-06-03 18:29:49 +00:00
|
|
|
htmlBody.WriteString(`</head><body>`)
|
2023-10-20 22:18:35 +00:00
|
|
|
|
|
|
|
var first, last string
|
|
|
|
|
|
|
|
if Index && sortOrder != "" {
|
2024-01-15 14:51:41 +00:00
|
|
|
first, last, err = getRange(path, index, filename)
|
2023-10-20 22:18:35 +00:00
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
serverError(w, r, nil)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-06 22:10:06 +00:00
|
|
|
if Index && !NoButtons && sortOrder != "" {
|
2024-01-15 14:51:41 +00:00
|
|
|
paginated, err := paginate(path, first, last, queryParams, filename, formats)
|
2023-10-20 22:18:35 +00:00
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
serverError(w, r, nil)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-15 14:51:41 +00:00
|
|
|
htmlBody.WriteString(paginated)
|
2023-10-20 22:18:35 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 18:29:49 +00:00
|
|
|
if refreshInterval != "0ms" {
|
2024-01-30 19:07:59 +00:00
|
|
|
htmlBody.WriteString(refreshFunction(rootUrl, refreshTimer, nonce))
|
2023-06-03 18:29:49 +00:00
|
|
|
}
|
2023-09-15 19:28:21 +00:00
|
|
|
|
2024-01-30 19:07:59 +00:00
|
|
|
body, err := format.Body(rootUrl, fileUri, path, fileName, Prefix, mediaType, nonce)
|
2023-09-15 19:28:21 +00:00
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
serverError(w, r, nil)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
htmlBody.WriteString(body)
|
2023-10-20 22:18:35 +00:00
|
|
|
|
2023-09-11 02:02:03 +00:00
|
|
|
htmlBody.WriteString(`</body></html>`)
|
|
|
|
|
2023-09-15 20:13:45 +00:00
|
|
|
formattedPage := gohtml.Format(htmlBody.String())
|
|
|
|
|
2024-01-15 14:51:41 +00:00
|
|
|
written, err := io.WriteString(w, formattedPage+"\n")
|
2023-06-03 18:29:49 +00:00
|
|
|
if err != nil {
|
2023-09-15 19:28:21 +00:00
|
|
|
errorChannel <- err
|
2023-06-03 23:37:06 +00:00
|
|
|
|
2023-08-06 02:38:28 +00:00
|
|
|
serverError(w, r, nil)
|
2023-06-20 15:24:12 +00:00
|
|
|
|
2023-06-03 18:29:49 +00:00
|
|
|
return
|
|
|
|
}
|
2023-09-15 20:13:45 +00:00
|
|
|
|
|
|
|
if format.Type() != "embed" {
|
|
|
|
if Verbose {
|
2023-09-26 10:29:55 +00:00
|
|
|
fmt.Printf("%s | SERVE: %s (%s) to %s in %s\n",
|
2023-09-15 20:13:45 +00:00
|
|
|
startTime.Format(logDate),
|
|
|
|
path,
|
2023-09-15 20:30:52 +00:00
|
|
|
humanReadableSize(written),
|
2023-09-15 20:13:45 +00:00
|
|
|
realIP(r),
|
|
|
|
time.Since(startTime).Round(time.Microsecond),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if Russian {
|
2024-01-08 04:43:51 +00:00
|
|
|
err := kill(path, index)
|
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2023-09-15 20:13:45 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-03 18:29:49 +00:00
|
|
|
|
2024-01-08 04:43:51 +00:00
|
|
|
func serveVersion(errorChannel chan<- error) httprouter.Handle {
|
2023-06-03 20:51:08 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
2024-01-08 04:43:51 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
|
2023-09-12 18:06:45 +00:00
|
|
|
data := []byte(fmt.Sprintf("roulette v%s\n", ReleaseVersion))
|
2023-06-03 20:51:08 +00:00
|
|
|
|
2024-01-30 19:22:42 +00:00
|
|
|
w.Header().Add("Content-Security-Policy", "default-src 'self';")
|
|
|
|
|
2024-01-25 15:30:28 +00:00
|
|
|
w.Header().Set("Content-Type", "text/plain;charset=UTF-8")
|
|
|
|
|
2024-01-15 15:41:11 +00:00
|
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
|
2023-06-03 23:37:06 +00:00
|
|
|
|
2024-01-08 04:43:51 +00:00
|
|
|
written, err := w.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if Verbose {
|
|
|
|
fmt.Printf("%s | SERVE: Version page (%s) to %s in %s\n",
|
|
|
|
startTime.Format(logDate),
|
|
|
|
humanReadableSize(written),
|
|
|
|
realIP(r),
|
|
|
|
time.Since(startTime).Round(time.Microsecond),
|
|
|
|
)
|
|
|
|
}
|
2023-06-03 20:51:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 04:24:29 +00:00
|
|
|
func redirectRoot() httprouter.Handle {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
newUrl := fmt.Sprintf("http://%s%s",
|
|
|
|
r.Host,
|
|
|
|
Prefix,
|
|
|
|
)
|
|
|
|
|
2023-09-28 09:42:30 +00:00
|
|
|
http.Redirect(w, r, newUrl, redirectStatusCode)
|
2023-09-14 04:24:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
func ServePage(args []string) error {
|
2024-01-30 12:25:14 +00:00
|
|
|
var err error
|
|
|
|
|
2023-07-17 12:41:32 +00:00
|
|
|
timeZone := os.Getenv("TZ")
|
|
|
|
if timeZone != "" {
|
|
|
|
time.Local, err = time.LoadLocation(timeZone)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-30 12:49:40 +00:00
|
|
|
if Verbose {
|
|
|
|
fmt.Printf("%s | START: roulette v%s\n",
|
|
|
|
time.Now().Format(logDate),
|
|
|
|
ReleaseVersion,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-09-12 18:06:45 +00:00
|
|
|
bindHost, err := net.LookupHost(Bind)
|
2023-05-08 16:45:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bindAddr := net.ParseIP(bindHost[0])
|
|
|
|
if bindAddr == nil {
|
2023-05-31 18:01:24 +00:00
|
|
|
return errors.New("invalid bind address provided")
|
2023-05-08 16:45:57 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 19:09:49 +00:00
|
|
|
formats := make(types.Types)
|
2023-09-11 15:43:09 +00:00
|
|
|
|
2023-09-12 18:06:45 +00:00
|
|
|
if Audio || All {
|
2023-10-19 14:44:41 +00:00
|
|
|
formats.Add(audio.Format{})
|
2023-09-11 15:43:09 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 22:37:22 +00:00
|
|
|
if Code || All {
|
2023-10-04 22:33:03 +00:00
|
|
|
formats.Add(code.Format{Fun: Fun, Theme: CodeTheme})
|
2023-09-14 22:37:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-12 18:06:45 +00:00
|
|
|
if Flash || All {
|
2023-10-19 14:44:41 +00:00
|
|
|
formats.Add(flash.Format{})
|
2023-09-12 18:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if Text || All {
|
2023-10-19 14:44:41 +00:00
|
|
|
formats.Add(text.Format{})
|
2023-09-12 00:11:45 +00:00
|
|
|
}
|
|
|
|
|
2023-09-12 18:06:45 +00:00
|
|
|
if Videos || All {
|
2023-10-19 14:44:41 +00:00
|
|
|
formats.Add(video.Format{})
|
2023-09-11 15:43:09 +00:00
|
|
|
}
|
|
|
|
|
2023-11-05 14:23:49 +00:00
|
|
|
if Images || All {
|
2024-02-06 22:10:06 +00:00
|
|
|
formats.Add(images.Format{NoButtons: NoButtons, Fun: Fun})
|
2023-09-13 03:35:15 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 01:45:28 +00:00
|
|
|
paths, err := validatePaths(args, formats)
|
2023-01-24 18:03:26 +00:00
|
|
|
if err != nil {
|
2023-01-25 01:06:15 +00:00
|
|
|
return err
|
2023-01-20 02:31:02 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 09:44:18 +00:00
|
|
|
if len(paths) == 0 {
|
2023-09-11 02:08:51 +00:00
|
|
|
return ErrNoMediaFound
|
2023-04-11 09:44:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-28 11:15:10 +00:00
|
|
|
listenHost := net.JoinHostPort(Bind, strconv.Itoa(Port))
|
|
|
|
|
2023-09-28 15:09:45 +00:00
|
|
|
index := &fileIndex{
|
|
|
|
mutex: &sync.RWMutex{},
|
2023-09-06 22:31:05 +00:00
|
|
|
list: []string{},
|
|
|
|
}
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
mux := httprouter.New()
|
|
|
|
|
2023-09-06 22:31:05 +00:00
|
|
|
srv := &http.Server{
|
2023-09-26 10:29:55 +00:00
|
|
|
Addr: listenHost,
|
2023-09-06 22:31:05 +00:00
|
|
|
Handler: mux,
|
|
|
|
IdleTimeout: 10 * time.Minute,
|
|
|
|
ReadTimeout: 5 * time.Second,
|
|
|
|
WriteTimeout: 5 * time.Minute,
|
|
|
|
}
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2023-06-20 15:24:12 +00:00
|
|
|
mux.PanicHandler = serverErrorHandler()
|
2023-05-30 23:47:26 +00:00
|
|
|
|
2023-09-15 19:28:21 +00:00
|
|
|
errorChannel := make(chan error)
|
|
|
|
|
2024-01-06 15:41:30 +00:00
|
|
|
go func() {
|
|
|
|
for err := range errorChannel {
|
|
|
|
switch {
|
2024-02-06 22:10:06 +00:00
|
|
|
case ErrorExit:
|
2024-01-10 19:07:24 +00:00
|
|
|
fmt.Printf("%s | FATAL: %v\n", time.Now().Format(logDate), err)
|
|
|
|
case Debug && errors.Is(err, os.ErrNotExist) || errors.Is(err, os.ErrPermission):
|
|
|
|
fmt.Printf("%s | DEBUG: %v\n", time.Now().Format(logDate), err)
|
|
|
|
case errors.Is(err, os.ErrNotExist) || errors.Is(err, os.ErrPermission):
|
2024-01-06 15:41:30 +00:00
|
|
|
continue
|
2024-01-10 19:07:24 +00:00
|
|
|
default:
|
|
|
|
fmt.Printf("%s | ERROR: %v\n", time.Now().Format(logDate), err)
|
2024-01-06 15:41:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-01-15 14:51:41 +00:00
|
|
|
filename := regexp.MustCompile(`(.+?)([0-9]*)(\..+)`)
|
|
|
|
|
2024-01-15 15:28:59 +00:00
|
|
|
if !strings.HasSuffix(Prefix, "/") {
|
|
|
|
Prefix = Prefix + "/"
|
|
|
|
}
|
|
|
|
|
2024-02-06 16:12:55 +00:00
|
|
|
mux.GET(Prefix, serveRoot(paths, index, filename, formats, errorChannel))
|
2023-09-14 05:01:50 +00:00
|
|
|
|
|
|
|
Prefix = strings.TrimSuffix(Prefix, "/")
|
2023-09-14 04:24:29 +00:00
|
|
|
|
|
|
|
if Prefix != "" {
|
2024-01-15 15:28:59 +00:00
|
|
|
mux.GET("/", redirectRoot())
|
2023-09-14 04:24:29 +00:00
|
|
|
}
|
2023-09-06 22:31:05 +00:00
|
|
|
|
2024-01-15 15:28:59 +00:00
|
|
|
mux.GET(Prefix+"/favicons/*favicon", serveFavicons(errorChannel))
|
2023-09-06 22:31:05 +00:00
|
|
|
|
2024-01-15 15:28:59 +00:00
|
|
|
mux.GET(Prefix+"/favicon.ico", serveFavicons(errorChannel))
|
2023-09-06 22:31:05 +00:00
|
|
|
|
2024-01-15 15:28:59 +00:00
|
|
|
mux.GET(Prefix+mediaPrefix+"/*media", serveMedia(paths, index, filename, formats, errorChannel))
|
2023-09-06 22:31:05 +00:00
|
|
|
|
2024-01-15 15:28:59 +00:00
|
|
|
mux.GET(Prefix+sourcePrefix+"/*static", serveStaticFile(paths, index, errorChannel))
|
2023-09-06 22:31:05 +00:00
|
|
|
|
2024-01-15 15:28:59 +00:00
|
|
|
mux.GET(Prefix+"/version", serveVersion(errorChannel))
|
2023-09-06 22:31:05 +00:00
|
|
|
|
2024-01-30 10:27:18 +00:00
|
|
|
quit := make(chan struct{})
|
2024-01-30 11:12:26 +00:00
|
|
|
defer close(quit)
|
2024-01-30 10:27:18 +00:00
|
|
|
|
2024-01-30 16:12:05 +00:00
|
|
|
if API {
|
2024-02-06 16:12:55 +00:00
|
|
|
registerAPIHandlers(mux, paths, index, formats, errorChannel)
|
2024-01-30 16:12:05 +00:00
|
|
|
}
|
2023-09-29 13:31:35 +00:00
|
|
|
|
2024-01-30 16:12:05 +00:00
|
|
|
if Index {
|
2024-02-06 16:12:55 +00:00
|
|
|
importIndex(paths, index, formats, errorChannel)
|
2024-01-30 10:27:18 +00:00
|
|
|
|
|
|
|
if IndexInterval != "" {
|
2024-02-06 16:12:55 +00:00
|
|
|
registerIndexInterval(paths, index, formats, quit, errorChannel)
|
2024-01-30 10:27:18 +00:00
|
|
|
}
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
2023-09-12 18:06:45 +00:00
|
|
|
if Profile {
|
2023-09-13 17:00:24 +00:00
|
|
|
registerProfileHandlers(mux)
|
2023-09-06 15:15:11 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
if Russian {
|
|
|
|
fmt.Printf("WARNING! Files *will* be deleted after serving!\n\n")
|
|
|
|
}
|
2023-09-06 22:31:05 +00:00
|
|
|
|
2023-09-28 11:15:10 +00:00
|
|
|
if Verbose {
|
|
|
|
fmt.Printf("%s | SERVE: Listening on http://%s%s/\n",
|
|
|
|
time.Now().Format(logDate),
|
|
|
|
listenHost,
|
2024-01-30 21:22:15 +00:00
|
|
|
Prefix)
|
2023-09-28 11:15:10 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 17:43:07 +00:00
|
|
|
err = srv.ListenAndServe()
|
|
|
|
if !errors.Is(err, http.ErrServerClosed) {
|
2023-01-25 01:06:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|