api_host.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. LocalListenPort int `json:"locallistenport"`
  18. ProxyListenPort int `json:"proxy_listen_port"`
  19. PublicListenPort int `json:"public_listen_port" yaml:"public_listen_port"`
  20. MTU int `json:"mtu" yaml:"mtu"`
  21. Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
  22. DefaultInterface string `json:"defaultinterface" yaml:"defautlinterface"`
  23. EndpointIP string `json:"endpointip" yaml:"endpointip"`
  24. PublicKey string `json:"publickey"`
  25. MacAddress string `json:"macaddress"`
  26. InternetGateway string `json:"internetgateway"`
  27. Nodes []string `json:"nodes"`
  28. ProxyEnabled bool `json:"proxy_enabled" yaml:"proxy_enabled"`
  29. IsDefault bool `json:"isdefault" yaml:"isdefault"`
  30. IsRelayed bool `json:"isrelayed" bson:"isrelayed" yaml:"isrelayed"`
  31. RelayedBy string `json:"relayed_by" bson:"relayed_by" yaml:"relayed_by"`
  32. IsRelay bool `json:"isrelay" bson:"isrelay" yaml:"isrelay"`
  33. RelayedHosts []string `json:"relay_hosts" bson:"relay_hosts" yaml:"relay_hosts"`
  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.InternetGateway = h.InternetGateway.String()
  48. if isEmptyAddr(a.InternetGateway) {
  49. a.InternetGateway = ""
  50. }
  51. a.IsStatic = h.IsStatic
  52. a.ListenPort = h.ListenPort
  53. a.MTU = h.MTU
  54. a.MacAddress = h.MacAddress.String()
  55. a.Name = h.Name
  56. a.OS = h.OS
  57. a.Nodes = h.Nodes
  58. a.ProxyEnabled = h.ProxyEnabled
  59. a.PublicListenPort = h.PublicListenPort
  60. a.ProxyListenPort = h.ProxyListenPort
  61. a.PublicKey = h.PublicKey.String()
  62. a.Verbosity = h.Verbosity
  63. a.Version = h.Version
  64. a.IsDefault = h.IsDefault
  65. a.IsRelay = h.IsRelay
  66. a.RelayedHosts = h.RelayedHosts
  67. a.IsRelayed = h.IsRelayed
  68. a.RelayedBy = h.RelayedBy
  69. return &a
  70. }
  71. // APIHost.ConvertAPIHostToNMHost - convert's a given apihost struct to
  72. // a Host struct
  73. func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *Host) *Host {
  74. h := Host{}
  75. h.ID = currentHost.ID
  76. h.HostPass = currentHost.HostPass
  77. h.DaemonInstalled = currentHost.DaemonInstalled
  78. if len(a.EndpointIP) == 0 || strings.Contains(a.EndpointIP, "nil") {
  79. h.EndpointIP = currentHost.EndpointIP
  80. } else {
  81. h.EndpointIP = net.ParseIP(a.EndpointIP)
  82. }
  83. h.Debug = a.Debug
  84. h.FirewallInUse = a.FirewallInUse
  85. h.IPForwarding = currentHost.IPForwarding
  86. h.Interface = currentHost.Interface
  87. h.Interfaces = currentHost.Interfaces
  88. h.DefaultInterface = currentHost.DefaultInterface
  89. h.InternetGateway = currentHost.InternetGateway
  90. h.IsDocker = currentHost.IsDocker
  91. h.IsK8S = currentHost.IsK8S
  92. h.IsStatic = a.IsStatic
  93. h.ListenPort = a.ListenPort
  94. h.ProxyListenPort = a.ProxyListenPort
  95. h.PublicListenPort = currentHost.PublicListenPort
  96. h.MTU = a.MTU
  97. h.MacAddress = currentHost.MacAddress
  98. h.PublicKey = currentHost.PublicKey
  99. h.Name = a.Name
  100. h.Version = currentHost.Version
  101. h.Verbosity = a.Verbosity
  102. h.Nodes = currentHost.Nodes
  103. h.TrafficKeyPublic = currentHost.TrafficKeyPublic
  104. h.OS = currentHost.OS
  105. h.RelayedBy = a.RelayedBy
  106. h.RelayedHosts = a.RelayedHosts
  107. h.IsRelay = a.IsRelay
  108. h.IsRelayed = a.IsRelayed
  109. h.ProxyEnabled = a.ProxyEnabled
  110. h.IsDefault = a.IsDefault
  111. return &h
  112. }