api_host.go 4.2 KB

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