api_defender.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (C) 2019 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package httpd
  15. import (
  16. "encoding/hex"
  17. "errors"
  18. "fmt"
  19. "net"
  20. "net/http"
  21. "github.com/go-chi/render"
  22. "github.com/drakkan/sftpgo/v2/internal/common"
  23. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  24. "github.com/drakkan/sftpgo/v2/internal/util"
  25. )
  26. func getDefenderHosts(w http.ResponseWriter, r *http.Request) {
  27. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  28. hosts, err := common.GetDefenderHosts()
  29. if err != nil {
  30. sendAPIResponse(w, r, err, "", getRespStatus(err))
  31. return
  32. }
  33. if hosts == nil {
  34. render.JSON(w, r, make([]dataprovider.DefenderEntry, 0))
  35. return
  36. }
  37. render.JSON(w, r, hosts)
  38. }
  39. func getDefenderHostByID(w http.ResponseWriter, r *http.Request) {
  40. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  41. ip, err := getIPFromID(r)
  42. if err != nil {
  43. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  44. return
  45. }
  46. host, err := common.GetDefenderHost(ip)
  47. if err != nil {
  48. sendAPIResponse(w, r, err, "", getRespStatus(err))
  49. return
  50. }
  51. render.JSON(w, r, host)
  52. }
  53. func deleteDefenderHostByID(w http.ResponseWriter, r *http.Request) {
  54. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  55. ip, err := getIPFromID(r)
  56. if err != nil {
  57. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  58. return
  59. }
  60. if !common.DeleteDefenderHost(ip) {
  61. sendAPIResponse(w, r, nil, "Not found", http.StatusNotFound)
  62. return
  63. }
  64. sendAPIResponse(w, r, nil, "OK", http.StatusOK)
  65. }
  66. func getIPFromID(r *http.Request) (string, error) {
  67. decoded, err := hex.DecodeString(getURLParam(r, "id"))
  68. if err != nil {
  69. return "", errors.New("invalid host id")
  70. }
  71. ip := util.BytesToString(decoded)
  72. err = validateIPAddress(ip)
  73. if err != nil {
  74. return "", err
  75. }
  76. return ip, nil
  77. }
  78. func validateIPAddress(ip string) error {
  79. if net.ParseIP(ip) == nil {
  80. return fmt.Errorf("ip address %q is not valid", ip)
  81. }
  82. return nil
  83. }