http.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "net/http"
  7. "strconv"
  8. "github.com/abh/geodns/monitor"
  9. "github.com/abh/geodns/zones"
  10. "github.com/prometheus/client_golang/prometheus/promhttp"
  11. )
  12. type httpServer struct {
  13. mux *http.ServeMux
  14. zones *zones.MuxManager
  15. serverInfo *monitor.ServerInfo
  16. }
  17. type rate struct {
  18. Name string
  19. Count int64
  20. Metrics zones.ZoneMetrics
  21. }
  22. type rates []*rate
  23. func (s rates) Len() int { return len(s) }
  24. func (s rates) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  25. type ratesByCount struct{ rates }
  26. func (s ratesByCount) Less(i, j int) bool {
  27. ic := s.rates[i].Count
  28. jc := s.rates[j].Count
  29. if ic == jc {
  30. return s.rates[i].Name < s.rates[j].Name
  31. }
  32. return ic > jc
  33. }
  34. func topParam(req *http.Request, def int) int {
  35. req.ParseForm()
  36. topOption := def
  37. topParam := req.Form["top"]
  38. if len(topParam) > 0 {
  39. var err error
  40. topOption, err = strconv.Atoi(topParam[0])
  41. if err != nil {
  42. topOption = def
  43. }
  44. }
  45. return topOption
  46. }
  47. func NewHTTPServer(mm *zones.MuxManager, serverInfo *monitor.ServerInfo) *httpServer {
  48. hs := &httpServer{
  49. zones: mm,
  50. mux: &http.ServeMux{},
  51. serverInfo: serverInfo,
  52. }
  53. hs.mux.HandleFunc("/", hs.mainServer)
  54. hs.mux.Handle("/metrics", promhttp.Handler())
  55. return hs
  56. }
  57. func (hs *httpServer) Mux() *http.ServeMux {
  58. return hs.mux
  59. }
  60. func (hs *httpServer) Run(listen string) {
  61. log.Println("Starting HTTP interface on", listen)
  62. log.Fatal(http.ListenAndServe(listen, &basicauth{h: hs.mux}))
  63. }
  64. func (hs *httpServer) mainServer(w http.ResponseWriter, req *http.Request) {
  65. if req.RequestURI != "/version" {
  66. http.NotFound(w, req)
  67. return
  68. }
  69. w.Header().Set("Content-Type", "text/plain")
  70. w.WriteHeader(200)
  71. io.WriteString(w, `GeoDNS `+hs.serverInfo.Version+`\n`)
  72. }
  73. type basicauth struct {
  74. h http.Handler
  75. }
  76. func (b *basicauth) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  77. cfgMutex.RLock()
  78. user := Config.HTTP.User
  79. password := Config.HTTP.Password
  80. cfgMutex.RUnlock()
  81. if len(user) == 0 {
  82. b.h.ServeHTTP(w, r)
  83. return
  84. }
  85. ruser, rpass, ok := r.BasicAuth()
  86. if ok {
  87. if ruser == user && rpass == password {
  88. b.h.ServeHTTP(w, r)
  89. return
  90. }
  91. }
  92. w.Header().Set("WWW-Authenticate", fmt.Sprintf(`Basic realm=%q`, "GeoDNS Status"))
  93. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  94. return
  95. }