diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 3a4aafc..4f75b56 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -1 +1,2 @@ Navaneeth K N +Jared De Blander diff --git a/README.md b/README.md index 739dd93..b9f754d 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,10 @@ Provided you have ```serve``` under your $PATH somewhere: This will serve the current directory at ```http://localhost:5000/``` + serve -a + +Will require visitor to provide http-auth credentials. + serve -p 9999 ~/my-awesome-blog Will serve the contents of the folder ```~/my-awesome-blog``` at ```http://localhost:9999/``` diff --git a/bin/serve b/bin/serve new file mode 100755 index 0000000..95415b4 Binary files /dev/null and b/bin/serve differ diff --git a/main.go b/main.go index 6de67cd..d1e45e6 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,21 @@ // Copyright 2014 Karan Misra. // Use of this source code is governed by the MIT // license that can be found in the LICENSE file. +// +// http authentication support added. References: +// http://github.com/abbot/go-http-auth +// http://stackoverflow.com/questions/25552107/golang-how-to-serve-static-files-with-basic-authentication package main import ( + "crypto/sha1" + "encoding/base64" "flag" "fmt" + "github.com/abbot/go-http-auth" + "log" + "math/rand" "net" "net/http" "os" @@ -15,15 +24,35 @@ import ( "time" ) -var version = "0.2.3" +var version = "0.2.4" var ( port = flag.Int("p", 5000, "port to serve on") prefix = flag.String("x", "/", "prefix to serve under") showVersion = flag.Bool("v", false, "show version info") openBrowser = flag.Bool("o", false, "open the url") + httpAuth = flag.Bool("a", false, "requires random http auth") + password = "secretpassword" + username = "serve" ) +func RandomString(strlen int) string { + rand.Seed(time.Now().UTC().UnixNano()) + const chars = "abcdefghijklmnopqrstuvwxyz0123456789" + result := make([]byte, strlen) + for i := 0; i < strlen; i++ { + result[i] = chars[rand.Intn(len(chars))] + } + return string(result) +} + +func Secret(user, realm string) string { + if user == username { + return password + } + return "" +} + func main() { flag.Parse() @@ -55,7 +84,15 @@ func main() { fmt.Printf("Service traffic from %v under port %v with prefix %v\n", dir, *port, *prefix) fmt.Printf("Or simply put, just open %v to get rocking!\n", uri) - + if *httpAuth { + username = RandomString(5) + password = RandomString(5) + h := sha1.New() + h.Write([]byte(password)) + fmt.Printf("user: %v password: %v\n", username, password) + password = "{SHA}" + base64.StdEncoding.EncodeToString(h.Sum(nil)) + // fmt.Println(password) + } go func() { if *openBrowser { success := waitForWebserver() @@ -72,13 +109,27 @@ func main() { } }() - http.Handle(*prefix, http.StripPrefix(*prefix, http.FileServer(http.Dir(dir)))) + authenticator := auth.NewBasicAuthenticator("Serve Credentials", Secret) + if *httpAuth { + http.HandleFunc("/", auth.JustCheck(authenticator, handleFileServer(dir, "/"))) + } else { + http.HandleFunc("/", handleFileServer(dir, "/")) + } if err := http.ListenAndServe(portStr, nil); err != nil { fmt.Fprintf(os.Stderr, "Error while starting the web server\n%v\n", err) os.Exit(1) } } +func handleFileServer(dir, prefix string) http.HandlerFunc { + fs := http.FileServer(http.Dir(dir)) + realHandler := http.StripPrefix(prefix, fs).ServeHTTP + return func(w http.ResponseWriter, req *http.Request) { + log.Println(req.URL) + realHandler(w, req) + } +} + func waitForWebserver() bool { timeout := time.After(1 * time.Second) connStr := fmt.Sprintf("127.0.0.1:%v", *port) diff --git a/pkg/linux_amd64/github.com/abbot/go-http-auth.a b/pkg/linux_amd64/github.com/abbot/go-http-auth.a new file mode 100644 index 0000000..226ff0c Binary files /dev/null and b/pkg/linux_amd64/github.com/abbot/go-http-auth.a differ diff --git a/pkg/linux_amd64/golang.org/x/crypto/bcrypt.a b/pkg/linux_amd64/golang.org/x/crypto/bcrypt.a new file mode 100644 index 0000000..de0ec81 Binary files /dev/null and b/pkg/linux_amd64/golang.org/x/crypto/bcrypt.a differ diff --git a/pkg/linux_amd64/golang.org/x/crypto/blowfish.a b/pkg/linux_amd64/golang.org/x/crypto/blowfish.a new file mode 100644 index 0000000..5240f0f Binary files /dev/null and b/pkg/linux_amd64/golang.org/x/crypto/blowfish.a differ diff --git a/pkg/linux_amd64/golang.org/x/net/context.a b/pkg/linux_amd64/golang.org/x/net/context.a new file mode 100644 index 0000000..3474e67 Binary files /dev/null and b/pkg/linux_amd64/golang.org/x/net/context.a differ diff --git a/src/github.com/abbot/go-http-auth b/src/github.com/abbot/go-http-auth new file mode 160000 index 0000000..cb43723 --- /dev/null +++ b/src/github.com/abbot/go-http-auth @@ -0,0 +1 @@ +Subproject commit cb4372376e1e00e9f6ab9ec142e029302c9e7140 diff --git a/src/golang.org/x/crypto b/src/golang.org/x/crypto new file mode 160000 index 0000000..5f31782 --- /dev/null +++ b/src/golang.org/x/crypto @@ -0,0 +1 @@ +Subproject commit 5f31782cfb2b6373211f8f9fbf31283fa234b570 diff --git a/src/golang.org/x/net b/src/golang.org/x/net new file mode 160000 index 0000000..8b4af36 --- /dev/null +++ b/src/golang.org/x/net @@ -0,0 +1 @@ +Subproject commit 8b4af36cd21a1f85a7484b49feb7c79363106d8e