2023-09-28 15:09:45 +00:00
|
|
|
/*
|
2024-01-14 18:39:14 +00:00
|
|
|
Copyright © 2024 Seednode <seednode@seedno.de>
|
2023-09-28 15:09:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/gob"
|
2024-01-30 20:38:05 +00:00
|
|
|
"encoding/json"
|
2023-09-28 15:09:45 +00:00
|
|
|
"fmt"
|
2024-01-30 20:38:05 +00:00
|
|
|
"net/http"
|
2023-09-28 15:09:45 +00:00
|
|
|
"os"
|
2024-01-30 20:38:05 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2023-09-28 15:09:45 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2024-01-30 20:38:05 +00:00
|
|
|
"github.com/julienschmidt/httprouter"
|
2023-09-28 15:09:45 +00:00
|
|
|
"github.com/klauspost/compress/zstd"
|
|
|
|
"seedno.de/seednode/roulette/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type fileIndex struct {
|
|
|
|
mutex *sync.RWMutex
|
|
|
|
list []string
|
|
|
|
}
|
|
|
|
|
2024-02-06 21:26:28 +00:00
|
|
|
func makeTree(list []string) ([]byte, error) {
|
|
|
|
tree := make(map[string]any)
|
|
|
|
|
2024-02-06 21:41:34 +00:00
|
|
|
current := tree
|
2024-02-06 21:26:28 +00:00
|
|
|
|
|
|
|
for _, entry := range list {
|
|
|
|
path := strings.Split(entry, string(os.PathSeparator))
|
|
|
|
|
|
|
|
for i, last := 0, len(path)-1; i < len(path); i++ {
|
|
|
|
if i == last {
|
2024-02-06 21:41:34 +00:00
|
|
|
current[path[i]] = nil
|
2024-02-06 21:26:28 +00:00
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2024-02-06 21:41:34 +00:00
|
|
|
v, ok := current[path[i]].(map[string]any)
|
2024-02-06 21:26:28 +00:00
|
|
|
if !ok || v == nil {
|
|
|
|
v = make(map[string]any)
|
2024-02-06 21:41:34 +00:00
|
|
|
current[path[i]] = v
|
2024-02-06 21:26:28 +00:00
|
|
|
}
|
|
|
|
|
2024-02-06 21:41:34 +00:00
|
|
|
current = v
|
2024-02-06 21:26:28 +00:00
|
|
|
}
|
|
|
|
|
2024-02-06 21:41:34 +00:00
|
|
|
current = tree
|
2024-02-06 21:26:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := json.MarshalIndent(tree, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
|
2024-02-09 04:10:09 +00:00
|
|
|
return resp, nil
|
2024-02-06 21:26:28 +00:00
|
|
|
}
|
|
|
|
|
2023-09-28 15:09:45 +00:00
|
|
|
func (index *fileIndex) List() []string {
|
|
|
|
index.mutex.RLock()
|
2024-02-06 21:26:28 +00:00
|
|
|
list := make([]string, len(index.list))
|
|
|
|
copy(list, index.list)
|
2023-09-28 15:09:45 +00:00
|
|
|
index.mutex.RUnlock()
|
|
|
|
|
2024-02-06 21:26:28 +00:00
|
|
|
sort.SliceStable(list, func(p, q int) bool {
|
|
|
|
return strings.ToLower(list[p]) < strings.ToLower(list[q])
|
|
|
|
})
|
|
|
|
|
|
|
|
return list
|
2023-09-28 15:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (index *fileIndex) remove(path string) {
|
|
|
|
index.mutex.RLock()
|
2024-02-11 16:30:10 +00:00
|
|
|
t := make([]string, len(index.list))
|
|
|
|
copy(t, index.list)
|
2023-09-28 15:09:45 +00:00
|
|
|
index.mutex.RUnlock()
|
|
|
|
|
|
|
|
var position int
|
|
|
|
|
2024-02-11 16:30:10 +00:00
|
|
|
for k, v := range t {
|
2023-09-28 15:09:45 +00:00
|
|
|
if path == v {
|
|
|
|
position = k
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-11 16:30:10 +00:00
|
|
|
t[position] = t[len(t)-1]
|
2023-09-28 15:09:45 +00:00
|
|
|
|
|
|
|
index.mutex.Lock()
|
2024-02-11 16:30:10 +00:00
|
|
|
index.list = make([]string, len(t)-1)
|
|
|
|
copy(index.list, t[:len(t)-1])
|
2023-09-28 15:09:45 +00:00
|
|
|
index.mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2024-02-06 16:12:55 +00:00
|
|
|
func (index *fileIndex) set(val []string, errorChannel chan<- error) {
|
2023-09-28 15:09:45 +00:00
|
|
|
length := len(val)
|
|
|
|
|
|
|
|
if length < 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
index.mutex.Lock()
|
|
|
|
index.list = make([]string, length)
|
|
|
|
copy(index.list, val)
|
|
|
|
index.mutex.Unlock()
|
|
|
|
|
|
|
|
if Index && IndexFile != "" {
|
2024-02-06 16:12:55 +00:00
|
|
|
index.Export(IndexFile, errorChannel)
|
2023-09-28 15:09:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *fileIndex) clear() {
|
|
|
|
index.mutex.Lock()
|
|
|
|
index.list = nil
|
|
|
|
index.mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *fileIndex) isEmpty() bool {
|
|
|
|
index.mutex.RLock()
|
|
|
|
length := len(index.list)
|
|
|
|
index.mutex.RUnlock()
|
|
|
|
|
|
|
|
return length == 0
|
|
|
|
}
|
|
|
|
|
2024-02-06 16:12:55 +00:00
|
|
|
func (index *fileIndex) Export(path string, errorChannel chan<- error) {
|
2023-09-28 15:09:45 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
2024-01-14 15:29:22 +00:00
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
return
|
2023-09-28 15:09:45 +00:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2024-02-06 16:12:55 +00:00
|
|
|
encoder, err := zstd.NewWriter(file, zstd.WithEncoderLevel(zstd.SpeedBestCompression))
|
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
2024-01-14 15:29:22 +00:00
|
|
|
|
2024-02-06 16:12:55 +00:00
|
|
|
return
|
|
|
|
}
|
2024-01-06 02:39:05 +00:00
|
|
|
defer encoder.Close()
|
2023-09-28 15:09:45 +00:00
|
|
|
|
2024-02-06 21:48:39 +00:00
|
|
|
enc := gob.NewEncoder(encoder)
|
2023-09-28 15:09:45 +00:00
|
|
|
|
|
|
|
index.mutex.RLock()
|
2023-10-04 20:47:43 +00:00
|
|
|
err = enc.Encode(&index.list)
|
|
|
|
if err != nil {
|
2024-01-04 18:43:26 +00:00
|
|
|
index.mutex.RUnlock()
|
|
|
|
|
2024-01-14 15:29:22 +00:00
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
return
|
2023-10-04 20:47:43 +00:00
|
|
|
}
|
2023-09-28 15:09:45 +00:00
|
|
|
length := len(index.list)
|
|
|
|
index.mutex.RUnlock()
|
|
|
|
|
2024-01-04 18:43:26 +00:00
|
|
|
// Close encoder prior to checking file size,
|
|
|
|
// to ensure the correct value is returned.
|
2024-01-14 15:29:22 +00:00
|
|
|
encoder.Close()
|
2024-01-04 18:43:26 +00:00
|
|
|
|
2024-01-04 14:43:52 +00:00
|
|
|
stats, err := file.Stat()
|
|
|
|
if err != nil {
|
2024-01-14 15:29:22 +00:00
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
return
|
2024-01-04 14:43:52 +00:00
|
|
|
}
|
|
|
|
|
2023-09-28 15:09:45 +00:00
|
|
|
if Verbose {
|
2024-01-04 14:43:52 +00:00
|
|
|
fmt.Printf("%s | INDEX: Exported %d entries to %s (%s) in %s\n",
|
2023-09-28 15:09:45 +00:00
|
|
|
time.Now().Format(logDate),
|
|
|
|
length,
|
|
|
|
path,
|
2024-01-04 14:43:52 +00:00
|
|
|
humanReadableSize(int(stats.Size())),
|
2024-01-11 09:36:16 +00:00
|
|
|
time.Since(startTime).Round(time.Microsecond),
|
2023-09-28 15:09:45 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-14 15:29:22 +00:00
|
|
|
func (index *fileIndex) Import(path string, errorChannel chan<- error) {
|
2023-09-28 15:09:45 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
file, err := os.OpenFile(path, os.O_RDONLY, 0600)
|
|
|
|
if err != nil {
|
2024-01-14 15:29:22 +00:00
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
return
|
2023-09-28 15:09:45 +00:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2024-01-04 14:43:52 +00:00
|
|
|
stats, err := file.Stat()
|
|
|
|
if err != nil {
|
2024-01-14 15:29:22 +00:00
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
return
|
2024-01-04 14:43:52 +00:00
|
|
|
}
|
|
|
|
|
2024-01-14 15:43:22 +00:00
|
|
|
reader, err := zstd.NewReader(file)
|
2023-09-28 15:09:45 +00:00
|
|
|
if err != nil {
|
2024-01-14 15:29:22 +00:00
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
return
|
2023-09-28 15:09:45 +00:00
|
|
|
}
|
2024-01-09 19:39:08 +00:00
|
|
|
defer reader.Close()
|
2023-09-28 15:09:45 +00:00
|
|
|
|
2024-01-06 02:39:05 +00:00
|
|
|
dec := gob.NewDecoder(reader)
|
2023-09-28 15:09:45 +00:00
|
|
|
|
|
|
|
index.mutex.Lock()
|
|
|
|
err = dec.Decode(&index.list)
|
|
|
|
if err != nil {
|
2024-01-04 18:43:26 +00:00
|
|
|
index.mutex.Unlock()
|
|
|
|
|
2024-01-14 15:29:22 +00:00
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
return
|
2023-09-28 15:09:45 +00:00
|
|
|
}
|
2023-10-04 20:47:43 +00:00
|
|
|
length := len(index.list)
|
|
|
|
index.mutex.Unlock()
|
2023-09-28 15:09:45 +00:00
|
|
|
|
|
|
|
if Verbose {
|
2024-01-04 14:43:52 +00:00
|
|
|
fmt.Printf("%s | INDEX: Imported %d entries from %s (%s) in %s\n",
|
2023-09-28 15:09:45 +00:00
|
|
|
time.Now().Format(logDate),
|
|
|
|
length,
|
|
|
|
path,
|
2024-01-04 14:43:52 +00:00
|
|
|
humanReadableSize(int(stats.Size())),
|
2024-01-11 09:36:16 +00:00
|
|
|
time.Since(startTime).Round(time.Microsecond),
|
2023-09-28 15:09:45 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-06 16:12:55 +00:00
|
|
|
func rebuildIndex(paths []string, index *fileIndex, formats types.Types, errorChannel chan<- error) {
|
2024-01-30 20:38:05 +00:00
|
|
|
index.clear()
|
|
|
|
|
2024-02-06 16:12:55 +00:00
|
|
|
fileList(paths, &filters{}, "", index, formats, errorChannel)
|
2024-01-30 20:38:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-06 16:12:55 +00:00
|
|
|
func importIndex(paths []string, index *fileIndex, formats types.Types, errorChannel chan<- error) {
|
2024-01-30 20:38:05 +00:00
|
|
|
if IndexFile != "" {
|
|
|
|
index.Import(IndexFile, errorChannel)
|
|
|
|
}
|
|
|
|
|
2024-02-06 16:12:55 +00:00
|
|
|
fileList(paths, &filters{}, "", index, formats, errorChannel)
|
2024-01-30 20:38:05 +00:00
|
|
|
}
|
|
|
|
|
2024-01-31 17:30:37 +00:00
|
|
|
func serveIndex(index *fileIndex, errorChannel chan<- error) httprouter.Handle {
|
2024-01-30 20:38:05 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
w.Header().Add("Content-Security-Policy", "default-src 'self';")
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
|
|
|
|
|
2024-02-06 21:26:28 +00:00
|
|
|
response, err := makeTree(index.List())
|
2024-01-30 20:38:05 +00:00
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
serverError(w, r, nil)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response = append(response, []byte("\n")...)
|
|
|
|
|
|
|
|
written, err := w.Write(response)
|
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
|
|
|
}
|
|
|
|
|
|
|
|
if Verbose {
|
|
|
|
fmt.Printf("%s | SERVE: JSON index page (%s) to %s in %s\n",
|
|
|
|
startTime.Format(logDate),
|
|
|
|
humanReadableSize(written),
|
|
|
|
realIP(r),
|
|
|
|
time.Since(startTime).Round(time.Microsecond),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-06 16:12:55 +00:00
|
|
|
func serveIndexRebuild(paths []string, index *fileIndex, formats types.Types, errorChannel chan<- error) httprouter.Handle {
|
2024-01-30 20:38:05 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
if Verbose {
|
|
|
|
fmt.Printf("%s | SERVE: Index rebuild requested by %s\n",
|
|
|
|
time.Now().Format(logDate),
|
2024-01-30 21:22:15 +00:00
|
|
|
realIP(r))
|
2024-01-30 20:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Add("Content-Security-Policy", "default-src 'self';")
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/plain;charset=UTF-8")
|
|
|
|
|
2024-02-06 16:12:55 +00:00
|
|
|
rebuildIndex(paths, index, formats, errorChannel)
|
2024-01-30 20:38:05 +00:00
|
|
|
|
|
|
|
_, err := w.Write([]byte("Ok\n"))
|
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-06 16:12:55 +00:00
|
|
|
func registerIndexInterval(paths []string, index *fileIndex, formats types.Types, quit <-chan struct{}, errorChannel chan<- error) {
|
2024-01-30 12:25:14 +00:00
|
|
|
interval, err := time.ParseDuration(IndexInterval)
|
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2024-01-30 12:42:59 +00:00
|
|
|
|
2024-01-30 12:25:14 +00:00
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
if Verbose {
|
2024-01-30 21:22:15 +00:00
|
|
|
fmt.Printf("%s | INDEX: Started scheduled index rebuild\n", time.Now().Format(logDate))
|
2024-01-30 12:25:14 +00:00
|
|
|
}
|
2024-01-30 20:38:05 +00:00
|
|
|
|
2024-02-06 16:12:55 +00:00
|
|
|
rebuildIndex(paths, index, formats, errorChannel)
|
2024-01-30 12:25:14 +00:00
|
|
|
case <-quit:
|
|
|
|
ticker.Stop()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|