/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ package net import ( "net" ) // IPAddrs finds and returns a list of non-loopback IPv4 addresses of the // current machine. func IPAddrs() (ips []net.IP, err error) { ips = []net.IP{} addrs, err := net.InterfaceAddrs() if err != nil { return } for _, a := range addrs { if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { if ipnet.IP.To4() != nil { ips = append(ips, ipnet.IP) } } } return ips, nil } // FormatAddress checks that the address has a consistent format. func FormatAddress(addr string) string { if ip := net.ParseIP(addr); ip != nil { // If this is an IPv6 address, encapsulate it in brackets if ip.To4() == nil { return "[" + ip.String() + "]" } return ip.String() } return addr }