blacklist.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "imuslab.com/arozos/ReverseProxy/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. }