api_defender.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (C) 2019-2023 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. )
  25. func getDefenderHosts(w http.ResponseWriter, r *http.Request) {
  26. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  27. hosts, err := common.GetDefenderHosts()
  28. if err != nil {
  29. sendAPIResponse(w, r, err, "", getRespStatus(err))
  30. return
  31. }
  32. if hosts == nil {
  33. render.JSON(w, r, make([]dataprovider.DefenderEntry, 0))
  34. return
  35. }
  36. render.JSON(w, r, hosts)
  37. }
  38. func getDefenderHostByID(w http.ResponseWriter, r *http.Request) {
  39. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  40. ip, err := getIPFromID(r)
  41. if err != nil {
  42. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  43. return
  44. }
  45. host, err := common.GetDefenderHost(ip)
  46. if err != nil {
  47. sendAPIResponse(w, r, err, "", getRespStatus(err))
  48. return
  49. }
  50. render.JSON(w, r, host)
  51. }
  52. func deleteDefenderHostByID(w http.ResponseWriter, r *http.Request) {
  53. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  54. ip, err := getIPFromID(r)
  55. if err != nil {
  56. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  57. return
  58. }
  59. if !common.DeleteDefenderHost(ip) {
  60. sendAPIResponse(w, r, nil, "Not found", http.StatusNotFound)
  61. return
  62. }
  63. sendAPIResponse(w, r, nil, "OK", http.StatusOK)
  64. }
  65. func getIPFromID(r *http.Request) (string, error) {
  66. decoded, err := hex.DecodeString(getURLParam(r, "id"))
  67. if err != nil {
  68. return "", errors.New("invalid host id")
  69. }
  70. ip := string(decoded)
  71. err = validateIPAddress(ip)
  72. if err != nil {
  73. return "", err
  74. }
  75. return ip, nil
  76. }
  77. func validateIPAddress(ip string) error {
  78. if net.ParseIP(ip) == nil {
  79. return fmt.Errorf("ip address %q is not valid", ip)
  80. }
  81. return nil
  82. }