2024-09-07 13:46:59 +00:00
|
|
|
package children
|
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"
|
2024-09-07 13:11:04 +00:00
|
|
|
"isle/daemon/daecommon"
|
2023-08-05 21:53:17 +00:00
|
|
|
"isle/dnsmasq"
|
2022-10-26 20:18:16 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
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 dnsmasqWriteConfig(
|
2024-10-27 13:31:10 +00:00
|
|
|
ctx context.Context,
|
|
|
|
logger *mlog.Logger,
|
2024-07-19 18:49:04 +00:00
|
|
|
runtimeDirPath string,
|
2024-09-10 20:51:33 +00:00
|
|
|
networkConfig daecommon.NetworkConfig,
|
2024-07-19 18:49:04 +00:00
|
|
|
hostBootstrap bootstrap.Bootstrap,
|
|
|
|
) (
|
2024-10-27 13:31:10 +00:00
|
|
|
string, bool, error,
|
2024-07-19 18:49:04 +00:00
|
|
|
) {
|
2024-10-27 13:31:10 +00:00
|
|
|
hosts := make([]dnsmasq.ConfDataHost, 0, len(hostBootstrap.Hosts))
|
|
|
|
for _, host := range hostBootstrap.Hosts {
|
|
|
|
hosts = append(hosts, dnsmasq.ConfDataHost{
|
|
|
|
Name: string(host.Name),
|
|
|
|
IP: host.IP().String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-19 18:49:04 +00:00
|
|
|
var (
|
|
|
|
confPath = filepath.Join(runtimeDirPath, "dnsmasq.conf")
|
2024-10-27 13:31:10 +00:00
|
|
|
confData = dnsmasq.ConfData{
|
|
|
|
Resolvers: networkConfig.DNS.Resolvers,
|
|
|
|
Domain: hostBootstrap.NetworkCreationParams.Domain,
|
|
|
|
IP: hostBootstrap.ThisHost().IP().String(),
|
|
|
|
Hosts: hosts,
|
|
|
|
}
|
2024-07-19 18:49:04 +00:00
|
|
|
)
|
2022-10-26 20:18:16 +00:00
|
|
|
|
2024-10-27 13:31:10 +00:00
|
|
|
changed, err := dnsmasq.WriteConfFile(ctx, logger, confPath, confData)
|
|
|
|
if err != nil {
|
|
|
|
return "", false, fmt.Errorf(
|
|
|
|
"writing dnsmasq.conf to %q: %w", confPath, err,
|
|
|
|
)
|
2024-07-19 18:49:04 +00:00
|
|
|
}
|
|
|
|
|
2024-10-27 13:31:10 +00:00
|
|
|
return confPath, changed, nil
|
2024-07-19 18:49:04 +00:00
|
|
|
}
|
|
|
|
|
2024-10-29 14:11:13 +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.
|
|
|
|
func dnsmasqPmuxProc(
|
2024-10-27 13:31:10 +00:00
|
|
|
ctx context.Context,
|
2024-07-19 18:49:04 +00:00
|
|
|
logger *mlog.Logger,
|
|
|
|
runtimeDirPath, binDirPath string,
|
2024-09-10 20:51:33 +00:00
|
|
|
networkConfig daecommon.NetworkConfig,
|
2024-07-19 18:49:04 +00:00
|
|
|
hostBootstrap bootstrap.Bootstrap,
|
|
|
|
) (
|
2024-10-29 14:11:13 +00:00
|
|
|
*pmuxlib.Process, error,
|
2024-07-19 18:49:04 +00:00
|
|
|
) {
|
2024-10-27 13:31:10 +00:00
|
|
|
confPath, _, err := dnsmasqWriteConfig(
|
|
|
|
ctx, logger, runtimeDirPath, networkConfig, hostBootstrap,
|
2024-07-19 18:49:04 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2024-10-29 14:11:13 +00:00
|
|
|
return nil, fmt.Errorf(
|
2024-07-19 18:49:04 +00:00
|
|
|
"writing dnsmasq config: %w", err,
|
|
|
|
)
|
2022-10-26 20:18:16 +00:00
|
|
|
}
|
|
|
|
|
2024-10-29 14:11:13 +00:00
|
|
|
cfg := 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-10-29 14:11:13 +00:00
|
|
|
}
|
|
|
|
cfg = withPmuxLoggers(ctx, logger, "dnsmasq", cfg)
|
|
|
|
|
|
|
|
return pmuxlib.NewProcess(cfg), nil
|
2022-10-26 20:18:16 +00:00
|
|
|
}
|