api_host.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. InternetGateway string `json:"internetgateway"`
  25. Nodes []string `json:"nodes"`
  26. IsDefault bool `json:"isdefault" yaml:"isdefault"`
  27. IsRelayed bool `json:"isrelayed" bson:"isrelayed" yaml:"isrelayed"`
  28. RelayedBy string `json:"relayed_by" bson:"relayed_by" yaml:"relayed_by"`
  29. IsRelay bool `json:"isrelay" bson:"isrelay" yaml:"isrelay"`
  30. RelayedHosts []string `json:"relay_hosts" bson:"relay_hosts" yaml:"relay_hosts"`
  31. NatType string `json:"nat_type" yaml:"nat_type"`
  32. }
  33. // Host.ConvertNMHostToAPI - converts a Netmaker host to an API editable host
  34. func (h *Host) ConvertNMHostToAPI() *ApiHost {
  35. a := ApiHost{}
  36. a.Debug = h.Debug
  37. a.EndpointIP = h.EndpointIP.String()
  38. a.FirewallInUse = h.FirewallInUse
  39. a.ID = h.ID.String()
  40. a.Interfaces = h.Interfaces
  41. for i := range a.Interfaces {
  42. a.Interfaces[i].AddressString = a.Interfaces[i].Address.String()
  43. }
  44. a.DefaultInterface = h.DefaultInterface
  45. a.InternetGateway = h.InternetGateway.String()
  46. if isEmptyAddr(a.InternetGateway) {
  47. a.InternetGateway = ""
  48. }
  49. a.IsStatic = h.IsStatic
  50. a.ListenPort = h.ListenPort
  51. a.MTU = h.MTU
  52. a.MacAddress = h.MacAddress.String()
  53. a.Name = h.Name
  54. a.OS = h.OS
  55. a.Nodes = h.Nodes
  56. a.WgPublicListenPort = h.WgPublicListenPort
  57. a.PublicKey = h.PublicKey.String()
  58. a.Verbosity = h.Verbosity
  59. a.Version = h.Version
  60. a.IsDefault = h.IsDefault
  61. a.NatType = h.NatType
  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.InternetGateway = currentHost.InternetGateway
  83. h.IsDocker = currentHost.IsDocker
  84. h.IsK8S = currentHost.IsK8S
  85. h.IsStatic = a.IsStatic
  86. h.ListenPort = a.ListenPort
  87. h.MTU = a.MTU
  88. h.MacAddress = currentHost.MacAddress
  89. h.PublicKey = currentHost.PublicKey
  90. h.Name = a.Name
  91. h.Version = currentHost.Version
  92. h.Verbosity = a.Verbosity
  93. h.Nodes = currentHost.Nodes
  94. h.TrafficKeyPublic = currentHost.TrafficKeyPublic
  95. h.OS = currentHost.OS
  96. h.IsDefault = a.IsDefault
  97. h.NatType = currentHost.NatType
  98. h.TurnEndpoint = currentHost.TurnEndpoint
  99. return &h
  100. }