api_host.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. LocalListenPort int `json:"locallistenport"`
  15. ProxyListenPort int `json:"proxy_listen_port"`
  16. PublicListenPort int `json:"public_listen_port" yaml:"public_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.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.PublicListenPort = h.PublicListenPort
  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.DefaultInterface = currentHost.DefaultInterface
  82. h.InternetGateway = currentHost.InternetGateway
  83. h.IsDocker = currentHost.IsDocker
  84. h.IsK8S = currentHost.IsK8S
  85. h.IsStatic = a.IsStatic
  86. h.ListenPort = a.ListenPort
  87. h.ProxyListenPort = a.ProxyListenPort
  88. h.PublicListenPort = currentHost.PublicListenPort
  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. h.ProxyEnabled = a.ProxyEnabled
  103. h.IsDefault = a.IsDefault
  104. return &h
  105. }