diff --git a/.env b/.env new file mode 100644 index 0000000..51d9d41 --- /dev/null +++ b/.env @@ -0,0 +1,5 @@ +ROOT_DIRECTORY=storage +HTTP_INTERFACE=localhost +HTTP_PORT=8080 +NO_AUTH=false +HTTP_METHODS=GET \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..31801d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +storage +.env diff --git a/go.mod b/go.mod index 03ca22b..b9df152 100644 --- a/go.mod +++ b/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 +) diff --git a/go.sum b/go.sum index 569b1fe..239b4ca 100644 --- a/go.sum +++ b/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= diff --git a/httpfs b/httpfs deleted file mode 100755 index aa8af76..0000000 Binary files a/httpfs and /dev/null differ diff --git a/main.go b/main.go index 178dd5e..28fb75f 100644 --- a/main.go +++ b/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)) } diff --git a/test2.txt b/test2.txt deleted file mode 100644 index e69de29..0000000