-
Notifications
You must be signed in to change notification settings - Fork 43
/
cli_allowlist_add_subnet.go
74 lines (59 loc) · 1.81 KB
/
cli_allowlist_add_subnet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package cli
import (
"context"
"fmt"
"net"
"github.com/NordSecurity/nordvpn-linux/daemon/pb"
"github.com/NordSecurity/nordvpn-linux/internal"
"golang.org/x/exp/slices"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
)
// Allowlist add subnet help text
const (
AllowlistAddSubnetUsageText = "Adds subnet to the allowlist"
AllowlistAddSubnetArgsUsageText = `<address>`
AllowlistAddSubnetDescription = `Use this command to allowlist subnet.
Example: 'nordvpn allowlist add subnet 192.168.1.1/24'
Notes:
Address should be in CIDR notation`
)
func (c *cmd) AllowlistAddSubnet(ctx *cli.Context) error {
args := ctx.Args()
if args.Len() != 1 {
return formatError(argsCountError(ctx))
}
_, subnet, err := net.ParseCIDR(args.First())
if err != nil {
return formatError(argsParseError(ctx))
}
settings, err := c.getSettings()
if err != nil {
return formatError(err)
}
allowlist := settings.GetAllowlist()
if slices.Contains(allowlist.Subnets, subnet.String()) {
return formatError(fmt.Errorf(AllowlistAddSubnetExistsError, subnet.String()))
}
allowlist.Subnets = append(allowlist.Subnets, subnet.String())
resp, err := c.client.SetAllowlist(context.Background(), &pb.SetAllowlistRequest{
Allowlist: allowlist,
})
if err != nil {
return formatError(err)
}
switch resp.Type {
case internal.CodeConfigError:
return formatError(ErrConfig)
case internal.CodeFailure:
return formatError(fmt.Errorf(AllowlistAddSubnetExistsError, subnet))
case internal.CodeVPNMisconfig:
return formatError(internal.ErrUnhandled)
case internal.CodePrivateSubnetLANDiscovery:
return formatError(fmt.Errorf(AllowlistAddSubnetLANDiscovery))
case internal.CodeSuccess:
color.Green(fmt.Sprintf(AllowlistAddSubnetSuccess, subnet))
}
return nil
}
func (c *cmd) AllowlistAddSubnetAutoComplete(ctx *cli.Context) {}