geodns.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package main
  2. import (
  3. "flag"
  4. "log"
  5. "net"
  6. "os"
  7. "os/signal"
  8. "runtime/pprof"
  9. "strings"
  10. "time"
  11. )
  12. var VERSION string = "2.2.0"
  13. var gitVersion string
  14. var serverId string
  15. var timeStarted = time.Now()
  16. var qCounter uint64 = 0
  17. var (
  18. flagconfig = flag.String("config", "./dns/", "directory of zone files")
  19. flaginter = flag.String("interface", "*", "set the listener address")
  20. flagport = flag.String("port", "53", "default port number")
  21. flaghttp = flag.String("http", ":8053", "http listen address (:8053)")
  22. flaglog = flag.Bool("log", false, "be more verbose")
  23. cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
  24. memprofile = flag.String("memprofile", "", "write memory profile to this file")
  25. )
  26. func init() {
  27. if len(gitVersion) > 0 {
  28. VERSION = VERSION + "/" + gitVersion
  29. }
  30. log.SetPrefix("geodns ")
  31. log.SetFlags(log.Lmicroseconds | log.Lshortfile)
  32. }
  33. func main() {
  34. flag.Parse()
  35. log.Printf("Starting geodns %s\n", VERSION)
  36. if *cpuprofile != "" {
  37. prof, err := os.Create(*cpuprofile)
  38. if err != nil {
  39. panic(err.Error())
  40. }
  41. pprof.StartCPUProfile(prof)
  42. defer func() {
  43. log.Println("closing file")
  44. prof.Close()
  45. }()
  46. defer func() {
  47. log.Println("stopping profile")
  48. pprof.StopCPUProfile()
  49. }()
  50. }
  51. if *flaginter == "*" {
  52. addrs, _ := net.InterfaceAddrs()
  53. ips := make([]string, 0)
  54. for _, addr := range addrs {
  55. ip, _, err := net.ParseCIDR(addr.String())
  56. if err != nil {
  57. continue
  58. }
  59. if !(ip.IsLoopback() || ip.IsGlobalUnicast()) {
  60. continue
  61. }
  62. ips = append(ips, ip.String())
  63. }
  64. *flaginter = strings.Join(ips, ",")
  65. }
  66. inter := getInterfaces()
  67. go monitor()
  68. dirName := *flagconfig
  69. Zones := make(Zones)
  70. setupPgeodnsZone(Zones)
  71. go configReader(dirName, Zones)
  72. for _, host := range inter {
  73. go listenAndServe(host, &Zones)
  74. }
  75. terminate := make(chan os.Signal)
  76. signal.Notify(terminate, os.Interrupt)
  77. <-terminate
  78. log.Printf("geodns: signal received, stopping")
  79. if *memprofile != "" {
  80. f, err := os.Create(*memprofile)
  81. if err != nil {
  82. log.Fatal(err)
  83. }
  84. pprof.WriteHeapProfile(f)
  85. f.Close()
  86. }
  87. //os.Exit(0)
  88. }