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


Golang dns.ListenAndServe函數代碼示例

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


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

示例1: main

func main() {
	var (
		addr string
	)
	flag.StringVar(&addr, "l", "[::]:53", "listen on (both tcp and udp)")
	flag.StringVar(&listd, "d", "./list.d", "Dir with blacklisted domain files")
	handler := &Handle{}

	Adlist = make(map[string]bool)
	if e := filepath.Walk(listd, visit); e != nil {
		panic(e)
	}

	//for _, addr := range strings.Split(listen, ",") {
	fmt.Printf("DNS Listen %s\n", addr)
	go func() {
		if err := dns.ListenAndServe(addr, "udp", handler); err != nil {
			panic(err)
		}
	}()

	if err := dns.ListenAndServe(addr, "tcp", handler); err != nil {
		panic(err)
	}
	//}
}
開發者ID:mpdroog,項目名稱:dnsd,代碼行數:26,代碼來源:main.go

示例2: dnsSetup

func dnsSetup(cfg *Config) chan error {
	log.Println("DNSSETUP")

	// FIXME: Make the default TTL into a configuration parameter
	// FIXME: Check whether this default is being applied to unanswered queries
	defaultTTL := uint32(10800) // this is the default TTL = 3 hours

	cache := dnscache.New(dnsCacheBufferSize, cfg.DNSCacheMaxTTL(), cfg.DNSCacheMissingTTL(), func(c dnscache.Context, q dns.Question) []dns.RR {
		return answerQuestion(cfg, c, &q, defaultTTL, 0)
	})

	dns.HandleFunc(".", func(w dns.ResponseWriter, req *dns.Msg) { dnsQueryServe(cfg, cache, w, req) })
	cfg.db.InitDNS()
	exit := make(chan error, 1)

	go func() {
		exit <- dns.ListenAndServe("0.0.0.0:53", "tcp", nil) // TODO: should use cfg to define the listening ip/port
	}()

	go func() {
		exit <- dns.ListenAndServe("0.0.0.0:53", "udp", nil) // TODO: should use cfg to define the listening ip/port
	}()

	return exit
}
開發者ID:dustywilson,項目名稱:netcore,代碼行數:25,代碼來源:dns.go

示例3: 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

示例4: main

func main() {

	var (
		S_SERVERS       string
		S_LISTEN        string
		S_ACCESS        string
		timeout         int
		max_entries     int64
		expire_interval int64
	)

	flag.StringVar(&S_SERVERS, "proxy", "127.0.0.1:53", "we proxy requests to those servers")
	flag.StringVar(&S_LISTEN, "listen", "[::1]:5353,127.0.0.1:5353",
		"listen on (both tcp and udp), [ipv6address]:port, ipv4address:port")
	flag.StringVar(&S_ACCESS, "access", "0.0.0.0/0", "allow those networks, use 0.0.0.0/0 to allow everything")
	flag.IntVar(&timeout, "timeout", 5, "timeout")
	flag.Int64Var(&expire_interval, "expire_interval", 300, "delete expired entries every N seconds")
	flag.BoolVar(&DEBUG, "debug", false, "enable/disable debug")
	flag.Int64Var(&max_entries, "max_cache_entries", 2000000, "max cache entries")

	flag.Parse()
	servers := strings.Split(S_SERVERS, ",")
	proxyer := ServerProxy{
		giant:       new(sync.RWMutex),
		ACCESS:      make([]*net.IPNet, 0),
		SERVERS:     servers,
		s_len:       len(servers),
		NOW:         time.Now().UTC().Unix(),
		entries:     0,
		timeout:     time.Duration(timeout) * time.Second,
		max_entries: max_entries}

	for _, mask := range strings.Split(S_ACCESS, ",") {
		_, cidr, err := net.ParseCIDR(mask)
		if err != nil {
			panic(err)
		}
		_D("added access for %s\n", mask)
		proxyer.ACCESS = append(proxyer.ACCESS, cidr)
	}
	for _, addr := range strings.Split(S_LISTEN, ",") {
		_D("listening @ :%s\n", addr)
		go func() {
			if err := dns.ListenAndServe(addr, "udp", proxyer); err != nil {
				log.Fatal(err)
			}
		}()

		go func() {
			if err := dns.ListenAndServe(addr, "tcp", proxyer); err != nil {
				log.Fatal(err)
			}
		}()
	}

	for {
		proxyer.NOW = time.Now().UTC().Unix()
		time.Sleep(time.Duration(1) * time.Second)
	}
}
開發者ID:yiyuandao,項目名稱:DNS-layer-Fragmentation,代碼行數:60,代碼來源:ServerProxy.go

示例5: Run

// Run is a blocking operation that starts the server listening on the DNS ports.
func (s *server) Run() error {
	mux := dns.NewServeMux()
	mux.Handle(".", s)

	dnsReadyMsg := func(addr, net string) {
		if s.config.DNSSEC == "" {
			log.Printf("skydns: ready for queries on %s for %s://%s [rcache %d]", s.config.Domain, net, addr, s.config.RCache)
		} else {
			log.Printf("skydns: ready for queries on %s for %s://%s [rcache %d], signing with %s [scache %d]", s.config.Domain, net, addr, s.config.RCache, s.config.DNSSEC, s.config.SCache)
		}
	}

	s.group.Add(1)
	go func() {
		defer s.group.Done()
		if err := dns.ListenAndServe(s.config.DnsAddr, s.dnsTCPclient.Net, mux); err != nil {
			log.Fatalf("skydns: %s", err)
		}
	}()
	dnsReadyMsg(s.config.DnsAddr, s.dnsTCPclient.Net)
	s.group.Add(1)
	go func() {
		defer s.group.Done()
		if err := dns.ListenAndServe(s.config.DnsAddr, s.dnsUDPclient.Net, mux); err != nil {
			log.Fatalf("skydns: %s", err)
		}
	}()
	dnsReadyMsg(s.config.DnsAddr, s.dnsUDPclient.Net)

	s.group.Wait()
	return nil
}
開發者ID:jhadvig,項目名稱:origin,代碼行數:33,代碼來源:server.go

示例6: main

func main() {
	flag.Parse()

	tdns := &TrivialDnsServer{
		Servers:  readUpstreamServersFromConfig(),
		Database: readDatabaseFromConfig(),
		stats:    make(map[string]int),
	}

	log.Printf("Starting with nameservers %v", tdns.Servers)
	log.Printf("Starting with database %v", tdns.Database)

	addr := ":53"

	go func() {
		if err := dns.ListenAndServe(addr, "udp", tdns); err != nil {
			log.Fatal(err)
		}
	}()

	go func() {
		if err := dns.ListenAndServe(addr, "tcp", tdns); err != nil {
			log.Fatal(err)
		}
	}()

	log.Printf("DNS server started")
	log.Printf("Starting web interface on %s", *webListenAddr)
	http.HandleFunc("/", tdns.WebIndexPage)
	http.HandleFunc("/save_hosts", tdns.WebSaveHosts)
	http.ListenAndServe(*webListenAddr, nil)
}
開發者ID:e-max,項目名稱:trivialdns,代碼行數:32,代碼來源:trivialdns.go

示例7: 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

示例8: main

func main() {
	dns.HandleFunc(".", proxyServe)

	failure := make(chan error, 1)

	go func(failure chan error) {
		failure <- dns.ListenAndServe(*local, "tcp", nil)
	}(failure)

	go func(failure chan error) {
		failure <- dns.ListenAndServe(*local, "udp", nil)
	}(failure)

	log.Printf("ready for accept connection on tcp/upd %s ...\n", *local)
	fmt.Println(<-failure)
}
開發者ID:wjzhangq,項目名稱:dnsproxy,代碼行數:16,代碼來源:dnsproxy1.go

示例9: ListenAndServeDNS

func ListenAndServeDNS(address string) error {
	dns.HandleFunc("dev.", localhostDNSHandler)

	err := dns.ListenAndServe(address, "udp", nil)
	if err != nil {
		return err
	}
	return nil
}
開發者ID:potatosalad,項目名稱:gow,代碼行數:9,代碼來源:dns.go

示例10: ListenAndServe

func (d *DNS) ListenAndServe() error {
	udpErr := make(chan error)
	tcpErr := make(chan error)
	go func() {
		udpErr <- dns.ListenAndServe(d.Config.Bind, "udp", dns.HandlerFunc(d.handler))
	}()
	go func() {
		tcpErr <- dns.ListenAndServe(d.Config.Bind, "tcp", dns.HandlerFunc(d.handler))
	}()

	select {
	case err := <-udpErr:
		return err
	case err := <-tcpErr:
		return err
	}

	return nil
}
開發者ID:pgpst,項目名稱:tunnel,代碼行數:19,代碼來源:dns.go

示例11: classicServe

func (s *Server) classicServe() {
	glog.Infof("DNS listening on %s", s.Addr)

	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		defer wg.Done()
		err := dns.ListenAndServe(s.Addr, "udp", dns.HandlerFunc(s.Handler))
		glog.Fatalf("Exiting UDP: %v", err)
	}()

	wg.Add(1)
	go func() {
		defer wg.Done()
		err := dns.ListenAndServe(s.Addr, "tcp", dns.HandlerFunc(s.Handler))
		glog.Fatalf("Exiting TCP: %v", err)
	}()

	wg.Wait()
}
開發者ID:albertito,項目名稱:dnss,代碼行數:20,代碼來源:dnstox.go

示例12: initDNS

func initDNS() {
	if GConf.LocalDNS.CacheSize > 0 {
		dnsCache, _ = lru.New(GConf.LocalDNS.CacheSize)
	}
	if len(GConf.LocalDNS.Listen) > 0 {
		err := dns.ListenAndServe(GConf.LocalDNS.Listen, "udp", dns.HandlerFunc(proxyDNS))
		if nil != err {
			log.Printf("Failed to start dns server:%v", err)
		}
	}
}
開發者ID:yinqiwen,項目名稱:gsnova,代碼行數:11,代碼來源:dns.go

示例13: serve

// TODO: make port and bind address configurable
func serve(net, name, secret string) {
	switch name {
	case "":
		err := dns.ListenAndServe(":53", net, nil)
		if err != nil {
			fmt.Printf("Failed to setup the "+net+" server: %s\n", err.Error())
		}
	default:
		server := &dns.Server{Addr: ":53", Net: net, TsigSecret: map[string]string{name: secret}}
		err := server.ListenAndServe()
		if err != nil {
			fmt.Printf("Failed to setup the "+net+" server: %s\n", err.Error())
		}
	}
}
開發者ID:inve1,項目名稱:idyns,代碼行數:16,代碼來源:idyns.go

示例14: main

func main() {
	keenProject := os.Getenv("KEEN_PROJECT")
	keenWriteKey := os.Getenv("KEEN_WRITE_KEY")
	githubProject := os.Getenv("GITHUB_PROJECT")
	githubToken := os.Getenv("GITHUB_ACCESS_TOKEN")

	host := os.Getenv("HOST")
	if host == "" {
		host = "0.0.0.0"
	}

	port := os.Getenv("PORT")
	if port == "" {
		port = "53"
	}

	addr := net.JoinHostPort(host, port)

	if keenProject == "" || keenWriteKey == "" || githubProject == "" {
		log.Fatal("Please set KEEN_PROJECT, KEEN_WRITE_KEY, and GITHUB_PROJECT")
	}

	keenClient := &keen.Client{WriteKey: keenWriteKey, ProjectID: keenProject}
	keenBatchClient := keen.NewBatchClient(keenClient, *keenFlushInterval)

	var authClient *http.Client

	if githubToken != "" {
		authClient = oauth2.NewClient(oauth2.NoContext, oauth2.StaticTokenSource(
			&oauth2.Token{AccessToken: githubToken},
		))
	}

	githubClient := github.NewClient(authClient)

	tracker := UsageTracker{
		keenClient:    keenBatchClient,
		githubClient:  githubClient,
		githubProject: githubProject,
	}

	err := dns.ListenAndServe(addr, "udp", &tracker)
	if err != nil {
		log.Fatal(err)
	}
}
開發者ID:gliderlabs,項目名稱:pkg,代碼行數:46,代碼來源:main.go

示例15:

func main () {

    var servers []string
    args := flag.Args ()

    if len(args) > 0 {
        servers = append (servers, args...)
    } else {
        servers = DEFAULT_SERVERS
    }

    LOG.Printf ("Servers: %s", servers)

    proxyer := Proxy{servers}

    if err := dns.ListenAndServe ("127.0.0.1:53", "udp", proxyer); err != nil {
        LOG.Fatal (err)
    }
}
開發者ID:zincxenon,項目名稱:CodeTest,代碼行數:19,代碼來源:gfc.go


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