Fix GetDomainName()
This commit is contained in:
parent
e3c33d25ac
commit
e8f5f2cc53
@ -1,9 +1,6 @@
|
||||
package functions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/text/cases"
|
||||
@ -18,40 +15,3 @@ func Title(s string) string {
|
||||
func Split(sep, s string) []string {
|
||||
return strings.Split(s, sep)
|
||||
}
|
||||
|
||||
// getDomainName attempts to find the domain name of the host.
|
||||
// It returns the domain name if found, otherwise an error.
|
||||
func getDomainName() (string, error) {
|
||||
// Get the hostname
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error getting hostname: %v", err)
|
||||
}
|
||||
|
||||
// Lookup IP addresses associated with the hostname
|
||||
ips, err := net.LookupIP(hostname)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error looking up IP addresses for hostname %s: %v", hostname, err)
|
||||
}
|
||||
|
||||
for _, ip := range ips {
|
||||
// Perform a reverse lookup to find the FQDN
|
||||
names, err := net.LookupAddr(ip.String())
|
||||
if err != nil {
|
||||
// If there's an error, continue to the next IP
|
||||
continue
|
||||
}
|
||||
|
||||
for _, name := range names {
|
||||
cleanName := strings.TrimSuffix(name, ".")
|
||||
return cleanName, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("domain name not found for host: %s", hostname)
|
||||
}
|
||||
|
||||
func GetDomainName() string {
|
||||
domain, _ := getDomainName()
|
||||
return domain
|
||||
}
|
||||
|
18
functions/getdomain.go
Normal file
18
functions/getdomain.go
Normal file
@ -0,0 +1,18 @@
|
||||
package functions
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetDomainName() string {
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
return "exmaple.ir"
|
||||
}
|
||||
parts := strings.Split(hostname, ".")
|
||||
if len(parts) < 2 {
|
||||
return "exmaple.ir"
|
||||
}
|
||||
return strings.Join(parts[1:], ".")
|
||||
}
|
Loading…
Reference in New Issue
Block a user