api_host.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package models
  2. import "net"
  3. // ApiHost - the host struct for API usage
  4. type ApiHost struct {
  5. ID string `json:"id"`
  6. Verbosity int `json:"verbosity"`
  7. FirewallInUse string `json:"firewallinuse"`
  8. Version string `json:"version"`
  9. Name string `json:"name"`
  10. OS string `json:"os"`
  11. Debug bool `json:"debug"`
  12. IsStatic bool `json:"isstatic"`
  13. ListenPort int `json:"listenport"`
  14. LocalRange string `json:"localrange"`
  15. LocalListenPort int `json:"locallistenport"`
  16. ProxyListenPort int `json:"proxy_listen_port"`
  17. MTU int `json:"mtu" yaml:"mtu"`
  18. Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
  19. EndpointIP string `json:"endpointip" yaml:"endpointip"`
  20. PublicKey string `json:"publickey"`
  21. MacAddress string `json:"macaddress"`
  22. InternetGateway string `json:"internetgateway"`
  23. Nodes []string `json:"nodes"`
  24. ProxyEnabled bool `json:"proxy_enabled" yaml:"proxy_enabled"`
  25. IsDefault bool `json:"isdefault" yaml:"isdefault"`
  26. IsRelayed bool `json:"isrelayed" bson:"isrelayed" yaml:"isrelayed"`
  27. RelayedBy string `json:"relayed_by" bson:"relayed_by" yaml:"relayed_by"`
  28. IsRelay bool `json:"isrelay" bson:"isrelay" yaml:"isrelay"`
  29. RelayedHosts []string `json:"relay_hosts" bson:"relay_hosts" yaml:"relay_hosts"`
  30. }
  31. // Host.ConvertNMHostToAPI - converts a Netmaker host to an API editable host
  32. func (h *Host) ConvertNMHostToAPI() *ApiHost {
  33. a := ApiHost{}
  34. a.Debug = h.Debug
  35. a.EndpointIP = h.EndpointIP.String()
  36. a.FirewallInUse = h.FirewallInUse
  37. a.ID = h.ID.String()
  38. a.Interfaces = h.Interfaces
  39. a.InternetGateway = h.InternetGateway.String()
  40. if isEmptyAddr(a.InternetGateway) {
  41. a.InternetGateway = ""
  42. }
  43. a.IsStatic = h.IsStatic
  44. a.ListenPort = h.ListenPort
  45. a.LocalListenPort = h.LocalListenPort
  46. a.LocalRange = h.LocalRange.String()
  47. if isEmptyAddr(a.LocalRange) {
  48. a.LocalRange = ""
  49. }
  50. a.MTU = h.MTU
  51. a.MacAddress = h.MacAddress.String()
  52. a.Name = h.Name
  53. a.OS = h.OS
  54. a.Nodes = h.Nodes
  55. a.ProxyEnabled = h.ProxyEnabled
  56. a.ProxyListenPort = h.ProxyListenPort
  57. a.PublicKey = h.PublicKey.String()
  58. a.Verbosity = h.Verbosity
  59. a.Version = h.Version
  60. a.IsDefault = h.IsDefault
  61. a.IsRelay = h.IsRelay
  62. a.RelayedHosts = h.RelayedHosts
  63. a.IsRelayed = h.IsRelayed
  64. a.RelayedBy = h.RelayedBy
  65. return &a
  66. }
  67. // APIHost.ConvertAPIHostToNMHost - convert's a given apihost struct to
  68. // a Host struct
  69. func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *Host) *Host {
  70. h := Host{}
  71. h.ID = currentHost.ID
  72. h.HostPass = currentHost.HostPass
  73. h.DaemonInstalled = currentHost.DaemonInstalled
  74. h.EndpointIP = net.ParseIP(a.EndpointIP)
  75. h.Debug = a.Debug
  76. h.FirewallInUse = a.FirewallInUse
  77. h.IPForwarding = currentHost.IPForwarding
  78. h.Interface = currentHost.Interface
  79. h.Interfaces = currentHost.Interfaces
  80. h.InternetGateway = currentHost.InternetGateway
  81. h.IsDocker = currentHost.IsDocker
  82. h.IsK8S = currentHost.IsK8S
  83. h.IsStatic = a.IsStatic
  84. h.ListenPort = a.ListenPort
  85. h.LocalListenPort = currentHost.ListenPort
  86. h.MTU = a.MTU
  87. h.MacAddress = currentHost.MacAddress
  88. h.PublicKey = currentHost.PublicKey
  89. h.Name = a.Name
  90. h.Version = currentHost.Version
  91. h.Verbosity = a.Verbosity
  92. h.Nodes = currentHost.Nodes
  93. h.TrafficKeyPublic = currentHost.TrafficKeyPublic
  94. h.OS = currentHost.OS
  95. h.RelayedBy = a.RelayedBy
  96. h.RelayedHosts = a.RelayedHosts
  97. h.IsRelay = a.IsRelay
  98. h.IsRelayed = a.IsRelayed
  99. if len(a.LocalRange) > 0 {
  100. _, localRange, err := net.ParseCIDR(a.LocalRange)
  101. if err == nil {
  102. h.LocalRange = *localRange
  103. }
  104. } else if !isEmptyAddr(currentHost.LocalRange.String()) {
  105. h.LocalRange = currentHost.LocalRange
  106. }
  107. h.ProxyEnabled = a.ProxyEnabled
  108. h.IsDefault = a.IsDefault
  109. return &h
  110. }