hosts.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. package logic
  2. import (
  3. "crypto/md5"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "os"
  8. "sort"
  9. "sync"
  10. "github.com/google/uuid"
  11. "golang.org/x/crypto/bcrypt"
  12. "golang.org/x/exp/slog"
  13. "github.com/gravitl/netmaker/database"
  14. "github.com/gravitl/netmaker/logger"
  15. "github.com/gravitl/netmaker/models"
  16. "github.com/gravitl/netmaker/servercfg"
  17. "github.com/gravitl/netmaker/utils"
  18. )
  19. var (
  20. hostCacheMutex = &sync.RWMutex{}
  21. hostsCacheMap = make(map[string]models.Host)
  22. )
  23. var (
  24. // ErrHostExists error indicating that host exists when trying to create new host
  25. ErrHostExists error = errors.New("host already exists")
  26. // ErrInvalidHostID
  27. ErrInvalidHostID error = errors.New("invalid host id")
  28. )
  29. var GetHostLocInfo = func(ip, token string) string { return "" }
  30. func getHostsFromCache() (hosts []models.Host) {
  31. hostCacheMutex.RLock()
  32. for _, host := range hostsCacheMap {
  33. hosts = append(hosts, host)
  34. }
  35. hostCacheMutex.RUnlock()
  36. return
  37. }
  38. func getHostsMapFromCache() (hostsMap map[string]models.Host) {
  39. hostCacheMutex.RLock()
  40. hostsMap = hostsCacheMap
  41. hostCacheMutex.RUnlock()
  42. return
  43. }
  44. func getHostFromCache(hostID string) (host models.Host, ok bool) {
  45. hostCacheMutex.RLock()
  46. host, ok = hostsCacheMap[hostID]
  47. hostCacheMutex.RUnlock()
  48. return
  49. }
  50. func storeHostInCache(h models.Host) {
  51. hostCacheMutex.Lock()
  52. hostsCacheMap[h.ID.String()] = h
  53. hostCacheMutex.Unlock()
  54. }
  55. func deleteHostFromCache(hostID string) {
  56. hostCacheMutex.Lock()
  57. delete(hostsCacheMap, hostID)
  58. hostCacheMutex.Unlock()
  59. }
  60. func loadHostsIntoCache(hMap map[string]models.Host) {
  61. hostCacheMutex.Lock()
  62. hostsCacheMap = hMap
  63. hostCacheMutex.Unlock()
  64. }
  65. const (
  66. maxPort = 1<<16 - 1
  67. minPort = 1025
  68. )
  69. // GetAllHosts - returns all hosts in flat list or error
  70. func GetAllHosts() ([]models.Host, error) {
  71. var currHosts []models.Host
  72. if servercfg.CacheEnabled() {
  73. currHosts := getHostsFromCache()
  74. if len(currHosts) != 0 {
  75. return currHosts, nil
  76. }
  77. }
  78. records, err := database.FetchRecords(database.HOSTS_TABLE_NAME)
  79. if err != nil && !database.IsEmptyRecord(err) {
  80. return nil, err
  81. }
  82. currHostsMap := make(map[string]models.Host)
  83. if servercfg.CacheEnabled() {
  84. defer loadHostsIntoCache(currHostsMap)
  85. }
  86. for k := range records {
  87. var h models.Host
  88. err = json.Unmarshal([]byte(records[k]), &h)
  89. if err != nil {
  90. return nil, err
  91. }
  92. currHosts = append(currHosts, h)
  93. currHostsMap[h.ID.String()] = h
  94. }
  95. return currHosts, nil
  96. }
  97. // GetAllHostsWithStatus - returns all hosts with at least one
  98. // node with given status.
  99. func GetAllHostsWithStatus(status models.NodeStatus) ([]models.Host, error) {
  100. hosts, err := GetAllHosts()
  101. if err != nil {
  102. return nil, err
  103. }
  104. var validHosts []models.Host
  105. for _, host := range hosts {
  106. if len(host.Nodes) == 0 {
  107. continue
  108. }
  109. nodes := GetHostNodes(&host)
  110. for _, node := range nodes {
  111. getNodeCheckInStatus(&node, false)
  112. if node.Status == status {
  113. validHosts = append(validHosts, host)
  114. break
  115. }
  116. }
  117. }
  118. return validHosts, nil
  119. }
  120. // GetAllHostsAPI - get's all the hosts in an API usable format
  121. func GetAllHostsAPI(hosts []models.Host) []models.ApiHost {
  122. apiHosts := []models.ApiHost{}
  123. for i := range hosts {
  124. newApiHost := hosts[i].ConvertNMHostToAPI()
  125. apiHosts = append(apiHosts, *newApiHost)
  126. }
  127. return apiHosts[:]
  128. }
  129. // GetHostsMap - gets all the current hosts on machine in a map
  130. func GetHostsMap() (map[string]models.Host, error) {
  131. if servercfg.CacheEnabled() {
  132. hostsMap := getHostsMapFromCache()
  133. if len(hostsMap) != 0 {
  134. return hostsMap, nil
  135. }
  136. }
  137. records, err := database.FetchRecords(database.HOSTS_TABLE_NAME)
  138. if err != nil && !database.IsEmptyRecord(err) {
  139. return nil, err
  140. }
  141. currHostMap := make(map[string]models.Host)
  142. if servercfg.CacheEnabled() {
  143. defer loadHostsIntoCache(currHostMap)
  144. }
  145. for k := range records {
  146. var h models.Host
  147. err = json.Unmarshal([]byte(records[k]), &h)
  148. if err != nil {
  149. return nil, err
  150. }
  151. currHostMap[h.ID.String()] = h
  152. }
  153. return currHostMap, nil
  154. }
  155. func DoesHostExistinTheNetworkAlready(h *models.Host, network models.NetworkID) bool {
  156. if len(h.Nodes) > 0 {
  157. for _, nodeID := range h.Nodes {
  158. node, err := GetNodeByID(nodeID)
  159. if err == nil && node.Network == network.String() {
  160. return true
  161. }
  162. }
  163. }
  164. return false
  165. }
  166. // GetHost - gets a host from db given id
  167. func GetHost(hostid string) (*models.Host, error) {
  168. if servercfg.CacheEnabled() {
  169. if host, ok := getHostFromCache(hostid); ok {
  170. return &host, nil
  171. }
  172. }
  173. record, err := database.FetchRecord(database.HOSTS_TABLE_NAME, hostid)
  174. if err != nil {
  175. return nil, err
  176. }
  177. var h models.Host
  178. if err = json.Unmarshal([]byte(record), &h); err != nil {
  179. return nil, err
  180. }
  181. if servercfg.CacheEnabled() {
  182. storeHostInCache(h)
  183. }
  184. return &h, nil
  185. }
  186. // GetHostByPubKey - gets a host from db given pubkey
  187. func GetHostByPubKey(hostPubKey string) (*models.Host, error) {
  188. hosts, err := GetAllHosts()
  189. if err != nil {
  190. return nil, err
  191. }
  192. for _, host := range hosts {
  193. if host.PublicKey.String() == hostPubKey {
  194. return &host, nil
  195. }
  196. }
  197. return nil, errors.New("host not found")
  198. }
  199. // CreateHost - creates a host if not exist
  200. func CreateHost(h *models.Host) error {
  201. hosts, hErr := GetAllHosts()
  202. clients, cErr := GetAllExtClients()
  203. if (hErr != nil && !database.IsEmptyRecord(hErr)) ||
  204. (cErr != nil && !database.IsEmptyRecord(cErr)) ||
  205. len(hosts)+len(clients) >= MachinesLimit {
  206. return errors.New("free tier limits exceeded on machines")
  207. }
  208. _, err := GetHost(h.ID.String())
  209. if (err != nil && !database.IsEmptyRecord(err)) || (err == nil) {
  210. return ErrHostExists
  211. }
  212. // encrypt that password so we never see it
  213. hash, err := bcrypt.GenerateFromPassword([]byte(h.HostPass), 5)
  214. if err != nil {
  215. return err
  216. }
  217. h.HostPass = string(hash)
  218. h.AutoUpdate = AutoUpdateEnabled()
  219. if GetServerSettings().ManageDNS {
  220. h.DNS = "yes"
  221. } else {
  222. h.DNS = "no"
  223. }
  224. if h.EndpointIP != nil {
  225. h.Location = GetHostLocInfo(h.EndpointIP.String(), os.Getenv("IP_INFO_TOKEN"))
  226. } else if h.EndpointIPv6 != nil {
  227. h.Location = GetHostLocInfo(h.EndpointIPv6.String(), os.Getenv("IP_INFO_TOKEN"))
  228. }
  229. checkForZombieHosts(h)
  230. return UpsertHost(h)
  231. }
  232. // UpdateHost - updates host data by field
  233. func UpdateHost(newHost, currentHost *models.Host) {
  234. // unchangeable fields via API here
  235. newHost.DaemonInstalled = currentHost.DaemonInstalled
  236. newHost.OS = currentHost.OS
  237. newHost.IPForwarding = currentHost.IPForwarding
  238. newHost.HostPass = currentHost.HostPass
  239. newHost.MacAddress = currentHost.MacAddress
  240. newHost.Debug = currentHost.Debug
  241. newHost.Nodes = currentHost.Nodes
  242. newHost.PublicKey = currentHost.PublicKey
  243. newHost.TrafficKeyPublic = currentHost.TrafficKeyPublic
  244. // changeable fields
  245. if len(newHost.Version) == 0 {
  246. newHost.Version = currentHost.Version
  247. }
  248. if len(newHost.Name) == 0 {
  249. newHost.Name = currentHost.Name
  250. }
  251. if newHost.MTU == 0 {
  252. newHost.MTU = currentHost.MTU
  253. }
  254. if newHost.ListenPort == 0 {
  255. newHost.ListenPort = currentHost.ListenPort
  256. }
  257. if newHost.PersistentKeepalive == 0 {
  258. newHost.PersistentKeepalive = currentHost.PersistentKeepalive
  259. }
  260. }
  261. // UpdateHostFromClient - used for updating host on server with update recieved from client
  262. func UpdateHostFromClient(newHost, currHost *models.Host) (sendPeerUpdate bool) {
  263. if newHost.PublicKey != currHost.PublicKey {
  264. currHost.PublicKey = newHost.PublicKey
  265. sendPeerUpdate = true
  266. }
  267. if newHost.ListenPort != 0 && currHost.ListenPort != newHost.ListenPort {
  268. currHost.ListenPort = newHost.ListenPort
  269. sendPeerUpdate = true
  270. }
  271. if newHost.WgPublicListenPort != 0 &&
  272. currHost.WgPublicListenPort != newHost.WgPublicListenPort {
  273. currHost.WgPublicListenPort = newHost.WgPublicListenPort
  274. sendPeerUpdate = true
  275. }
  276. isEndpointChanged := false
  277. if !currHost.EndpointIP.Equal(newHost.EndpointIP) {
  278. currHost.EndpointIP = newHost.EndpointIP
  279. sendPeerUpdate = true
  280. isEndpointChanged = true
  281. }
  282. if !currHost.EndpointIPv6.Equal(newHost.EndpointIPv6) {
  283. currHost.EndpointIPv6 = newHost.EndpointIPv6
  284. sendPeerUpdate = true
  285. isEndpointChanged = true
  286. }
  287. for i := range newHost.Interfaces {
  288. newHost.Interfaces[i].AddressString = newHost.Interfaces[i].Address.String()
  289. }
  290. utils.SortIfacesByName(currHost.Interfaces)
  291. utils.SortIfacesByName(newHost.Interfaces)
  292. if !utils.CompareIfaces(currHost.Interfaces, newHost.Interfaces) {
  293. currHost.Interfaces = newHost.Interfaces
  294. sendPeerUpdate = true
  295. }
  296. if isEndpointChanged {
  297. for _, nodeID := range currHost.Nodes {
  298. node, err := GetNodeByID(nodeID)
  299. if err != nil {
  300. slog.Error("failed to get node:", "id", node.ID, "error", err)
  301. continue
  302. }
  303. if node.FailedOverBy != uuid.Nil {
  304. ResetFailedOverPeer(&node)
  305. }
  306. }
  307. }
  308. currHost.DaemonInstalled = newHost.DaemonInstalled
  309. currHost.Debug = newHost.Debug
  310. currHost.Verbosity = newHost.Verbosity
  311. currHost.Version = newHost.Version
  312. currHost.IsStaticPort = newHost.IsStaticPort
  313. currHost.IsStatic = newHost.IsStatic
  314. currHost.MTU = newHost.MTU
  315. currHost.Name = newHost.Name
  316. if len(newHost.NatType) > 0 && newHost.NatType != currHost.NatType {
  317. currHost.NatType = newHost.NatType
  318. sendPeerUpdate = true
  319. }
  320. return
  321. }
  322. // UpsertHost - upserts into DB a given host model, does not check for existence*
  323. func UpsertHost(h *models.Host) error {
  324. data, err := json.Marshal(h)
  325. if err != nil {
  326. return err
  327. }
  328. err = database.Insert(h.ID.String(), string(data), database.HOSTS_TABLE_NAME)
  329. if err != nil {
  330. return err
  331. }
  332. if servercfg.CacheEnabled() {
  333. storeHostInCache(*h)
  334. }
  335. return nil
  336. }
  337. // UpdateHostNode - handles updates from client nodes
  338. func UpdateHostNode(h *models.Host, newNode *models.Node) (publishDeletedNodeUpdate, publishPeerUpdate bool) {
  339. currentNode, err := GetNodeByID(newNode.ID.String())
  340. if err != nil {
  341. return
  342. }
  343. ifaceDelta := IfaceDelta(&currentNode, newNode)
  344. newNode.SetLastCheckIn()
  345. if err := UpdateNode(&currentNode, newNode); err != nil {
  346. slog.Error("error saving node", "name", h.Name, "network", newNode.Network, "error", err)
  347. return
  348. }
  349. if ifaceDelta { // reduce number of unneeded updates, by only sending on iface changes
  350. if !newNode.Connected {
  351. publishDeletedNodeUpdate = true
  352. }
  353. publishPeerUpdate = true
  354. // reset failover data for this node
  355. ResetFailedOverPeer(newNode)
  356. }
  357. return
  358. }
  359. // RemoveHost - removes a given host from server
  360. func RemoveHost(h *models.Host, forceDelete bool) error {
  361. if !forceDelete && len(h.Nodes) > 0 {
  362. return fmt.Errorf("host still has associated nodes")
  363. }
  364. if len(h.Nodes) > 0 {
  365. if err := DisassociateAllNodesFromHost(h.ID.String()); err != nil {
  366. return err
  367. }
  368. }
  369. err := database.DeleteRecord(database.HOSTS_TABLE_NAME, h.ID.String())
  370. if err != nil {
  371. return err
  372. }
  373. if servercfg.CacheEnabled() {
  374. deleteHostFromCache(h.ID.String())
  375. }
  376. go func() {
  377. if servercfg.IsDNSMode() {
  378. SetDNS()
  379. }
  380. }()
  381. return nil
  382. }
  383. // RemoveHostByID - removes a given host by id from server
  384. func RemoveHostByID(hostID string) error {
  385. err := database.DeleteRecord(database.HOSTS_TABLE_NAME, hostID)
  386. if err != nil {
  387. return err
  388. }
  389. if servercfg.CacheEnabled() {
  390. deleteHostFromCache(hostID)
  391. }
  392. return nil
  393. }
  394. // UpdateHostNetwork - adds/deletes host from a network
  395. func UpdateHostNetwork(h *models.Host, network string, add bool) (*models.Node, error) {
  396. for _, nodeID := range h.Nodes {
  397. node, err := GetNodeByID(nodeID)
  398. if err != nil || node.PendingDelete {
  399. continue
  400. }
  401. if node.Network == network {
  402. if !add {
  403. return &node, nil
  404. } else {
  405. return &node, errors.New("host already part of network " + network)
  406. }
  407. }
  408. }
  409. if !add {
  410. return nil, errors.New("host not part of the network " + network)
  411. } else {
  412. newNode := models.Node{}
  413. newNode.Server = servercfg.GetServer()
  414. newNode.Network = network
  415. newNode.HostID = h.ID
  416. if err := AssociateNodeToHost(&newNode, h); err != nil {
  417. return nil, err
  418. }
  419. return &newNode, nil
  420. }
  421. }
  422. // AssociateNodeToHost - associates and creates a node with a given host
  423. // should be the only way nodes get created as of 0.18
  424. func AssociateNodeToHost(n *models.Node, h *models.Host) error {
  425. if len(h.ID.String()) == 0 || h.ID == uuid.Nil {
  426. return ErrInvalidHostID
  427. }
  428. n.HostID = h.ID
  429. err := createNode(n)
  430. if err != nil {
  431. return err
  432. }
  433. currentHost, err := GetHost(h.ID.String())
  434. if err != nil {
  435. return err
  436. }
  437. h.HostPass = currentHost.HostPass
  438. h.Nodes = append(currentHost.Nodes, n.ID.String())
  439. return UpsertHost(h)
  440. }
  441. // DissasociateNodeFromHost - deletes a node and removes from host nodes
  442. // should be the only way nodes are deleted as of 0.18
  443. func DissasociateNodeFromHost(n *models.Node, h *models.Host) error {
  444. if len(h.ID.String()) == 0 || h.ID == uuid.Nil {
  445. return ErrInvalidHostID
  446. }
  447. if n.HostID != h.ID { // check if node actually belongs to host
  448. return fmt.Errorf("node is not associated with host")
  449. }
  450. if len(h.Nodes) == 0 {
  451. return fmt.Errorf("no nodes present in given host")
  452. }
  453. nList := []string{}
  454. for i := range h.Nodes {
  455. if h.Nodes[i] != n.ID.String() {
  456. nList = append(nList, h.Nodes[i])
  457. }
  458. }
  459. h.Nodes = nList
  460. go func() {
  461. if servercfg.IsPro {
  462. if clients, err := GetNetworkExtClients(n.Network); err != nil {
  463. for i := range clients {
  464. AllowClientNodeAccess(&clients[i], n.ID.String())
  465. }
  466. }
  467. }
  468. }()
  469. if err := DeleteNodeByID(n); err != nil {
  470. return err
  471. }
  472. return UpsertHost(h)
  473. }
  474. // DisassociateAllNodesFromHost - deletes all nodes of the host
  475. func DisassociateAllNodesFromHost(hostID string) error {
  476. host, err := GetHost(hostID)
  477. if err != nil {
  478. return err
  479. }
  480. for _, nodeID := range host.Nodes {
  481. node, err := GetNodeByID(nodeID)
  482. if err != nil {
  483. logger.Log(0, "failed to get host node, node id:", nodeID, err.Error())
  484. continue
  485. }
  486. if err := DeleteNode(&node, true); err != nil {
  487. logger.Log(0, "failed to delete node", node.ID.String(), err.Error())
  488. continue
  489. }
  490. logger.Log(3, "deleted node", node.ID.String(), "of host", host.ID.String())
  491. }
  492. host.Nodes = []string{}
  493. return UpsertHost(host)
  494. }
  495. // GetDefaultHosts - retrieve all hosts marked as default from DB
  496. func GetDefaultHosts() []models.Host {
  497. defaultHostList := []models.Host{}
  498. hosts, err := GetAllHosts()
  499. if err != nil {
  500. return defaultHostList
  501. }
  502. for i := range hosts {
  503. if hosts[i].IsDefault {
  504. defaultHostList = append(defaultHostList, hosts[i])
  505. }
  506. }
  507. return defaultHostList[:]
  508. }
  509. // GetHostNetworks - fetches all the networks
  510. func GetHostNetworks(hostID string) []string {
  511. currHost, err := GetHost(hostID)
  512. if err != nil {
  513. return nil
  514. }
  515. nets := []string{}
  516. for i := range currHost.Nodes {
  517. n, err := GetNodeByID(currHost.Nodes[i])
  518. if err != nil {
  519. return nil
  520. }
  521. nets = append(nets, n.Network)
  522. }
  523. return nets
  524. }
  525. // GetRelatedHosts - fetches related hosts of a given host
  526. func GetRelatedHosts(hostID string) []models.Host {
  527. relatedHosts := []models.Host{}
  528. networks := GetHostNetworks(hostID)
  529. networkMap := make(map[string]struct{})
  530. for _, network := range networks {
  531. networkMap[network] = struct{}{}
  532. }
  533. hosts, err := GetAllHosts()
  534. if err == nil {
  535. for _, host := range hosts {
  536. if host.ID.String() == hostID {
  537. continue
  538. }
  539. networks := GetHostNetworks(host.ID.String())
  540. for _, network := range networks {
  541. if _, ok := networkMap[network]; ok {
  542. relatedHosts = append(relatedHosts, host)
  543. break
  544. }
  545. }
  546. }
  547. }
  548. return relatedHosts
  549. }
  550. // CheckHostPort checks host endpoints to ensures that hosts on the same server
  551. // with the same endpoint have different listen ports
  552. // in the case of 64535 hosts or more with same endpoint, ports will not be changed
  553. func CheckHostPorts(h *models.Host) (changed bool) {
  554. portsInUse := make(map[int]bool, 0)
  555. hosts, err := GetAllHosts()
  556. if err != nil {
  557. return
  558. }
  559. originalPort := h.ListenPort
  560. defer func() {
  561. if originalPort != h.ListenPort {
  562. changed = true
  563. }
  564. }()
  565. if h.EndpointIP == nil {
  566. return
  567. }
  568. for _, host := range hosts {
  569. if host.ID.String() == h.ID.String() {
  570. // skip self
  571. continue
  572. }
  573. if host.EndpointIP == nil {
  574. continue
  575. }
  576. if !host.EndpointIP.Equal(h.EndpointIP) {
  577. continue
  578. }
  579. portsInUse[host.ListenPort] = true
  580. }
  581. // iterate until port is not found or max iteration is reached
  582. for i := 0; portsInUse[h.ListenPort] && i < maxPort-minPort+1; i++ {
  583. if h.ListenPort == 443 {
  584. h.ListenPort = 51821
  585. } else {
  586. h.ListenPort++
  587. }
  588. if h.ListenPort > maxPort {
  589. h.ListenPort = minPort
  590. }
  591. }
  592. return
  593. }
  594. // HostExists - checks if given host already exists
  595. func HostExists(h *models.Host) bool {
  596. _, err := GetHost(h.ID.String())
  597. return (err != nil && !database.IsEmptyRecord(err)) || (err == nil)
  598. }
  599. // GetHostByNodeID - returns a host if found to have a node's ID, else nil
  600. func GetHostByNodeID(id string) *models.Host {
  601. hosts, err := GetAllHosts()
  602. if err != nil {
  603. return nil
  604. }
  605. for i := range hosts {
  606. h := hosts[i]
  607. if StringSliceContains(h.Nodes, id) {
  608. return &h
  609. }
  610. }
  611. return nil
  612. }
  613. // ConvHostPassToHash - converts password to md5 hash
  614. func ConvHostPassToHash(hostPass string) string {
  615. return fmt.Sprintf("%x", md5.Sum([]byte(hostPass)))
  616. }
  617. // SortApiHosts - Sorts slice of ApiHosts by their ID alphabetically with numbers first
  618. func SortApiHosts(unsortedHosts []models.ApiHost) {
  619. sort.Slice(unsortedHosts, func(i, j int) bool {
  620. return unsortedHosts[i].ID < unsortedHosts[j].ID
  621. })
  622. }