-
Notifications
You must be signed in to change notification settings - Fork 43
/
cli_allowlist_remove_subnet.go
81 lines (67 loc) · 2.04 KB
/
cli_allowlist_remove_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
75
76
77
78
79
80
81
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 remove subnet help text
const (
AllowlistRemoveSubnetUsageText = "Removes subnet from the allowlist"
AllowlistRemoveSubnetArgsUsageText = `<address>`
AllowlistRemoveSubnetArgsDescription = `Use this command to remove subnet from the allowlist.
Example: 'nordvpn allowlist remove subnet 192.168.1.1/24'`
)
func (c *cmd) AllowlistRemoveSubnet(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()
subnetIndex := slices.Index(allowlist.Subnets, subnet.String())
if subnetIndex < 0 {
return formatError(fmt.Errorf(AllowlistRemoveSubnetExistsError, subnet.String()))
}
allowlist.Subnets = slices.Delete(allowlist.Subnets, subnetIndex, subnetIndex+1)
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(AllowlistRemoveSubnetExistsError, subnet))
case internal.CodeVPNMisconfig:
return formatError(internal.ErrUnhandled)
case internal.CodeSuccess:
color.Green(fmt.Sprintf(AllowlistRemoveSubnetSuccess, subnet))
}
return nil
}
func (c *cmd) AllowlistRemoveSubnetAutoComplete(ctx *cli.Context) {
settings, err := c.client.Settings(context.Background(), &pb.SettingsRequest{})
if err != nil {
return
}
allowlist := settings.GetData().GetAllowlist()
for _, subnet := range allowlist.Subnets {
if !slices.Contains(ctx.Args().Slice(), subnet) {
fmt.Println(subnet)
}
}
}