From dffbda7fe483de7ec6843f1ffcf2f51faea444d5 Mon Sep 17 00:00:00 2001 From: ache Date: Sun, 16 May 2021 05:35:14 +0200 Subject: Force IP version --- config/config.go | 3 +++ config/config.json | 3 ++- server.go | 29 ++++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index f20ce9e..5abdaf6 100644 --- a/config/config.go +++ b/config/config.go @@ -14,6 +14,8 @@ type config struct { Auth bool `json:"auth"` RootPath string `json:"root-path"` Cache bool `json:"cache"` + OnlyIPv4 bool `json:"ipv4"` + OnlyIPv6 bool `json:"ipv6"` } var Config config @@ -44,4 +46,5 @@ func SetDefaultValue() { Config.HideHidden = true Config.Auth = false Config.RootPath = "." + Config.OnlyIPv6 = true } diff --git a/config/config.json b/config/config.json index 09243ed..cb0e719 100644 --- a/config/config.json +++ b/config/config.json @@ -3,5 +3,6 @@ "allow-hidden": true, "hide-hidden": true, "auth": true, - "cache": true + "cache": true, + "ipv6": true } diff --git a/server.go b/server.go index eab1e75..0c50b92 100644 --- a/server.go +++ b/server.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "net/http" + "net" "os" "io" "html/template" @@ -415,6 +416,10 @@ func main() { var help, version bool var port = -1 var host string + var ip_version = "" + + var isV4 bool + var isV6 bool helpString := `This is the help` versionString := "0.0.0" @@ -439,6 +444,8 @@ func main() { flag.BoolVar(&version, "version", false, usageVersion) flag.BoolVar(&version, "v", false, usageVersion+" (shorthand)") + flag.BoolVar(&isV4, "ipv4", false, "force use of IPv4") + flag.BoolVar(&isV6, "ipv6", false, "force use of IPv6") flag.Parse() if help { @@ -474,6 +481,17 @@ func main() { if host != "" { config.Config.Hostnames = []string{host} } + if isV4 { + if !isV6 { + ip_version = "tcp4" + } else { + fmt.Println("Can't be only IPv4 and IPv6") + return + } + } else if isV6 { + ip_version = "tcp6" + } + fmap := template.FuncMap{ "formatURL": encodeURL, @@ -511,7 +529,16 @@ func main() { go func(host string) { defer wg.Done() fmt.Println("Launch server listen on " + host + ":" + strconv.Itoa(port)) - log.Fatal(http.ListenAndServe(host + ":" + strconv.Itoa(port), nil)) + if ip_version != "" { + n, err := net.Listen(ip_version, host + ":" + strconv.Itoa(port)) + if err == nil { + log.Fatal(http.Serve(n, nil)) + } else { + fmt.Println(err) + } + } else { + log.Fatal(http.ListenAndServe(host + ":" + strconv.Itoa(port), nil)) + } }(host) } wg.Wait() -- cgit v1.2.3