mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-11 23:54:22 +08:00
dns working
This commit is contained in:
parent
2b77ae6723
commit
731f938efe
3 changed files with 42 additions and 39 deletions
|
@ -173,6 +173,40 @@ func GetCustomDNS(network string) ([]models.DNSEntry, error){
|
|||
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){
|
||||
|
||||
num := 0
|
||||
|
@ -406,45 +440,16 @@ func pushDNS(w http.ResponseWriter, r *http.Request) {
|
|||
// Set header
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
err := WriteHosts()
|
||||
err := SetDNS()
|
||||
|
||||
if err != nil {
|
||||
returnErrorResponse(w, r, formatError(err, "internal"))
|
||||
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
|
||||
}
|
||||
|
||||
func ValidateDNSCreate(entry models.DNSEntry) error {
|
||||
|
||||
v := validator.New()
|
||||
|
|
|
@ -17,7 +17,7 @@ func FileExists(f string) bool {
|
|||
return !info.IsDir()
|
||||
}
|
||||
|
||||
func ConfigureDNS() error {
|
||||
func SetCorefile(domains string) error {
|
||||
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -30,10 +30,9 @@ func ConfigureDNS() error {
|
|||
return err
|
||||
}
|
||||
|
||||
if !FileExists(dir + "/config/dnsconfig/Corefile") {
|
||||
|
||||
corefile := `. {
|
||||
corefile := domains + ` {
|
||||
hosts /root/dnsconfig/netmaker.hosts {
|
||||
reload 15s
|
||||
fallthrough
|
||||
}
|
||||
forward . 8.8.8.8 8.8.4.4
|
||||
|
@ -48,6 +47,5 @@ func ConfigureDNS() error {
|
|||
log.Println("")
|
||||
return err
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
|
6
main.go
6
main.go
|
@ -77,7 +77,7 @@ func main() {
|
|||
}
|
||||
}
|
||||
if dnsmode == "on" {
|
||||
err := functions.ConfigureDNS()
|
||||
err := controller.SetDNS()
|
||||
if err != nil {
|
||||
fmt.Printf("Error setting DNS: %v", err)
|
||||
}
|
||||
|
@ -119,14 +119,14 @@ func runGRPC(wg *sync.WaitGroup, installserver bool) {
|
|||
grpcport = ":" + os.Getenv("GRPC_PORT")
|
||||
}
|
||||
PortGRPC = grpcport
|
||||
if os.Getenv("BACKEND_URL") == "" {
|
||||
if os.Getenv("SERVER_DOMAIN") == "" {
|
||||
if config.Config.Server.Host == "" {
|
||||
ServerGRPC, _ = serverctl.GetPublicIP()
|
||||
} else {
|
||||
ServerGRPC = config.Config.Server.Host
|
||||
}
|
||||
} else {
|
||||
ServerGRPC = os.Getenv("BACKEND_URL")
|
||||
ServerGRPC = os.Getenv("SERVER_DOMAIN")
|
||||
}
|
||||
fmt.Println("GRPC Server set to: " + ServerGRPC)
|
||||
fmt.Println("GRPC Port set to: " + PortGRPC)
|
||||
|
|
Loading…
Add table
Reference in a new issue