Compare commits
No commits in common. "master" and "v0.0.2" have entirely different histories.
|
@ -1,6 +1,3 @@
|
||||||
Get() the full hostname for the OS.
|
Get() the full hostname for the OS.
|
||||||
|
|
||||||
* only cares about the hostname in DNS
|
* only cares about the hostname in DNS
|
||||||
* I wrote this before or not being aware of os.Hostname()
|
|
||||||
|
|
||||||
probably this package should go away
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ var ErrorEmptyDomainName error = errors.New("domain name is empty")
|
||||||
var ErrorDomainNameMisconfigured error = errors.New("your OS domain name is not configured correctly")
|
var ErrorDomainNameMisconfigured error = errors.New("your OS domain name is not configured correctly")
|
||||||
|
|
||||||
// returns 'hostname', 'domainname'
|
// returns 'hostname', 'domainname'
|
||||||
func SplitOld(s string) (string, string) {
|
func Split(s string) (string, string) {
|
||||||
parts := strings.Fields(s)
|
parts := strings.Fields(s)
|
||||||
if len(parts) == 0 {
|
if len(parts) == 0 {
|
||||||
return "", ""
|
return "", ""
|
||||||
|
@ -24,7 +24,7 @@ func SplitOld(s string) (string, string) {
|
||||||
|
|
||||||
// checks to make sure there is a domainname
|
// checks to make sure there is a domainname
|
||||||
func ValidDomainname(s string) error {
|
func ValidDomainname(s string) error {
|
||||||
_, domainname := SplitOld(s)
|
_, domainname := Split(s)
|
||||||
if domainname == "" {
|
if domainname == "" {
|
||||||
return ErrorEmptyDomainName
|
return ErrorEmptyDomainName
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,62 +0,0 @@
|
||||||
package hostname
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// for www.corp.wit.com retuns:
|
|
||||||
// "www", "corp.wit.com", nil
|
|
||||||
func GetDomainname() (string, string, error) {
|
|
||||||
hname, err := osGetHostname()
|
|
||||||
if err != nil {
|
|
||||||
return hname, "", err
|
|
||||||
}
|
|
||||||
hname = strings.TrimSpace(hname)
|
|
||||||
hname = strings.Trim(hname, ".")
|
|
||||||
parts := strings.Split(hname, ".")
|
|
||||||
if len(parts) == 0 {
|
|
||||||
return "", "", fmt.Errorf("domainname not set")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(parts) == 1 {
|
|
||||||
return parts[0], "", fmt.Errorf("domainname not set")
|
|
||||||
}
|
|
||||||
|
|
||||||
dname := strings.Join(parts[1:], ".")
|
|
||||||
|
|
||||||
return hname, dname, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// splits hostname into first and last part
|
|
||||||
// for www.corp.wit.com retuns:
|
|
||||||
// "www", "corp.wit.com", nil
|
|
||||||
func Split(hname string) (string, string, error) {
|
|
||||||
hname = strings.TrimSpace(hname)
|
|
||||||
hname = strings.Trim(hname, ".")
|
|
||||||
parts := strings.Split(hname, ".")
|
|
||||||
if len(parts) == 0 {
|
|
||||||
return "", "", fmt.Errorf("domainname not set")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(parts) == 1 {
|
|
||||||
return parts[0], "", fmt.Errorf("domainname not set")
|
|
||||||
}
|
|
||||||
|
|
||||||
dname := strings.Join(parts[1:], ".")
|
|
||||||
|
|
||||||
return hname, dname, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func Join(hname string, dname string) string {
|
|
||||||
hname = strings.TrimSpace(hname)
|
|
||||||
hname = strings.Trim(hname, ".")
|
|
||||||
dname = strings.TrimSpace(dname)
|
|
||||||
dname = strings.Trim(dname, ".")
|
|
||||||
|
|
||||||
fullname := strings.Join([]string{hname, dname}, ".")
|
|
||||||
fullname = strings.TrimSpace(fullname)
|
|
||||||
fullname = strings.Trim(fullname, ".")
|
|
||||||
|
|
||||||
return fullname
|
|
||||||
}
|
|
8
get.go
8
get.go
|
@ -1,17 +1,9 @@
|
||||||
package hostname
|
package hostname
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
// returns the hostname
|
// returns the hostname
|
||||||
// hostname is always set to the best effort
|
// hostname is always set to the best effort
|
||||||
// error is set if hostname isn't real
|
// error is set if hostname isn't real
|
||||||
func Get() (string, error) {
|
func Get() (string, error) {
|
||||||
hostname, err := osGetHostname()
|
hostname, err := osGetHostname()
|
||||||
if hostname == "" {
|
|
||||||
if err == nil {
|
|
||||||
err = fmt.Errorf("your machines' hostname is not set")
|
|
||||||
}
|
|
||||||
hostname = "unconfigured.hostname"
|
|
||||||
}
|
|
||||||
return hostname, err
|
return hostname, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,12 +12,12 @@ import (
|
||||||
// scutil --set HostName my-mac.example.com
|
// scutil --set HostName my-mac.example.com
|
||||||
|
|
||||||
func osGetHostname() (string, error) {
|
func osGetHostname() (string, error) {
|
||||||
return scutil([]string{"--get", "HostName"})
|
return scutil([]string{"-get", "HostName"})
|
||||||
}
|
}
|
||||||
|
|
||||||
// getDomainName executes the 'domainname' command and returns its output.
|
// getDomainName executes the 'domainname' command and returns its output.
|
||||||
func scutil(args []string) (string, error) {
|
func scutil(args []string) (string, error) {
|
||||||
cmd := exec.Command("scutil", args...)
|
cmd := exec.Command("scutil", args)
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
cmd.Stdout = &out
|
cmd.Stdout = &out
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
|
|
47
whois.go
47
whois.go
|
@ -1,47 +0,0 @@
|
||||||
package hostname
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
"net"
|
|
||||||
)
|
|
||||||
|
|
||||||
// GetWhois performs a raw whois query for a given domain.
|
|
||||||
// Note: This does not recursively find the right server; it uses a known one.
|
|
||||||
func GetWhois(domain, server string) (string, error) {
|
|
||||||
// Connect to the whois server on port 43
|
|
||||||
conn, err := net.Dial("tcp", net.JoinHostPort(server, "43"))
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
// Send the domain query
|
|
||||||
_, err = conn.Write([]byte(domain + "\r\n"))
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read the entire response
|
|
||||||
result, err := io.ReadAll(conn)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return string(result), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
func main() {
|
|
||||||
// We have to know the server for .com domains
|
|
||||||
const whoisServer = "whois.verisign-grs.com"
|
|
||||||
domain :="google.com"
|
|
||||||
|
|
||||||
whoisData, err := GetWhois(domain, whoisServer)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal"Error getting whois info: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(whoisData)
|
|
||||||
}
|
|
||||||
|
|
||||||
*/
|
|
Loading…
Reference in New Issue