Skip to content

Commit 9a1d1df

Browse files
authored
Merge pull request #11 from projectdiscovery/feature-basic-auth-single-option
Feature basic auth single option
2 parents e1b3244 + 260a0d7 commit 9a1d1df

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

simplehttpserver.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ import (
99
"net/http"
1010
"net/http/httputil"
1111
"path"
12+
"strings"
1213

1314
"github.com/projectdiscovery/gologger"
1415
)
1516

1617
type options struct {
1718
ListenAddress string
1819
Folder string
19-
Username string
20-
Password string
20+
BasicAuth string
21+
username string
22+
password string
2123
Realm string
2224
Certificate string
2325
Key string
@@ -36,8 +38,7 @@ func main() {
3638
flag.StringVar(&opts.Certificate, "cert", "", "Certificate")
3739
flag.StringVar(&opts.Key, "key", "", "Key")
3840
flag.BoolVar(&opts.Verbose, "v", false, "Verbose")
39-
flag.StringVar(&opts.Username, "username", "", "Basic auth username")
40-
flag.StringVar(&opts.Password, "password", "", "Basic auth password")
41+
flag.StringVar(&opts.BasicAuth, "basic-auth", "", "Basic auth (username:password)")
4142
flag.StringVar(&opts.Realm, "realm", "Please enter username and password", "Realm")
4243

4344
flag.Parse()
@@ -48,7 +49,14 @@ func main() {
4849

4950
gologger.Print().Msgf("Serving %s on http://%s/...", opts.Folder, opts.ListenAddress)
5051
layers := loglayer(http.FileServer(http.Dir(opts.Folder)))
51-
if opts.Username != "" || opts.Password != "" {
52+
if opts.BasicAuth != "" {
53+
baTokens := strings.SplitN(opts.BasicAuth, ":", 2)
54+
if len(baTokens) > 0 {
55+
opts.username = baTokens[0]
56+
}
57+
if len(baTokens) > 1 {
58+
opts.password = baTokens[1]
59+
}
5260
layers = loglayer(basicauthlayer(http.FileServer(http.Dir(opts.Folder))))
5361
}
5462

@@ -96,7 +104,7 @@ func loglayer(handler http.Handler) http.Handler {
96104
func basicauthlayer(handler http.Handler) http.HandlerFunc {
97105
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
98106
user, pass, ok := r.BasicAuth()
99-
if !ok || user != opts.Username || pass != opts.Password {
107+
if !ok || user != opts.username || pass != opts.password {
100108
w.Header().Set("WWW-Authenticate", fmt.Sprintf("Basic realm=\"%s\"", opts.Realm))
101109
w.WriteHeader(http.StatusUnauthorized)
102110
w.Write([]byte("Unauthorized.\n")) //nolint

0 commit comments

Comments
 (0)