handler.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package neighbour
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "imuslab.com/arozos/mod/network/mdns"
  6. "imuslab.com/arozos/mod/utils"
  7. )
  8. /*
  9. Static handlers for Cluster Neighbourhood
  10. author: tobychui
  11. */
  12. type ScanResults struct {
  13. LastUpdate int64 //Last update timestamp for the scan results
  14. ThisHost *mdns.NetworkHost //The host information this host is sending out (also looping back)
  15. NearbyHosts []*mdns.NetworkHost //Other hosts in the network
  16. }
  17. //Handle HTTP request for scanning and return the result
  18. func (d *Discoverer) HandleScanningRequest(w http.ResponseWriter, r *http.Request) {
  19. result := new(ScanResults)
  20. hosts := d.GetNearbyHosts()
  21. for _, host := range hosts {
  22. if host.UUID == d.Host.Host.UUID {
  23. //This a loopback signal
  24. result.ThisHost = host
  25. } else {
  26. //This is a signal from other host in the network
  27. result.NearbyHosts = append(result.NearbyHosts, host)
  28. }
  29. }
  30. result.LastUpdate = d.LastScanningTime
  31. js, _ := json.Marshal(result)
  32. utils.SendJSONResponse(w, string(js))
  33. }
  34. //Get networkHosts that are offline
  35. func (d *Discoverer) HandleScanRecord(w http.ResponseWriter, r *http.Request) {
  36. offlineNodes, err := d.GetOfflineHosts()
  37. if err != nil {
  38. utils.SendErrorResponse(w, err.Error())
  39. return
  40. }
  41. js, err := json.Marshal(offlineNodes)
  42. if err != nil {
  43. utils.SendErrorResponse(w, err.Error())
  44. return
  45. }
  46. utils.SendJSONResponse(w, string(js))
  47. }
  48. //Send wake on land to target
  49. func (d *Discoverer) HandleWakeOnLan(w http.ResponseWriter, r *http.Request) {
  50. mac, err := utils.Mv(r, "mac", false)
  51. if err != nil {
  52. utils.SendErrorResponse(w, "Invalid mac address")
  53. return
  54. }
  55. err = d.SendWakeOnLan(mac)
  56. if err != nil {
  57. utils.SendErrorResponse(w, err.Error())
  58. return
  59. }
  60. utils.SendOK(w)
  61. }