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


Golang dns.HandleFunc函數代碼示例

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


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

示例1: TestParseMXNegativeTests

func TestParseMXNegativeTests(t *testing.T) {

	/* helper functions */

	dns.HandleFunc(".", rootZone)
	defer dns.HandleRemove(".")

	hosts := make(map[uint16][]string)

	hosts[dns.TypeMX] = []string{
		"mail.matching.com. 0 IN MX 5 mail.matching.com.",
		"mail.matching.com. 0 IN MX 10 mail2.matching.com.",
		"mail.matching.com. 0 IN MX 15 mail3.matching.com.",
	}
	hosts[dns.TypeAAAA] = []string{
		"mail.matching.com. 0 IN AAAA 2001:4860:1:2001::80",
	}

	hosts[dns.TypeA] = []string{
		"mail.matching.com. 0 IN A 172.18.0.2",
		"mail2.matching.com. 0 IN A 172.20.20.20",
		"mail3.matching.com. 0 IN A 172.100.0.1",
	}
	mxMatchingCom := generateZone(hosts)
	dns.HandleFunc("matching.com.", mxMatchingCom)
	defer dns.HandleRemove("matching.com.")

	s, addr, err := runLocalUDPServer(dnsServer)
	if err != nil {
		t.Fatalf("unable to run test server: %v", err)
	}
	defer s.Shutdown()
	Nameserver = addr
	/* ***************** */
	ip := net.IP{127, 0, 0, 1}
	domain := "matching.com"
	p := NewParser(domain, domain, ip, stub)

	testcases := []TokenTestCase{
		TokenTestCase{&Token{tMX, qPlus, "matching.com"}, Pass, false},
		TokenTestCase{&Token{tMX, qPlus, ""}, Pass, false},
		//TokenTestCase{&Token{tMX, qPlus, "google.com"}, Pass, false},
		TokenTestCase{&Token{tMX, qPlus, "idontexist"}, Pass, false},
		TokenTestCase{&Token{tMX, qMinus, "matching.com"}, Fail, false},
	}

	var match bool
	var result SPFResult

	for _, testcase := range testcases {
		match, result = p.parseMX(testcase.Input)
		if testcase.Match != match {
			t.Error("Match mismatch, expected ", testcase.Match, " got ", match)
		}
		if testcase.Result != result {
			t.Error("Result mismatch, expected ", testcase.Result, " got ", result)
		}
	}
}
開發者ID:zaccone,項目名稱:goSPF,代碼行數:59,代碼來源:parser_test.go

示例2: TestParseExists

// TestParseExists executes tests for exists term.
func TestParseExists(t *testing.T) {

	dns.HandleFunc(".", rootZone)
	defer dns.HandleRemove(".")

	hosts := make(map[uint16][]string)
	hosts[dns.TypeA] = []string{
		"postitive.matching.net. 0 IN A 172.20.20.20",
		"postitive.matching.net. 0 IN A 172.18.0.1",
		"postitive.matching.net. 0 IN A 172.18.0.2",
	}
	dns.HandleFunc("positive.matching.net.", generateZone(hosts))
	defer dns.HandleRemove("positive.matching.net.")

	hosts = make(map[uint16][]string)
	hosts[dns.TypeA] = []string{
		"postitive.matching.com. 0 IN A 172.20.20.20",
		"postitive.matching.com. 0 IN A 172.18.0.1",
		"postitive.matching.com. 0 IN A 172.18.0.2",
	}
	dns.HandleFunc("positive.matching.com.", generateZone(hosts))
	defer dns.HandleRemove("positive.matching.com.")
	s, addr, err := runLocalUDPServer(dnsServer)
	if err != nil {
		t.Fatalf("unable to run test server: %v", err)
	}
	defer s.Shutdown()
	Nameserver = addr

	domain := "matching.com"
	p := NewParser(domain, domain, ip, stub)
	testcases := []TokenTestCase{
		TokenTestCase{&Token{tExists, qPlus, "positive.matching.net"}, Pass, true},
		TokenTestCase{&Token{tExists, qMinus, "positive.matching.net"}, Fail, true},
		TokenTestCase{&Token{tExists, qMinus, "idontexist.matching.net"}, Fail, false},
		TokenTestCase{&Token{tExists, qMinus, "idontexist.%{d}"}, Fail, false},
		TokenTestCase{&Token{tExists, qTilde, "positive.%{d}"}, Softfail, true},
		TokenTestCase{&Token{tExists, qTilde, "positive.%{d}"}, Softfail, true},
		TokenTestCase{&Token{tExists, qTilde, ""}, Permerror, true},
		TokenTestCase{&Token{tExists, qTilde, "invalidsyntax%{}"}, Permerror, true},
	}

	for _, testcase := range testcases {
		match, result := p.parseExists(testcase.Input)
		if testcase.Match != match {
			t.Error("Match mismatch, expected ", testcase.Match, " got ", match)
		}
		if testcase.Result != result {
			t.Error("Result mismatch, expected ", testcase.Result, " got ", result)
		}
	}
}
開發者ID:zaccone,項目名稱:goSPF,代碼行數:53,代碼來源:parser_test.go

示例3: TestHandleExplanation

func TestHandleExplanation(t *testing.T) {
	const domain = "matching.com"
	// static.exp.matching.com.        IN      TXT "Invalid SPF record"
	// ip.exp.matching.com.            IN      TXT "%{i} is not one of %{d}'s designated mail servers."
	// redirect.exp.matching.com.      IN      TXT "See http://%{d}/why.html?s=%{s}&i=%{i}"

	dns.HandleFunc(".", rootZone)
	defer dns.HandleRemove(".")

	hosts := make(map[uint16][]string)
	hosts[dns.TypeTXT] = []string{
		"static.exp.matching.com. 0 IN TXT \"Invalid SPF record\"",
	}
	dns.HandleFunc("static.exp.matching.com.", generateZone(hosts))
	defer dns.HandleRemove("static.exp.matching.com.")

	hosts = make(map[uint16][]string)
	hosts[dns.TypeTXT] = []string{
		"ip.exp.matching.com. 0 in TXT \"%{i} is not one of %{d}'s designated mail servers.\"",
	}
	dns.HandleFunc("ip.exp.matching.com.", generateZone(hosts))
	defer dns.HandleRemove("ip.exp.matching.com.")

	s, addr, err := runLocalUDPServer(dnsServer)
	if err != nil {
		t.Fatalf("unable to run test server: %v", err)
	}
	defer s.Shutdown()
	Nameserver = addr

	ExpTestCases := []ExpTestCase{
		ExpTestCase{"v=spf1 -all exp=static.exp.matching.com",
			"Invalid SPF record"},
		ExpTestCase{"v=spf1 -all exp=ip.exp.matching.com",
			"127.0.0.1 is not one of matching.com's designated mail servers."},
		// TODO(zaccone): Cover this testcase
		//ExpTestCase{"v=spf1 -all exp=redirect.exp.matching.com",
		//ExpT"See http://matching.com/why.html?s=&i="},
	}

	for _, testcase := range ExpTestCases {

		p := NewParser(domain, domain, ip, testcase.Query)
		_, exp, err := p.Parse()
		if err != nil {
			t.Error("Unexpected error while parsing: ", err)
		} else if exp != testcase.Explanation {
			t.Errorf("Explanation mismatch, expected %s, got %s\n",
				testcase.Explanation, exp)
		}
	}
}
開發者ID:zaccone,項目名稱:goSPF,代碼行數:52,代碼來源:parser_test.go

示例4: LaunchDNS

// LaunchDNS starts a (TCP and UDP) DNS server for the Resolver,
// returning a error channel to which errors are asynchronously sent.
func (res *Resolver) LaunchDNS() <-chan error {
	// Handers for Mesos requests
	dns.HandleFunc(res.config.Domain+".", panicRecover(res.HandleMesos))
	// Handler for nonMesos requests
	dns.HandleFunc(".", panicRecover(res.HandleNonMesos))

	errCh := make(chan error, 2)
	_, e1 := res.Serve("tcp")
	go func() { errCh <- <-e1 }()
	_, e2 := res.Serve("udp")
	go func() { errCh <- <-e2 }()
	return errCh
}
開發者ID:alberts,項目名稱:mesos-dns,代碼行數:15,代碼來源:resolver.go

示例5: Setup

func (manager *DnsManager) Setup() error {
	dns.HandleFunc(manager.BaseName, manager.dnsHandler)

	go func() {
		manager.udpServer = &dns.Server{
			Addr:       fmt.Sprintf("%v:%v", manager.ServiceAddr, manager.ServicePort),
			Net:        "udp",
			TsigSecret: nil,
		}
		err := manager.udpServer.ListenAndServe()
		if err != nil {
			log.Fatal(err)
		}
	}()

	go func() {
		manager.tcpServer = &dns.Server{
			Addr:       fmt.Sprintf("%v:%v", manager.ServiceAddr, manager.ServicePort),
			Net:        "tcp",
			TsigSecret: nil,
		}
		err := manager.tcpServer.ListenAndServe()
		if err != nil {
			log.Fatal(err)
		}
	}()

	return nil
}
開發者ID:dawanda,項目名稱:mmsd,代碼行數:29,代碼來源:dns_manager.go

示例6: main

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU() * 4)
	flag.Parse()

	configuration := nasello.ReadConfig(*configFile)
	for _, filter := range configuration.Filters {
		// Ensure that each pattern is a FQDN name
		pattern := dns.Fqdn(filter.Pattern)

		log.Printf("Proxing %s on %v\n", pattern, strings.Join(filter.Addresses, ", "))
		dns.HandleFunc(pattern, nasello.ServerHandler(filter.Addresses))
	}

	go serve("tcp", *listenAddr)
	go serve("udp", *listenAddr)

	log.Printf("Started DNS server on: %s\n", *listenAddr)

	sig := make(chan os.Signal, 1)
	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
forever:
	for {
		select {
		case s := <-sig:
			log.Printf("Signal (%s) received, stopping\n", s.String())
			break forever
		}
	}
}
開發者ID:piger,項目名稱:nasello,代碼行數:29,代碼來源:nasello.go

示例7: serveTestResolver

func (ts *testSrv) serveTestResolver() {
	dns.HandleFunc(".", ts.dnsHandler)
	dnsServer := &dns.Server{
		Addr:         "0.0.0.0:8053",
		Net:          "tcp",
		ReadTimeout:  time.Millisecond,
		WriteTimeout: time.Millisecond,
	}
	go func() {
		err := dnsServer.ListenAndServe()
		if err != nil {
			fmt.Println(err)
			return
		}
	}()
	webServer := &http.Server{
		Addr:    "0.0.0.0:8055",
		Handler: http.HandlerFunc(ts.setTXT),
	}
	go func() {
		err := webServer.ListenAndServe()
		if err != nil {
			fmt.Println(err)
			return
		}
	}()
}
開發者ID:MTRNord,項目名稱:boulder-freifunk_support,代碼行數:27,代碼來源:main.go

示例8: updateDns

func updateDns(ethereum *eth.Ethereum) {
	stateObject := ethereum.StateManager().CurrentState().GetStateObject(dnsreg)
	if stateObject != nil {
		ethutil.Config.Log.Debugln("Updating DNS")
		stateObject.State().EachStorage(func(name string, value *ethutil.Value) {
			val := value.Bytes()[1:]
			name = sanitizeString(name) + ".eth."
			dns.HandleRemove(name)
			zoneString := fmt.Sprintf("%s 2044 IN A %s", name, val)
			zone := NewRR(zoneString)
			if zone != nil {
				ethutil.Config.Log.Debugln("[DNS] Updated zone:", zone)
				dns.HandleFunc(name, func(w dns.ResponseWriter, r *dns.Msg) {
					switch r.Question[0].Qtype {
					case dns.TypeA:
						m := new(dns.Msg)
						m.SetReply(r)
						m.Answer = []dns.RR{zone}
						m.RecursionAvailable = true
						w.WriteMsg(m)
					default:
						ethutil.Config.Log.Debugln("[DNS] Type not supported yet")
					}

				})
			} else {
				ethutil.Config.Log.Debugln("Invalid zone", zoneString)
			}
		})
	}
}
開發者ID:obscuren,項目名稱:dnseth,代碼行數:31,代碼來源:main.go

示例9: main

func main() {
	parse_flags()

	var err error
	if enable_cache {
		// create cache
		dns_cache, err = lru.New(1000)
		if err != nil {
			log.Fatal(err)
		}
	}

	dns.HandleFunc(".", handleRoot)

	logger = NewLogger(logfile, debug)

	logger.Info("Listen on %s\n", bind_addr)

	go func() {
		/* listen tcp */
		err := dns.ListenAndServe(bind_addr, "tcp", nil)
		if err != nil {
			log.Fatal(err)
		}
	}()

	/* listen udp */
	err = dns.ListenAndServe(bind_addr, "udp", nil)
	if err != nil {
		log.Fatal(err)
	}
}
開發者ID:fangdingjun,項目名稱:gdns,代碼行數:32,代碼來源:server.go

示例10: Serve

func (d *DNSResponder) Serve(domains []string) error {
	for _, domain := range domains {
		dns.HandleFunc(domain+".", d.handleDNS)
	}

	addr := d.Address
	if addr == "" {
		addr = DefaultAddress
	}

	var wg sync.WaitGroup

	wg.Add(2)

	go func() {
		defer wg.Done()
		server := &dns.Server{Addr: addr, Net: "udp", TsigSecret: nil}
		server.ListenAndServe()
	}()

	go func() {
		defer wg.Done()
		server := &dns.Server{Addr: addr, Net: "tcp", TsigSecret: nil}
		server.ListenAndServe()
	}()

	wg.Wait()

	return nil
}
開發者ID:y-yagi,項目名稱:puma-dev,代碼行數:30,代碼來源:dns.go

示例11: main

func main() {
	port := flag.Int("port", 5353, "port to run on")
	flag.Parse()

	dns.HandleFunc(".", handleFirstHost)

	serve := func(net string, port int) {
		server := &dns.Server{Addr: ":" + strconv.Itoa(port), Net: net}
		err := server.ListenAndServe()
		if err != nil {
			glog.Infof("Failed to setup the "+net+" server: %s\n", err.Error())
		}
	}

	go serve("tcp", *port)
	go serve("udp", *port)

	sig := make(chan os.Signal)
	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
forever:
	for {
		select {
		case s := <-sig:
			log.Fatalf("Signal (%d) received, stopping\n", s)
			break forever
		}
	}
}
開發者ID:gesellix,項目名稱:mini-dns,代碼行數:28,代碼來源:mini-dns.go

示例12: main

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU() * 4)
	for z, rr := range zones {
		rrx := rr.(*dns.SOA) // Needed to create the actual RR, and not an reference.
		dns.HandleFunc(z, func(w dns.ResponseWriter, r *dns.Msg) {
			m := new(dns.Msg)
			m.SetReply(r)
			m.Authoritative = true
			m.Ns = []dns.RR{rrx}
			w.WriteMsg(m)
		})
	}
	go func() {
		err := dns.ListenAndServe(":8053", "tcp", nil)
		if err != nil {
			log.Fatal("Failed to set tcp listener %s\n", err.Error())
		}
	}()
	go func() {
		err := dns.ListenAndServe(":8053", "udp", nil)
		if err != nil {
			log.Fatal("Failed to set udp listener %s\n", err.Error())
		}
	}()
	sig := make(chan os.Signal)
	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
	for {
		select {
		case s := <-sig:
			log.Fatalf("Signal (%d) received, stopping\n", s)
		}
	}
}
開發者ID:vonwenm,項目名稱:golang-dns,代碼行數:33,代碼來源:as112.go

示例13: TestTransferIn

func TestTransferIn(t *testing.T) {
	soa := soa{250}

	dns.HandleFunc(testZone, soa.Handler)
	defer dns.HandleRemove(testZone)

	s, addrstr, err := test.TCPServer("127.0.0.1:0")
	if err != nil {
		t.Fatalf("unable to run test server: %v", err)
	}
	defer s.Shutdown()

	z := new(Zone)
	z.Expired = new(bool)
	z.origin = testZone
	z.TransferFrom = []string{addrstr}

	err = z.TransferIn()
	if err != nil {
		t.Fatalf("unable to run TransferIn: %v", err)
	}
	if z.Apex.SOA.String() != fmt.Sprintf("%s	3600	IN	SOA	bla. bla. 250 0 0 0 0", testZone) {
		t.Fatalf("unknown SOA transferred")
	}
}
開發者ID:yuewko,項目名稱:coredns,代碼行數:25,代碼來源:secondary_test.go

示例14: addZoneHandles

func addZoneHandles() {
	client := connectToRedis()
	zones := client.Keys("zone:*").Val()
	for i := 0; i < len(zones); i++ {
		dns.HandleFunc(zones[i][5:], handleRequest)
	}
}
開發者ID:inve1,項目名稱:idyns,代碼行數:7,代碼來源:idyns.go

示例15: main

func main() {
	flag.Parse()
	if *defaultServer == "" {
		log.Fatal("-default is required")
	}
	transferIPs = strings.Split(*allowTransfer, ",")
	routes = make(map[string]string)
	if *routeList != "" {
		for _, s := range strings.Split(*routeList, ",") {
			s := strings.SplitN(s, "=", 2)
			if len(s) != 2 {
				log.Fatal("invalid -routes format")
			}
			if !strings.HasSuffix(s[0], ".") {
				s[0] += "."
			}
			routes[s[0]] = s[1]
		}
	}

	udpServer := &dns.Server{Addr: *address, Net: "udp"}
	tcpServer := &dns.Server{Addr: *address, Net: "tcp"}
	dns.HandleFunc(".", route)
	go func() {
		log.Fatal(udpServer.ListenAndServe())
	}()
	log.Fatal(tcpServer.ListenAndServe())
}
開發者ID:40a,項目名稱:dns-reverse-proxy,代碼行數:28,代碼來源:dns_reverse_proxy.go


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