From b2bb326b1140a6f968738c47e23197c98cba70bb Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Tue, 1 May 2018 11:50:55 -0700 Subject: [PATCH] Fixed private ip lookup Signed-off-by: Vishal Rana --- Makefile | 2 +- armor.go | 2 +- http.go | 2 +- util/util.go | 11 +++++++++-- website/content/guide.md | 2 +- 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index a2f13f3..5dd65bd 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ IMAGE = labstack/armor -VERSION = 0.4.9 +VERSION = 0.4.10 run: go run cmd/armor/main.go diff --git a/armor.go b/armor.go index a6d778b..32c2671 100644 --- a/armor.go +++ b/armor.go @@ -92,7 +92,7 @@ type ( ) const ( - Version = "0.4.9" + Version = "0.4.10" Website = "https://armor.labstack.com" ) diff --git a/http.go b/http.go index d566c92..403b55c 100644 --- a/http.go +++ b/http.go @@ -99,7 +99,7 @@ func (h *HTTP) Start() error { e := h.echo if a.DefaultConfig { a.Colorer.Printf("⇨ serving from %s (Local)\n", a.Colorer.Green("http://localhost"+a.Address)) - ip := util.GetPrivateIP() + ip := util.PrivateIP() if ip != "" { _, port, _ := net.SplitHostPort(a.Address) a.Colorer.Printf("⇨ serving from %s (Intranet)\n", a.Colorer.Green(fmt.Sprintf("http://%s:%s", ip, port))) diff --git a/util/util.go b/util/util.go index 9e45e82..a91fd8c 100644 --- a/util/util.go +++ b/util/util.go @@ -24,7 +24,7 @@ func StripPort(host string) string { return host[:colon] } -func GetPrivateIP() string { +func PrivateIP() string { addrs, err := net.InterfaceAddrs() if err != nil { return "" @@ -32,7 +32,14 @@ func GetPrivateIP() string { for _, address := range addrs { if ipNet, ok := address.(*net.IPNet); ok && !ipNet.IP.IsLoopback() { if ipNet.IP.To4() != nil { - return ipNet.IP.String() + ip := ipNet.IP + _, cidr24BitBlock, _ := net.ParseCIDR("10.0.0.0/8") + _, cidr20BitBlock, _ := net.ParseCIDR("172.16.0.0/12") + _, cidr16BitBlock, _ := net.ParseCIDR("192.168.0.0/16") + private := cidr24BitBlock.Contains(ip) || cidr20BitBlock.Contains(ip) || cidr16BitBlock.Contains(ip) + if private { + return ip.String() + } } } } diff --git a/website/content/guide.md b/website/content/guide.md index 141d74a..62efc26 100644 --- a/website/content/guide.md +++ b/website/content/guide.md @@ -44,7 +44,7 @@ Type `armor` in your terminal ___ / _ | ______ _ ___ ____ / __ |/ __/ ' \/ _ \/ __/ -/_/ |_/_/ /_/_/_/\___/_/ v0.4.9 +/_/ |_/_/ /_/_/_/\___/_/ v0.4.10 Uncomplicated, modern HTTP server https://armor.labstack.com