web.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package web
  2. import (
  3. "encoding/json"
  4. "io"
  5. "io/ioutil"
  6. "net"
  7. "net/http"
  8. "regexp"
  9. "strconv"
  10. "strings"
  11. "github.com/go-chi/chi"
  12. "github.com/go-chi/chi/middleware"
  13. "github.com/go-chi/cors"
  14. "github.com/go-chi/render"
  15. log "github.com/sirupsen/logrus"
  16. "backend/config"
  17. "backend/results"
  18. )
  19. const (
  20. // chunk size is 1 mib
  21. chunkSize = 1048576
  22. )
  23. var (
  24. // generate random data for download test on start to minimize runtime overhead
  25. randomData = getRandomData(chunkSize)
  26. )
  27. func ListenAndServe(conf *config.Config) error {
  28. r := chi.NewMux()
  29. r.Use(middleware.RealIP)
  30. cs := cors.New(cors.Options{
  31. AllowedOrigins: []string{"*"},
  32. AllowedMethods: []string{"GET", "POST", "OPTIONS"},
  33. AllowedHeaders: []string{"*"},
  34. })
  35. r.Use(cs.Handler)
  36. r.Use(middleware.NoCache)
  37. r.Use(middleware.Logger)
  38. log.Infof("Starting backend server on port %s", conf.Port)
  39. r.Get("/*", pages)
  40. r.HandleFunc("/empty", empty)
  41. r.Get("/garbage", garbage)
  42. r.Get("/getIP", getIP)
  43. r.Get("/results", results.DrawPNG)
  44. r.Get("/results/", results.DrawPNG)
  45. r.Post("/results/telemetry", results.Record)
  46. r.HandleFunc("/stats", results.Stats)
  47. // PHP frontend default values compatibility
  48. r.HandleFunc("/empty.php", empty)
  49. r.Get("/garbage.php", garbage)
  50. r.Get("/getIP.php", getIP)
  51. r.Post("/results/telemetry.php", results.Record)
  52. r.HandleFunc("/stats.php", results.Stats)
  53. return http.ListenAndServe(net.JoinHostPort(conf.BindAddress, conf.Port), r)
  54. }
  55. func pages(w http.ResponseWriter, r *http.Request) {
  56. if r.RequestURI == "/" {
  57. r.RequestURI = "/index.html"
  58. }
  59. uri := strings.Split(r.RequestURI, "?")[0]
  60. if strings.HasSuffix(uri, ".html") || strings.HasSuffix(uri, ".js") {
  61. http.FileServer(http.Dir("assets")).ServeHTTP(w, r)
  62. } else {
  63. w.WriteHeader(http.StatusForbidden)
  64. }
  65. }
  66. func empty(w http.ResponseWriter, r *http.Request) {
  67. io.Copy(ioutil.Discard, r.Body)
  68. r.Body.Close()
  69. w.Header().Set("Connection", "keep-alive")
  70. w.WriteHeader(http.StatusOK)
  71. }
  72. func garbage(w http.ResponseWriter, r *http.Request) {
  73. w.Header().Set("Content-Description", "File Transfer")
  74. w.Header().Set("Content-Type", "application/octet-stream")
  75. w.Header().Set("Content-Disposition", "attachment; filename=random.dat")
  76. w.Header().Set("Content-Transfer-Encoding", "binary")
  77. conf := config.LoadedConfig()
  78. ckSize := r.FormValue("ckSize")
  79. chunks := conf.DownloadChunks
  80. if ckSize != "" {
  81. i, err := strconv.ParseInt(ckSize, 10, 64)
  82. if err == nil && i > 0 && i < 1024 {
  83. chunks = int(i)
  84. } else {
  85. log.Errorf("Invalid chunk size: %s", ckSize)
  86. log.Warn("Will use default value %d", chunks)
  87. }
  88. }
  89. for i := 0; i < chunks; i++ {
  90. if _, err := w.Write(randomData); err != nil {
  91. log.Errorf("Error writing back to client at chunk number %d: %s", i, err)
  92. break
  93. }
  94. }
  95. }
  96. func getIP(w http.ResponseWriter, r *http.Request) {
  97. var ret results.Result
  98. clientIP := r.RemoteAddr
  99. if strings.Contains(clientIP, ":") {
  100. ip, _, _ := net.SplitHostPort(r.RemoteAddr)
  101. clientIP = ip
  102. }
  103. strings.ReplaceAll(clientIP, "::ffff:", "")
  104. isSpecialIP := true
  105. switch {
  106. case clientIP == "::1":
  107. ret.ProcessedString = clientIP + " - localhost IPv6 access"
  108. case strings.HasPrefix(clientIP, "fe80:"):
  109. ret.ProcessedString = clientIP + " - link-local IPv6 access"
  110. case strings.HasPrefix(clientIP, "127."):
  111. ret.ProcessedString = clientIP + " - localhost IPv4 access"
  112. case strings.HasPrefix(clientIP, "10."):
  113. ret.ProcessedString = clientIP + " - private IPv4 access"
  114. case regexp.MustCompile(`^172\.(1[6-9]|2\d|3[01])\.`).MatchString(clientIP):
  115. ret.ProcessedString = clientIP + " - private IPv4 access"
  116. case strings.HasPrefix(clientIP, "192.168"):
  117. ret.ProcessedString = clientIP + " - private IPv4 access"
  118. case strings.HasPrefix(clientIP, "169.254"):
  119. ret.ProcessedString = clientIP + " - link-local IPv4 access"
  120. case regexp.MustCompile(`^100\.([6-9][0-9]|1[0-2][0-7])\.`).MatchString(clientIP):
  121. ret.ProcessedString = clientIP + " - CGNAT IPv4 access"
  122. default:
  123. isSpecialIP = false
  124. }
  125. if isSpecialIP {
  126. b, _ := json.Marshal(&ret)
  127. if _, err := w.Write(b); err != nil {
  128. log.Errorf("Error writing to client: %s", err)
  129. }
  130. return
  131. }
  132. getISPInfo := r.FormValue("isp") == "true"
  133. getDistance := r.FormValue("distance") == "true"
  134. ret.ProcessedString = clientIP
  135. if getISPInfo {
  136. rawIspInfo, ispInfo := getIPInfo(clientIP)
  137. ret.RawISPInfo = rawIspInfo
  138. removeRegexp := regexp.MustCompile(`AS\d+\s`)
  139. isp := removeRegexp.ReplaceAllString(ispInfo.Organization, "")
  140. if isp == "" {
  141. isp = "Unknown ISP"
  142. }
  143. if ispInfo.Country != "" {
  144. isp += ", " + ispInfo.Country
  145. }
  146. if ispInfo.Location != "" && getDistance {
  147. isp += " (" + calculateDistance(ispInfo.Location, config.LoadedConfig().DistanceUnit) + ")"
  148. }
  149. ret.ProcessedString += " - " + isp
  150. } else {
  151. // return an empty JSON object to avoid parse errors
  152. ret.RawISPInfo = "{}"
  153. }
  154. render.JSON(w, r, ret)
  155. }