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. ProxyListenPort int `json:"proxy_listen_port"`
  16. MTU int `json:"mtu" yaml:"mtu"`
  17. Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
  18. EndpointIP string `json:"endpointip" yaml:"endpointip"`
  19. PublicKey string `json:"publickey"`
  20. MacAddress string `json:"macaddress"`
  21. InternetGateway string `json:"internetgateway"`
  22. Nodes []string `json:"nodes"`
  23. ProxyEnabled bool `json:"proxy_enabled" yaml:"proxy_enabled"`
  24. IsDefault bool `json:"isdefault" yaml:"isdefault"`
  25. IsRelayed bool `json:"isrelayed" bson:"isrelayed" yaml:"isrelayed"`
  26. RelayedBy string `json:"relayed_by" bson:"relayed_by" yaml:"relayed_by"`
  27. IsRelay bool `json:"isrelay" bson:"isrelay" yaml:"isrelay"`
  28. RelayedHosts []string `json:"relay_hosts" bson:"relay_hosts" yaml:"relay_hosts"`
  29. }
  30. // Host.ConvertNMHostToAPI - converts a Netmaker host to an API editable host
  31. func (h *Host) ConvertNMHostToAPI() *ApiHost {
  32. a := ApiHost{}
  33. a.Debug = h.Debug
  34. a.EndpointIP = h.EndpointIP.String()
  35. a.FirewallInUse = h.FirewallInUse
  36. a.ID = h.ID.String()
  37. a.Interfaces = h.Interfaces
  38. for i := range a.Interfaces {
  39. a.Interfaces[i].AddressString = a.Interfaces[i].Address.String()
  40. }
  41. a.InternetGateway = h.InternetGateway.String()
  42. if isEmptyAddr(a.InternetGateway) {
  43. a.InternetGateway = ""
  44. }
  45. a.IsStatic = h.IsStatic
  46. a.ListenPort = h.ListenPort
  47. a.LocalRange = h.LocalRange.String()
  48. if isEmptyAddr(a.LocalRange) {
  49. a.LocalRange = ""
  50. }
  51. a.MTU = h.MTU
  52. a.MacAddress = h.MacAddress.String()
  53. a.Name = h.Name
  54. a.OS = h.OS
  55. a.Nodes = h.Nodes
  56. a.ProxyEnabled = h.ProxyEnabled
  57. a.ProxyListenPort = h.ProxyListenPort
  58. a.PublicKey = h.PublicKey.String()
  59. a.Verbosity = h.Verbosity
  60. a.Version = h.Version
  61. a.IsDefault = h.IsDefault
  62. a.IsRelay = h.IsRelay
  63. a.RelayedHosts = h.RelayedHosts
  64. a.IsRelayed = h.IsRelayed
  65. a.RelayedBy = h.RelayedBy
  66. return &a
  67. }
  68. // APIHost.ConvertAPIHostToNMHost - convert's a given apihost struct to
  69. // a Host struct
  70. func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *Host) *Host {
  71. h := Host{}
  72. h.ID = currentHost.ID
  73. h.HostPass = currentHost.HostPass
  74. h.DaemonInstalled = currentHost.DaemonInstalled
  75. h.EndpointIP = net.ParseIP(a.EndpointIP)
  76. h.Debug = a.Debug
  77. h.FirewallInUse = a.FirewallInUse
  78. h.IPForwarding = currentHost.IPForwarding
  79. h.Interface = currentHost.Interface
  80. h.Interfaces = currentHost.Interfaces
  81. h.InternetGateway = currentHost.InternetGateway
  82. h.IsDocker = currentHost.IsDocker
  83. h.IsK8S = currentHost.IsK8S
  84. h.IsStatic = a.IsStatic
  85. h.ListenPort = a.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. }