api_host.go 4.5 KB

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