From 4be011755b587d87824141fd06bf9f845bafe1cd Mon Sep 17 00:00:00 2001 From: Seednode Date: Mon, 8 May 2023 11:45:57 -0500 Subject: [PATCH] Added -b|--bind to bind to specific address --- README.md | 1 + cmd/root.go | 2 ++ cmd/version.go | 2 +- cmd/web.go | 14 +++++++++++++- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2b62af6..8bed2d5 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,7 @@ Available Commands: version Print version Flags: + -b, --bind string address to bind to (default "0.0.0.0") -c, --cache generate directory cache at startup --cache-file string path to optional persistent cache file -d, --debug expose debug endpoint diff --git a/cmd/root.go b/cmd/root.go index 6478541..340e569 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,6 +12,7 @@ import ( ) var ( + bind string cache bool cacheFile string debug bool @@ -52,6 +53,7 @@ func Execute() { } func init() { + rootCmd.Flags().StringVarP(&bind, "bind", "b", "0.0.0.0", "address to bind to") rootCmd.Flags().BoolVarP(&cache, "cache", "c", false, "generate directory cache at startup") rootCmd.Flags().StringVar(&cacheFile, "cache-file", "", "path to optional persistent cache file") rootCmd.Flags().BoolVarP(&debug, "debug", "d", false, "expose debug endpoint") diff --git a/cmd/version.go b/cmd/version.go index 1b125bc..f56ad92 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.43.1" +var Version = "0.43.2" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index 537777f..506850f 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -12,6 +12,7 @@ import ( "io" "log" "math/rand" + "net" "net/http" _ "net/http/pprof" "net/url" @@ -835,6 +836,17 @@ func doNothing(http.ResponseWriter, *http.Request) {} func ServePage(args []string) error { fmt.Printf("roulette v%s\n\n", Version) + bindHost, err := net.LookupHost(bind) + if err != nil { + return err + } + + bindAddr := net.ParseIP(bindHost[0]) + if bindAddr == nil { + fmt.Println("Invalid bind address provided. Please specify an IPv4 or IPv6 address in dotted decimal or IPv6 format.") + os.Exit(1) + } + paths, err := normalizePaths(args) if err != nil { return err @@ -909,7 +921,7 @@ func ServePage(args []string) error { http.Handle("/_/json", serveJsonDebugHandler(args, index)) } - err = http.ListenAndServe(":"+strconv.FormatInt(int64(port), 10), nil) + err = http.ListenAndServe(net.JoinHostPort(bind, strconv.FormatInt(int64(port), 10)), nil) if err != nil { return err }