api_host.go 4.0 KB

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