dns.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package logic
  2. import (
  3. "encoding/json"
  4. "os"
  5. validator "github.com/go-playground/validator/v10"
  6. "github.com/gravitl/netmaker/database"
  7. "github.com/gravitl/netmaker/logger"
  8. "github.com/gravitl/netmaker/models"
  9. "github.com/txn2/txeh"
  10. )
  11. // SetDNS - sets the dns on file
  12. func SetDNS() error {
  13. hostfile := txeh.Hosts{}
  14. var corefilestring string
  15. networks, err := GetNetworks()
  16. if err != nil && !database.IsEmptyRecord(err) {
  17. return err
  18. }
  19. for _, net := range networks {
  20. corefilestring = corefilestring + net.NetID + " "
  21. dns, err := GetDNS(net.NetID)
  22. if err != nil && !database.IsEmptyRecord(err) {
  23. return err
  24. }
  25. for _, entry := range dns {
  26. hostfile.AddHost(entry.Address, entry.Name+"."+entry.Network)
  27. }
  28. }
  29. if corefilestring == "" {
  30. corefilestring = "example.com"
  31. }
  32. err = hostfile.SaveAs("./config/dnsconfig/netmaker.hosts")
  33. if err != nil {
  34. return err
  35. }
  36. /* if something goes wrong with server DNS, check here
  37. // commented out bc we were not using IsSplitDNS
  38. if servercfg.IsSplitDNS() {
  39. err = SetCorefile(corefilestring)
  40. }
  41. */
  42. return err
  43. }
  44. // GetDNS - gets the DNS of a current network
  45. func GetDNS(network string) ([]models.DNSEntry, error) {
  46. dns, err := GetNodeDNS(network)
  47. if err != nil && !database.IsEmptyRecord(err) {
  48. return dns, err
  49. }
  50. customdns, err := GetCustomDNS(network)
  51. if err != nil && !database.IsEmptyRecord(err) {
  52. return dns, err
  53. }
  54. dns = append(dns, customdns...)
  55. return dns, nil
  56. }
  57. // GetNodeDNS - gets the DNS of a network node
  58. func GetNodeDNS(network string) ([]models.DNSEntry, error) {
  59. var dns []models.DNSEntry
  60. collection, err := database.FetchRecords(database.NODES_TABLE_NAME)
  61. if err != nil {
  62. return dns, err
  63. }
  64. for _, value := range collection {
  65. var node models.Node
  66. if err = json.Unmarshal([]byte(value), &node); err != nil {
  67. continue
  68. }
  69. if node.Network != network {
  70. continue
  71. }
  72. host, err := GetHost(node.HostID.String())
  73. if err != nil {
  74. continue
  75. }
  76. var entry = models.DNSEntry{}
  77. entry.Name = host.Name
  78. entry.Network = network
  79. if node.Address.IP != nil {
  80. entry.Address = node.Address.IP.String()
  81. }
  82. if node.Address6.IP != nil {
  83. entry.Address6 = node.Address6.IP.String()
  84. }
  85. dns = append(dns, entry)
  86. }
  87. return dns, nil
  88. }
  89. // GetCustomDNS - gets the custom DNS of a network
  90. func GetCustomDNS(network string) ([]models.DNSEntry, error) {
  91. var dns []models.DNSEntry
  92. collection, err := database.FetchRecords(database.DNS_TABLE_NAME)
  93. if err != nil {
  94. return dns, err
  95. }
  96. for _, value := range collection { // filter for entries based on network
  97. var entry models.DNSEntry
  98. if err := json.Unmarshal([]byte(value), &entry); err != nil {
  99. continue
  100. }
  101. if entry.Network == network {
  102. dns = append(dns, entry)
  103. }
  104. }
  105. return dns, err
  106. }
  107. // SetCorefile - sets the core file of the system
  108. func SetCorefile(domains string) error {
  109. dir, err := os.Getwd()
  110. if err != nil {
  111. return err
  112. }
  113. _, err = os.Stat(dir + "/config/dnsconfig")
  114. if os.IsNotExist(err) {
  115. err = os.MkdirAll(dir+"/config/dnsconfig", 0744)
  116. }
  117. if err != nil {
  118. logger.Log(0, "couldnt find or create /config/dnsconfig")
  119. return err
  120. }
  121. corefile := domains + ` {
  122. reload 15s
  123. hosts /root/dnsconfig/netmaker.hosts {
  124. fallthrough
  125. }
  126. forward . 8.8.8.8 8.8.4.4
  127. log
  128. }
  129. `
  130. corebytes := []byte(corefile)
  131. err = os.WriteFile(dir+"/config/dnsconfig/Corefile", corebytes, 0644)
  132. if err != nil {
  133. return err
  134. }
  135. return err
  136. }
  137. // GetAllDNS - gets all dns entries
  138. func GetAllDNS() ([]models.DNSEntry, error) {
  139. var dns []models.DNSEntry
  140. networks, err := GetNetworks()
  141. if err != nil && !database.IsEmptyRecord(err) {
  142. return []models.DNSEntry{}, err
  143. }
  144. for _, net := range networks {
  145. netdns, err := GetDNS(net.NetID)
  146. if err != nil {
  147. return []models.DNSEntry{}, nil
  148. }
  149. dns = append(dns, netdns...)
  150. }
  151. return dns, nil
  152. }
  153. // GetDNSEntryNum - gets which entry the dns was
  154. func GetDNSEntryNum(domain string, network string) (int, error) {
  155. num := 0
  156. entries, err := GetDNS(network)
  157. if err != nil {
  158. return 0, err
  159. }
  160. for i := 0; i < len(entries); i++ {
  161. if domain == entries[i].Name {
  162. num++
  163. }
  164. }
  165. return num, nil
  166. }
  167. // ValidateDNSCreate - checks if an entry is valid
  168. func ValidateDNSCreate(entry models.DNSEntry) error {
  169. v := validator.New()
  170. _ = v.RegisterValidation("name_unique", func(fl validator.FieldLevel) bool {
  171. num, err := GetDNSEntryNum(entry.Name, entry.Network)
  172. return err == nil && num == 0
  173. })
  174. _ = v.RegisterValidation("network_exists", func(fl validator.FieldLevel) bool {
  175. _, err := GetParentNetwork(entry.Network)
  176. return err == nil
  177. })
  178. err := v.Struct(entry)
  179. if err != nil {
  180. for _, e := range err.(validator.ValidationErrors) {
  181. logger.Log(1, e.Error())
  182. }
  183. }
  184. return err
  185. }
  186. // ValidateDNSUpdate - validates a DNS update
  187. func ValidateDNSUpdate(change models.DNSEntry, entry models.DNSEntry) error {
  188. v := validator.New()
  189. _ = v.RegisterValidation("name_unique", func(fl validator.FieldLevel) bool {
  190. //if name & net not changing name we are good
  191. if change.Name == entry.Name && change.Network == entry.Network {
  192. return true
  193. }
  194. num, err := GetDNSEntryNum(change.Name, change.Network)
  195. return err == nil && num == 0
  196. })
  197. _ = v.RegisterValidation("network_exists", func(fl validator.FieldLevel) bool {
  198. _, err := GetParentNetwork(change.Network)
  199. return err == nil
  200. })
  201. err := v.Struct(change)
  202. if err != nil {
  203. for _, e := range err.(validator.ValidationErrors) {
  204. logger.Log(1, e.Error())
  205. }
  206. }
  207. return err
  208. }
  209. // DeleteDNS - deletes a DNS entry
  210. func DeleteDNS(domain string, network string) error {
  211. key, err := GetRecordKey(domain, network)
  212. if err != nil {
  213. return err
  214. }
  215. err = database.DeleteRecord(database.DNS_TABLE_NAME, key)
  216. return err
  217. }
  218. // CreateDNS - creates a DNS entry
  219. func CreateDNS(entry models.DNSEntry) (models.DNSEntry, error) {
  220. k, err := GetRecordKey(entry.Name, entry.Network)
  221. if err != nil {
  222. return models.DNSEntry{}, err
  223. }
  224. data, err := json.Marshal(&entry)
  225. if err != nil {
  226. return models.DNSEntry{}, err
  227. }
  228. err = database.Insert(k, string(data), database.DNS_TABLE_NAME)
  229. return entry, err
  230. }