api_host.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package models
  2. import (
  3. "net"
  4. "strings"
  5. "time"
  6. )
  7. // ApiHost - the host struct for API usage
  8. type ApiHost struct {
  9. ID string `json:"id"`
  10. Verbosity int `json:"verbosity"`
  11. FirewallInUse string `json:"firewallinuse"`
  12. Version string `json:"version"`
  13. Name string `json:"name"`
  14. OS string `json:"os"`
  15. Debug bool `json:"debug"`
  16. IsStatic bool `json:"isstatic"`
  17. ListenPort int `json:"listenport"`
  18. WgPublicListenPort int `json:"wg_public_listen_port" yaml:"wg_public_listen_port"`
  19. MTU int `json:"mtu" yaml:"mtu"`
  20. Interfaces []ApiIface `json:"interfaces" yaml:"interfaces"`
  21. DefaultInterface string `json:"defaultinterface" yaml:"defautlinterface"`
  22. EndpointIP string `json:"endpointip" yaml:"endpointip"`
  23. EndpointIPv6 string `json:"endpointipv6" yaml:"endpointipv6"`
  24. PublicKey string `json:"publickey"`
  25. MacAddress string `json:"macaddress"`
  26. Nodes []string `json:"nodes"`
  27. IsDefault bool `json:"isdefault" yaml:"isdefault"`
  28. NatType string `json:"nat_type" yaml:"nat_type"`
  29. PersistentKeepalive int `json:"persistentkeepalive" yaml:"persistentkeepalive"`
  30. AutoUpdate bool `json:"autoupdate" yaml:"autoupdate"`
  31. }
  32. // ApiIface - the interface struct for API usage
  33. // The original Iface struct contains a net.Address, which does not get marshalled correctly
  34. type ApiIface struct {
  35. Name string `json:"name"`
  36. AddressString string `json:"addressString"`
  37. }
  38. // Host.ConvertNMHostToAPI - converts a Netmaker host to an API editable host
  39. func (h *Host) ConvertNMHostToAPI() *ApiHost {
  40. a := ApiHost{}
  41. a.Debug = h.Debug
  42. a.EndpointIP = h.EndpointIP.String()
  43. a.EndpointIPv6 = h.EndpointIPv6.String()
  44. a.FirewallInUse = h.FirewallInUse
  45. a.ID = h.ID.String()
  46. a.Interfaces = make([]ApiIface, len(h.Interfaces))
  47. for i := range a.Interfaces {
  48. a.Interfaces[i] = ApiIface{
  49. Name: h.Interfaces[i].Name,
  50. AddressString: h.Interfaces[i].Address.String(),
  51. }
  52. }
  53. a.DefaultInterface = h.DefaultInterface
  54. a.IsStatic = h.IsStatic
  55. a.ListenPort = h.ListenPort
  56. a.MTU = h.MTU
  57. a.MacAddress = h.MacAddress.String()
  58. a.Name = h.Name
  59. a.OS = h.OS
  60. a.Nodes = h.Nodes
  61. a.WgPublicListenPort = h.WgPublicListenPort
  62. a.PublicKey = h.PublicKey.String()
  63. a.Verbosity = h.Verbosity
  64. a.Version = h.Version
  65. a.IsDefault = h.IsDefault
  66. a.NatType = h.NatType
  67. a.PersistentKeepalive = int(h.PersistentKeepalive.Seconds())
  68. a.AutoUpdate = h.AutoUpdate
  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. if len(a.EndpointIP) == 0 || strings.Contains(a.EndpointIP, "nil") {
  79. h.EndpointIP = currentHost.EndpointIP
  80. } else {
  81. h.EndpointIP = net.ParseIP(a.EndpointIP)
  82. }
  83. if len(a.EndpointIPv6) == 0 || strings.Contains(a.EndpointIPv6, "nil") {
  84. h.EndpointIPv6 = currentHost.EndpointIPv6
  85. } else {
  86. h.EndpointIPv6 = net.ParseIP(a.EndpointIPv6)
  87. }
  88. h.Debug = a.Debug
  89. h.FirewallInUse = a.FirewallInUse
  90. h.IPForwarding = currentHost.IPForwarding
  91. h.Interface = currentHost.Interface
  92. h.Interfaces = currentHost.Interfaces
  93. h.DefaultInterface = currentHost.DefaultInterface
  94. h.IsDocker = currentHost.IsDocker
  95. h.IsK8S = currentHost.IsK8S
  96. h.IsStatic = a.IsStatic
  97. h.ListenPort = a.ListenPort
  98. h.MTU = a.MTU
  99. h.MacAddress = currentHost.MacAddress
  100. h.PublicKey = currentHost.PublicKey
  101. h.Name = a.Name
  102. h.Version = currentHost.Version
  103. h.Verbosity = a.Verbosity
  104. h.Nodes = currentHost.Nodes
  105. h.TrafficKeyPublic = currentHost.TrafficKeyPublic
  106. h.OS = currentHost.OS
  107. h.IsDefault = a.IsDefault
  108. h.NatType = currentHost.NatType
  109. h.TurnEndpoint = currentHost.TurnEndpoint
  110. h.PersistentKeepalive = time.Duration(a.PersistentKeepalive) * time.Second
  111. h.AutoUpdate = a.AutoUpdate
  112. return &h
  113. }