Browse Source

dns working

afeiszli 4 years ago
parent
commit
731f938efe
3 changed files with 42 additions and 39 deletions
  1. 36 31
      controllers/dnsHttpController.go
  2. 3 5
      functions/local.go
  3. 3 3
      main.go

+ 36 - 31
controllers/dnsHttpController.go

@@ -173,6 +173,40 @@ func GetCustomDNS(network string) ([]models.DNSEntry, error){
         return dns, err
         return dns, err
 }
 }
 
 
+func SetDNS() error {
+        hostfile := txeh.Hosts{}
+        var corefilestring string
+        networks, err := functions.ListNetworks()
+        if err != nil {
+                return err
+        }
+
+        for _, net := range networks {
+                corefilestring = corefilestring + net.NetID + " "
+                dns, err := GetDNS(net.NetID)
+                if err != nil {
+                        return err
+                }
+                for _, entry := range dns {
+                        hostfile.AddHost(entry.Address, entry.Name+"."+entry.Network)
+                        if err != nil {
+                                return err
+                        }
+                }
+        }
+        if corefilestring == "" {
+                corefilestring = "example.com"
+        }
+
+        err = hostfile.SaveAs("./config/dnsconfig/netmaker.hosts")
+        if err != nil {
+                return err
+        }
+        err = functions.SetCorefile(corefilestring)
+
+        return err
+}
+
 func GetDNSEntryNum(domain string, network string) (int, error){
 func GetDNSEntryNum(domain string, network string) (int, error){
 
 
         num := 0
         num := 0
@@ -406,43 +440,14 @@ func pushDNS(w http.ResponseWriter, r *http.Request) {
         // Set header
         // Set header
         w.Header().Set("Content-Type", "application/json")
         w.Header().Set("Content-Type", "application/json")
 
 
-        err := WriteHosts()
+        err := SetDNS()
 
 
         if err != nil {
         if err != nil {
                 returnErrorResponse(w, r, formatError(err, "internal"))
                 returnErrorResponse(w, r, formatError(err, "internal"))
                 return
                 return
 	}
 	}
-        json.NewEncoder(w).Encode("DNS Pushed to CoreDNS")
-}
 
 
-
-func WriteHosts() error {
-	//hostfile, err := txeh.NewHostsDefault()
-	hostfile := txeh.Hosts{}
-	/*
-	if err != nil {
-                return err
-        }
-	*/
-	networks, err := functions.ListNetworks()
-        if err != nil {
-                return err
-        }
-
-	for _, net := range networks {
-		dns, err := GetDNS(net.NetID)
-		if err != nil {
-			return err
-		}
-		for _, entry := range dns {
-			hostfile.AddHost(entry.Address, entry.Name+"."+entry.Network)
-			if err != nil {
-				return err
-	                }
-		}
-	}
-	err = hostfile.SaveAs("./config/dnsconfig/netmaker.hosts")
-	return err
+        json.NewEncoder(w).Encode("DNS Pushed to CoreDNS")
 }
 }
 
 
 func ValidateDNSCreate(entry models.DNSEntry) error {
 func ValidateDNSCreate(entry models.DNSEntry) error {

+ 3 - 5
functions/local.go

@@ -17,7 +17,7 @@ func FileExists(f string) bool {
     return !info.IsDir()
     return !info.IsDir()
 }
 }
 
 
-func ConfigureDNS() error {
+func SetCorefile(domains string) error {
 	dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
 	dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
 	if err != nil {
 	if err != nil {
             return err
             return err
@@ -30,10 +30,9 @@ func ConfigureDNS() error {
                 return err
                 return err
         }
         }
 
 
-	if !FileExists(dir + "/config/dnsconfig/Corefile") {
-
-		corefile := `. {
+		corefile := domains + ` {
     hosts /root/dnsconfig/netmaker.hosts {
     hosts /root/dnsconfig/netmaker.hosts {
+	reload 15s
 	fallthrough	
 	fallthrough	
     }
     }
     forward . 8.8.8.8 8.8.4.4
     forward . 8.8.8.8 8.8.4.4
@@ -48,6 +47,5 @@ func ConfigureDNS() error {
 			log.Println("")
 			log.Println("")
 			return err
 			return err
 		}
 		}
-	}
 	return err
 	return err
 }
 }

+ 3 - 3
main.go

@@ -77,7 +77,7 @@ func main() {
 	}
 	}
 	}
 	}
 	if dnsmode == "on" {
 	if dnsmode == "on" {
-		err := functions.ConfigureDNS()
+		err := controller.SetDNS()
                 if err != nil {
                 if err != nil {
                         fmt.Printf("Error setting DNS: %v", err)
                         fmt.Printf("Error setting DNS: %v", err)
                 }
                 }
@@ -119,14 +119,14 @@ func runGRPC(wg *sync.WaitGroup, installserver bool) {
 		grpcport = ":" + os.Getenv("GRPC_PORT")
 		grpcport = ":" + os.Getenv("GRPC_PORT")
         }
         }
 	PortGRPC = grpcport
 	PortGRPC = grpcport
-	if os.Getenv("BACKEND_URL") == ""  {
+	if os.Getenv("SERVER_DOMAIN") == ""  {
 		if config.Config.Server.Host == "" {
 		if config.Config.Server.Host == "" {
 			ServerGRPC, _ = serverctl.GetPublicIP()
 			ServerGRPC, _ = serverctl.GetPublicIP()
 		} else {
 		} else {
 			ServerGRPC = config.Config.Server.Host
 			ServerGRPC = config.Config.Server.Host
 		}
 		}
 	} else {
 	} else {
-		ServerGRPC = os.Getenv("BACKEND_URL")
+		ServerGRPC = os.Getenv("SERVER_DOMAIN")
 	}
 	}
 	fmt.Println("GRPC Server set to: " + ServerGRPC)
 	fmt.Println("GRPC Server set to: " + ServerGRPC)
 	fmt.Println("GRPC Port set to: " + PortGRPC)
 	fmt.Println("GRPC Port set to: " + PortGRPC)