Пример #1
0
func init() {

	flag.Usage = func() {
		fmt.Printf("Usage: envconv [options] file [files...]\n\n")
		flag.PrintDefaults()
	}

	version := flag.Bool("v", false, "Prints current version")

	flag.Parse()

	if *version {
		fmt.Println("envconf version " + VERSION)
		os.Exit(0)
	}

	templateFuncMap = template.FuncMap{
		"default": func(args ...string) string {
			defer recovery()
			if len(args) <= 0 {
				return ""
			}
			return args[len(args)-1]
		},
		"in": func(arg string, slice []string) bool {
			defer recovery()
			for _, i := range slice {
				if arg == i {
					return true
				}
			}
			return false
		},
	}
}
Пример #2
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU() * 4)

	flag.BoolVar(&printf, "printf", false, "print replies")
	flag.BoolVar(&compress, "compress", false, "compress replies")
	flag.StringVar(&tsig, "tsig", "", "use MD5 hmac tsig: keyname:base64")
	flag.StringVar(&config, "config", "", "alternative configuration file")
	flag.StringVar(&bind_addr, "bind_addr", "127.0.0.1:53", "bind HTTP to HOST and PORT")
	flag.StringVar(&dnsfwd_addr, "dnsfwd_addr", "8.8.8.8:53", "DNS server to forward request to")
	flag.StringVar(&redis_addr, "redis_addr", "127.0.0.1:6379", "address of Redis instance")
	flag.StringVar(&portal_addr, "portal_addr", "127.0.0.1:80", "address of portal")
	flag.StringVar(&log_file, "log_file", "", "path to log file")

	var name, secret string
	flag.Usage = func() {
		flag.PrintDefaults()
	}
	flag.Parse()

	// Setup Log file
	if len(log_file) > 0 {
		f, err := os.OpenFile(log_file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0640)
		defer f.Close()
		if err != nil {
			log.Fatalf("Error opening file: %v", err)
		}
		log.SetOutput(f)
	}

	// Tsig
	if tsig != "" {
		a := strings.SplitN(tsig, ":", 2)
		name, secret = dns.Fqdn(a[0]), a[1] // fqdn the name, which everybody forgets...
	}

	// DNS
	dns.HandleFunc(".", handleAll)

	go serve("tcp", name, secret)
	go serve("udp", name, secret)
	log.Printf("* Running on %s\n", bind_addr)
	sig := make(chan os.Signal)
	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
forever:
	for {
		select {
		case s := <-sig:
			fmt.Printf("Signal (%d) received, stopping\n", s)
			break forever
		}
	}
}