// 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() DNSAddr = util.GetFreePort() // Test http server. httpsrv := httptest.NewServer(http.HandlerFunc(DNSHandler)) // DNS to HTTPS server. r := dnstox.NewHTTPSResolver(httpsrv.URL, "") dth := dnstox.New(DNSAddr, r, "") go dth.ListenAndServe() // Wait for the servers to start up. err := util.WaitForDNSServer(DNSAddr) 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() }
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() }