api_host.go 3.9 KB

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