github.com/blend/go-sdk@v1.20220411.3/webutil/local_ip.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package webutil
     9  
    10  import "net"
    11  
    12  // LocalIP returns the local server ip.
    13  func LocalIP() string {
    14  	addrs, err := net.InterfaceAddrs()
    15  	if err != nil {
    16  		return ""
    17  	}
    18  	for _, address := range addrs {
    19  		if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
    20  			if ipnet.IP.To4() != nil {
    21  				return ipnet.IP.String()
    22  			}
    23  		}
    24  	}
    25  	// return the loopback ...
    26  	for _, address := range addrs {
    27  		if ipnet, ok := address.(*net.IPNet); ok && ipnet.IP.IsLoopback() {
    28  			if ipnet.IP.To4() != nil {
    29  				return ipnet.IP.String()
    30  			}
    31  		}
    32  	}
    33  	return ""
    34  }