api_host.go 3.8 KB

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