Exemplo n.º 1
0
// realMain is the real main function, which returns the value to pass to
// os.Exit(). We have to do this so we can use defer.
func realMain(m *testing.M) int {
	flag.Parse()
	defer glog.Flush()

	// Generate certificates in a temporary directory.
	tmpDir, err := ioutil.TempDir("", "dnss_test:")
	if err != nil {
		fmt.Printf("Failed to create temp dir: %v\n", tmpDir)
		return 1
	}
	defer os.RemoveAll(tmpDir)

	err = generateCert(tmpDir)
	if err != nil {
		fmt.Printf("Failed to generate cert for testing: %v\n", err)
		return 1
	}

	// DNS to GRPC server.
	gr := dnstox.NewGRPCResolver(grpcToDnsAddr, tmpDir+"/cert.pem")
	cr := dnstox.NewCachingResolver(gr)
	dtg := dnstox.New(dnsToGrpcAddr, cr, "")
	go dtg.ListenAndServe()

	// GRPC to DNS server.
	gtd := &grpctodns.Server{
		Addr:     grpcToDnsAddr,
		Upstream: dnsSrvAddr,
		CertFile: tmpDir + "/cert.pem",
		KeyFile:  tmpDir + "/key.pem",
	}
	go gtd.ListenAndServe()

	// DNS test server.
	dnsSrv := dnsServer{
		Addr: dnsSrvAddr,
	}
	go dnsSrv.ListenAndServe()

	// Wait for the servers to start up.
	err = util.WaitForDNSServer(dnsToGrpcAddr)
	if err != nil {
		fmt.Printf("Error waiting for the test servers to start: %v\n", err)
		fmt.Printf("Check the INFO logs for more details\n")
		return 1
	}

	return m.Run()
}
Exemplo n.º 2
0
func main() {
	defer glog.Flush()

	flag.Parse()

	go flushLogs()

	grpc.EnableTracing = false
	if *monitoringListenAddr != "" {
		launchMonitoringServer(*monitoringListenAddr)
	}

	if !*enableDNStoGRPC && !*enableGRPCtoDNS && !*enableDNStoHTTPS {
		glog.Error("Need to set one of the following:")
		glog.Error("  --enable_dns_to_https")
		glog.Error("  --enable_dns_to_grpc")
		glog.Error("  --enable_grpc_to_dns")
		glog.Fatal("")
	}

	if *enableDNStoGRPC && *enableDNStoHTTPS {
		glog.Error("The following options cannot be set at the same time:")
		glog.Error("  --enable_dns_to_grpc and --enable_dns_to_https")
		glog.Fatal("")
	}

	var wg sync.WaitGroup

	// DNS to GRPC.
	if *enableDNStoGRPC {
		r := dnstox.NewGRPCResolver(*grpcUpstream, *grpcClientCAFile)
		cr := dnstox.NewCachingResolver(r)
		cr.RegisterDebugHandlers()
		dtg := dnstox.New(*dnsListenAddr, cr, *dnsUnqualifiedUpstream)
		dtg.SetFallback(
			*fallbackUpstream, strings.Split(*fallbackDomains, " "))
		wg.Add(1)
		go func() {
			defer wg.Done()
			dtg.ListenAndServe()
		}()
	}

	// GRPC to DNS.
	if *enableGRPCtoDNS {
		gtd := &grpctodns.Server{
			Addr:     *grpcListenAddr,
			Upstream: *dnsUpstream,
			CertFile: *grpcCert,
			KeyFile:  *grpcKey,
		}
		wg.Add(1)
		go func() {
			defer wg.Done()
			gtd.ListenAndServe()
		}()
	}

	// DNS to HTTPS.
	if *enableDNStoHTTPS {
		r := dnstox.NewHTTPSResolver(*httpsUpstream, *httpsClientCAFile)
		cr := dnstox.NewCachingResolver(r)
		cr.RegisterDebugHandlers()
		dth := dnstox.New(*dnsListenAddr, cr, *dnsUnqualifiedUpstream)
		dth.SetFallback(
			*fallbackUpstream, strings.Split(*fallbackDomains, " "))
		wg.Add(1)
		go func() {
			defer wg.Done()
			dth.ListenAndServe()
		}()
	}

	wg.Wait()
}