api_host.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package models
  2. import (
  3. "net"
  4. "strings"
  5. )
  6. // ApiHost - the host struct for API usage
  7. type ApiHost struct {
  8. ID string `json:"id"`
  9. Verbosity int `json:"verbosity"`
  10. FirewallInUse string `json:"firewallinuse"`
  11. Version string `json:"version"`
  12. Name string `json:"name"`
  13. OS string `json:"os"`
  14. Debug bool `json:"debug"`
  15. IsStatic bool `json:"isstatic"`
  16. ListenPort int `json:"listenport"`
  17. WgPublicListenPort int `json:"wg_public_listen_port" yaml:"wg_public_listen_port"`
  18. MTU int `json:"mtu" yaml:"mtu"`
  19. Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
  20. DefaultInterface string `json:"defaultinterface" yaml:"defautlinterface"`
  21. EndpointIP string `json:"endpointip" yaml:"endpointip"`
  22. PublicKey string `json:"publickey"`
  23. MacAddress string `json:"macaddress"`
  24. Nodes []string `json:"nodes"`
  25. IsDefault bool `json:"isdefault" yaml:"isdefault"`
  26. IsRelayed bool `json:"isrelayed" bson:"isrelayed" yaml:"isrelayed"`
  27. RelayedBy string `json:"relayed_by" bson:"relayed_by" yaml:"relayed_by"`
  28. IsRelay bool `json:"isrelay" bson:"isrelay" yaml:"isrelay"`
  29. RelayedHosts []string `json:"relay_hosts" bson:"relay_hosts" yaml:"relay_hosts"`
  30. NatType string `json:"nat_type" yaml:"nat_type"`
  31. }
  32. // Host.ConvertNMHostToAPI - converts a Netmaker host to an API editable host
  33. func (h *Host) ConvertNMHostToAPI() *ApiHost {
  34. a := ApiHost{}
  35. a.Debug = h.Debug
  36. a.EndpointIP = h.EndpointIP.String()
  37. a.FirewallInUse = h.FirewallInUse
  38. a.ID = h.ID.String()
  39. a.Interfaces = h.Interfaces
  40. for i := range a.Interfaces {
  41. a.Interfaces[i].AddressString = a.Interfaces[i].Address.String()
  42. }
  43. a.DefaultInterface = h.DefaultInterface
  44. a.IsStatic = h.IsStatic
  45. a.ListenPort = h.ListenPort
  46. a.MTU = h.MTU
  47. a.MacAddress = h.MacAddress.String()
  48. a.Name = h.Name
  49. a.OS = h.OS
  50. a.Nodes = h.Nodes
  51. a.WgPublicListenPort = h.WgPublicListenPort
  52. a.PublicKey = h.PublicKey.String()
  53. a.Verbosity = h.Verbosity
  54. a.Version = h.Version
  55. a.IsDefault = h.IsDefault
  56. a.NatType = h.NatType
  57. return &a
  58. }
  59. // APIHost.ConvertAPIHostToNMHost - convert's a given apihost struct to
  60. // a Host struct
  61. func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *Host) *Host {
  62. h := Host{}
  63. h.ID = currentHost.ID
  64. h.HostPass = currentHost.HostPass
  65. h.DaemonInstalled = currentHost.DaemonInstalled
  66. if len(a.EndpointIP) == 0 || strings.Contains(a.EndpointIP, "nil") {
  67. h.EndpointIP = currentHost.EndpointIP
  68. } else {
  69. h.EndpointIP = net.ParseIP(a.EndpointIP)
  70. }
  71. h.Debug = a.Debug
  72. h.FirewallInUse = a.FirewallInUse
  73. h.IPForwarding = currentHost.IPForwarding
  74. h.Interface = currentHost.Interface
  75. h.Interfaces = currentHost.Interfaces
  76. h.DefaultInterface = currentHost.DefaultInterface
  77. h.IsDocker = currentHost.IsDocker
  78. h.IsK8S = currentHost.IsK8S
  79. h.IsStatic = a.IsStatic
  80. h.ListenPort = a.ListenPort
  81. h.MTU = a.MTU
  82. h.MacAddress = currentHost.MacAddress
  83. h.PublicKey = currentHost.PublicKey
  84. h.Name = a.Name
  85. h.Version = currentHost.Version
  86. h.Verbosity = a.Verbosity
  87. h.Nodes = currentHost.Nodes
  88. h.TrafficKeyPublic = currentHost.TrafficKeyPublic
  89. h.OS = currentHost.OS
  90. h.IsDefault = a.IsDefault
  91. h.NatType = currentHost.NatType
  92. h.TurnEndpoint = currentHost.TurnEndpoint
  93. return &h
  94. }