2022-09-08 15:12:06 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2022 Seednode <seednode@seedno.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-09-08 20:57:52 +00:00
|
|
|
"errors"
|
2022-09-10 23:03:04 +00:00
|
|
|
"fmt"
|
2022-09-08 15:12:06 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2022-09-08 20:30:51 +00:00
|
|
|
"net/url"
|
2022-09-08 15:57:59 +00:00
|
|
|
"os"
|
2022-09-08 15:12:06 +00:00
|
|
|
"strconv"
|
2022-09-08 20:57:52 +00:00
|
|
|
"strings"
|
2022-09-10 23:03:04 +00:00
|
|
|
"time"
|
2022-09-08 15:12:06 +00:00
|
|
|
)
|
|
|
|
|
2022-09-10 23:03:04 +00:00
|
|
|
const LOGDATE string = "2006-01-02T15:04:05.000000000-07:00"
|
|
|
|
|
2022-09-09 22:21:03 +00:00
|
|
|
func generatePageHtml(w http.ResponseWriter, r http.Request, paths []string) error {
|
2022-09-08 20:57:52 +00:00
|
|
|
fileList, err := getFileList(paths)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-09 22:21:03 +00:00
|
|
|
fileName, filePath, err := pickFile(fileList)
|
|
|
|
if err != nil {
|
|
|
|
http.NotFound(w, &r)
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-08 20:57:52 +00:00
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "text/html")
|
|
|
|
|
|
|
|
htmlBody := `<html lang="en">
|
2022-09-08 15:12:06 +00:00
|
|
|
<head>
|
2022-09-08 20:30:51 +00:00
|
|
|
<style>img{max-width:100%;max-height:97vh;height:auto;}</style>
|
|
|
|
<title>`
|
2022-09-08 20:57:52 +00:00
|
|
|
htmlBody += fileName
|
|
|
|
htmlBody += `</title>
|
2022-09-08 15:12:06 +00:00
|
|
|
</head>
|
|
|
|
<body>
|
2022-09-08 20:57:52 +00:00
|
|
|
<a href="/"><img src="`
|
|
|
|
htmlBody += filePath
|
|
|
|
htmlBody += `"></img></a>
|
2022-09-09 00:11:07 +00:00
|
|
|
</body>
|
|
|
|
</html>`
|
|
|
|
|
2022-09-08 20:57:52 +00:00
|
|
|
_, err = io.WriteString(w, htmlBody)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-08 15:12:06 +00:00
|
|
|
|
2022-09-08 20:57:52 +00:00
|
|
|
return nil
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 19:14:54 +00:00
|
|
|
func serveStaticFile(w http.ResponseWriter, r http.Request, paths []string) error {
|
|
|
|
request := r.RequestURI
|
2022-09-08 20:57:52 +00:00
|
|
|
|
|
|
|
filePath, err := url.QueryUnescape(request)
|
2022-09-08 15:12:06 +00:00
|
|
|
if err != nil {
|
2022-09-08 20:57:52 +00:00
|
|
|
return err
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 20:57:52 +00:00
|
|
|
var matchesPrefix = false
|
|
|
|
|
|
|
|
for i := 0; i < len(paths); i++ {
|
|
|
|
if strings.HasPrefix(filePath, paths[i]) {
|
|
|
|
matchesPrefix = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if matchesPrefix == false {
|
2022-09-10 21:17:55 +00:00
|
|
|
if Verbose {
|
2022-09-10 23:03:04 +00:00
|
|
|
fmt.Printf("%v Failed to serve file outside specified path(s): %v", time.Now().Format(LOGDATE), filePath)
|
2022-09-10 21:17:55 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 19:14:54 +00:00
|
|
|
http.NotFound(w, &r)
|
2022-09-08 20:57:52 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-08 15:12:06 +00:00
|
|
|
|
2022-09-08 20:57:52 +00:00
|
|
|
_, err = os.Stat(filePath)
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
2022-09-10 21:17:55 +00:00
|
|
|
if Verbose {
|
2022-09-10 23:03:04 +00:00
|
|
|
fmt.Printf("%v Failed to serve non-existent file: %v", time.Now().Format(LOGDATE), filePath)
|
2022-09-10 21:17:55 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 19:14:54 +00:00
|
|
|
http.NotFound(w, &r)
|
2022-09-08 20:57:52 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
} else if !errors.Is(err, os.ErrNotExist) && err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-12 00:01:56 +00:00
|
|
|
var startTime time.Time
|
|
|
|
|
2022-09-10 23:03:04 +00:00
|
|
|
if Verbose {
|
2022-09-12 00:01:56 +00:00
|
|
|
startTime = time.Now()
|
2022-09-12 01:05:55 +00:00
|
|
|
fmt.Printf("%v Serving file: %v", startTime.Format(LOGDATE), filePath)
|
2022-09-10 23:03:04 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 20:57:52 +00:00
|
|
|
buf, err := os.ReadFile(filePath)
|
2022-09-08 15:12:06 +00:00
|
|
|
if err != nil {
|
2022-09-08 20:57:52 +00:00
|
|
|
return err
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|
2022-09-08 20:57:52 +00:00
|
|
|
|
|
|
|
w.Write(buf)
|
|
|
|
|
2022-09-10 21:17:55 +00:00
|
|
|
if Verbose {
|
2022-09-12 01:48:52 +00:00
|
|
|
fmt.Printf(" (Finished in %v)\n", time.Now().Sub(startTime).Round(time.Microsecond))
|
2022-09-10 21:17:55 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 20:57:52 +00:00
|
|
|
return nil
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 20:30:51 +00:00
|
|
|
func servePageHandler(paths []string) http.HandlerFunc {
|
2022-09-08 15:12:06 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2022-09-08 20:30:51 +00:00
|
|
|
if r.RequestURI == "/" {
|
2022-09-09 22:21:03 +00:00
|
|
|
err := generatePageHtml(w, *r, paths)
|
2022-09-08 20:30:51 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-09-08 20:57:52 +00:00
|
|
|
} else {
|
2022-09-09 19:14:54 +00:00
|
|
|
err := serveStaticFile(w, *r, paths)
|
2022-09-08 20:30:51 +00:00
|
|
|
if err != nil {
|
2022-09-08 20:57:52 +00:00
|
|
|
log.Fatal(err)
|
2022-09-08 20:30:51 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func doNothing(http.ResponseWriter, *http.Request) {}
|
|
|
|
|
2022-09-08 15:57:59 +00:00
|
|
|
func ServePage(args []string) {
|
2022-09-09 19:28:57 +00:00
|
|
|
paths, err := normalizePaths(args)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-09-08 20:30:51 +00:00
|
|
|
|
|
|
|
http.HandleFunc("/", servePageHandler(paths))
|
2022-09-08 15:12:06 +00:00
|
|
|
http.HandleFunc("/favicon.ico", doNothing)
|
|
|
|
|
|
|
|
port := strconv.Itoa(Port)
|
|
|
|
|
|
|
|
log.Fatal(http.ListenAndServe(":"+port, nil))
|
|
|
|
}
|