當前位置: 首頁>>代碼示例>>Golang>>正文


Golang net.LookupHost函數代碼示例

本文整理匯總了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
}
開發者ID:ronpastore,項目名稱:grafana,代碼行數:29,代碼來源:endpointdiscovery.go

示例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
}
開發者ID:reduxdj,項目名稱:grafana,代碼行數:12,代碼來源:endpointdiscovery.go

示例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
}
開發者ID:juju2013,項目名稱:gsnova,代碼行數:31,代碼來源:spac_func.go

示例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
}
開發者ID:ryansb,項目名稱:cjdcmd,代碼行數:30,代碼來源:dns.go

示例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
}
開發者ID:nickwei84,項目名稱:cf-acceptance-tests,代碼行數:26,代碼來源:config_struct.go

示例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)
		}
	}
}
開發者ID:philpennock,項目名稱:sks_spider,代碼行數:35,代碼來源:countries_test.go

示例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
}
開發者ID:ineiti,項目名稱:prifi,代碼行數:31,代碼來源:config.go

示例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
}
開發者ID:CrocdileChan,項目名稱:dvara,代碼行數:35,代碼來源:replica_set.go

示例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
}
開發者ID:facessl,項目名稱:letsencrypt-go,代碼行數:7,代碼來源:acme_test.go

示例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
}
開發者ID:hayajo,項目名稱:go-rblcheck,代碼行數:30,代碼來源:net.go

示例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
}
開發者ID:Spalf,項目名稱:resolver,代碼行數:25,代碼來源:resolver.go

示例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
}
開發者ID:badoo,項目名稱:thunder,代碼行數:29,代碼來源:dns.go

示例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
}
開發者ID:mycopy,項目名稱:socks,代碼行數:35,代碼來源:example_test.go

示例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
}
開發者ID:kambeena,項目名稱:udtgo,代碼行數:27,代碼來源:udt.go

示例15: lookup

func lookup() {
	for {
		req := <-requestChan
		req.addrs, req.err = net.LookupHost(req.host)
		req.done <- 1
	}
}
開發者ID:cyfdecyf,項目名稱:dnspool,代碼行數:7,代碼來源:dnspool.go


注:本文中的net.LookupHost函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。