api_host.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. DefaultInterface string `json:"defaultinterface" yaml:"defautlinterface"`
  20. EndpointIP string `json:"endpointip" yaml:"endpointip"`
  21. PublicKey string `json:"publickey"`
  22. MacAddress string `json:"macaddress"`
  23. InternetGateway string `json:"internetgateway"`
  24. Nodes []string `json:"nodes"`
  25. ProxyEnabled bool `json:"proxy_enabled" yaml:"proxy_enabled"`
  26. IsDefault bool `json:"isdefault" yaml:"isdefault"`
  27. IsRelayed bool `json:"isrelayed" bson:"isrelayed" yaml:"isrelayed"`
  28. RelayedBy string `json:"relayed_by" bson:"relayed_by" yaml:"relayed_by"`
  29. IsRelay bool `json:"isrelay" bson:"isrelay" yaml:"isrelay"`
  30. RelayedHosts []string `json:"relay_hosts" bson:"relay_hosts" yaml:"relay_hosts"`
  31. }
  32. // Host.ConvertNMHostToAPI - converts a Netmaker host to an API editable host
  33. func (h *Host) ConvertNMHostToAPI() *ApiHost {
  34. a := ApiHost{}
  35. a.Debug = h.Debug
  36. a.EndpointIP = h.EndpointIP.String()
  37. a.FirewallInUse = h.FirewallInUse
  38. a.ID = h.ID.String()
  39. a.Interfaces = h.Interfaces
  40. for i := range a.Interfaces {
  41. a.Interfaces[i].AddressString = a.Interfaces[i].Address.String()
  42. }
  43. a.DefaultInterface = h.DefaultInterface
  44. a.InternetGateway = h.InternetGateway.String()
  45. if isEmptyAddr(a.InternetGateway) {
  46. a.InternetGateway = ""
  47. }
  48. a.IsStatic = h.IsStatic
  49. a.ListenPort = h.ListenPort
  50. a.LocalRange = h.LocalRange.String()
  51. if isEmptyAddr(a.LocalRange) {
  52. a.LocalRange = ""
  53. }
  54. a.MTU = h.MTU
  55. a.MacAddress = h.MacAddress.String()
  56. a.Name = h.Name
  57. a.OS = h.OS
  58. a.Nodes = h.Nodes
  59. a.ProxyEnabled = h.ProxyEnabled
  60. a.ProxyListenPort = h.ProxyListenPort
  61. a.PublicKey = h.PublicKey.String()
  62. a.Verbosity = h.Verbosity
  63. a.Version = h.Version
  64. a.IsDefault = h.IsDefault
  65. a.IsRelay = h.IsRelay
  66. a.RelayedHosts = h.RelayedHosts
  67. a.IsRelayed = h.IsRelayed
  68. a.RelayedBy = h.RelayedBy
  69. return &a
  70. }
  71. // APIHost.ConvertAPIHostToNMHost - convert's a given apihost struct to
  72. // a Host struct
  73. func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *Host) *Host {
  74. h := Host{}
  75. h.ID = currentHost.ID
  76. h.HostPass = currentHost.HostPass
  77. h.DaemonInstalled = currentHost.DaemonInstalled
  78. h.EndpointIP = net.ParseIP(a.EndpointIP)
  79. h.Debug = a.Debug
  80. h.FirewallInUse = a.FirewallInUse
  81. h.IPForwarding = currentHost.IPForwarding
  82. h.Interface = currentHost.Interface
  83. h.Interfaces = currentHost.Interfaces
  84. h.DefaultInterface = currentHost.DefaultInterface
  85. h.InternetGateway = currentHost.InternetGateway
  86. h.IsDocker = currentHost.IsDocker
  87. h.IsK8S = currentHost.IsK8S
  88. h.IsStatic = a.IsStatic
  89. h.ListenPort = a.ListenPort
  90. h.ProxyListenPort = a.ProxyListenPort
  91. h.PublicListenPort = currentHost.PublicListenPort
  92. h.MTU = a.MTU
  93. h.MacAddress = currentHost.MacAddress
  94. h.PublicKey = currentHost.PublicKey
  95. h.Name = a.Name
  96. h.Version = currentHost.Version
  97. h.Verbosity = a.Verbosity
  98. h.Nodes = currentHost.Nodes
  99. h.TrafficKeyPublic = currentHost.TrafficKeyPublic
  100. h.OS = currentHost.OS
  101. h.RelayedBy = a.RelayedBy
  102. h.RelayedHosts = a.RelayedHosts
  103. h.IsRelay = a.IsRelay
  104. h.IsRelayed = a.IsRelayed
  105. if len(a.LocalRange) > 0 {
  106. _, localRange, err := net.ParseCIDR(a.LocalRange)
  107. if err == nil {
  108. h.LocalRange = *localRange
  109. }
  110. } else if !isEmptyAddr(currentHost.LocalRange.String()) {
  111. h.LocalRange = currentHost.LocalRange
  112. }
  113. h.ProxyEnabled = a.ProxyEnabled
  114. h.IsDefault = a.IsDefault
  115. return &h
  116. }