api_host.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. for i := range a.Interfaces {
  40. a.Interfaces[i].AddressString = a.Interfaces[i].Address.String()
  41. }
  42. a.InternetGateway = h.InternetGateway.String()
  43. if isEmptyAddr(a.InternetGateway) {
  44. a.InternetGateway = ""
  45. }
  46. a.IsStatic = h.IsStatic
  47. a.ListenPort = h.ListenPort
  48. a.LocalListenPort = h.LocalListenPort
  49. a.LocalRange = h.LocalRange.String()
  50. if isEmptyAddr(a.LocalRange) {
  51. a.LocalRange = ""
  52. }
  53. a.MTU = h.MTU
  54. a.MacAddress = h.MacAddress.String()
  55. a.Name = h.Name
  56. a.OS = h.OS
  57. a.Nodes = h.Nodes
  58. a.ProxyEnabled = h.ProxyEnabled
  59. a.ProxyListenPort = h.ProxyListenPort
  60. a.PublicKey = h.PublicKey.String()
  61. a.Verbosity = h.Verbosity
  62. a.Version = h.Version
  63. a.IsDefault = h.IsDefault
  64. a.IsRelay = h.IsRelay
  65. a.RelayedHosts = h.RelayedHosts
  66. a.IsRelayed = h.IsRelayed
  67. a.RelayedBy = h.RelayedBy
  68. return &a
  69. }
  70. // APIHost.ConvertAPIHostToNMHost - convert's a given apihost struct to
  71. // a Host struct
  72. func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *Host) *Host {
  73. h := Host{}
  74. h.ID = currentHost.ID
  75. h.HostPass = currentHost.HostPass
  76. h.DaemonInstalled = currentHost.DaemonInstalled
  77. h.EndpointIP = net.ParseIP(a.EndpointIP)
  78. h.Debug = a.Debug
  79. h.FirewallInUse = a.FirewallInUse
  80. h.IPForwarding = currentHost.IPForwarding
  81. h.Interface = currentHost.Interface
  82. h.Interfaces = currentHost.Interfaces
  83. h.InternetGateway = currentHost.InternetGateway
  84. h.IsDocker = currentHost.IsDocker
  85. h.IsK8S = currentHost.IsK8S
  86. h.IsStatic = a.IsStatic
  87. h.ListenPort = a.ListenPort
  88. h.LocalListenPort = currentHost.ListenPort
  89. h.MTU = a.MTU
  90. h.MacAddress = currentHost.MacAddress
  91. h.PublicKey = currentHost.PublicKey
  92. h.Name = a.Name
  93. h.Version = currentHost.Version
  94. h.Verbosity = a.Verbosity
  95. h.Nodes = currentHost.Nodes
  96. h.TrafficKeyPublic = currentHost.TrafficKeyPublic
  97. h.OS = currentHost.OS
  98. h.RelayedBy = a.RelayedBy
  99. h.RelayedHosts = a.RelayedHosts
  100. h.IsRelay = a.IsRelay
  101. h.IsRelayed = a.IsRelayed
  102. if len(a.LocalRange) > 0 {
  103. _, localRange, err := net.ParseCIDR(a.LocalRange)
  104. if err == nil {
  105. h.LocalRange = *localRange
  106. }
  107. } else if !isEmptyAddr(currentHost.LocalRange.String()) {
  108. h.LocalRange = currentHost.LocalRange
  109. }
  110. h.ProxyEnabled = a.ProxyEnabled
  111. h.IsDefault = a.IsDefault
  112. return &h
  113. }