Skip to content

Commit

Permalink
Use LAN discovery at the daemon startup
Browse files Browse the repository at this point in the history
Signed-off-by: Marius Sincovici <marius.sincovici@nordsec.com>

When daemon starts check the LAN discovery setting and when is true add
the LAN IPs to the allowed list.
  • Loading branch information
mariusSincovici committed May 13, 2024
1 parent 3a008b7 commit cf62056
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/daemon/events_linux.go
Expand Up @@ -33,6 +33,7 @@ func (*dummyAnalytics) NotifyAccountCheck(core.ServicesResponse) error { return
func (*dummyAnalytics) NotifyRequestAPI(events.DataRequestAPI) error { return nil }
func (*dummyAnalytics) NotifyRate(events.ServerRating) error { return nil }
func (*dummyAnalytics) NotifyHeartBeat(int) error { return nil }
func (*dummyAnalytics) NotifyLANDiscovery(bool) error { return nil }

func newAnalytics(eventsDbPath string, fs *config.FilesystemConfigManager,
version, env, id string) *dummyAnalytics {
Expand Down
1 change: 1 addition & 0 deletions cmd/daemon/main.go
Expand Up @@ -159,6 +159,7 @@ func main() {
&subs.Subject[core.ServicesResponse]{},
&subs.Subject[events.ServerRating]{},
&subs.Subject[int]{},
&subs.Subject[bool]{},
)
meshnetEvents := meshnet.NewEvents(
&subs.Subject[[]string]{},
Expand Down
6 changes: 6 additions & 0 deletions daemon/events.go
Expand Up @@ -33,6 +33,7 @@ func NewEvents(
accountCheck events.PublishSubcriber[core.ServicesResponse],
rate events.PublishSubcriber[events.ServerRating],
heartBeat events.PublishSubcriber[int],
lanDiscovery events.PublishSubcriber[bool],
) *Events {
return &Events{
Settings: &SettingsEvents{
Expand All @@ -50,6 +51,7 @@ func NewEvents(
Meshnet: meshnet,
Ipv6: ipv6,
Defaults: defaults,
LANDiscovery: lanDiscovery,
},
Service: &ServiceEvents{
Connect: connect,
Expand Down Expand Up @@ -87,6 +89,7 @@ type SettingsPublisher interface {
NotifyMeshnet(bool) error
NotifyIpv6(bool) error
NotifyDefaults(any) error
NotifyLANDiscovery(bool) error
}

type SettingsEvents struct {
Expand All @@ -104,6 +107,7 @@ type SettingsEvents struct {
Meshnet events.PublishSubcriber[bool]
Ipv6 events.PublishSubcriber[bool]
Defaults events.PublishSubcriber[any]
LANDiscovery events.PublishSubcriber[bool]
}

func (s *SettingsEvents) Subscribe(to SettingsPublisher) {
Expand All @@ -121,6 +125,7 @@ func (s *SettingsEvents) Subscribe(to SettingsPublisher) {
s.Meshnet.Subscribe(to.NotifyMeshnet)
s.Ipv6.Subscribe(to.NotifyIpv6)
s.Defaults.Subscribe(to.NotifyDefaults)
s.LANDiscovery.Subscribe(to.NotifyLANDiscovery)
}

type ServicePublisher interface {
Expand Down Expand Up @@ -168,4 +173,5 @@ func (s *SettingsEvents) Publish(cfg config.Config) {
s.Technology.Publish(cfg.Technology)
s.Obfuscate.Publish(cfg.AutoConnectData.Obfuscate)
s.Notify.Publish(cfg.UsersData.Notify != nil && len(cfg.UsersData.Notify) > 0)
s.LANDiscovery.Publish(cfg.LanDiscovery)
}
3 changes: 3 additions & 0 deletions daemon/events_test.go
Expand Up @@ -38,6 +38,7 @@ func TestNewDaemonSubjects(t *testing.T) {
&subs.Subject[core.ServicesResponse]{},
&subs.Subject[events.ServerRating]{},
&subs.Subject[int]{},
&subs.Subject[bool]{},
))
assert.True(t, valid)
}
Expand Down Expand Up @@ -66,6 +67,7 @@ func TestDaemonSubjectsSubscribe(t *testing.T) {
&subs.Subject[core.ServicesResponse]{},
&subs.Subject[events.ServerRating]{},
&subs.Subject[int]{},
&subs.Subject[bool]{},
)
subjects.Subscribe(&mockDaemonSubscriber{})
_, min := isValid(subjects)
Expand Down Expand Up @@ -94,6 +96,7 @@ func (mockDaemonSubscriber) NotifyDefaults(any) error { re
func (mockDaemonSubscriber) NotifyMeshnet(bool) error { return nil }
func (mockDaemonSubscriber) NotifyRate(events.ServerRating) error { return nil }
func (mockDaemonSubscriber) NotifyHeartBeat(int) error { return nil }
func (mockDaemonSubscriber) NotifyLANDiscovery(bool) error { return nil }

// isValid returns true if given val is not nil. In case val is struct,
// it checks if any of exported fields are not nil
Expand Down
6 changes: 5 additions & 1 deletion daemon/jobs.go
Expand Up @@ -61,7 +61,11 @@ func (r *RPC) StartKillSwitch() {
}

if cfg.KillSwitch {
if err := r.netw.SetKillSwitch(cfg.AutoConnectData.Allowlist); err != nil {
allowlist := cfg.AutoConnectData.Allowlist
if cfg.LanDiscovery {
allowlist = addLANPermissions(allowlist)
}
if err := r.netw.SetKillSwitch(allowlist); err != nil {
log.Println(internal.ErrorPrefix, "starting killswitch:", err)
return
}
Expand Down
2 changes: 2 additions & 0 deletions daemon/jobs_test.go
Expand Up @@ -122,6 +122,7 @@ func TestStartAutoConnect(t *testing.T) {
&subs.Subject[core.ServicesResponse]{},
&subs.Subject[events.ServerRating]{},
&subs.Subject[int]{},
&subs.Subject[bool]{},
),
func(config.Technology) (vpn.VPN, error) {
return &mock.WorkingVPN{}, nil
Expand Down Expand Up @@ -308,6 +309,7 @@ func TestStartAutoMeshnet(t *testing.T) {
&subs.Subject[core.ServicesResponse]{},
&subs.Subject[events.ServerRating]{},
&subs.Subject[int]{},
&subs.Subject[bool]{},
),
func(config.Technology) (vpn.VPN, error) {
return &mock.WorkingVPN{}, nil
Expand Down
2 changes: 2 additions & 0 deletions daemon/rpc_connect_test.go
Expand Up @@ -198,6 +198,7 @@ func TestRpcConnect(t *testing.T) {
&subs.Subject[core.ServicesResponse]{},
&subs.Subject[events.ServerRating]{},
&subs.Subject[int]{},
&subs.Subject[bool]{},
),
test.factory,
newEndpointResolverMock(netip.MustParseAddr("127.0.0.1")),
Expand Down Expand Up @@ -277,6 +278,7 @@ func TestRpcReconnect(t *testing.T) {
&subs.Subject[core.ServicesResponse]{},
&subs.Subject[events.ServerRating]{},
&subs.Subject[int]{},
&subs.Subject[bool]{},
),
factory,
newEndpointResolverMock(netip.MustParseAddr("127.0.0.1")),
Expand Down
5 changes: 5 additions & 0 deletions events/logger/settings.go
Expand Up @@ -85,6 +85,11 @@ func (l *DaemonSettingsSubscriber) NotifyDefaults(any) error {
return nil
}

func (l *DaemonSettingsSubscriber) NotifyLANDiscovery(data bool) error {
printSettingsChange("LAN Discovery", boolToString(data))
return nil
}

func printSettingsChange(settingName string, val string) {
log.Printf("%s set to: %s", settingName, val)
}
Expand Down
4 changes: 4 additions & 0 deletions events/moose/moose.go
Expand Up @@ -563,3 +563,7 @@ func DrainStart(dbPath string) uint {
false,
)
}

func (s *Subscriber) NotifyLANDiscovery(enabled bool) error {
return nil
}

0 comments on commit cf62056

Please sign in to comment.