helpers.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "time"
  7. "imuslab.com/zoraxy/mod/dynamicproxy"
  8. "imuslab.com/zoraxy/mod/ipscan"
  9. "imuslab.com/zoraxy/mod/mdns"
  10. "imuslab.com/zoraxy/mod/uptime"
  11. "imuslab.com/zoraxy/mod/utils"
  12. )
  13. /*
  14. Proxy Utils
  15. */
  16. //Check if site support TLS
  17. func HandleCheckSiteSupportTLS(w http.ResponseWriter, r *http.Request) {
  18. targetURL, err := utils.PostPara(r, "url")
  19. if err != nil {
  20. utils.SendErrorResponse(w, "invalid url given")
  21. return
  22. }
  23. httpsUrl := fmt.Sprintf("https://%s", targetURL)
  24. httpUrl := fmt.Sprintf("http://%s", targetURL)
  25. client := http.Client{Timeout: 5 * time.Second}
  26. resp, err := client.Head(httpsUrl)
  27. if err == nil && resp.StatusCode == http.StatusOK {
  28. js, _ := json.Marshal("https")
  29. utils.SendJSONResponse(w, string(js))
  30. return
  31. }
  32. resp, err = client.Head(httpUrl)
  33. if err == nil && resp.StatusCode == http.StatusOK {
  34. js, _ := json.Marshal("http")
  35. utils.SendJSONResponse(w, string(js))
  36. return
  37. }
  38. utils.SendErrorResponse(w, "invalid url given")
  39. }
  40. /*
  41. Statistic Summary
  42. */
  43. //Handle conversion of statistic daily summary to country summary
  44. func HandleCountryDistrSummary(w http.ResponseWriter, r *http.Request) {
  45. requestClientCountry := map[string]int{}
  46. statisticCollector.DailySummary.RequestClientIp.Range(func(key, value interface{}) bool {
  47. //Get this client country of original
  48. clientIp := key.(string)
  49. //requestCount := value.(int)
  50. ci, err := geodbStore.ResolveCountryCodeFromIP(clientIp)
  51. if err != nil {
  52. return true
  53. }
  54. isoCode := ci.CountryIsoCode
  55. if isoCode == "" {
  56. //local or reserved addr
  57. isoCode = "local"
  58. }
  59. uc, ok := requestClientCountry[isoCode]
  60. if !ok {
  61. //Create the counter
  62. requestClientCountry[isoCode] = 1
  63. } else {
  64. requestClientCountry[isoCode] = uc + 1
  65. }
  66. return true
  67. })
  68. js, _ := json.Marshal(requestClientCountry)
  69. utils.SendJSONResponse(w, string(js))
  70. }
  71. /*
  72. Up Time Monitor
  73. */
  74. //Generate uptime monitor targets from reverse proxy rules
  75. func GetUptimeTargetsFromReverseProxyRules(dp *dynamicproxy.Router) []*uptime.Target {
  76. subds := dp.GetSDProxyEndpointsAsMap()
  77. vdirs := dp.GetVDProxyEndpointsAsMap()
  78. UptimeTargets := []*uptime.Target{}
  79. for subd, target := range subds {
  80. url := "http://" + target.Domain
  81. protocol := "http"
  82. if target.RequireTLS {
  83. url = "https://" + target.Domain
  84. protocol = "https"
  85. }
  86. UptimeTargets = append(UptimeTargets, &uptime.Target{
  87. ID: subd,
  88. Name: subd,
  89. URL: url,
  90. Protocol: protocol,
  91. })
  92. }
  93. for vdir, target := range vdirs {
  94. url := "http://" + target.Domain
  95. protocol := "http"
  96. if target.RequireTLS {
  97. url = "https://" + target.Domain
  98. protocol = "https"
  99. }
  100. UptimeTargets = append(UptimeTargets, &uptime.Target{
  101. ID: vdir,
  102. Name: "*" + vdir,
  103. URL: url,
  104. Protocol: protocol,
  105. })
  106. }
  107. return UptimeTargets
  108. }
  109. //Handle rendering up time monitor data
  110. func HandleUptimeMonitorListing(w http.ResponseWriter, r *http.Request) {
  111. if uptimeMonitor != nil {
  112. uptimeMonitor.HandleUptimeLogRead(w, r)
  113. } else {
  114. http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
  115. return
  116. }
  117. }
  118. //Handle listing current registered mdns nodes
  119. func HandleMdnsListing(w http.ResponseWriter, r *http.Request) {
  120. js, _ := json.Marshal(previousmdnsScanResults)
  121. utils.SendJSONResponse(w, string(js))
  122. }
  123. func HandleMdnsScanning(w http.ResponseWriter, r *http.Request) {
  124. domain, err := utils.PostPara(r, "domain")
  125. var hosts []*mdns.NetworkHost
  126. if err != nil {
  127. //Search for arozos node
  128. hosts = mdnsScanner.Scan(30, "")
  129. previousmdnsScanResults = hosts
  130. } else {
  131. //Search for other nodes
  132. hosts = mdnsScanner.Scan(30, domain)
  133. }
  134. js, _ := json.Marshal(hosts)
  135. utils.SendJSONResponse(w, string(js))
  136. }
  137. //handle traceroute tools
  138. func HandleIpScan(w http.ResponseWriter, r *http.Request) {
  139. cidr, err := utils.PostPara(r, "cidr")
  140. if err != nil {
  141. //Ip range mode
  142. start, err := utils.PostPara(r, "start")
  143. if err != nil {
  144. utils.SendErrorResponse(w, "missing start ip")
  145. return
  146. }
  147. end, err := utils.PostPara(r, "end")
  148. if err != nil {
  149. utils.SendErrorResponse(w, "missing end ip")
  150. return
  151. }
  152. discoveredHosts, err := ipscan.ScanIpRange(start, end)
  153. if err != nil {
  154. utils.SendErrorResponse(w, err.Error())
  155. return
  156. }
  157. js, _ := json.Marshal(discoveredHosts)
  158. utils.SendJSONResponse(w, string(js))
  159. } else {
  160. //CIDR mode
  161. discoveredHosts, err := ipscan.ScanCIDRRange(cidr)
  162. if err != nil {
  163. utils.SendErrorResponse(w, err.Error())
  164. return
  165. }
  166. js, _ := json.Marshal(discoveredHosts)
  167. utils.SendJSONResponse(w, string(js))
  168. }
  169. }