/
rpc_set_lan_discovery.go
76 lines (65 loc) · 2.25 KB
/
rpc_set_lan_discovery.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
package daemon
import (
"context"
"log"
"net/netip"
"github.com/NordSecurity/nordvpn-linux/config"
"github.com/NordSecurity/nordvpn-linux/daemon/pb"
)
func (r *RPC) SetLANDiscovery(ctx context.Context, in *pb.SetLANDiscoveryRequest) (*pb.SetLANDiscoveryResponse, error) {
var cfg config.Config
err := r.cm.Load(&cfg)
if err != nil {
return &pb.SetLANDiscoveryResponse{
Response: &pb.SetLANDiscoveryResponse_ErrorCode{
ErrorCode: pb.SetErrorCode_CONFIG_ERROR,
}}, nil
}
if cfg.LanDiscovery == in.GetEnabled() {
return &pb.SetLANDiscoveryResponse{
Response: &pb.SetLANDiscoveryResponse_ErrorCode{
ErrorCode: pb.SetErrorCode_ALREADY_SET,
}}, nil
}
subnets := cfg.AutoConnectData.Allowlist.Subnets
allowlist := cfg.AutoConnectData.Allowlist
status := pb.SetLANDiscoveryStatus_DISCOVERY_CONFIGURED
r.netw.SetLanDiscovery(in.Enabled)
if in.GetEnabled() {
// Make a new list of allowlist of subnets based on the old allowlist, filter all of the
// private networks as they will be allowed by lan-discovery.
subnets = make(config.Subnets)
for subnet := range cfg.AutoConnectData.Allowlist.Subnets {
if prefix, err := netip.ParsePrefix(subnet); err != nil {
log.Println("Failed to parse subnet: ", err)
} else if !prefix.Addr().IsPrivate() && !prefix.Addr().IsLinkLocalUnicast() {
subnets[subnet] = true
} else {
status = pb.SetLANDiscoveryStatus_DISCOVERY_CONFIGURED_ALLOWLIST_RESET
}
}
cfg.AutoConnectData.Allowlist.Subnets = subnets
allowlist = addLANPermissions(cfg.AutoConnectData.Allowlist)
}
if err := r.netw.SetAllowlist(allowlist); err != nil {
log.Printf("Failed to set allowlist: %v", err)
return &pb.SetLANDiscoveryResponse{
Response: &pb.SetLANDiscoveryResponse_ErrorCode{
ErrorCode: pb.SetErrorCode_FAILURE,
},
}, nil
}
if err := r.cm.SaveWith(func(c config.Config) config.Config {
c.LanDiscovery = in.GetEnabled()
c.AutoConnectData.Allowlist.Subnets = subnets
return c
}); err != nil {
return &pb.SetLANDiscoveryResponse{
Response: &pb.SetLANDiscoveryResponse_ErrorCode{
ErrorCode: pb.SetErrorCode_CONFIG_ERROR,
}}, nil
}
return &pb.SetLANDiscoveryResponse{
Response: &pb.SetLANDiscoveryResponse_SetLanDiscoveryStatus{
SetLanDiscoveryStatus: status}}, nil
}