github.com/sujit-baniya/log@v1.0.73/fqdn/legacy.go (about)

     1  package fqdn
     2  
     3  import (
     4  	"net"
     5  	"os"
     6  	"strings"
     7  )
     8  
     9  // Get Fully Qualified Domain Name
    10  // returns "unknown" or hostname in case of error
    11  //
    12  // Deprecated:
    13  //
    14  //	This function has bad API, works poorly and is replace by
    15  //	Hostname. Please please do not use it. It *will* be removed
    16  //	in the next version.
    17  func Get() string {
    18  	hostname, err := os.Hostname()
    19  	if err != nil {
    20  		return "unknown"
    21  	}
    22  
    23  	addrs, err := net.LookupIP(hostname)
    24  	if err != nil {
    25  		return hostname
    26  	}
    27  
    28  	for _, addr := range addrs {
    29  		if ipv4 := addr.To4(); ipv4 != nil {
    30  			ip, err := ipv4.MarshalText()
    31  			if err != nil {
    32  				return hostname
    33  			}
    34  			hosts, err := net.LookupAddr(string(ip))
    35  			if err != nil || len(hosts) == 0 {
    36  				return hostname
    37  			}
    38  			fqdn := hosts[0]
    39  			return strings.TrimSuffix(fqdn, ".") // return fqdn without trailing dot
    40  		}
    41  	}
    42  	return hostname
    43  }