helpers.go 4.9 KB

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