handlers.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package ipscan
  2. /*
  3. ipscan http handlers
  4. This script provide http handlers for ipscan module
  5. */
  6. import (
  7. "encoding/json"
  8. "net"
  9. "net/http"
  10. "imuslab.com/zoraxy/mod/utils"
  11. )
  12. // HandleScanPort is the HTTP handler for scanning opened ports on a given IP address
  13. func HandleScanPort(w http.ResponseWriter, r *http.Request) {
  14. targetIp, err := utils.GetPara(r, "ip")
  15. if err != nil {
  16. utils.SendErrorResponse(w, "target IP address not given")
  17. return
  18. }
  19. // Check if the IP is a valid IP address
  20. ip := net.ParseIP(targetIp)
  21. if ip == nil {
  22. utils.SendErrorResponse(w, "invalid IP address")
  23. return
  24. }
  25. // Scan the ports
  26. openPorts := ScanPorts(targetIp)
  27. jsonData, err := json.Marshal(openPorts)
  28. if err != nil {
  29. utils.SendErrorResponse(w, "failed to marshal JSON")
  30. return
  31. }
  32. utils.SendJSONResponse(w, string(jsonData))
  33. }
  34. // HandleIpScan is the HTTP handler for scanning IP addresses in a given range or CIDR
  35. func HandleIpScan(w http.ResponseWriter, r *http.Request) {
  36. cidr, err := utils.PostPara(r, "cidr")
  37. if err != nil {
  38. //Ip range mode
  39. start, err := utils.PostPara(r, "start")
  40. if err != nil {
  41. utils.SendErrorResponse(w, "missing start ip")
  42. return
  43. }
  44. end, err := utils.PostPara(r, "end")
  45. if err != nil {
  46. utils.SendErrorResponse(w, "missing end ip")
  47. return
  48. }
  49. discoveredHosts, err := ScanIpRange(start, end)
  50. if err != nil {
  51. utils.SendErrorResponse(w, err.Error())
  52. return
  53. }
  54. js, _ := json.Marshal(discoveredHosts)
  55. utils.SendJSONResponse(w, string(js))
  56. } else {
  57. //CIDR mode
  58. discoveredHosts, err := ScanCIDRRange(cidr)
  59. if err != nil {
  60. utils.SendErrorResponse(w, err.Error())
  61. return
  62. }
  63. js, _ := json.Marshal(discoveredHosts)
  64. utils.SendJSONResponse(w, string(js))
  65. }
  66. }