From 4a69686f819ecb5e5855adcc0f57d789072d9a5f Mon Sep 17 00:00:00 2001 From: Tom Andrade Date: Wed, 1 May 2019 00:50:00 +0200 Subject: [PATCH] improve cli input validation --- main.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 27536ce..7d63687 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "log" "math/rand" + "net" "net/http" "net/url" "os" @@ -205,7 +206,7 @@ func main() { flag.StringVar(&domain, "domain", "localhost", "Domain to write to the URLs") flag.StringVar(&dumpFile, "dump", "urls.json", "Path to the file to dump the kv db") flag.StringVar(&path, "path", "", "Path to the base URL (https://localhost/PATH/... remember to append a / at the end") - flag.StringVar(&port, "port", "8080", "Port to listen for connections") + flag.IntVar(&port, "port", 8080, "Port to listen for connections") flag.StringVar(&proto, "proto", "https", "proto to the base URL (HTTPS://localhost/path/... no real https here just to set the url (for like a proxy offloading https") flag.IntVar(&urlSize, "urlsize", 10, "Define the size of the shortened String, default 10") version := flag.Bool("v", false, "prints current version") @@ -215,11 +216,26 @@ func main() { os.Exit(0) } + if port > 65535 || port < 1 { + + } if path != "" && !strings.HasSuffix(path, "/") { path = path + "/" } + ip := net.ParseIP(addr) + if ip != nil { + listenAddr = ip.String() + ":" + strconv.Itoa(port) + } else { + if govalidator.IsDNSName(addr) { + listenAddr = addr + ":" + strconv.Itoa(port) + } else { + log.Fatalln("Invalid ip address") + } + } - listenAddr := addr + ":" + port + if !govalidator.IsDNSName(domain) { + log.Fatalln("Invalid domain address") + } r := mux.NewRouter()