1
0

accesslist.go 4.5 KB

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