zerotier.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package v2
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/IceWhaleTech/CasaOS-Common/utils"
  6. "github.com/IceWhaleTech/CasaOS/codegen"
  7. "github.com/IceWhaleTech/CasaOS/common"
  8. "github.com/IceWhaleTech/CasaOS/pkg/utils/httper"
  9. "github.com/labstack/echo/v4"
  10. "github.com/tidwall/gjson"
  11. )
  12. func (s *CasaOS) SetZerotierNetworkStatus(ctx echo.Context, networkId string) error {
  13. ip := `,"via":"10.147.19.0"`
  14. m := make(map[string]string)
  15. ctx.Bind(&m)
  16. status := m["status"]
  17. if status == "online" {
  18. ip = ``
  19. }
  20. body := `{
  21. "routes": [
  22. {
  23. "target": "10.147.20.0/24"` + ip + `
  24. }
  25. ]
  26. }`
  27. res, err := httper.ZTPost("/controller/network/"+networkId, body)
  28. if err != nil {
  29. fmt.Println(err)
  30. return ctx.JSON(http.StatusInternalServerError, codegen.BaseResponse{Message: utils.Ptr(err.Error())})
  31. }
  32. info := codegen.GetZTInfoOK{}
  33. via := gjson.GetBytes(res, "routes.0.via").Str
  34. info.Id = utils.Ptr(gjson.GetBytes(res, "id").Str)
  35. info.Name = utils.Ptr(gjson.GetBytes(res, "name").Str)
  36. if len(via) == 0 {
  37. info.Status = utils.Ptr("online")
  38. } else {
  39. info.Status = utils.Ptr("offline")
  40. }
  41. return ctx.JSON(http.StatusOK, info)
  42. }
  43. func (s *CasaOS) GetZerotierInfo(ctx echo.Context) error {
  44. info := codegen.GetZTInfoOK{}
  45. respBody, err := httper.ZTGet("/controller/network")
  46. if err != nil {
  47. return ctx.JSON(http.StatusInternalServerError, codegen.BaseResponse{Message: utils.Ptr(err.Error())})
  48. }
  49. networkNames := gjson.ParseBytes(respBody).Array()
  50. for _, v := range networkNames {
  51. res, err := httper.ZTGet("/controller/network/" + v.Str)
  52. if err != nil {
  53. fmt.Println(err)
  54. return ctx.JSON(http.StatusInternalServerError, codegen.BaseResponse{Message: utils.Ptr(err.Error())})
  55. }
  56. name := gjson.GetBytes(res, "name").Str
  57. if name == common.RANW_NAME {
  58. via := gjson.GetBytes(res, "routes.0.via").Str
  59. info.Id = utils.Ptr(gjson.GetBytes(res, "id").Str)
  60. info.Name = &name
  61. if len(via) == 0 {
  62. info.Status = utils.Ptr("online")
  63. } else {
  64. info.Status = utils.Ptr("offline")
  65. }
  66. break
  67. }
  68. }
  69. return ctx.JSON(http.StatusOK, info)
  70. }