accesslist.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. strip "github.com/grokify/html-strip-tags-go"
  6. "imuslab.com/zoraxy/mod/geodb"
  7. "imuslab.com/zoraxy/mod/utils"
  8. )
  9. /*
  10. accesslist.go
  11. This script file is added to extend the
  12. reverse proxy function to include
  13. banning / whitelist a specific IP address or country code
  14. */
  15. /*
  16. Blacklist Related
  17. */
  18. // List a of blacklisted ip address or country code
  19. func handleListBlacklisted(w http.ResponseWriter, r *http.Request) {
  20. bltype, err := utils.GetPara(r, "type")
  21. if err != nil {
  22. bltype = "country"
  23. }
  24. resulst := []string{}
  25. if bltype == "country" {
  26. resulst = geodbStore.GetAllBlacklistedCountryCode()
  27. } else if bltype == "ip" {
  28. resulst = geodbStore.GetAllBlacklistedIp()
  29. }
  30. js, _ := json.Marshal(resulst)
  31. utils.SendJSONResponse(w, string(js))
  32. }
  33. func handleCountryBlacklistAdd(w http.ResponseWriter, r *http.Request) {
  34. countryCode, err := utils.PostPara(r, "cc")
  35. if err != nil {
  36. utils.SendErrorResponse(w, "invalid or empty country code")
  37. return
  38. }
  39. geodbStore.AddCountryCodeToBlackList(countryCode)
  40. utils.SendOK(w)
  41. }
  42. func handleCountryBlacklistRemove(w http.ResponseWriter, r *http.Request) {
  43. countryCode, err := utils.PostPara(r, "cc")
  44. if err != nil {
  45. utils.SendErrorResponse(w, "invalid or empty country code")
  46. return
  47. }
  48. geodbStore.RemoveCountryCodeFromBlackList(countryCode)
  49. utils.SendOK(w)
  50. }
  51. func handleIpBlacklistAdd(w http.ResponseWriter, r *http.Request) {
  52. ipAddr, err := utils.PostPara(r, "ip")
  53. if err != nil {
  54. utils.SendErrorResponse(w, "invalid or empty ip address")
  55. return
  56. }
  57. geodbStore.AddIPToBlackList(ipAddr)
  58. }
  59. func handleIpBlacklistRemove(w http.ResponseWriter, r *http.Request) {
  60. ipAddr, err := utils.PostPara(r, "ip")
  61. if err != nil {
  62. utils.SendErrorResponse(w, "invalid or empty ip address")
  63. return
  64. }
  65. geodbStore.RemoveIPFromBlackList(ipAddr)
  66. utils.SendOK(w)
  67. }
  68. func handleBlacklistEnable(w http.ResponseWriter, r *http.Request) {
  69. enable, err := utils.PostPara(r, "enable")
  70. if err != nil {
  71. //Return the current enabled state
  72. currentEnabled := geodbStore.BlacklistEnabled
  73. js, _ := json.Marshal(currentEnabled)
  74. utils.SendJSONResponse(w, string(js))
  75. } else {
  76. if enable == "true" {
  77. geodbStore.ToggleBlacklist(true)
  78. } else if enable == "false" {
  79. geodbStore.ToggleBlacklist(false)
  80. } else {
  81. utils.SendErrorResponse(w, "invalid enable state: only true and false is accepted")
  82. return
  83. }
  84. utils.SendOK(w)
  85. }
  86. }
  87. /*
  88. Whitelist Related
  89. */
  90. func handleListWhitelisted(w http.ResponseWriter, r *http.Request) {
  91. bltype, err := utils.GetPara(r, "type")
  92. if err != nil {
  93. bltype = "country"
  94. }
  95. resulst := []*geodb.WhitelistEntry{}
  96. if bltype == "country" {
  97. resulst = geodbStore.GetAllWhitelistedCountryCode()
  98. } else if bltype == "ip" {
  99. resulst = geodbStore.GetAllWhitelistedIp()
  100. }
  101. js, _ := json.Marshal(resulst)
  102. utils.SendJSONResponse(w, string(js))
  103. }
  104. func handleCountryWhitelistAdd(w http.ResponseWriter, r *http.Request) {
  105. countryCode, err := utils.PostPara(r, "cc")
  106. if err != nil {
  107. utils.SendErrorResponse(w, "invalid or empty country code")
  108. return
  109. }
  110. comment, _ := utils.PostPara(r, "comment")
  111. comment = strip.StripTags(comment)
  112. geodbStore.AddCountryCodeToWhitelist(countryCode, comment)
  113. utils.SendOK(w)
  114. }
  115. func handleCountryWhitelistRemove(w http.ResponseWriter, r *http.Request) {
  116. countryCode, err := utils.PostPara(r, "cc")
  117. if err != nil {
  118. utils.SendErrorResponse(w, "invalid or empty country code")
  119. return
  120. }
  121. geodbStore.RemoveCountryCodeFromWhitelist(countryCode)
  122. utils.SendOK(w)
  123. }
  124. func handleIpWhitelistAdd(w http.ResponseWriter, r *http.Request) {
  125. ipAddr, err := utils.PostPara(r, "ip")
  126. if err != nil {
  127. utils.SendErrorResponse(w, "invalid or empty ip address")
  128. return
  129. }
  130. comment, _ := utils.PostPara(r, "comment")
  131. comment = strip.StripTags(comment)
  132. geodbStore.AddIPToWhiteList(ipAddr, comment)
  133. }
  134. func handleIpWhitelistRemove(w http.ResponseWriter, r *http.Request) {
  135. ipAddr, err := utils.PostPara(r, "ip")
  136. if err != nil {
  137. utils.SendErrorResponse(w, "invalid or empty ip address")
  138. return
  139. }
  140. geodbStore.RemoveIPFromWhiteList(ipAddr)
  141. utils.SendOK(w)
  142. }
  143. func handleWhitelistEnable(w http.ResponseWriter, r *http.Request) {
  144. enable, err := utils.PostPara(r, "enable")
  145. if err != nil {
  146. //Return the current enabled state
  147. currentEnabled := geodbStore.WhitelistEnabled
  148. js, _ := json.Marshal(currentEnabled)
  149. utils.SendJSONResponse(w, string(js))
  150. } else {
  151. if enable == "true" {
  152. geodbStore.ToggleWhitelist(true)
  153. } else if enable == "false" {
  154. geodbStore.ToggleWhitelist(false)
  155. } else {
  156. utils.SendErrorResponse(w, "invalid enable state: only true and false is accepted")
  157. return
  158. }
  159. utils.SendOK(w)
  160. }
  161. }