2024-06-17 18:51:02 +00:00
|
|
|
package daemon
|
2021-04-20 21:31:37 +00:00
|
|
|
|
|
|
|
import (
|
2022-10-20 19:59:46 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-08-05 21:53:17 +00:00
|
|
|
"isle/bootstrap"
|
|
|
|
"isle/yamlutil"
|
2021-04-20 21:31:37 +00:00
|
|
|
"net"
|
|
|
|
"path/filepath"
|
|
|
|
|
2023-07-06 15:51:38 +00:00
|
|
|
"code.betamike.com/micropelago/pmux/pmuxlib"
|
2024-07-06 13:36:48 +00:00
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mctx"
|
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
|
2024-06-15 21:02:24 +00:00
|
|
|
"github.com/slackhq/nebula/cert"
|
2021-04-20 21:31:37 +00:00
|
|
|
)
|
|
|
|
|
2022-10-20 19:59:46 +00:00
|
|
|
// waitForNebula waits for the nebula interface to have been started up. It does
|
|
|
|
// this by attempting to create a UDP connection which has the nebula IP set as
|
|
|
|
// its source. If this succeeds we can assume that at the very least the nebula
|
|
|
|
// interface has been initialized.
|
2024-06-17 18:51:02 +00:00
|
|
|
func waitForNebula(
|
2024-07-06 13:36:48 +00:00
|
|
|
ctx context.Context, logger *mlog.Logger, hostBootstrap bootstrap.Bootstrap,
|
2024-06-17 18:51:02 +00:00
|
|
|
) error {
|
2024-07-06 13:36:48 +00:00
|
|
|
var (
|
2024-07-13 14:08:13 +00:00
|
|
|
ip = net.IP(hostBootstrap.ThisHost().IP().AsSlice())
|
2024-07-06 13:36:48 +00:00
|
|
|
lUDPAddr = &net.UDPAddr{IP: ip, Port: 0}
|
|
|
|
rUDPAddr = &net.UDPAddr{IP: ip, Port: 45535}
|
|
|
|
)
|
2021-04-20 21:31:37 +00:00
|
|
|
|
2024-07-06 13:36:48 +00:00
|
|
|
ctx = mctx.Annotate(ctx, "lUDPAddr", lUDPAddr, "rUDPAddr", rUDPAddr)
|
2021-04-20 21:31:37 +00:00
|
|
|
|
2024-07-06 13:36:48 +00:00
|
|
|
until(
|
|
|
|
ctx,
|
|
|
|
logger,
|
|
|
|
"Creating UDP socket from nebula addr",
|
|
|
|
func(context.Context) error {
|
|
|
|
conn, err := net.DialUDP("udp", lUDPAddr, rUDPAddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
2022-10-20 19:59:46 +00:00
|
|
|
|
2024-07-06 13:36:48 +00:00
|
|
|
return ctx.Err()
|
2022-10-20 19:59:46 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 21:21:31 +00:00
|
|
|
func nebulaPmuxProcConfig(
|
2024-06-17 18:51:02 +00:00
|
|
|
runtimeDirPath, binDirPath string,
|
|
|
|
daemonConfig Config,
|
2024-07-06 13:36:48 +00:00
|
|
|
hostBootstrap bootstrap.Bootstrap,
|
2022-10-26 21:21:31 +00:00
|
|
|
) (
|
|
|
|
pmuxlib.ProcessConfig, error,
|
|
|
|
) {
|
2022-10-26 19:47:39 +00:00
|
|
|
|
2021-04-20 21:31:37 +00:00
|
|
|
var (
|
|
|
|
lighthouseHostIPs []string
|
|
|
|
staticHostMap = map[string][]string{}
|
|
|
|
)
|
|
|
|
|
2022-10-26 22:23:39 +00:00
|
|
|
for _, host := range hostBootstrap.Hosts {
|
2021-04-20 21:31:37 +00:00
|
|
|
|
|
|
|
if host.Nebula.PublicAddr == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-10-29 19:11:40 +00:00
|
|
|
ip := host.IP().String()
|
|
|
|
lighthouseHostIPs = append(lighthouseHostIPs, ip)
|
|
|
|
staticHostMap[ip] = []string{host.Nebula.PublicAddr}
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 12:37:10 +00:00
|
|
|
caCertPEM, err := hostBootstrap.CAPublicCredentials.Cert.Unwrap().MarshalToPEM()
|
2024-06-15 21:02:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return pmuxlib.ProcessConfig{}, fmt.Errorf(
|
|
|
|
"marshaling CA cert to PEM: :%w", err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-06-23 12:37:10 +00:00
|
|
|
hostCertPEM, err := hostBootstrap.PublicCredentials.Cert.Unwrap().MarshalToPEM()
|
2024-06-15 21:02:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return pmuxlib.ProcessConfig{}, fmt.Errorf(
|
|
|
|
"marshaling host cert to PEM: :%w", err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
hostKeyPEM := cert.MarshalX25519PrivateKey(
|
|
|
|
hostBootstrap.PrivateCredentials.EncryptingPrivateKey.Bytes(),
|
|
|
|
)
|
|
|
|
|
2021-04-20 21:31:37 +00:00
|
|
|
config := map[string]interface{}{
|
|
|
|
"pki": map[string]string{
|
2024-06-15 21:02:24 +00:00
|
|
|
"ca": string(caCertPEM),
|
|
|
|
"cert": string(hostCertPEM),
|
|
|
|
"key": string(hostKeyPEM),
|
2021-04-20 21:31:37 +00:00
|
|
|
},
|
|
|
|
"static_host_map": staticHostMap,
|
|
|
|
"punchy": map[string]bool{
|
|
|
|
"punch": true,
|
|
|
|
"respond": true,
|
|
|
|
},
|
|
|
|
"tun": map[string]interface{}{
|
2023-09-01 14:45:21 +00:00
|
|
|
"dev": daemonConfig.VPN.Tun.Device,
|
2021-04-20 21:31:37 +00:00
|
|
|
},
|
2022-10-26 21:21:31 +00:00
|
|
|
"firewall": daemonConfig.VPN.Firewall,
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 21:21:31 +00:00
|
|
|
if publicAddr := daemonConfig.VPN.PublicAddr; publicAddr == "" {
|
2021-04-20 21:31:37 +00:00
|
|
|
|
|
|
|
config["listen"] = map[string]string{
|
|
|
|
"host": "0.0.0.0",
|
|
|
|
"port": "0",
|
|
|
|
}
|
|
|
|
|
|
|
|
config["lighthouse"] = map[string]interface{}{
|
|
|
|
"hosts": lighthouseHostIPs,
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
_, port, err := net.SplitHostPort(publicAddr)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-10-20 19:59:46 +00:00
|
|
|
return pmuxlib.ProcessConfig{}, fmt.Errorf("parsing public address %q: %w", publicAddr, err)
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config["listen"] = map[string]string{
|
|
|
|
"host": "0.0.0.0",
|
|
|
|
"port": port,
|
|
|
|
}
|
|
|
|
|
|
|
|
config["lighthouse"] = map[string]interface{}{
|
|
|
|
"hosts": []string{},
|
|
|
|
"am_lighthouse": true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-17 18:51:02 +00:00
|
|
|
nebulaYmlPath := filepath.Join(runtimeDirPath, "nebula.yml")
|
2021-04-20 21:31:37 +00:00
|
|
|
|
2024-07-14 13:50:24 +00:00
|
|
|
if err := yamlutil.WriteYamlFile(config, nebulaYmlPath, 0600); err != nil {
|
2022-10-20 19:59:46 +00:00
|
|
|
return pmuxlib.ProcessConfig{}, fmt.Errorf("writing nebula.yml to %q: %w", nebulaYmlPath, err)
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 19:59:46 +00:00
|
|
|
return pmuxlib.ProcessConfig{
|
|
|
|
Name: "nebula",
|
2024-06-17 18:51:02 +00:00
|
|
|
Cmd: filepath.Join(binDirPath, "nebula"),
|
2022-10-20 19:59:46 +00:00
|
|
|
Args: []string{"-config", nebulaYmlPath},
|
|
|
|
}, nil
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|