Added -b|--bind to bind to specific address

This commit is contained in:
Seednode 2023-05-08 11:45:57 -05:00
parent 5beb1fc858
commit 4be011755b
4 changed files with 17 additions and 2 deletions

View File

@ -99,6 +99,7 @@ Available Commands:
version Print version version Print version
Flags: Flags:
-b, --bind string address to bind to (default "0.0.0.0")
-c, --cache generate directory cache at startup -c, --cache generate directory cache at startup
--cache-file string path to optional persistent cache file --cache-file string path to optional persistent cache file
-d, --debug expose debug endpoint -d, --debug expose debug endpoint

View File

@ -12,6 +12,7 @@ import (
) )
var ( var (
bind string
cache bool cache bool
cacheFile string cacheFile string
debug bool debug bool
@ -52,6 +53,7 @@ func Execute() {
} }
func init() { 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().BoolVarP(&cache, "cache", "c", false, "generate directory cache at startup")
rootCmd.Flags().StringVar(&cacheFile, "cache-file", "", "path to optional persistent cache file") rootCmd.Flags().StringVar(&cacheFile, "cache-file", "", "path to optional persistent cache file")
rootCmd.Flags().BoolVarP(&debug, "debug", "d", false, "expose debug endpoint") rootCmd.Flags().BoolVarP(&debug, "debug", "d", false, "expose debug endpoint")

View File

@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var Version = "0.43.1" var Version = "0.43.2"
func init() { func init() {
rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(versionCmd)

View File

@ -12,6 +12,7 @@ import (
"io" "io"
"log" "log"
"math/rand" "math/rand"
"net"
"net/http" "net/http"
_ "net/http/pprof" _ "net/http/pprof"
"net/url" "net/url"
@ -835,6 +836,17 @@ func doNothing(http.ResponseWriter, *http.Request) {}
func ServePage(args []string) error { func ServePage(args []string) error {
fmt.Printf("roulette v%s\n\n", Version) 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) paths, err := normalizePaths(args)
if err != nil { if err != nil {
return err return err
@ -909,7 +921,7 @@ func ServePage(args []string) error {
http.Handle("/_/json", serveJsonDebugHandler(args, index)) 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 { if err != nil {
return err return err
} }