ip.go 464 B

12345678910111213141516171819202122232425
  1. package network
  2. import "net"
  3. // FetchCurrentIps fetches the current IP addresses of the machine
  4. // only IPv4 addresses are returned
  5. func FetchCurrentIps() ([]net.IP, error) {
  6. ips := []net.IP{}
  7. addrs, err := net.InterfaceAddrs()
  8. if err != nil {
  9. return ips, err
  10. }
  11. for _, addr := range addrs {
  12. if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
  13. if ipNet.IP.To4() != nil {
  14. ips = append(ips, ipNet.IP)
  15. }
  16. }
  17. }
  18. return ips, nil
  19. }