2
0

api_host.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. if a.EndpointIP == "<nil>" {
  44. a.EndpointIP = ""
  45. }
  46. a.EndpointIPv6 = h.EndpointIPv6.String()
  47. if a.EndpointIPv6 == "<nil>" {
  48. a.EndpointIPv6 = ""
  49. }
  50. a.FirewallInUse = h.FirewallInUse
  51. a.ID = h.ID.String()
  52. a.Interfaces = make([]ApiIface, len(h.Interfaces))
  53. for i := range a.Interfaces {
  54. a.Interfaces[i] = ApiIface{
  55. Name: h.Interfaces[i].Name,
  56. AddressString: h.Interfaces[i].Address.String(),
  57. }
  58. }
  59. a.DefaultInterface = h.DefaultInterface
  60. a.IsStatic = h.IsStatic
  61. a.ListenPort = h.ListenPort
  62. a.MTU = h.MTU
  63. a.MacAddress = h.MacAddress.String()
  64. a.Name = h.Name
  65. a.OS = h.OS
  66. a.Nodes = h.Nodes
  67. a.WgPublicListenPort = h.WgPublicListenPort
  68. a.PublicKey = h.PublicKey.String()
  69. a.Verbosity = h.Verbosity
  70. a.Version = h.Version
  71. a.IsDefault = h.IsDefault
  72. a.NatType = h.NatType
  73. a.PersistentKeepalive = int(h.PersistentKeepalive.Seconds())
  74. a.AutoUpdate = h.AutoUpdate
  75. return &a
  76. }
  77. // APIHost.ConvertAPIHostToNMHost - convert's a given apihost struct to
  78. // a Host struct
  79. func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *Host) *Host {
  80. h := Host{}
  81. h.ID = currentHost.ID
  82. h.HostPass = currentHost.HostPass
  83. h.DaemonInstalled = currentHost.DaemonInstalled
  84. if len(a.EndpointIP) == 0 || strings.Contains(a.EndpointIP, "nil") {
  85. h.EndpointIP = currentHost.EndpointIP
  86. } else {
  87. h.EndpointIP = net.ParseIP(a.EndpointIP)
  88. }
  89. if len(a.EndpointIPv6) == 0 || strings.Contains(a.EndpointIPv6, "nil") {
  90. h.EndpointIPv6 = currentHost.EndpointIPv6
  91. } else {
  92. h.EndpointIPv6 = net.ParseIP(a.EndpointIPv6)
  93. }
  94. h.Debug = a.Debug
  95. h.FirewallInUse = a.FirewallInUse
  96. h.IPForwarding = currentHost.IPForwarding
  97. h.Interface = currentHost.Interface
  98. h.Interfaces = currentHost.Interfaces
  99. h.DefaultInterface = currentHost.DefaultInterface
  100. h.IsDocker = currentHost.IsDocker
  101. h.IsK8S = currentHost.IsK8S
  102. h.IsStatic = a.IsStatic
  103. h.ListenPort = a.ListenPort
  104. h.MTU = a.MTU
  105. h.MacAddress = currentHost.MacAddress
  106. h.PublicKey = currentHost.PublicKey
  107. h.Name = a.Name
  108. h.Version = currentHost.Version
  109. h.Verbosity = a.Verbosity
  110. h.Nodes = currentHost.Nodes
  111. h.TrafficKeyPublic = currentHost.TrafficKeyPublic
  112. h.OS = currentHost.OS
  113. h.IsDefault = a.IsDefault
  114. h.NatType = currentHost.NatType
  115. h.TurnEndpoint = currentHost.TurnEndpoint
  116. h.PersistentKeepalive = time.Duration(a.PersistentKeepalive) * time.Second
  117. h.AutoUpdate = a.AutoUpdate
  118. return &h
  119. }