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


Golang dns.SplitDomainName函數代碼示例

本文整理匯總了Golang中github.com/miekg/dns.SplitDomainName函數的典型用法代碼示例。如果您正苦於以下問題:Golang SplitDomainName函數的具體用法?Golang SplitDomainName怎麽用?Golang SplitDomainName使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了SplitDomainName函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Get

// Get retrieves a list of services from the registry that matches the given domain pattern:
//
// uuid.host.region.version.service.environment
// any of these positions may supply the wildcard "*", to have all values match in this position.
// additionally, you only need to specify as much of the domain as needed the domain version.service.environment is perfectly acceptable,
// and will assume "*" for all the ommited subdomain positions
func (r *DefaultRegistry) Get(domain string) ([]msg.Service, error) {
	// TODO: account for version wildcards
	r.mutex.Lock()
	defer r.mutex.Unlock()

	// Ensure we are using lowercase keys, as this is the way they are stored
	domain = strings.ToLower(domain)

	// DNS queries have a trailing .
	if strings.HasSuffix(domain, ".") {
		domain = domain[:len(domain)-1]
	}

	tree := dns.SplitDomainName(domain)

	// Domains can be partial, and we should assume wildcards for the unsupplied portions
	if len(tree) < 6 {
		pad := 6 - len(tree)
		t := make([]string, pad)

		for i := 0; i < pad; i++ {
			t[i] = "*"
		}

		tree = append(t, tree...)
	}
	return r.tree.get(tree)
}
開發者ID:nettedfish,項目名稱:skydns1,代碼行數:34,代碼來源:registry.go

示例2: ToPunycode

// ToPunycode converts unicode domain names to DNS-appropriate punycode names.
// This function will return an empty string result for domain names with
// invalid unicode strings. This function expects domain names in lowercase.
func ToPunycode(s string) string {
	// Early check to see if encoding is needed.
	// This will prevent making heap allocations when not needed.
	if !needToPunycode(s) {
		return s
	}

	tokens := dns.SplitDomainName(s)
	switch {
	case s == "":
		return ""
	case tokens == nil: // s == .
		return "."
	case s[len(s)-1] == '.':
		tokens = append(tokens, "")
	}

	for i := range tokens {
		t := encode([]byte(tokens[i]))
		if t == nil {
			return ""
		}
		tokens[i] = string(t)
	}
	return strings.Join(tokens, ".")
}
開發者ID:2722,項目名稱:lantern,代碼行數:29,代碼來源:punycode.go

示例3: parent

func parent(name string) (string, bool) {
	labels := dns.SplitDomainName(name)
	if labels == nil {
		return "", false
	}
	return toLowerFQDN(strings.Join(labels[1:], ".")), true
}
開發者ID:pages-alex-alex2006hw,項目名稱:dnsr,代碼行數:7,代碼來源:strings.go

示例4: NewNSEC3

// NewNSEC3 returns the NSEC3 record need to denial qname, or gives back a NODATA NSEC3.
func (s *server) NewNSEC3(qname string) *dns.NSEC {
	qlabels := dns.SplitDomainName(qname)
	if len(qlabels) < s.domainLabels {
		// TODO(miek): can not happen...?
	}
	// Strip the last s.domainLabels, return up to 4 before
	// that. Four labels is the maximum qname we can handle.
	ls := len(qlabels) - s.domainLabels
	ls4 := ls - 4
	if ls4 < 0 {
		ls4 = 0
	}
	key := qlabels[ls4:ls]
	key = key // TODO(miek)
	// TODO etcd here
	//	prev, next := s.registry.GetNSEC(strings.Join(key, "."))
	prev, next := "", ""
	nsec := &dns.NSEC{Hdr: dns.RR_Header{Name: prev + s.config.Domain + ".", Rrtype: dns.TypeNSEC, Class: dns.ClassINET, Ttl: 60},
		NextDomain: next + s.config.Domain + "."}
	if prev == "" {
		nsec.TypeBitMap = []uint16{dns.TypeA, dns.TypeSOA, dns.TypeNS, dns.TypeAAAA, dns.TypeRRSIG, dns.TypeNSEC, dns.TypeDNSKEY}
	} else {
		nsec.TypeBitMap = []uint16{dns.TypeA, dns.TypeAAAA, dns.TypeSRV, dns.TypeRRSIG, dns.TypeNSEC}
	}
	return nsec
}
開發者ID:jonboulle,項目名稱:skydns2,代碼行數:27,代碼來源:dnssec.go

示例5: Hostname

func (d *Device) Hostname() string {
	l := dns.SplitDomainName(d.Name)
	if len(l) > 0 {
		return l[0]
	}
	return d.Name
}
開發者ID:ozym,項目名稱:zone,代碼行數:7,代碼來源:device.go

示例6: UpdateStubZones

// Look in .../dns/stub/<domain>/xx for msg.Services. Loop through them
// extract <domain> and add them as forwarders (ip:port-combos) for
// the stub zones. Only numeric (i.e. IP address) hosts are used.
func (s *server) UpdateStubZones() {
	stubmap := make(map[string][]string)

	services, err := s.backend.Records("stub.dns."+s.config.Domain, false, net.IP{})
	if err != nil {
		logf("stub zone update failed: %s", err)
		return
	}
	for _, serv := range services {
		if serv.Port == 0 {
			serv.Port = 53
		}
		ip := net.ParseIP(serv.Host)
		if ip == nil {
			logf("stub zone non-address %s seen for: %s", serv.Key, serv.Host)
			continue
		}

		domain := msg.Domain(serv.Key)
		// Chop of left most label, because that is used as the nameserver place holder
		// and drop the right most labels that belong to localDomain.
		labels := dns.SplitDomainName(domain)
		domain = dns.Fqdn(strings.Join(labels[1:len(labels)-dns.CountLabel(s.config.localDomain)], "."))

		// If the remaining name equals s.config.LocalDomain we ignore it.
		if domain == s.config.localDomain {
			logf("not adding stub zone for my own domain")
			continue
		}
		stubmap[domain] = append(stubmap[domain], net.JoinHostPort(serv.Host, strconv.Itoa(serv.Port)))
	}

	s.config.stub = &stubmap
}
開發者ID:CMGS,項目名稱:skydns,代碼行數:37,代碼來源:stub.go

示例7: getKey

func getKey(domain string, rtype uint16) (r string, e error) {
	if *debug {
		Log.Printf("getKey:  domain: %s, resource type: %d\n", domain, rtype)
	}

	if n, ok := dns.IsDomainName(domain); ok {
		labels := dns.SplitDomainName(domain)

		// Reverse domain, starting from top-level domain
		// eg.  ".com.mkaczanowski.test "
		var tmp string
		for i := 0; i < int(math.Floor(float64(n/2))); i++ {
			tmp = labels[i]
			labels[i] = labels[n-1]
			labels[n-1] = tmp
		}

		reverseDomain := strings.Join(labels, ".")
		r = strings.Join([]string{reverseDomain, strconv.Itoa(int(rtype))}, "_")
	} else {
		e = errors.New("Invailid domain: " + domain)
		Log.Println(e.Error())
	}

	return r, e
}
開發者ID:ae6rt,項目名稱:dyndns,代碼行數:26,代碼來源:main.go

示例8: Path

// Path converts a domainname to an etcd path. If s looks like service.staging.skydns.local.,
// the resulting key will be /skydns/local/skydns/staging/service .
func Path(s string) string {
	l := dns.SplitDomainName(s)
	for i, j := 0, len(l)-1; i < j; i, j = i+1, j-1 {
		l[i], l[j] = l[j], l[i]
	}
	return path.Join(append([]string{"/skydns/"}, l...)...)
}
開發者ID:WIZARD-CXY,項目名稱:golang-devops-stuff,代碼行數:9,代碼來源:service.go

示例9: GetRootDomain

func GetRootDomain(s string) string {
	labels := dns.SplitDomainName(s)
	if len(labels) < 2 {
		return s
	}
	root := dns.Fqdn(strings.Join([]string{labels[len(labels)-2], labels[len(labels)-1]}, "."))
	return root
}
開發者ID:namsral,項目名稱:dennis,代碼行數:8,代碼來源:dennis.go

示例10: path

// path converts a domainname to an etcd path. If s looks like service.staging.skydns.local.,
// the resulting key will be /skydns/local/skydns/staging/service .
func path(s string) string {
	l := dns.SplitDomainName(s)
	for i, j := 0, len(l)-1; i < j; i, j = i+1, j-1 {
		l[i], l[j] = l[j], l[i]
	}
	// TODO(miek): escape slashes in s.
	return "/skydns/" + strings.Join(l, "/")
}
開發者ID:wangfakang,項目名稱:skydns2,代碼行數:10,代碼來源:server.go

示例11: ServeDNS

func (h *handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
	m := new(dns.Msg)
	m.SetReply(r)
	defer w.WriteMsg(m)

	if len(r.Question) != 1 {
		return
	}

	q := r.Question[0]
	if q.Qclass != dns.ClassINET {
		return
	}
	if q.Qtype != dns.TypeA {
		return
	}

	withID := false
	labels := dns.SplitDomainName(q.Name)
	for i, j := 0, len(labels)-1; i < j; i, j = i+1, j-1 {
		labels[i], labels[j] = labels[j], labels[i]
	}
	if len(labels) > 0 && labels[0] == "" {
		labels = labels[1:]
	}
	if len(labels) > 0 && labels[0] == "switch" {
		labels = labels[1:]
	}
	if len(labels) > 0 && labels[0] == "id" {
		labels = labels[1:]
		withID = true
	}
	name := strings.Join(labels, "/")

	var host *hosts.Host
	if withID {
		host = h.vnet.Hosts().GetTable().LookupByID(name)
	} else {
		host = h.vnet.Hosts().GetTable().LookupByName(name)
	}
	if host == nil {
		return
	}

	for _, ip := range host.IPv4Addrs {
		m.Answer = append(m.Answer, &dns.A{
			Hdr: dns.RR_Header{
				Name:   q.Name,
				Rrtype: dns.TypeA,
				Class:  dns.ClassINET,
				Ttl:    60,
			},
			A: ip,
		})
	}
}
開發者ID:fd,項目名稱:switchboard,代碼行數:56,代碼來源:server.go

示例12: GenerateParentDomain

func GenerateParentDomain(d string) (string, *MyError.MyError) {
	x := dns.SplitDomainName(d)
	if cap(x) > 1 {
		//		fmt.Println(x)
		return strings.Join(x[1:], "."), nil
	} else {
		return d, MyError.NewError(MyError.ERROR_NORESULT, d+" has no subdomain")
	}
	return d, MyError.NewError(MyError.ERROR_UNKNOWN, d+" unknown error")
}
開發者ID:chunshengster,項目名稱:httpDispatcher,代碼行數:10,代碼來源:query_dns.go

示例13: getSRVRecords

func (s *Server) getSRVRecords(q dns.Question) (records []dns.RR, err error) {
	var weight uint16
	services := make([]msg.Service, 0)

	key := strings.TrimSuffix(q.Name, s.domain+".")
	services, err = s.registry.Get(key)

	if err != nil {
		return
	}

	weight = 0
	if len(services) > 0 {
		weight = uint16(math.Floor(float64(100 / len(services))))
	}

	for _, serv := range services {
		// TODO: Dynamically set weight
		records = append(records, &dns.SRV{Hdr: dns.RR_Header{Name: q.Name, Rrtype: dns.TypeSRV, Class: dns.ClassINET, Ttl: serv.TTL}, Priority: 10, Weight: weight, Port: serv.Port, Target: serv.Host + "."})
	}

	// Append matching entries in different region than requested with a higher priority
	labels := dns.SplitDomainName(key)

	pos := len(labels) - 4
	if len(labels) >= 4 && labels[pos] != "any" && labels[pos] != "all" {
		region := labels[pos]
		labels[pos] = "any"

		// TODO: This is pretty much a copy of the above, and should be abstracted
		additionalServices := make([]msg.Service, len(services))
		additionalServices, err = s.registry.Get(strings.Join(labels, "."))

		if err != nil {
			return
		}

		weight = 0
		if len(additionalServices) <= len(services) {
			return
		}

		weight = uint16(math.Floor(float64(100 / (len(additionalServices) - len(services)))))
		for _, serv := range additionalServices {
			// Exclude entries we already have
			if strings.ToLower(serv.Region) == region {
				continue
			}
			// TODO: Dynamically set priority and weight
			records = append(records, &dns.SRV{Hdr: dns.RR_Header{Name: q.Name, Rrtype: dns.TypeSRV, Class: dns.ClassINET, Ttl: serv.TTL}, Priority: 20, Weight: weight, Port: serv.Port, Target: serv.Host + "."})
		}
	}

	return
}
開發者ID:velebak,項目名稱:skydns,代碼行數:55,代碼來源:server.go

示例14: dispatch

// dispatch is used to parse a request and invoke the correct handler
func (d *DNSServer) dispatch(network string, req, resp *dns.Msg) {
	// By default the query is in the default datacenter
	datacenter := d.agent.config.Datacenter

	// Get the QName without the domain suffix
	qName := dns.Fqdn(req.Question[0].Name)
	qName = strings.TrimSuffix(qName, d.domain)

	// Split into the label parts
	labels := dns.SplitDomainName(qName)

	// The last label is either "node", "service" or a datacenter name
PARSE:
	n := len(labels)
	if n == 0 {
		goto INVALID
	}
	switch labels[n-1] {
	case "service":
		if n == 1 {
			goto INVALID
		}

		// Extract the service
		service := labels[n-2]

		// Support "." in the label, re-join all the parts
		tag := ""
		if n >= 3 {
			tag = strings.Join(labels[:n-2], ".")
		}

		// Handle lookup with and without tag
		d.serviceLookup(network, datacenter, service, tag, req, resp)

	case "node":
		if len(labels) == 1 {
			goto INVALID
		}
		// Allow a "." in the node name, just join all the parts
		node := strings.Join(labels[:n-1], ".")
		d.nodeLookup(network, datacenter, node, req, resp)

	default:
		// Store the DC, and re-parse
		datacenter = labels[n-1]
		labels = labels[:n-1]
		goto PARSE
	}
	return
INVALID:
	d.logger.Printf("[WARN] dns: QName invalid: %s", qName)
	resp.SetRcode(req, dns.RcodeNameError)
}
開發者ID:kawaken,項目名稱:consul,代碼行數:55,代碼來源:dns.go

示例15: getResponse

func (s HelixServer) getResponse(q dns.Question) (Response, error) {
	addr := dns.SplitDomainName(q.Name)
	path := []string{"helix"}

	for i := len(addr) - 1; i >= 0; i-- {
		path = append(path, addr[i])
	}

	path = append(path, dns.TypeToString[q.Qtype])

	return s.Client.Get(strings.Join(path, "/"))
}
開發者ID:ChristianKniep,項目名稱:helixdns,代碼行數:12,代碼來源:server.go


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