2023-08-27 14:09:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-06-10 16:56:36 +00:00
|
|
|
"isle/jsonutil"
|
2024-07-12 14:11:42 +00:00
|
|
|
"isle/nebula"
|
2023-08-27 14:09:03 +00:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
var subCmdNebulaShow = subCmd{
|
|
|
|
name: "show",
|
2024-06-10 16:56:36 +00:00
|
|
|
descr: "Writes nebula network information to stdout in JSON format",
|
2023-08-27 14:09:03 +00:00
|
|
|
do: func(subCmdCtx subCmdCtx) error {
|
|
|
|
|
|
|
|
flags := subCmdCtx.flagSet(false)
|
|
|
|
if err := flags.Parse(subCmdCtx.args); err != nil {
|
|
|
|
return fmt.Errorf("parsing flags: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-07-12 14:11:42 +00:00
|
|
|
hosts, err := subCmdCtx.getHosts()
|
2023-08-27 14:09:03 +00:00
|
|
|
if err != nil {
|
2024-07-12 14:11:42 +00:00
|
|
|
return fmt.Errorf("getting hosts: %w", err)
|
2023-08-27 14:09:03 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 14:11:42 +00:00
|
|
|
var caPublicCreds nebula.CAPublicCredentials
|
|
|
|
err = subCmdCtx.daemonRCPClient.Call(
|
|
|
|
subCmdCtx.ctx, &caPublicCreds, "GetNebulaCAPublicCredentials", nil,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("calling GetNebulaCAPublicCredentials: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
caCert := caPublicCreds.Cert.Unwrap()
|
2024-06-15 21:02:24 +00:00
|
|
|
caCertPEM, err := caCert.MarshalToPEM()
|
2023-08-27 14:09:03 +00:00
|
|
|
if err != nil {
|
2024-06-15 21:02:24 +00:00
|
|
|
return fmt.Errorf("marshaling CA cert to PEM: %w", err)
|
2023-08-27 14:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(caCert.Details.Subnets) != 1 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"malformed ca.crt, contains unexpected subnets %#v",
|
|
|
|
caCert.Details.Subnets,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
subnet := caCert.Details.Subnets[0]
|
|
|
|
|
|
|
|
type outLighthouse struct {
|
2024-06-10 16:56:36 +00:00
|
|
|
PublicAddr string
|
|
|
|
IP string
|
2023-08-27 14:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out := struct {
|
2024-06-10 16:56:36 +00:00
|
|
|
CACert string
|
|
|
|
SubnetCIDR string
|
|
|
|
Lighthouses []outLighthouse
|
2023-08-27 14:09:03 +00:00
|
|
|
}{
|
2024-06-15 21:02:24 +00:00
|
|
|
CACert: string(caCertPEM),
|
2023-08-27 14:09:03 +00:00
|
|
|
SubnetCIDR: subnet.String(),
|
|
|
|
}
|
|
|
|
|
2024-07-12 14:11:42 +00:00
|
|
|
for _, h := range hosts.Hosts {
|
2023-08-27 14:09:03 +00:00
|
|
|
if h.Nebula.PublicAddr == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
out.Lighthouses = append(out.Lighthouses, outLighthouse{
|
|
|
|
PublicAddr: h.Nebula.PublicAddr,
|
|
|
|
IP: h.IP().String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-06-10 16:56:36 +00:00
|
|
|
if err := jsonutil.WriteIndented(os.Stdout, out); err != nil {
|
|
|
|
return fmt.Errorf("encoding to stdout: %w", err)
|
2023-08-27 14:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var subCmdNebula = subCmd{
|
|
|
|
name: "nebula",
|
|
|
|
descr: "Sub-commands related to the nebula VPN",
|
|
|
|
do: func(subCmdCtx subCmdCtx) error {
|
|
|
|
return subCmdCtx.doSubCmd(
|
|
|
|
subCmdNebulaShow,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|