Skip to content

Commit

Permalink
Fixed private ip lookup
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <vr@labstack.com>
  • Loading branch information
vishr committed May 1, 2018
1 parent 3336020 commit b2bb326
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
IMAGE = labstack/armor
VERSION = 0.4.9
VERSION = 0.4.10

run:
go run cmd/armor/main.go
Expand Down
2 changes: 1 addition & 1 deletion armor.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type (
)

const (
Version = "0.4.9"
Version = "0.4.10"
Website = "https://armor.labstack.com"
)

Expand Down
2 changes: 1 addition & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
11 changes: 9 additions & 2 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,22 @@ func StripPort(host string) string {
return host[:colon]
}

func GetPrivateIP() string {
func PrivateIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
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()
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion website/content/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Type `armor` in your terminal
___
/ _ | ______ _ ___ ____
/ __ |/ __/ ' \/ _ \/ __/
/_/ |_/_/ /_/_/_/\___/_/ v0.4.9
/_/ |_/_/ /_/_/_/\___/_/ v0.4.10
Uncomplicated, modern HTTP server
https://armor.labstack.com
Expand Down

0 comments on commit b2bb326

Please sign in to comment.