api_host.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. IsStaticPort bool `json:"isstaticport"`
  17. IsStatic bool `json:"isstatic"`
  18. ListenPort int `json:"listenport"`
  19. WgPublicListenPort int `json:"wg_public_listen_port" yaml:"wg_public_listen_port"`
  20. MTU int `json:"mtu" yaml:"mtu"`
  21. Interfaces []ApiIface `json:"interfaces" yaml:"interfaces"`
  22. DefaultInterface string `json:"defaultinterface" yaml:"defautlinterface"`
  23. EndpointIP string `json:"endpointip" yaml:"endpointip"`
  24. EndpointIPv6 string `json:"endpointipv6" yaml:"endpointipv6"`
  25. PublicKey string `json:"publickey"`
  26. MacAddress string `json:"macaddress"`
  27. Nodes []string `json:"nodes"`
  28. IsDefault bool `json:"isdefault" yaml:"isdefault"`
  29. NatType string `json:"nat_type" yaml:"nat_type"`
  30. PersistentKeepalive int `json:"persistentkeepalive" yaml:"persistentkeepalive"`
  31. AutoUpdate bool `json:"autoupdate" yaml:"autoupdate"`
  32. DNS string `json:"dns" yaml:"dns"`
  33. Location string `json:"location"`
  34. }
  35. // ApiIface - the interface struct for API usage
  36. // The original Iface struct contains a net.Address, which does not get marshalled correctly
  37. type ApiIface struct {
  38. Name string `json:"name"`
  39. AddressString string `json:"addressString"`
  40. }
  41. // Host.ConvertNMHostToAPI - converts a Netmaker host to an API editable host
  42. func (h *Host) ConvertNMHostToAPI() *ApiHost {
  43. a := ApiHost{}
  44. a.Debug = h.Debug
  45. a.EndpointIP = h.EndpointIP.String()
  46. if a.EndpointIP == "<nil>" {
  47. a.EndpointIP = ""
  48. }
  49. a.EndpointIPv6 = h.EndpointIPv6.String()
  50. if a.EndpointIPv6 == "<nil>" {
  51. a.EndpointIPv6 = ""
  52. }
  53. a.FirewallInUse = h.FirewallInUse
  54. a.ID = h.ID.String()
  55. a.Interfaces = make([]ApiIface, len(h.Interfaces))
  56. for i := range a.Interfaces {
  57. a.Interfaces[i] = ApiIface{
  58. Name: h.Interfaces[i].Name,
  59. AddressString: h.Interfaces[i].Address.String(),
  60. }
  61. }
  62. a.DefaultInterface = h.DefaultInterface
  63. a.IsStaticPort = h.IsStaticPort
  64. a.IsStatic = h.IsStatic
  65. a.ListenPort = h.ListenPort
  66. a.MTU = h.MTU
  67. a.MacAddress = h.MacAddress.String()
  68. a.Name = h.Name
  69. a.OS = h.OS
  70. a.Nodes = h.Nodes
  71. a.WgPublicListenPort = h.WgPublicListenPort
  72. a.PublicKey = h.PublicKey.String()
  73. a.Verbosity = h.Verbosity
  74. a.Version = h.Version
  75. a.IsDefault = h.IsDefault
  76. a.NatType = h.NatType
  77. a.PersistentKeepalive = int(h.PersistentKeepalive.Seconds())
  78. a.AutoUpdate = h.AutoUpdate
  79. a.DNS = h.DNS
  80. a.Location = h.Location
  81. return &a
  82. }
  83. // APIHost.ConvertAPIHostToNMHost - convert's a given apihost struct to
  84. // a Host struct
  85. func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *Host) *Host {
  86. h := Host{}
  87. h.ID = currentHost.ID
  88. h.HostPass = currentHost.HostPass
  89. h.DaemonInstalled = currentHost.DaemonInstalled
  90. if len(a.EndpointIP) == 0 || strings.Contains(a.EndpointIP, "nil") {
  91. h.EndpointIP = currentHost.EndpointIP
  92. } else {
  93. h.EndpointIP = net.ParseIP(a.EndpointIP)
  94. }
  95. if len(a.EndpointIPv6) == 0 || strings.Contains(a.EndpointIPv6, "nil") {
  96. h.EndpointIPv6 = currentHost.EndpointIPv6
  97. } else {
  98. h.EndpointIPv6 = net.ParseIP(a.EndpointIPv6)
  99. }
  100. h.Debug = a.Debug
  101. h.FirewallInUse = a.FirewallInUse
  102. h.IPForwarding = currentHost.IPForwarding
  103. h.Interface = currentHost.Interface
  104. h.Interfaces = currentHost.Interfaces
  105. h.DefaultInterface = currentHost.DefaultInterface
  106. h.IsDocker = currentHost.IsDocker
  107. h.IsK8S = currentHost.IsK8S
  108. h.IsStaticPort = a.IsStaticPort
  109. h.IsStatic = a.IsStatic
  110. h.ListenPort = a.ListenPort
  111. h.MTU = a.MTU
  112. h.MacAddress = currentHost.MacAddress
  113. h.PublicKey = currentHost.PublicKey
  114. h.Name = a.Name
  115. h.Version = currentHost.Version
  116. h.Verbosity = a.Verbosity
  117. h.Nodes = currentHost.Nodes
  118. h.TrafficKeyPublic = currentHost.TrafficKeyPublic
  119. h.OS = currentHost.OS
  120. h.IsDefault = a.IsDefault
  121. h.NatType = currentHost.NatType
  122. h.TurnEndpoint = currentHost.TurnEndpoint
  123. h.PersistentKeepalive = time.Duration(a.PersistentKeepalive) * time.Second
  124. h.AutoUpdate = a.AutoUpdate
  125. h.DNS = strings.ToLower(a.DNS)
  126. h.Location = currentHost.Location
  127. return &h
  128. }