2021-04-20 21:31:37 +00:00
|
|
|
package nebula_entrypoint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cryptic-net/yamlutil"
|
|
|
|
"log"
|
|
|
|
"net"
|
2022-10-15 16:41:07 +00:00
|
|
|
"os"
|
2021-04-20 21:31:37 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2022-10-15 16:41:07 +00:00
|
|
|
"syscall"
|
2021-04-20 21:31:37 +00:00
|
|
|
|
|
|
|
crypticnet "cryptic-net"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Main() {
|
|
|
|
|
|
|
|
env, err := crypticnet.ReadEnv()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("reading envvars: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
lighthouseHostIPs []string
|
|
|
|
staticHostMap = map[string][]string{}
|
|
|
|
)
|
|
|
|
|
2022-10-15 14:28:03 +00:00
|
|
|
for _, host := range env.Bootstrap.Hosts {
|
2021-04-20 21:31:37 +00:00
|
|
|
|
|
|
|
if host.Nebula.PublicAddr == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
lighthouseHostIPs = append(lighthouseHostIPs, host.Nebula.IP)
|
|
|
|
staticHostMap[host.Nebula.IP] = []string{host.Nebula.PublicAddr}
|
|
|
|
}
|
|
|
|
|
|
|
|
config := map[string]interface{}{
|
|
|
|
"pki": map[string]string{
|
2022-10-16 14:39:05 +00:00
|
|
|
"ca": env.Bootstrap.NebulaHostCert.CACert,
|
|
|
|
"cert": env.Bootstrap.NebulaHostCert.HostCert,
|
|
|
|
"key": env.Bootstrap.NebulaHostCert.HostKey,
|
2021-04-20 21:31:37 +00:00
|
|
|
},
|
|
|
|
"static_host_map": staticHostMap,
|
|
|
|
"punchy": map[string]bool{
|
|
|
|
"punch": true,
|
|
|
|
"respond": true,
|
|
|
|
},
|
|
|
|
"tun": map[string]interface{}{
|
|
|
|
"dev": "cryptic-nebula1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if publicAddr := env.ThisDaemon().VPN.PublicAddr; publicAddr == "" {
|
|
|
|
|
|
|
|
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 {
|
|
|
|
log.Fatalf("parsing public address %q: %v", publicAddr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config["listen"] = map[string]string{
|
|
|
|
"host": "0.0.0.0",
|
|
|
|
"port": port,
|
|
|
|
}
|
|
|
|
|
|
|
|
config["lighthouse"] = map[string]interface{}{
|
|
|
|
"hosts": []string{},
|
|
|
|
"am_lighthouse": true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
thisDaemon := env.ThisDaemon()
|
|
|
|
|
|
|
|
var firewallInbound []crypticnet.ConfigFirewallRule
|
|
|
|
|
|
|
|
for _, alloc := range thisDaemon.Storage.Allocations {
|
|
|
|
firewallInbound = append(
|
|
|
|
firewallInbound,
|
|
|
|
crypticnet.ConfigFirewallRule{
|
2022-10-15 14:28:03 +00:00
|
|
|
Port: strconv.Itoa(alloc.S3APIPort),
|
2021-04-20 21:31:37 +00:00
|
|
|
Proto: "tcp",
|
|
|
|
Host: "any",
|
|
|
|
},
|
|
|
|
crypticnet.ConfigFirewallRule{
|
|
|
|
Port: strconv.Itoa(alloc.RPCPort),
|
|
|
|
Proto: "tcp",
|
|
|
|
Host: "any",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
firewall := thisDaemon.VPN.Firewall
|
|
|
|
|
|
|
|
firewall.Inbound = append(firewallInbound, firewall.Inbound...)
|
|
|
|
|
|
|
|
config["firewall"] = firewall
|
|
|
|
|
|
|
|
nebulaYmlPath := filepath.Join(env.RuntimeDirPath, "nebula.yml")
|
|
|
|
|
|
|
|
if err := yamlutil.WriteYamlFile(config, nebulaYmlPath); err != nil {
|
|
|
|
log.Fatalf("writing nebula.yml to %q: %v", nebulaYmlPath, err)
|
|
|
|
}
|
|
|
|
|
2022-10-15 16:41:07 +00:00
|
|
|
var (
|
|
|
|
binPath = env.BinPath("nebula")
|
|
|
|
args = []string{"nebula", "-config", nebulaYmlPath}
|
|
|
|
cliEnv = os.Environ()
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := syscall.Exec(binPath, args, cliEnv); err != nil {
|
|
|
|
log.Fatalf("calling exec(%q, %#v, %#v)", binPath, args, cliEnv)
|
|
|
|
}
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|