Skip to content

Commit 8cc0592

Browse files
authored
Merge branch 'dev' into feature-port-auto-retry
2 parents 69d59ff + 9a1d1df commit 8cc0592

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
@@ -15,15 +15,17 @@ import (
1515
"runtime"
1616
"strconv"
1717
"syscall"
18+
"strings"
1819

1920
"github.com/projectdiscovery/gologger"
2021
)
2122

2223
type options struct {
2324
ListenAddress string
2425
Folder string
25-
Username string
26-
Password string
26+
BasicAuth string
27+
username string
28+
password string
2729
Realm string
2830
Certificate string
2931
Key string
@@ -42,8 +44,7 @@ func main() {
4244
flag.StringVar(&opts.Certificate, "cert", "", "Certificate")
4345
flag.StringVar(&opts.Key, "key", "", "Key")
4446
flag.BoolVar(&opts.Verbose, "v", false, "Verbose")
45-
flag.StringVar(&opts.Username, "username", "", "Basic auth username")
46-
flag.StringVar(&opts.Password, "password", "", "Basic auth password")
47+
flag.StringVar(&opts.BasicAuth, "basic-auth", "", "Basic auth (username:password)")
4748
flag.StringVar(&opts.Realm, "realm", "Please enter username and password", "Realm")
4849

4950
flag.Parse()
@@ -54,7 +55,14 @@ func main() {
5455

5556
gologger.Print().Msgf("Serving %s on http://%s/...", opts.Folder, opts.ListenAddress)
5657
layers := loglayer(http.FileServer(http.Dir(opts.Folder)))
57-
if opts.Username != "" || opts.Password != "" {
58+
if opts.BasicAuth != "" {
59+
baTokens := strings.SplitN(opts.BasicAuth, ":", 2)
60+
if len(baTokens) > 0 {
61+
opts.username = baTokens[0]
62+
}
63+
if len(baTokens) > 1 {
64+
opts.password = baTokens[1]
65+
}
5866
layers = loglayer(basicauthlayer(http.FileServer(http.Dir(opts.Folder))))
5967
}
6068

@@ -116,7 +124,7 @@ func loglayer(handler http.Handler) http.Handler {
116124
func basicauthlayer(handler http.Handler) http.HandlerFunc {
117125
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
118126
user, pass, ok := r.BasicAuth()
119-
if !ok || user != opts.Username || pass != opts.Password {
127+
if !ok || user != opts.username || pass != opts.password {
120128
w.Header().Set("WWW-Authenticate", fmt.Sprintf("Basic realm=\"%s\"", opts.Realm))
121129
w.WriteHeader(http.StatusUnauthorized)
122130
w.Write([]byte("Unauthorized.\n")) //nolint

0 commit comments

Comments
 (0)