api_host.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. MTU int `json:"mtu" yaml:"mtu"`
  17. Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
  18. DefaultInterface string `json:"defaultinterface" yaml:"defautlinterface"`
  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.DefaultInterface = h.DefaultInterface
  43. a.InternetGateway = h.InternetGateway.String()
  44. if isEmptyAddr(a.InternetGateway) {
  45. a.InternetGateway = ""
  46. }
  47. a.IsStatic = h.IsStatic
  48. a.ListenPort = h.ListenPort
  49. a.MTU = h.MTU
  50. a.MacAddress = h.MacAddress.String()
  51. a.Name = h.Name
  52. a.OS = h.OS
  53. a.Nodes = h.Nodes
  54. a.ProxyEnabled = h.ProxyEnabled
  55. a.ProxyListenPort = h.ProxyListenPort
  56. a.PublicKey = h.PublicKey.String()
  57. a.Verbosity = h.Verbosity
  58. a.Version = h.Version
  59. a.IsDefault = h.IsDefault
  60. a.IsRelay = h.IsRelay
  61. a.RelayedHosts = h.RelayedHosts
  62. a.IsRelayed = h.IsRelayed
  63. a.RelayedBy = h.RelayedBy
  64. return &a
  65. }
  66. // APIHost.ConvertAPIHostToNMHost - convert's a given apihost struct to
  67. // a Host struct
  68. func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *Host) *Host {
  69. h := Host{}
  70. h.ID = currentHost.ID
  71. h.HostPass = currentHost.HostPass
  72. h.DaemonInstalled = currentHost.DaemonInstalled
  73. h.EndpointIP = net.ParseIP(a.EndpointIP)
  74. h.Debug = a.Debug
  75. h.FirewallInUse = a.FirewallInUse
  76. h.IPForwarding = currentHost.IPForwarding
  77. h.Interface = currentHost.Interface
  78. h.Interfaces = currentHost.Interfaces
  79. h.DefaultInterface = currentHost.DefaultInterface
  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.ProxyListenPort = a.ProxyListenPort
  86. h.PublicListenPort = currentHost.PublicListenPort
  87. h.MTU = a.MTU
  88. h.MacAddress = currentHost.MacAddress
  89. h.PublicKey = currentHost.PublicKey
  90. h.Name = a.Name
  91. h.Version = currentHost.Version
  92. h.Verbosity = a.Verbosity
  93. h.Nodes = currentHost.Nodes
  94. h.TrafficKeyPublic = currentHost.TrafficKeyPublic
  95. h.OS = currentHost.OS
  96. h.RelayedBy = a.RelayedBy
  97. h.RelayedHosts = a.RelayedHosts
  98. h.IsRelay = a.IsRelay
  99. h.IsRelayed = a.IsRelayed
  100. h.ProxyEnabled = a.ProxyEnabled
  101. h.IsDefault = a.IsDefault
  102. return &h
  103. }