2017-03-28 15:01:59 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-08-13 01:46:09 +08:00
|
|
|
"context"
|
2022-07-27 03:27:30 +08:00
|
|
|
"errors"
|
2025-03-27 03:23:00 +08:00
|
|
|
"fmt"
|
|
|
|
"html"
|
|
|
|
"io"
|
|
|
|
"io/fs"
|
2017-03-28 15:01:59 +08:00
|
|
|
"log"
|
2021-08-13 01:46:09 +08:00
|
|
|
"net"
|
2017-03-28 15:01:59 +08:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2025-03-27 03:23:00 +08:00
|
|
|
"path"
|
2017-03-28 15:01:59 +08:00
|
|
|
"path/filepath"
|
2021-08-13 01:46:09 +08:00
|
|
|
"strconv"
|
2025-03-27 03:23:00 +08:00
|
|
|
"strings"
|
2017-03-28 15:01:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func sendThatFile(basepath string) func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2025-03-27 03:23:00 +08:00
|
|
|
filename := filepath.Join(basepath, filepath.Clean(string([]rune{filepath.Separator})+filepath.FromSlash(r.URL.Path)))
|
2017-03-28 15:01:59 +08:00
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
2022-07-27 03:27:30 +08:00
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
2017-03-28 15:01:59 +08:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2025-03-27 03:23:00 +08:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2017-03-28 15:01:59 +08:00
|
|
|
return
|
|
|
|
}
|
2025-03-27 03:23:00 +08:00
|
|
|
defer f.Close()
|
2017-03-28 15:01:59 +08:00
|
|
|
finfo, err := f.Stat()
|
|
|
|
if err != nil {
|
2025-03-27 03:23:00 +08:00
|
|
|
http.Error(w, fmt.Sprintf("checking file info: %v", err), http.StatusInternalServerError)
|
2017-03-28 15:01:59 +08:00
|
|
|
return
|
|
|
|
}
|
2025-03-27 03:23:00 +08:00
|
|
|
content := io.ReadSeeker(f)
|
|
|
|
if finfo.IsDir() {
|
|
|
|
names, err := f.ReadDir(-1)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("reading directory: %v", err), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
var builder strings.Builder
|
|
|
|
builder.WriteString("<body><html>")
|
|
|
|
for _, name := range names {
|
|
|
|
suffix := ""
|
|
|
|
if name.IsDir() {
|
|
|
|
suffix = "/"
|
|
|
|
}
|
|
|
|
builder.WriteString(fmt.Sprintf("<a href=%q>%s</a><br/>\n", path.Join(r.URL.Path, name.Name())+suffix, html.EscapeString(fs.FormatDirEntry(name))))
|
|
|
|
}
|
|
|
|
builder.WriteString("</html></body>")
|
|
|
|
content = strings.NewReader(builder.String())
|
|
|
|
}
|
|
|
|
http.ServeContent(w, r, filename, finfo.ModTime(), content)
|
2017-03-28 15:01:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
args := os.Args
|
2021-08-13 01:46:09 +08:00
|
|
|
if len(args) < 2 {
|
2024-07-25 05:32:48 +08:00
|
|
|
log.Fatal("requires subdirectory path [and optional port [and optional port file name [and optional TLS cert and key file names [and optional pid file name]]]]")
|
2021-08-13 01:46:09 +08:00
|
|
|
}
|
|
|
|
basedir := args[1]
|
|
|
|
port := "0"
|
|
|
|
if len(args) > 2 {
|
|
|
|
port = args[2]
|
2017-03-28 15:01:59 +08:00
|
|
|
}
|
2024-07-25 05:32:48 +08:00
|
|
|
certs, key := "", ""
|
|
|
|
if len(args) > 5 && args[4] != "" && args[5] != "" {
|
|
|
|
certs = args[4]
|
|
|
|
key = args[5]
|
|
|
|
}
|
|
|
|
if len(args) > 6 && args[6] != "" {
|
2025-04-01 09:11:54 +08:00
|
|
|
err := os.WriteFile(args[6], []byte(strconv.Itoa(os.Getpid())), 0o644)
|
2024-07-25 05:32:48 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%v", err)
|
|
|
|
}
|
|
|
|
}
|
2017-03-28 15:01:59 +08:00
|
|
|
http.HandleFunc("/", sendThatFile(basedir))
|
2021-08-13 01:46:09 +08:00
|
|
|
server := http.Server{
|
|
|
|
Addr: ":" + port,
|
|
|
|
BaseContext: func(l net.Listener) context.Context {
|
|
|
|
if tcp, ok := l.Addr().(*net.TCPAddr); ok {
|
|
|
|
if len(args) > 3 {
|
2022-11-15 00:22:45 +08:00
|
|
|
f, err := os.CreateTemp(filepath.Dir(args[3]), filepath.Base(args[3]))
|
2021-08-13 01:46:09 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%v", err)
|
|
|
|
}
|
|
|
|
tempName := f.Name()
|
2024-07-25 05:32:48 +08:00
|
|
|
port := strconv.Itoa(tcp.Port)
|
|
|
|
if n, err := f.WriteString(port); err != nil || n != len(port) {
|
2021-08-13 01:46:09 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%v", err)
|
|
|
|
}
|
2024-07-25 05:32:48 +08:00
|
|
|
log.Fatalf("short write: %d != %d", n, len(port))
|
2021-08-13 01:46:09 +08:00
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
if err := os.Rename(tempName, args[3]); err != nil {
|
|
|
|
log.Fatalf("rename: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return context.Background()
|
|
|
|
},
|
|
|
|
}
|
2024-07-25 05:32:48 +08:00
|
|
|
if certs != "" && key != "" {
|
|
|
|
log.Fatal(server.ListenAndServeTLS(certs, key))
|
|
|
|
}
|
2024-08-07 03:07:45 +08:00
|
|
|
log.Fatal(server.ListenAndServe())
|
2017-03-28 15:01:59 +08:00
|
|
|
}
|