hosts.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. package logic
  2. import (
  3. "crypto/md5"
  4. "encoding/base64"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "log"
  9. "net/http"
  10. "strconv"
  11. "github.com/devilcove/httpclient"
  12. "github.com/google/uuid"
  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. "golang.org/x/crypto/bcrypt"
  18. )
  19. var (
  20. // ErrHostExists error indicating that host exists when trying to create new host
  21. ErrHostExists error = errors.New("host already exists")
  22. // ErrInvalidHostID
  23. ErrInvalidHostID error = errors.New("invalid host id")
  24. )
  25. const (
  26. maxPort = 1<<16 - 1
  27. minPort = 1025
  28. )
  29. // GetAllHosts - returns all hosts in flat list or error
  30. func GetAllHosts() ([]models.Host, error) {
  31. currHostMap, err := GetHostsMap()
  32. if err != nil {
  33. return nil, err
  34. }
  35. var currentHosts = []models.Host{}
  36. for k := range currHostMap {
  37. var h = *currHostMap[k]
  38. currentHosts = append(currentHosts, h)
  39. }
  40. return currentHosts, nil
  41. }
  42. // GetAllHostsAPI - get's all the hosts in an API usable format
  43. func GetAllHostsAPI(hosts []models.Host) []models.ApiHost {
  44. apiHosts := []models.ApiHost{}
  45. for i := range hosts {
  46. newApiHost := hosts[i].ConvertNMHostToAPI()
  47. apiHosts = append(apiHosts, *newApiHost)
  48. }
  49. return apiHosts[:]
  50. }
  51. // GetHostsMap - gets all the current hosts on machine in a map
  52. func GetHostsMap() (map[string]*models.Host, error) {
  53. records, err := database.FetchRecords(database.HOSTS_TABLE_NAME)
  54. if err != nil && !database.IsEmptyRecord(err) {
  55. return nil, err
  56. }
  57. currHostMap := make(map[string]*models.Host)
  58. for k := range records {
  59. var h models.Host
  60. err = json.Unmarshal([]byte(records[k]), &h)
  61. if err != nil {
  62. return nil, err
  63. }
  64. currHostMap[h.ID.String()] = &h
  65. }
  66. return currHostMap, nil
  67. }
  68. // GetHost - gets a host from db given id
  69. func GetHost(hostid string) (*models.Host, error) {
  70. record, err := database.FetchRecord(database.HOSTS_TABLE_NAME, hostid)
  71. if err != nil {
  72. return nil, err
  73. }
  74. var h models.Host
  75. if err = json.Unmarshal([]byte(record), &h); err != nil {
  76. return nil, err
  77. }
  78. return &h, nil
  79. }
  80. // CreateHost - creates a host if not exist
  81. func CreateHost(h *models.Host) error {
  82. _, err := GetHost(h.ID.String())
  83. if (err != nil && !database.IsEmptyRecord(err)) || (err == nil) {
  84. return ErrHostExists
  85. }
  86. err = RegisterHostWithTurn(h.ID.String(), h.HostPass)
  87. if err != nil {
  88. logger.Log(0, "failed to register host with turn server: ", err.Error())
  89. }
  90. // encrypt that password so we never see it
  91. hash, err := bcrypt.GenerateFromPassword([]byte(h.HostPass), 5)
  92. if err != nil {
  93. return err
  94. }
  95. h.HostPass = string(hash)
  96. // if another server has already updated proxyenabled, leave it alone
  97. if !h.ProxyEnabledSet {
  98. log.Println("checking default proxy", servercfg.GetServerConfig().DefaultProxyMode)
  99. if servercfg.GetServerConfig().DefaultProxyMode.Set {
  100. h.ProxyEnabledSet = true
  101. h.ProxyEnabled = servercfg.GetServerConfig().DefaultProxyMode.Value
  102. log.Println("set proxy enabled to ", h.ProxyEnabled)
  103. }
  104. }
  105. checkForZombieHosts(h)
  106. return UpsertHost(h)
  107. }
  108. // UpdateHost - updates host data by field
  109. func UpdateHost(newHost, currentHost *models.Host) {
  110. // unchangeable fields via API here
  111. newHost.DaemonInstalled = currentHost.DaemonInstalled
  112. newHost.OS = currentHost.OS
  113. newHost.IPForwarding = currentHost.IPForwarding
  114. newHost.HostPass = currentHost.HostPass
  115. newHost.MacAddress = currentHost.MacAddress
  116. newHost.Debug = currentHost.Debug
  117. newHost.Nodes = currentHost.Nodes
  118. newHost.PublicKey = currentHost.PublicKey
  119. newHost.InternetGateway = currentHost.InternetGateway
  120. newHost.TrafficKeyPublic = currentHost.TrafficKeyPublic
  121. // changeable fields
  122. if len(newHost.Version) == 0 {
  123. newHost.Version = currentHost.Version
  124. }
  125. if len(newHost.Name) == 0 {
  126. newHost.Name = currentHost.Name
  127. }
  128. if newHost.MTU == 0 {
  129. newHost.MTU = currentHost.MTU
  130. }
  131. if newHost.ListenPort == 0 {
  132. newHost.ListenPort = currentHost.ListenPort
  133. }
  134. if newHost.ProxyListenPort == 0 {
  135. newHost.ProxyListenPort = currentHost.ProxyListenPort
  136. }
  137. newHost.PublicListenPort = currentHost.PublicListenPort
  138. }
  139. // UpdateHostFromClient - used for updating host on server with update recieved from client
  140. func UpdateHostFromClient(newHost, currHost *models.Host) (sendPeerUpdate bool) {
  141. if newHost.ListenPort != 0 && currHost.ListenPort != newHost.ListenPort {
  142. currHost.ListenPort = newHost.ListenPort
  143. sendPeerUpdate = true
  144. }
  145. if newHost.ProxyListenPort != 0 && currHost.ProxyListenPort != newHost.ProxyListenPort {
  146. currHost.ProxyListenPort = newHost.ProxyListenPort
  147. sendPeerUpdate = true
  148. }
  149. if newHost.PublicListenPort != 0 && currHost.PublicListenPort != newHost.PublicListenPort {
  150. currHost.PublicListenPort = newHost.PublicListenPort
  151. sendPeerUpdate = true
  152. }
  153. if currHost.ProxyEnabled != newHost.ProxyEnabled {
  154. currHost.ProxyEnabled = newHost.ProxyEnabled
  155. sendPeerUpdate = true
  156. }
  157. if currHost.EndpointIP.String() != newHost.EndpointIP.String() {
  158. currHost.EndpointIP = newHost.EndpointIP
  159. sendPeerUpdate = true
  160. }
  161. currHost.DaemonInstalled = newHost.DaemonInstalled
  162. currHost.Debug = newHost.Debug
  163. currHost.Verbosity = newHost.Verbosity
  164. currHost.Version = newHost.Version
  165. if newHost.Name != "" {
  166. currHost.Name = newHost.Name
  167. }
  168. if len(newHost.NatType) > 0 && newHost.NatType != currHost.NatType {
  169. currHost.NatType = newHost.NatType
  170. sendPeerUpdate = true
  171. }
  172. return
  173. }
  174. // UpsertHost - upserts into DB a given host model, does not check for existence*
  175. func UpsertHost(h *models.Host) error {
  176. data, err := json.Marshal(h)
  177. if err != nil {
  178. return err
  179. }
  180. return database.Insert(h.ID.String(), string(data), database.HOSTS_TABLE_NAME)
  181. }
  182. // RemoveHost - removes a given host from server
  183. func RemoveHost(h *models.Host) error {
  184. if len(h.Nodes) > 0 {
  185. return fmt.Errorf("host still has associated nodes")
  186. }
  187. DeRegisterHostWithTurn(h.ID.String())
  188. return database.DeleteRecord(database.HOSTS_TABLE_NAME, h.ID.String())
  189. }
  190. // RemoveHostByID - removes a given host by id from server
  191. func RemoveHostByID(hostID string) error {
  192. DeRegisterHostWithTurn(hostID)
  193. return database.DeleteRecord(database.HOSTS_TABLE_NAME, hostID)
  194. }
  195. // UpdateHostNetwork - adds/deletes host from a network
  196. func UpdateHostNetwork(h *models.Host, network string, add bool) (*models.Node, error) {
  197. for _, nodeID := range h.Nodes {
  198. node, err := GetNodeByID(nodeID)
  199. if err != nil || node.PendingDelete {
  200. continue
  201. }
  202. if node.Network == network {
  203. if !add {
  204. return &node, nil
  205. } else {
  206. return nil, errors.New("host already part of network " + network)
  207. }
  208. }
  209. }
  210. if !add {
  211. return nil, errors.New("host not part of the network " + network)
  212. } else {
  213. newNode := models.Node{}
  214. newNode.Server = servercfg.GetServer()
  215. newNode.Network = network
  216. newNode.HostID = h.ID
  217. if err := AssociateNodeToHost(&newNode, h); err != nil {
  218. return nil, err
  219. }
  220. return &newNode, nil
  221. }
  222. }
  223. // AssociateNodeToHost - associates and creates a node with a given host
  224. // should be the only way nodes get created as of 0.18
  225. func AssociateNodeToHost(n *models.Node, h *models.Host) error {
  226. if len(h.ID.String()) == 0 || h.ID == uuid.Nil {
  227. return ErrInvalidHostID
  228. }
  229. n.HostID = h.ID
  230. err := createNode(n)
  231. if err != nil {
  232. return err
  233. }
  234. currentHost, err := GetHost(h.ID.String())
  235. if err != nil {
  236. return err
  237. }
  238. h.HostPass = currentHost.HostPass
  239. h.Nodes = append(currentHost.Nodes, n.ID.String())
  240. return UpsertHost(h)
  241. }
  242. // DissasociateNodeFromHost - deletes a node and removes from host nodes
  243. // should be the only way nodes are deleted as of 0.18
  244. func DissasociateNodeFromHost(n *models.Node, h *models.Host) error {
  245. if len(h.ID.String()) == 0 || h.ID == uuid.Nil {
  246. return ErrInvalidHostID
  247. }
  248. if n.HostID != h.ID { // check if node actually belongs to host
  249. return fmt.Errorf("node is not associated with host")
  250. }
  251. if len(h.Nodes) == 0 {
  252. return fmt.Errorf("no nodes present in given host")
  253. }
  254. index := -1
  255. for i := range h.Nodes {
  256. if h.Nodes[i] == n.ID.String() {
  257. index = i
  258. break
  259. }
  260. }
  261. if index < 0 {
  262. if len(h.Nodes) == 0 {
  263. return fmt.Errorf("node %s, not found in host, %s", n.ID.String(), h.ID.String())
  264. }
  265. } else {
  266. h.Nodes = RemoveStringSlice(h.Nodes, index)
  267. }
  268. if err := deleteNodeByID(n); err != nil {
  269. return err
  270. }
  271. return UpsertHost(h)
  272. }
  273. // DisassociateAllNodesFromHost - deletes all nodes of the host
  274. func DisassociateAllNodesFromHost(hostID string) error {
  275. host, err := GetHost(hostID)
  276. if err != nil {
  277. return err
  278. }
  279. for _, nodeID := range host.Nodes {
  280. node, err := GetNodeByID(nodeID)
  281. if err != nil {
  282. logger.Log(0, "failed to get host node", err.Error())
  283. continue
  284. }
  285. if err := DeleteNode(&node, true); err != nil {
  286. logger.Log(0, "failed to delete node", node.ID.String(), err.Error())
  287. continue
  288. }
  289. logger.Log(3, "deleted node", node.ID.String(), "of host", host.ID.String())
  290. }
  291. host.Nodes = []string{}
  292. return UpsertHost(host)
  293. }
  294. // GetDefaultHosts - retrieve all hosts marked as default from DB
  295. func GetDefaultHosts() []models.Host {
  296. defaultHostList := []models.Host{}
  297. hosts, err := GetAllHosts()
  298. if err != nil {
  299. return defaultHostList
  300. }
  301. for i := range hosts {
  302. if hosts[i].IsDefault {
  303. defaultHostList = append(defaultHostList, hosts[i])
  304. }
  305. }
  306. return defaultHostList[:]
  307. }
  308. // GetHostNetworks - fetches all the networks
  309. func GetHostNetworks(hostID string) []string {
  310. currHost, err := GetHost(hostID)
  311. if err != nil {
  312. return nil
  313. }
  314. nets := []string{}
  315. for i := range currHost.Nodes {
  316. n, err := GetNodeByID(currHost.Nodes[i])
  317. if err != nil {
  318. return nil
  319. }
  320. nets = append(nets, n.Network)
  321. }
  322. return nets
  323. }
  324. // GetRelatedHosts - fetches related hosts of a given host
  325. func GetRelatedHosts(hostID string) []models.Host {
  326. relatedHosts := []models.Host{}
  327. networks := GetHostNetworks(hostID)
  328. networkMap := make(map[string]struct{})
  329. for _, network := range networks {
  330. networkMap[network] = struct{}{}
  331. }
  332. hosts, err := GetAllHosts()
  333. if err == nil {
  334. for _, host := range hosts {
  335. if host.ID.String() == hostID {
  336. continue
  337. }
  338. networks := GetHostNetworks(host.ID.String())
  339. for _, network := range networks {
  340. if _, ok := networkMap[network]; ok {
  341. relatedHosts = append(relatedHosts, host)
  342. break
  343. }
  344. }
  345. }
  346. }
  347. return relatedHosts
  348. }
  349. // CheckHostPort checks host endpoints to ensures that hosts on the same server
  350. // with the same endpoint have different listen ports
  351. // in the case of 64535 hosts or more with same endpoint, ports will not be changed
  352. func CheckHostPorts(h *models.Host) {
  353. portsInUse := make(map[int]bool, 0)
  354. hosts, err := GetAllHosts()
  355. if err != nil {
  356. return
  357. }
  358. for _, host := range hosts {
  359. if host.ID.String() == h.ID.String() {
  360. //skip self
  361. continue
  362. }
  363. if !host.EndpointIP.Equal(h.EndpointIP) {
  364. continue
  365. }
  366. portsInUse[host.ListenPort] = true
  367. portsInUse[host.ProxyListenPort] = true
  368. }
  369. // iterate until port is not found or max iteration is reached
  370. for i := 0; portsInUse[h.ListenPort] && i < maxPort-minPort+1; i++ {
  371. h.ListenPort++
  372. if h.ListenPort > maxPort {
  373. h.ListenPort = minPort
  374. }
  375. }
  376. // allocate h.ListenPort so it is unavailable to h.ProxyListenPort
  377. portsInUse[h.ListenPort] = true
  378. for i := 0; portsInUse[h.ProxyListenPort] && i < maxPort-minPort+1; i++ {
  379. h.ProxyListenPort++
  380. if h.ProxyListenPort > maxPort {
  381. h.ProxyListenPort = minPort
  382. }
  383. }
  384. }
  385. // HostExists - checks if given host already exists
  386. func HostExists(h *models.Host) bool {
  387. _, err := GetHost(h.ID.String())
  388. return (err != nil && !database.IsEmptyRecord(err)) || (err == nil)
  389. }
  390. // GetHostByNodeID - returns a host if found to have a node's ID, else nil
  391. func GetHostByNodeID(id string) *models.Host {
  392. hosts, err := GetAllHosts()
  393. if err != nil {
  394. return nil
  395. }
  396. for i := range hosts {
  397. h := hosts[i]
  398. if StringSliceContains(h.Nodes, id) {
  399. return &h
  400. }
  401. }
  402. return nil
  403. }
  404. // ConvHostPassToHash - converts password to md5 hash
  405. func ConvHostPassToHash(hostPass string) string {
  406. return fmt.Sprintf("%x", md5.Sum([]byte(hostPass)))
  407. }
  408. // RegisterHostWithTurn - registers the host with the given turn server
  409. func RegisterHostWithTurn(hostID, hostPass string) error {
  410. auth := servercfg.GetTurnUserName() + ":" + servercfg.GetTurnPassword()
  411. api := httpclient.JSONEndpoint[models.SuccessResponse, models.ErrorResponse]{
  412. URL: servercfg.GetTurnApiHost(),
  413. Route: "/api/v1/host/register",
  414. Method: http.MethodPost,
  415. Authorization: fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(auth))),
  416. Data: models.HostTurnRegister{
  417. HostID: hostID,
  418. HostPassHash: ConvHostPassToHash(hostPass),
  419. },
  420. Response: models.SuccessResponse{},
  421. ErrorResponse: models.ErrorResponse{},
  422. }
  423. _, errData, err := api.GetJSON(models.SuccessResponse{}, models.ErrorResponse{})
  424. if err != nil {
  425. if errors.Is(err, httpclient.ErrStatus) {
  426. logger.Log(1, "error server status", strconv.Itoa(errData.Code), errData.Message)
  427. }
  428. return err
  429. }
  430. return nil
  431. }
  432. // DeRegisterHostWithTurn - to be called when host need to be deregistered from a turn server
  433. func DeRegisterHostWithTurn(hostID string) error {
  434. auth := servercfg.GetTurnUserName() + ":" + servercfg.GetTurnPassword()
  435. api := httpclient.JSONEndpoint[models.SuccessResponse, models.ErrorResponse]{
  436. URL: servercfg.GetTurnApiHost(),
  437. Route: fmt.Sprintf("/api/v1/host/deregister?host_id=%s", hostID),
  438. Method: http.MethodPost,
  439. Authorization: fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(auth))),
  440. Response: models.SuccessResponse{},
  441. ErrorResponse: models.ErrorResponse{},
  442. }
  443. _, errData, err := api.GetJSON(models.SuccessResponse{}, models.ErrorResponse{})
  444. if err != nil {
  445. if errors.Is(err, httpclient.ErrStatus) {
  446. logger.Log(1, "error server status", strconv.Itoa(errData.Code), errData.Message)
  447. }
  448. return err
  449. }
  450. return nil
  451. }