Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Navaneeth K N <navaneethkn@gmail.com>
Jared De Blander <jared0x90@gmail.com>
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/```
Expand Down
Binary file added bin/serve
Binary file not shown.
57 changes: 54 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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()

Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down
Binary file added pkg/linux_amd64/github.com/abbot/go-http-auth.a
Binary file not shown.
Binary file added pkg/linux_amd64/golang.org/x/crypto/bcrypt.a
Binary file not shown.
Binary file added pkg/linux_amd64/golang.org/x/crypto/blowfish.a
Binary file not shown.
Binary file added pkg/linux_amd64/golang.org/x/net/context.a
Binary file not shown.
1 change: 1 addition & 0 deletions src/github.com/abbot/go-http-auth
Submodule go-http-auth added at cb4372
1 change: 1 addition & 0 deletions src/golang.org/x/crypto
Submodule crypto added at 5f3178
1 change: 1 addition & 0 deletions src/golang.org/x/net
Submodule net added at 8b4af3