2024-12-16 11:19:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var subCmdVPNPublicAddressGet = subCmd{
|
|
|
|
name: "get",
|
|
|
|
descr: "Display the currently configured public address",
|
|
|
|
do: doWithOutput(func(ctx subCmdCtx) (any, error) {
|
|
|
|
ctx, err := ctx.withParsedFlags(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parsing flags: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-12-16 13:59:11 +00:00
|
|
|
daemonRPC, err := ctx.newDaemonRPC()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("creating daemon RPC client: %w", err)
|
|
|
|
}
|
|
|
|
defer daemonRPC.Close()
|
|
|
|
|
|
|
|
config, err := daemonRPC.GetConfig(ctx)
|
2024-12-16 11:19:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting network config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.VPN.PublicAddr == "" {
|
|
|
|
return nil, errors.New("No public address configured")
|
|
|
|
}
|
|
|
|
|
|
|
|
return config.VPN.PublicAddr, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
var subCmdVPNPublicAddressSet = subCmd{
|
|
|
|
name: "set",
|
|
|
|
descr: "Set the public address of the host, or overwrite the already configured one",
|
|
|
|
do: func(ctx subCmdCtx) error {
|
2024-12-17 10:33:19 +00:00
|
|
|
var publicAddr addrFlag
|
|
|
|
|
|
|
|
publicAddrF := ctx.flags.VarPF(
|
|
|
|
&publicAddr,
|
|
|
|
"to",
|
2024-12-16 11:19:48 +00:00
|
|
|
"",
|
|
|
|
"Public address (host:port) that this host is publicly available on",
|
|
|
|
)
|
|
|
|
|
|
|
|
ctx, err := ctx.withParsedFlags(nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing flags: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-12-17 10:33:19 +00:00
|
|
|
if !publicAddrF.Changed {
|
2024-12-16 11:19:48 +00:00
|
|
|
return errors.New("--public-addr is required")
|
|
|
|
}
|
|
|
|
|
2024-12-16 13:59:11 +00:00
|
|
|
daemonRPC, err := ctx.newDaemonRPC()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating daemon RPC client: %w", err)
|
|
|
|
}
|
|
|
|
defer daemonRPC.Close()
|
|
|
|
|
|
|
|
config, err := daemonRPC.GetConfig(ctx)
|
2024-12-16 11:19:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting network config: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-12-17 10:33:19 +00:00
|
|
|
config.VPN.PublicAddr = string(publicAddr.V)
|
2024-12-16 11:19:48 +00:00
|
|
|
|
2024-12-16 13:59:11 +00:00
|
|
|
return daemonRPC.SetConfig(ctx, config)
|
2024-12-16 11:19:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var subCmdVPNPublicAddressUnset = subCmd{
|
|
|
|
name: "unset",
|
|
|
|
descr: "Unset the public address",
|
|
|
|
do: func(ctx subCmdCtx) error {
|
|
|
|
ctx, err := ctx.withParsedFlags(nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing flags: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-12-16 13:59:11 +00:00
|
|
|
daemonRPC, err := ctx.newDaemonRPC()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating daemon RPC client: %w", err)
|
|
|
|
}
|
|
|
|
defer daemonRPC.Close()
|
|
|
|
|
|
|
|
config, err := daemonRPC.GetConfig(ctx)
|
2024-12-16 11:19:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting network config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config.VPN.PublicAddr = ""
|
|
|
|
|
2024-12-16 13:59:11 +00:00
|
|
|
return daemonRPC.SetConfig(ctx, config)
|
2024-12-16 11:19:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var subCmdVPNPublicAddress = subCmd{
|
|
|
|
name: "public-address",
|
|
|
|
descr: "Configure the public address of this host, allowing other hosts to use it in order to find and communicate directly with each other",
|
|
|
|
do: func(ctx subCmdCtx) error {
|
|
|
|
return ctx.doSubCmd(
|
|
|
|
subCmdVPNPublicAddressGet,
|
|
|
|
subCmdVPNPublicAddressSet,
|
|
|
|
subCmdVPNPublicAddressUnset,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|