api_host.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 []Iface `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. // Host.ConvertNMHostToAPI - converts a Netmaker host to an API editable host
  32. func (h *Host) ConvertNMHostToAPI() *ApiHost {
  33. a := ApiHost{}
  34. a.Debug = h.Debug
  35. a.EndpointIP = h.EndpointIP.String()
  36. a.FirewallInUse = h.FirewallInUse
  37. a.ID = h.ID.String()
  38. a.Interfaces = h.Interfaces
  39. for i := range a.Interfaces {
  40. a.Interfaces[i].AddressString = a.Interfaces[i].Address.String()
  41. }
  42. a.DefaultInterface = h.DefaultInterface
  43. a.IsStatic = h.IsStatic
  44. a.ListenPort = h.ListenPort
  45. a.MTU = h.MTU
  46. a.MacAddress = h.MacAddress.String()
  47. a.Name = h.Name
  48. a.OS = h.OS
  49. a.Nodes = h.Nodes
  50. a.WgPublicListenPort = h.WgPublicListenPort
  51. a.PublicKey = h.PublicKey.String()
  52. a.Verbosity = h.Verbosity
  53. a.Version = h.Version
  54. a.IsDefault = h.IsDefault
  55. a.NatType = h.NatType
  56. a.PersistentKeepalive = int(h.PersistentKeepalive.Seconds())
  57. a.AutoUpdate = h.AutoUpdate
  58. return &a
  59. }
  60. // APIHost.ConvertAPIHostToNMHost - convert's a given apihost struct to
  61. // a Host struct
  62. func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *Host) *Host {
  63. h := Host{}
  64. h.ID = currentHost.ID
  65. h.HostPass = currentHost.HostPass
  66. h.DaemonInstalled = currentHost.DaemonInstalled
  67. if len(a.EndpointIP) == 0 || strings.Contains(a.EndpointIP, "nil") {
  68. h.EndpointIP = currentHost.EndpointIP
  69. } else {
  70. h.EndpointIP = net.ParseIP(a.EndpointIP)
  71. }
  72. h.Debug = a.Debug
  73. h.FirewallInUse = a.FirewallInUse
  74. h.IPForwarding = currentHost.IPForwarding
  75. h.Interface = currentHost.Interface
  76. h.Interfaces = currentHost.Interfaces
  77. h.DefaultInterface = currentHost.DefaultInterface
  78. h.IsDocker = currentHost.IsDocker
  79. h.IsK8S = currentHost.IsK8S
  80. h.IsStatic = a.IsStatic
  81. h.ListenPort = a.ListenPort
  82. h.MTU = a.MTU
  83. h.MacAddress = currentHost.MacAddress
  84. h.PublicKey = currentHost.PublicKey
  85. h.Name = a.Name
  86. h.Version = currentHost.Version
  87. h.Verbosity = a.Verbosity
  88. h.Nodes = currentHost.Nodes
  89. h.TrafficKeyPublic = currentHost.TrafficKeyPublic
  90. h.OS = currentHost.OS
  91. h.IsDefault = a.IsDefault
  92. h.NatType = currentHost.NatType
  93. h.TurnEndpoint = currentHost.TurnEndpoint
  94. h.PersistentKeepalive = time.Duration(a.PersistentKeepalive) * time.Second
  95. h.AutoUpdate = a.AutoUpdate
  96. return &h
  97. }