hosts.go 17 KB

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