Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NET-812: deprecate setting DNS entries on hosts file #655

Merged
merged 5 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ func InitConfig(viper *viper.Viper) {
logger.FatalLog("could not create /etc/netclient dir" + err.Error())
}
}
//wireguard.WriteWgConfig(Netclient(), GetNodes())
}

func setupLogging(flags *viper.Viper) {
Expand Down
11 changes: 1 addition & 10 deletions functions/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type cachedMessage struct {
func Daemon() {
slog.Info("starting netclient daemon", "version", config.Version)
daemon.RemoveAllLockFiles()
go deleteAllDNS()
if err := ncutils.SavePID(); err != nil {
slog.Error("unable to save PID on daemon startup", "error", err)
os.Exit(1)
Expand Down Expand Up @@ -319,16 +320,6 @@ func setHostSubscription(client mqtt.Client, server string) {
slog.Error("unable to subscribe to host updates", "host", hostID, "server", server, "error", token.Error())
return
}
slog.Info("subscribing to dns updates for", "host", hostID, "server", server)
if token := client.Subscribe(fmt.Sprintf("dns/update/%s/%s", hostID.String(), server), 0, mqtt.MessageHandler(dnsUpdate)); token.Wait() && token.Error() != nil {
slog.Error("unable to subscribe to dns updates", "host", hostID, "server", server, "error", token.Error())
return
}
slog.Info("subscribing to all dns updates for", "host", hostID, "server", server)
if token := client.Subscribe(fmt.Sprintf("dns/all/%s/%s", hostID.String(), server), 0, mqtt.MessageHandler(dnsAll)); token.Wait() && token.Error() != nil {
slog.Error("unable to subscribe to all dns updates", "host", hostID, "server", server, "error", token.Error())
return
}

}

Expand Down
36 changes: 0 additions & 36 deletions functions/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,39 +68,3 @@ func deleteAllDNS() error {
}
return nil
}

func deleteNetworkDNS(network string) error {
temp := os.TempDir()
lockfile := temp + "/netclient-lock"
if err := config.Lock(lockfile); err != nil {
return err
}
defer config.Unlock(lockfile)
hosts, err := txeh.NewHostsDefault()
if err != nil {
return err
}
lines := hosts.GetHostFileLines()
addressesToRemove := []string{}
for _, line := range *lines {
if line.Comment == etcHostsComment {
if sliceContains(line.Hostnames, network) {
addressesToRemove = append(addressesToRemove, line.Address)
}
}
}
hosts.RemoveAddresses(addressesToRemove, etcHostsComment)
if err := hosts.Save(); err != nil {
return err
}
return nil
}

func sliceContains(s []string, v string) bool {
for _, e := range s {
if strings.Contains(e, v) {
return true
}
}
return false
}
139 changes: 0 additions & 139 deletions functions/mqhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package functions

import (
"encoding/json"
"log"
"net"
"os"
"strings"
"time"

Expand All @@ -16,7 +14,6 @@ import (
"github.com/gravitl/netclient/networking"
"github.com/gravitl/netclient/wireguard"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/txeh"
"golang.org/x/exp/slog"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
Expand Down Expand Up @@ -353,142 +350,6 @@ func parseServerFromTopic(topic string) string {
return strings.Split(topic, "/")[3]
}

// dnsUpdate - mq handler for host update dns/<HOSTID>/server
func dnsUpdate(client mqtt.Client, msg mqtt.Message) {
temp := os.TempDir()
lockfile := temp + "/netclient-lock"
if err := config.Lock(lockfile); err != nil {
slog.Error("could not create lock file", "error", err)
return
}
defer config.Unlock(lockfile)
var dns models.DNSUpdate
serverName := parseServerFromTopic(msg.Topic())
server := config.GetServer(serverName)
if server == nil {
slog.Error("server not found in config", "server", serverName)
return
}
data, err := decryptMsg(serverName, msg.Payload())
if err != nil {
return
}
if err := json.Unmarshal([]byte(data), &dns); err != nil {
slog.Error("error unmarshalling dns update", "error", err)
}
if config.Netclient().Debug {
log.Println("dnsUpdate received", dns)
}
var currentMessage = read("dns", lastDNSUpdate)
if currentMessage == string(data) {
slog.Info("cache hit on dns update ... skipping")
return
}
insert("dns", lastDNSUpdate, string(data))
slog.Info("received dns update", "name", dns.Name, "address", dns.Address, "action", dns.Action)
applyDNSUpdate(dns)
}

func applyDNSUpdate(dns models.DNSUpdate) {
if config.Netclient().Debug {
log.Println(dns)
}
hosts, err := txeh.NewHostsDefault()
if err != nil {
slog.Error("failed to read hosts file", "error", err)
return
}
switch dns.Action {
case models.DNSInsert:
// remove any existing entries
hosts.RemoveHost(dns.Name, etcHostsComment)
hosts.RemoveAddress(dns.Address, etcHostsComment)
hosts.AddHost(dns.Address, dns.Name, etcHostsComment)
case models.DNSDeleteByName:
hosts.RemoveHost(dns.Name, etcHostsComment)
case models.DNSDeleteByIP:
hosts.RemoveAddress(dns.Address, etcHostsComment)
case models.DNSReplaceName:
ok, ip, _ := hosts.HostAddressLookup(dns.Name, txeh.IPFamilyV4, etcHostsComment)
if !ok {
slog.Error("failed to find dns address for host", "host", dns.Name)
return
}
dns.Address = ip
hosts.RemoveHost(dns.Name, etcHostsComment)
hosts.AddHost(dns.Address, dns.NewName, etcHostsComment)
case models.DNSReplaceIP:
hosts.RemoveAddress(dns.Address, etcHostsComment)
hosts.AddHost(dns.NewAddress, dns.Name, etcHostsComment)
}
if err := hosts.Save(); err != nil {
slog.Error("error saving hosts file", "error", err)
return
}
}

// dnsAll- mq handler for host update dnsall/<HOSTID>/server
func dnsAll(client mqtt.Client, msg mqtt.Message) {
temp := os.TempDir()
lockfile := temp + "/netclient-lock"
if err := config.Lock(lockfile); err != nil {
slog.Error("could not create lock file", "error", err)
return
}
defer config.Unlock(lockfile)
var dns []models.DNSUpdate
serverName := parseServerFromTopic(msg.Topic())
server := config.GetServer(serverName)
if server == nil {
slog.Error("server not found in config", "server", serverName)
return
}
data, err := decryptMsg(serverName, msg.Payload())
if err != nil {
return
}
if err := json.Unmarshal([]byte(data), &dns); err != nil {
slog.Error("error unmarshalling dns update", "error", err)
}
if config.Netclient().Debug {
log.Println("all dns", dns)
}
var currentMessage = read("dnsall", lastALLDNSUpdate)
slog.Info("received initial dns", "dns", dns)
if currentMessage == string(data) {
slog.Info("cache hit on all dns ... skipping")
if config.Netclient().Debug {
log.Println("dns cache", currentMessage, string(data))
}
return
}
insert("dnsall", lastALLDNSUpdate, string(data))
applyAllDNS(dns)
}

func applyAllDNS(dns []models.DNSUpdate) {
hosts, err := txeh.NewHostsDefault()
if err != nil {
slog.Error("failed to read hosts file", "error", err)
return
}
for _, entry := range dns {
if entry.Action != models.DNSInsert {
slog.Info("invalid dns actions", "action", entry.Action)
continue
}
// remove any existing entries
hosts.RemoveHost(entry.Name, etcHostsComment)
hosts.RemoveAddress(entry.Address, etcHostsComment)
hosts.AddHost(entry.Address, entry.Name, etcHostsComment)
}

if err := hosts.Save(); err != nil {
slog.Error("error saving hosts file", "error", err)
return
}
}

func getAllAllowedIPs(peers []wgtypes.PeerConfig) (cidrs []net.IPNet) {
if len(peers) > 0 { // nil check
for i := range peers {
Expand Down
3 changes: 0 additions & 3 deletions functions/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ func LeaveNetwork(network string, isDaemon bool) ([]error, error) {
if err := deleteLocalNetwork(&node); err != nil {
faults = append(faults, fmt.Errorf("error deleting wireguard interface %w", err))
}
if err := deleteNetworkDNS(network); err != nil {
faults = append(faults, fmt.Errorf("error deleting dns entries %w", err))
}
// re-configure interface if daemon is calling leave
if isDaemon {
nc := wireguard.GetInterface()
Expand Down
Loading