本文整理汇总了Golang中net.LookupHost函数的典型用法代码示例。如果您正苦于以下问题:Golang LookupHost函数的具体用法?Golang LookupHost怎么用?Golang LookupHost使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了LookupHost函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewEndpoint
func NewEndpoint(domainName string) (*Endpoint, error) {
host := strings.ToLower(domainName)
e := &Endpoint{Host: host}
if strings.Contains(host, "://") {
u, err := url.Parse(host)
if err != nil {
return nil, err
}
e.Host = strings.Split(u.Host, ":")[0]
e.URL = u
}
if net.ParseIP(e.Host) != nil {
// the parsed host is an IP address.
e.IsIP = true
return e, nil
}
addr, err := net.LookupHost(e.Host)
if err != nil || len(addr) < 1 {
e.Host = "www." + domainName
addr, err = net.LookupHost(e.Host)
if err != nil || len(addr) < 1 {
return nil, fmt.Errorf("failed to lookup IP of domain %s.", e.Host)
}
}
return e, nil
}
示例2: getHostName
func getHostName(domainName string) (string, error) {
host := strings.ToLower(domainName)
addr, err := net.LookupHost(host)
if err != nil || len(addr) < 1 {
host = "www." + domainName
addr, err = net.LookupHost(host)
if err != nil || len(addr) < 1 {
return "", errors.New("failed to lookup IP of domain.")
}
}
return host, nil
}
示例3: isHostInCN
func isHostInCN(req *http.Request) bool {
if nil == ipfunc {
return true
}
host := req.Host
if strings.Contains(host, ":") {
host, _, _ = net.SplitHostPort(host)
}
//consider use trusted DNS
var ip string
var ok bool
if strings.HasSuffix(host, ".cn") {
return true
} else {
if ips, err := net.LookupHost(host); nil == err && len(ips) > 0 {
ip = ips[0]
ok = true
}
}
if !ok || nil == ipfunc {
return false
}
country, err := ipfunc.FindCountry(ip)
if nil != err {
log.Printf("[WARN]Find country error:%v\n", err)
return false
}
ret := strings.EqualFold(country, "CN")
return ret
}
示例4: resolveHost
// Resolve a hostname to an IP address using the system DNS settings first, then HypeDNS
func resolveHost(hostname string) (ips []string, err error) {
var ip string
// Try the system DNS setup
result, _ := net.LookupHost(hostname)
if len(result) > 0 {
goto end
}
// Try with hypedns
ip, err = lookupHypeDNS(hostname)
if ip == "" || err != nil {
err = fmt.Errorf("Unable to resolve hostname. This is usually caused by not having a route to hypedns. Please try again in a few seconds.")
return
}
result = append(result, ip)
end:
for _, addr := range result {
tIP := net.ParseIP(addr)
// Only grab the cjdns IP's
if tIP[0] == 0xfc {
ips = append(ips, padIPv6(net.ParseIP(addr)))
}
}
return
}
示例5: validateApiEndpoint
func validateApiEndpoint(config *config) error {
if config.ApiEndpoint == nil {
return fmt.Errorf("* 'api' must not be null")
}
if config.GetApiEndpoint() == "" {
return fmt.Errorf("* Invalid configuration: 'api' must be a valid Cloud Controller endpoint but was blank")
}
u, err := url.Parse(config.GetApiEndpoint())
if err != nil {
return fmt.Errorf("* Invalid configuration: 'api' must be a valid URL but was set to '%s'", config.GetApiEndpoint())
}
host := u.Host
if host == "" {
// url.Parse misunderstood our convention and treated the hostname as a URL path
host = u.Path
}
if _, err = net.LookupHost(host); err != nil {
return fmt.Errorf("* Invalid configuration for 'api' <%s>: %s", config.GetApiEndpoint(), err)
}
return nil
}
示例6: TestCountrySpodhuis
func TestCountrySpodhuis(t *testing.T) {
ipList, err := net.LookupHost(checkSksHostname)
if err != nil {
t.Fatalf("LookupHost(%s) failed: %s", checkSksHostname, err)
}
if len(ipList) != checkSksIPCount {
t.Fatalf("Wrong number of IP addresses for \"%s\": expected %d got %d",
checkSksHostname, checkSksIPCount, len(ipList))
}
var expectSucceed bool
for _, ip := range ipList {
switch {
case net.ParseIP(ip).To4() != nil:
expectSucceed = true
default:
expectSucceed = checkSksExpectIPv6HasCountry
}
country, err := CountryForIPString(ip)
if err != nil {
if expectSucceed {
t.Fatalf("Failed to resolve country for [%s] (from \"%s\"): %s",
ip, checkSksHostname, err)
}
continue
}
if !expectSucceed {
t.Fatalf("Unexpectedly resolved country for [%s] (from \"%s\")",
ip, checkSksHostname)
}
if country != checkSksCountry {
t.Fatalf("Host \"%s\" IP [%s]: expected country \"%s\", got \"%s\"",
checkSksHostname, ip, checkSksCountry, country)
}
}
}
示例7: GetAddress
// getAddress gets the localhosts IPv4 address.
func GetAddress() (string, error) {
name, err := os.Hostname()
if err != nil {
log.Print("Error Resolving Hostname:", err)
return "", err
}
if ipv4host == "NONE" {
as, err := net.LookupHost(name)
if err != nil {
return "", err
}
addr := ""
for _, a := range as {
log.Printf("a = %+v", a)
if ipv4Reg.MatchString(a) {
log.Print("matches")
addr = a
}
}
if addr == "" {
err = errors.New("No IPv4 Address for Hostname")
}
return addr, err
}
return ipv4host, nil
}
示例8: proxyHostname
func (r *ReplicaSet) proxyHostname() string {
const home = "127.0.0.1"
hostname, err := os.Hostname()
if err != nil {
r.Log.Error(err)
return home
}
// The follow logic ensures that the hostname resolves to a local address.
// If it doesn't we don't use it since it probably wont work anyways.
hostnameAddrs, err := net.LookupHost(hostname)
if err != nil {
r.Log.Error(err)
return home
}
interfaceAddrs, err := net.InterfaceAddrs()
if err != nil {
r.Log.Error(err)
return home
}
for _, ia := range interfaceAddrs {
sa := ia.String()
for _, ha := range hostnameAddrs {
// check for an exact match or a match ignoring the suffix bits
if sa == ha || strings.HasPrefix(sa, ha+"/") {
return hostname
}
}
}
r.Log.Warnf("hostname %s doesn't resolve to the current host", hostname)
return home
}
示例9: requiresEtcHostsEdits
func requiresEtcHostsEdits(t *testing.T) {
addrs, err := net.LookupHost(testDomain)
if err != nil || len(addrs) != 1 || addrs[0] != "127.0.0.1" {
t.Skip("/etc/hosts file not properly configured, skipping test. see README for required edits")
}
return
}
示例10: LookupIP
func LookupIP(host string) (addrs []net.IP, err error) {
ip := net.ParseIP(host)
if ip == nil || ip.To4() == nil {
ip, ipnet, err := net.ParseCIDR(host)
if err != nil || ip.To4() == nil {
hosts, err := net.LookupHost(host)
if err != nil {
return nil, err
}
addrs := make([]net.IP, 0, len(hosts))
for _, v := range hosts {
ip = net.ParseIP(v)
ip = ip.To4()
if ip != nil {
addrs = append(addrs, ip)
}
}
return addrs, nil
}
addrs, _ := HostAddr(ipnet)
return addrs, nil
}
return []net.IP{ip}, nil
}
示例11: checkNameResolve
func checkNameResolve(inFilename string, debug bool) (err error) {
rawBytes, err := ioutil.ReadFile(inFilename)
if err != nil {
return err
}
text := string(rawBytes)
lines := strings.Split(text, "\n")
for _, line := range lines {
if line == "" {
if debug == true {
log.Println("End of file")
}
return nil
}
ip, err := net.LookupHost(line)
if debug == true {
log.Println("Try to resolve", line, "IP(s):", ip, "Error:", err)
} else {
if err != nil {
fmt.Println(line)
}
}
}
return nil
}
示例12: resolveAndUpdate
func (r *resolver) resolveAndUpdate(item cacheItem) (cacheItem, error) {
ips, err := net.LookupHost(item.host)
if err != nil {
if r.logger != nil {
r.logger.Warnf("resolveAndUpdate %v: %v", item.host, err)
}
r.cacheLock.Lock()
delete(r.cache, item.host)
r.cacheLock.Unlock()
return cacheItem{err: err}, err
}
if item.ips == nil || !util.StrSliceEqual(item.ips, ips) {
if r.logger != nil {
r.logger.Debugf("resolveAndUpdate: %s ips changed %v -> %v", item.host, item.ips, ips)
}
item.ips = ips
item.err = err
r.cacheLock.Lock()
r.cache[item.host] = item
r.cacheLock.Unlock()
}
return item, nil
}
示例13: ExampleSocks4Client
func ExampleSocks4Client() {
user := ""
client, err := NewSocks4Client("tcp", "127.0.0.1:1080", user, Direct)
if err != nil {
return
}
addrs, err := net.LookupHost("www.google.com")
if err != nil {
return
}
if len(addrs) == 0 {
return
}
conn, err := client.Dial("tcp", addrs[0]+":80")
if err != nil {
return
}
httpClient := httputil.NewClientConn(conn, nil)
defer httpClient.Close()
request, err := http.NewRequest("GET", "/", nil)
if err != nil {
return
}
resp, err := httpClient.Do(request)
if err != nil {
return
}
dump, err := httputil.DumpResponse(resp, true)
if err != nil {
return
}
println(string(dump))
return
}
示例14: Connect
func Connect(socket *Socket, host string, portno int) (retval int, err error) {
addrs, err := net.LookupHost(host)
if err != nil {
return -1, fmt.Errorf("Unable to connect to the socket: %s", err)
}
resolvedHost := addrs[0]
if socket.af == syscall.AF_INET6 {
resolvedHost = fmt.Sprintf("[%s]", resolvedHost)
}
rsa, salen, err := createSockaddr(resolvedHost, portno)
if err != nil {
return -1, fmt.Errorf("could not convert syscall.Sockaddr to syscall.RawSockaddrAny %s", err)
}
retval = int(C.udt_connect(socket.sock, (*C.struct_sockaddr)(unsafe.Pointer(rsa)),
C.int(salen)))
if retval != 0 {
return retval, udtErrDesc("Unable to connect to the socket")
}
return
}
示例15: lookup
func lookup() {
for {
req := <-requestChan
req.addrs, req.err = net.LookupHost(req.host)
req.done <- 1
}
}