2024-06-17 18:51:02 +00:00
|
|
|
package daemon
|
2022-10-26 20:18:16 +00:00
|
|
|
|
|
|
|
import (
|
2024-07-19 14:50:20 +00:00
|
|
|
"context"
|
2024-06-17 18:51:02 +00:00
|
|
|
"fmt"
|
2023-08-05 21:53:17 +00:00
|
|
|
"isle/bootstrap"
|
|
|
|
"isle/dnsmasq"
|
2022-10-26 20:18:16 +00:00
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
|
2023-07-06 15:51:38 +00:00
|
|
|
"code.betamike.com/micropelago/pmux/pmuxlib"
|
2024-07-19 14:50:20 +00:00
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
|
2022-10-26 20:18:16 +00:00
|
|
|
)
|
|
|
|
|
2024-07-19 18:49:04 +00:00
|
|
|
func dnsmasqConfig(
|
|
|
|
daemonConfig Config, hostBootstrap bootstrap.Bootstrap,
|
|
|
|
) dnsmasq.ConfData {
|
2022-10-29 19:11:40 +00:00
|
|
|
hostsSlice := make([]dnsmasq.ConfDataHost, 0, len(hostBootstrap.Hosts))
|
2022-10-26 22:23:39 +00:00
|
|
|
for _, host := range hostBootstrap.Hosts {
|
2022-10-29 19:11:40 +00:00
|
|
|
hostsSlice = append(hostsSlice, dnsmasq.ConfDataHost{
|
2024-07-12 13:30:21 +00:00
|
|
|
Name: string(host.Name),
|
2022-10-29 19:11:40 +00:00
|
|
|
IP: host.IP().String(),
|
|
|
|
})
|
2022-10-26 20:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(hostsSlice, func(i, j int) bool {
|
2022-10-29 19:11:40 +00:00
|
|
|
return hostsSlice[i].IP < hostsSlice[j].IP
|
2022-10-26 20:18:16 +00:00
|
|
|
})
|
|
|
|
|
2024-07-19 18:49:04 +00:00
|
|
|
return dnsmasq.ConfData{
|
2022-10-26 21:21:31 +00:00
|
|
|
Resolvers: daemonConfig.DNS.Resolvers,
|
2024-07-14 11:11:18 +00:00
|
|
|
Domain: hostBootstrap.NetworkCreationParams.Domain,
|
2022-10-29 19:11:40 +00:00
|
|
|
IP: hostBootstrap.ThisHost().IP().String(),
|
2022-10-26 20:18:16 +00:00
|
|
|
Hosts: hostsSlice,
|
|
|
|
}
|
2024-07-19 18:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func dnsmasqWriteConfig(
|
|
|
|
runtimeDirPath string,
|
|
|
|
daemonConfig Config,
|
|
|
|
hostBootstrap bootstrap.Bootstrap,
|
|
|
|
) (
|
|
|
|
string, error,
|
|
|
|
) {
|
|
|
|
var (
|
|
|
|
confPath = filepath.Join(runtimeDirPath, "dnsmasq.conf")
|
|
|
|
confData = dnsmasqConfig(daemonConfig, hostBootstrap)
|
|
|
|
)
|
2022-10-26 20:18:16 +00:00
|
|
|
|
|
|
|
if err := dnsmasq.WriteConfFile(confPath, confData); err != nil {
|
2024-07-19 18:49:04 +00:00
|
|
|
return "", fmt.Errorf("writing dnsmasq.conf to %q: %w", confPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return confPath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dnsmasqPmuxProcConfig(
|
|
|
|
logger *mlog.Logger,
|
|
|
|
runtimeDirPath, binDirPath string,
|
|
|
|
daemonConfig Config,
|
|
|
|
hostBootstrap bootstrap.Bootstrap,
|
|
|
|
) (
|
|
|
|
pmuxlib.ProcessConfig, error,
|
|
|
|
) {
|
|
|
|
confPath, err := dnsmasqWriteConfig(
|
|
|
|
runtimeDirPath, daemonConfig, hostBootstrap,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return pmuxlib.ProcessConfig{}, fmt.Errorf(
|
|
|
|
"writing dnsmasq config: %w", err,
|
|
|
|
)
|
2022-10-26 20:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pmuxlib.ProcessConfig{
|
2024-06-17 18:51:02 +00:00
|
|
|
Cmd: filepath.Join(binDirPath, "dnsmasq"),
|
2022-10-26 20:18:16 +00:00
|
|
|
Args: []string{"-d", "-C", confPath},
|
2024-07-19 14:50:20 +00:00
|
|
|
StartAfterFunc: func(ctx context.Context) error {
|
2024-09-01 10:20:37 +00:00
|
|
|
// TODO consider a shared dnsmasq across all the daemon's networks.
|
|
|
|
// This would have a few benefits:
|
|
|
|
// - Less processes, less problems
|
|
|
|
// - Less configuration for the user in the case of more than one
|
|
|
|
// network.
|
|
|
|
// - Can listen on 127.0.0.x:53, rather than on the nebula address.
|
|
|
|
// This allows DNS to come up before nebula, which is helpful when
|
|
|
|
// nebula depends on DNS.
|
2024-07-19 14:50:20 +00:00
|
|
|
return waitForNebula(ctx, logger, hostBootstrap)
|
|
|
|
},
|
2022-10-26 20:18:16 +00:00
|
|
|
}, nil
|
|
|
|
}
|