mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-26 23:15:42 +08:00
Merge pull request #895 from gravitl/feature_0.12.0_host_dns
match dns to acl
This commit is contained in:
commit
2e0e54879a
4 changed files with 15 additions and 17 deletions
|
@ -1,9 +1,9 @@
|
||||||
package logic
|
package logic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -176,11 +176,13 @@ func GetPeerUpdate(node *models.Node) (models.PeerUpdate, error) {
|
||||||
// #1 Set Keepalive values: set_keepalive
|
// #1 Set Keepalive values: set_keepalive
|
||||||
// #2 Set local address: set_local - could be a LOT BETTER and fix some bugs with additional logic
|
// #2 Set local address: set_local - could be a LOT BETTER and fix some bugs with additional logic
|
||||||
// #3 Set allowedips: set_allowedips
|
// #3 Set allowedips: set_allowedips
|
||||||
|
var dns string
|
||||||
for _, peer := range currentPeers {
|
for _, peer := range currentPeers {
|
||||||
if peer.ID == node.ID {
|
if peer.ID == node.ID {
|
||||||
//skip yourself
|
//skip yourself
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
dns = dns + fmt.Sprintf("%s %s.%s\n", peer.Address, peer.Name, peer.Network)
|
||||||
pubkey, err := wgtypes.ParseKey(peer.PublicKey)
|
pubkey, err := wgtypes.ParseKey(peer.PublicKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return models.PeerUpdate{}, err
|
return models.PeerUpdate{}, err
|
||||||
|
@ -236,12 +238,7 @@ func GetPeerUpdate(node *models.Node) (models.PeerUpdate, error) {
|
||||||
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
dns, err := os.ReadFile("./config/dnsconfig/netmaker.hosts")
|
peerUpdate.DNS = dns
|
||||||
if err != nil {
|
|
||||||
logger.Log(0, "failed to read netmaker.hosts", err.Error())
|
|
||||||
} else {
|
|
||||||
peerUpdate.DNS = dns
|
|
||||||
}
|
|
||||||
return peerUpdate, nil
|
return peerUpdate, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -152,6 +152,7 @@ func setPeerInfo(node *models.Node) models.Node {
|
||||||
peer.PublicKey = node.PublicKey
|
peer.PublicKey = node.PublicKey
|
||||||
peer.Endpoint = node.Endpoint
|
peer.Endpoint = node.Endpoint
|
||||||
peer.Name = node.Name
|
peer.Name = node.Name
|
||||||
|
peer.Network = node.Network
|
||||||
peer.LocalAddress = node.LocalAddress
|
peer.LocalAddress = node.LocalAddress
|
||||||
peer.ListenPort = node.ListenPort
|
peer.ListenPort = node.ListenPort
|
||||||
peer.AllowedIPs = node.AllowedIPs
|
peer.AllowedIPs = node.AllowedIPs
|
||||||
|
|
|
@ -7,7 +7,7 @@ type PeerUpdate struct {
|
||||||
Network string `json:"network" bson:"network" yaml:"network"`
|
Network string `json:"network" bson:"network" yaml:"network"`
|
||||||
ServerAddrs []ServerAddr `json:"serveraddrs" bson:"serveraddrs" yaml:"serveraddrs"`
|
ServerAddrs []ServerAddr `json:"serveraddrs" bson:"serveraddrs" yaml:"serveraddrs"`
|
||||||
Peers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
|
Peers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
|
||||||
DNS []byte `json:"dns" bson:'dns" yaml:"dns"`
|
DNS string `json:"dns" bson:"dns" yaml:"dns"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// KeyUpdate - key update struct
|
// KeyUpdate - key update struct
|
||||||
|
|
|
@ -141,7 +141,7 @@ func NodeUpdate(client mqtt.Client, msg mqtt.Message) {
|
||||||
//deal with DNS
|
//deal with DNS
|
||||||
if newNode.DNSOn != "yes" && shouldDNSChange && nodeCfg.Node.Interface != "" {
|
if newNode.DNSOn != "yes" && shouldDNSChange && nodeCfg.Node.Interface != "" {
|
||||||
ncutils.Log("settng DNS off")
|
ncutils.Log("settng DNS off")
|
||||||
if err := removeHostDNS(ncutils.IsWindows()); err != nil {
|
if err := removeHostDNS(nodeCfg.Network, ncutils.IsWindows()); err != nil {
|
||||||
ncutils.Log("error removing netmaker profile from /etc/hosts " + err.Error())
|
ncutils.Log("error removing netmaker profile from /etc/hosts " + err.Error())
|
||||||
}
|
}
|
||||||
// _, err := ncutils.RunCmd("/usr/bin/resolvectl revert "+nodeCfg.Node.Interface, true)
|
// _, err := ncutils.RunCmd("/usr/bin/resolvectl revert "+nodeCfg.Node.Interface, true)
|
||||||
|
@ -201,24 +201,24 @@ func UpdatePeers(client mqtt.Client, msg mqtt.Message) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if cfg.Node.DNSOn == "yes" {
|
if cfg.Node.DNSOn == "yes" {
|
||||||
if err := setHostDNS(peerUpdate.DNS, ncutils.IsWindows()); err != nil {
|
if err := setHostDNS(peerUpdate.DNS, cfg.Node.Network, ncutils.IsWindows()); err != nil {
|
||||||
ncutils.Log("error updating /etc/hosts " + err.Error())
|
ncutils.Log("error updating /etc/hosts " + err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err := removeHostDNS(ncutils.IsWindows()); err != nil {
|
if err := removeHostDNS(cfg.Node.Network, ncutils.IsWindows()); err != nil {
|
||||||
ncutils.Log("error removing netmaker profile from /etc/hosts " + err.Error())
|
ncutils.Log("error removing profile from /etc/hosts " + err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setHostDNS(dns []byte, windows bool) error {
|
func setHostDNS(dns, network string, windows bool) error {
|
||||||
etchosts := "/etc/hosts"
|
etchosts := "/etc/hosts"
|
||||||
if windows {
|
if windows {
|
||||||
etchosts = "c:\\windows\\system32\\drivers\\etc\\hosts"
|
etchosts = "c:\\windows\\system32\\drivers\\etc\\hosts"
|
||||||
}
|
}
|
||||||
dnsdata := strings.NewReader(string(dns))
|
dnsdata := strings.NewReader(dns)
|
||||||
profile, err := parser.ParseProfile(dnsdata)
|
profile, err := parser.ParseProfile(dnsdata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -227,7 +227,7 @@ func setHostDNS(dns []byte, windows bool) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
profile.Name = "netmaker"
|
profile.Name = network
|
||||||
profile.Status = types.Enabled
|
profile.Status = types.Enabled
|
||||||
if err := hosts.ReplaceProfile(profile); err != nil {
|
if err := hosts.ReplaceProfile(profile); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -238,7 +238,7 @@ func setHostDNS(dns []byte, windows bool) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeHostDNS(windows bool) error {
|
func removeHostDNS(network string, windows bool) error {
|
||||||
etchosts := "/etc/hosts"
|
etchosts := "/etc/hosts"
|
||||||
if windows {
|
if windows {
|
||||||
etchosts = "c:\\windows\\system32\\drivers\\etc\\hosts"
|
etchosts = "c:\\windows\\system32\\drivers\\etc\\hosts"
|
||||||
|
@ -247,7 +247,7 @@ func removeHostDNS(windows bool) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := hosts.RemoveProfile("netmaker"); err != nil {
|
if err := hosts.RemoveProfile(network); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := hosts.Flush(); err != nil {
|
if err := hosts.Flush(); err != nil {
|
||||||
|
|
Loading…
Add table
Reference in a new issue