migrated to git.milar.in
This commit is contained in:
parent
7e25c34766
commit
488b75360a
5
.env
Normal file
5
.env
Normal file
@ -0,0 +1,5 @@
|
||||
ROOT_DIRECTORY=storage
|
||||
HTTP_INTERFACE=localhost
|
||||
HTTP_PORT=8080
|
||||
NO_AUTH=false
|
||||
HTTP_METHODS=GET
|
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
storage
|
||||
.env
|
9
go.mod
9
go.mod
@ -1,5 +1,8 @@
|
||||
module git.tordarus.net/Tordarus/httpfs
|
||||
module git.milar.in/milarin/httpfs
|
||||
|
||||
go 1.17
|
||||
go 1.20
|
||||
|
||||
require git.tordarus.net/Tordarus/dockerhealth v0.0.1
|
||||
require (
|
||||
git.milar.in/milarin/envvars/v2 v2.0.0
|
||||
git.milar.in/milarin/slices v0.0.6
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@ -1,2 +1,6 @@
|
||||
git.milar.in/milarin/envvars/v2 v2.0.0 h1:DWRQCWaHqzDD8NGpSgv5tYLuF9A/dVFPAtTvz3oiIqE=
|
||||
git.milar.in/milarin/envvars/v2 v2.0.0/go.mod h1:HkdEi+gG2lJSmVq547bTlQV4qQ0hO333bE8IrE0B9yY=
|
||||
git.milar.in/milarin/slices v0.0.6 h1:AQoSarZ58WHYol9c6woWJSe8wFpPC2RC4cvIlZpfg9s=
|
||||
git.milar.in/milarin/slices v0.0.6/go.mod h1:NOr53AOeur/qscu/FBj3lsFR262PNYBccLYSTCAXRk4=
|
||||
git.tordarus.net/Tordarus/dockerhealth v0.0.1 h1:rxkwmCW5PDe9gbnXap7d3n5rK1Qyr6xpmJPZFom/ZXc=
|
||||
git.tordarus.net/Tordarus/dockerhealth v0.0.1/go.mod h1:U0IPsBJHAjqWgNyehWwGfYqTwPJgqBNhzk/eBpaZsnE=
|
||||
|
92
main.go
92
main.go
@ -1,15 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
@ -17,23 +14,28 @@ import (
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"git.tordarus.net/Tordarus/dockerhealth"
|
||||
"git.milar.in/milarin/envvars/v2"
|
||||
"git.milar.in/milarin/slices"
|
||||
)
|
||||
|
||||
var (
|
||||
directory = flag.String("d", ".", "directory")
|
||||
intf = flag.String("i", "", "interface")
|
||||
port = flag.Uint("p", 80, "port")
|
||||
noauth = flag.Bool("a", false, "dont use basic auth")
|
||||
Directory = envvars.String("ROOT_DIRECTORY", ".")
|
||||
Interface = envvars.String("HTTP_INTERFACE", "")
|
||||
Port = envvars.Uint16("HTTP_PORT", 80)
|
||||
NoAuth = envvars.Bool("NO_AUTH", false)
|
||||
AllowedMethods = envvars.StringSlice("HTTP_METHODS", ",", []string{"GET", "PUT", "HEAD", "DELETE", "COPY", "LINK"})
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
fmt.Println("Root Directory:", Directory)
|
||||
fmt.Println("HTTP interface:", Interface)
|
||||
fmt.Println("HTTP port:", Port)
|
||||
fmt.Println("Basic Auth enabled:", !NoAuth)
|
||||
fmt.Println("Allowed HTTP methods:", strings.Join(AllowedMethods, ","))
|
||||
|
||||
http.HandleFunc("/", handler)
|
||||
|
||||
dockerhealth.Healthy = true
|
||||
err := http.ListenAndServe(fmt.Sprintf("%s:%d", *intf, *port), nil)
|
||||
err := http.ListenAndServe(fmt.Sprintf("%s:%d", Interface, Port), nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -41,6 +43,13 @@ func main() {
|
||||
|
||||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println(r.Method, getPath(r.URL.Path, r))
|
||||
|
||||
if !slices.Contains(AllowedMethods, r.Method) {
|
||||
w.Header().Add("Allow", strings.Join(AllowedMethods, ", "))
|
||||
http.Error(w, "invalid method", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
get(w, r)
|
||||
@ -59,45 +68,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func get(w http.ResponseWriter, r *http.Request) {
|
||||
path := getPath(r.URL.Path, r)
|
||||
|
||||
fi, err := os.Stat(path)
|
||||
if err != nil {
|
||||
handleError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", getMimetype(path))
|
||||
w.Header().Add("File-Permissions", fi.Mode().Perm().String())
|
||||
w.Header().Add("Modified-Time", fi.ModTime().String())
|
||||
w.Header().Add("Content-Length", strconv.FormatInt(fi.Size(), 10))
|
||||
|
||||
username, groupname, err := getOwnerNames(getOwnerIDs(fi))
|
||||
if err == nil {
|
||||
w.Header().Add("File-Owner", username)
|
||||
w.Header().Add("Group-Owner", groupname)
|
||||
}
|
||||
|
||||
if fi.IsDir() {
|
||||
files, err := os.ReadDir(path)
|
||||
if err != nil {
|
||||
handleError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
fmt.Fprintln(w, url.QueryEscape(file.Name()))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
handleError(w, r, err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
io.Copy(w, file)
|
||||
http.ServeFile(w, r, path)
|
||||
}
|
||||
|
||||
func put(w http.ResponseWriter, r *http.Request) {
|
||||
@ -245,23 +216,10 @@ func getOwnerNames(uid, gid string) (username, groupname string, err error) {
|
||||
}
|
||||
|
||||
func getPath(dirty_path string, r *http.Request) string {
|
||||
if *noauth {
|
||||
return filepath.Join(*directory, filepath.Clean(dirty_path))
|
||||
if NoAuth {
|
||||
return filepath.Join(Directory, filepath.Clean(dirty_path))
|
||||
}
|
||||
|
||||
username, _, _ := basicAuth(r.Header.Get("Authorization"))
|
||||
return filepath.Join(*directory, filepath.Clean(username), filepath.Clean(dirty_path))
|
||||
}
|
||||
|
||||
func basicAuth(authHeader string) (username, password string, err error) {
|
||||
data, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(authHeader, "Basic "))
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
splits := strings.SplitN(string(data), ":", 2)
|
||||
if len(splits) < 2 {
|
||||
return "", "", errors.New("invalid auth header")
|
||||
}
|
||||
return splits[0], splits[1], nil
|
||||
username, _, _ := r.BasicAuth()
|
||||
return filepath.Join(Directory, filepath.Clean(username), filepath.Clean(dirty_path))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user