2022-10-20 19:59:46 +00:00
|
|
|
package main
|
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/daemon"
|
|
|
|
"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-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.
|
2022-10-26 22:23:39 +00:00
|
|
|
func waitForNebula(ctx context.Context, hostBootstrap bootstrap.Bootstrap) error {
|
2021-04-20 21:31:37 +00:00
|
|
|
|
2022-10-29 19:11:40 +00:00
|
|
|
ip := hostBootstrap.ThisHost().IP()
|
2021-04-20 21:31:37 +00:00
|
|
|
|
2022-10-20 19:59:46 +00:00
|
|
|
lUdpAddr := &net.UDPAddr{IP: ip, Port: 0}
|
|
|
|
rUdpAddr := &net.UDPAddr{IP: ip, Port: 45535}
|
|
|
|
|
|
|
|
return doOnce(ctx, func(context.Context) error {
|
|
|
|
conn, err := net.DialUDP("udp", lUdpAddr, rUdpAddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-26 21:21:31 +00:00
|
|
|
func nebulaPmuxProcConfig(
|
2022-10-26 22:23:39 +00:00
|
|
|
hostBootstrap bootstrap.Bootstrap,
|
|
|
|
daemonConfig daemon.Config,
|
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-15 21:02:24 +00:00
|
|
|
caCertPEM, err := hostBootstrap.CAPublicCredentials.Cert.MarshalToPEM()
|
|
|
|
if err != nil {
|
|
|
|
return pmuxlib.ProcessConfig{}, fmt.Errorf(
|
|
|
|
"marshaling CA cert to PEM: :%w", err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
hostCertPEM, err := hostBootstrap.PublicCredentials.Cert.MarshalToPEM()
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 22:37:03 +00:00
|
|
|
nebulaYmlPath := filepath.Join(envRuntimeDirPath, "nebula.yml")
|
2021-04-20 21:31:37 +00:00
|
|
|
|
2024-06-10 16:56:36 +00:00
|
|
|
if err := yamlutil.WriteYamlFile(config, nebulaYmlPath, 0440); 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",
|
2023-04-23 14:30:47 +00:00
|
|
|
Cmd: binPath("nebula"),
|
2022-10-20 19:59:46 +00:00
|
|
|
Args: []string{"-config", nebulaYmlPath},
|
|
|
|
}, nil
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|