blacklist.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "imuslab.com/zoraxy/mod/utils"
  6. )
  7. /*
  8. blacklist.go
  9. This script file is added to extend the
  10. reverse proxy function to include
  11. banning a specific IP address or country code
  12. */
  13. //List a of blacklisted ip address or country code
  14. func handleListBlacklisted(w http.ResponseWriter, r *http.Request) {
  15. bltype, err := utils.GetPara(r, "type")
  16. if err != nil {
  17. bltype = "country"
  18. }
  19. resulst := []string{}
  20. if bltype == "country" {
  21. resulst = geodbStore.GetAllBlacklistedCountryCode()
  22. } else if bltype == "ip" {
  23. resulst = geodbStore.GetAllBlacklistedIp()
  24. }
  25. js, _ := json.Marshal(resulst)
  26. utils.SendJSONResponse(w, string(js))
  27. }
  28. func handleCountryBlacklistAdd(w http.ResponseWriter, r *http.Request) {
  29. countryCode, err := utils.PostPara(r, "cc")
  30. if err != nil {
  31. utils.SendErrorResponse(w, "invalid or empty country code")
  32. return
  33. }
  34. geodbStore.AddCountryCodeToBlackList(countryCode)
  35. utils.SendOK(w)
  36. }
  37. func handleCountryBlacklistRemove(w http.ResponseWriter, r *http.Request) {
  38. countryCode, err := utils.PostPara(r, "cc")
  39. if err != nil {
  40. utils.SendErrorResponse(w, "invalid or empty country code")
  41. return
  42. }
  43. geodbStore.RemoveCountryCodeFromBlackList(countryCode)
  44. utils.SendOK(w)
  45. }
  46. func handleIpBlacklistAdd(w http.ResponseWriter, r *http.Request) {
  47. ipAddr, err := utils.PostPara(r, "ip")
  48. if err != nil {
  49. utils.SendErrorResponse(w, "invalid or empty ip address")
  50. return
  51. }
  52. geodbStore.AddIPToBlackList(ipAddr)
  53. }
  54. func handleIpBlacklistRemove(w http.ResponseWriter, r *http.Request) {
  55. ipAddr, err := utils.PostPara(r, "ip")
  56. if err != nil {
  57. utils.SendErrorResponse(w, "invalid or empty ip address")
  58. return
  59. }
  60. geodbStore.RemoveIPFromBlackList(ipAddr)
  61. utils.SendOK(w)
  62. }
  63. func handleBlacklistEnable(w http.ResponseWriter, r *http.Request) {
  64. enable, err := utils.PostPara(r, "enable")
  65. if err != nil {
  66. //Return the current enabled state
  67. currentEnabled := geodbStore.Enabled
  68. js, _ := json.Marshal(currentEnabled)
  69. utils.SendJSONResponse(w, string(js))
  70. } else {
  71. if enable == "true" {
  72. geodbStore.ToggleBlacklist(true)
  73. } else if enable == "false" {
  74. geodbStore.ToggleBlacklist(false)
  75. } else {
  76. utils.SendErrorResponse(w, "invalid enable state: only true and false is accepted")
  77. return
  78. }
  79. utils.SendOK(w)
  80. }
  81. }