Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show actual ip address of the user #734

Open
wants to merge 43 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
65dc7d9
add missing `ip` field in Insights
gbaranski Jan 8, 2025
38f861a
update insights data on connect
gbaranski Jan 8, 2025
334ee8f
use ip from insights data for rpc_status
gbaranski Jan 8, 2025
3b24e3a
job_actual_ip and actualIP in DataManager
gbaranski Jan 9, 2025
3f5e1a1
remove time.Sleep that was used for testing
gbaranski Jan 9, 2025
d994c3f
on disconnect, don't spawn a new goroutine
gbaranski Jan 9, 2025
7041597
no context cancellation in case of other events
gbaranski Jan 9, 2025
557ef1d
fix confusion of && and ||
gbaranski Jan 9, 2025
a0bde68
fix incorrect use of switch
gbaranski Jan 10, 2025
2521a58
insightsUntilSuccess -> insightsIPUntilSuccess
gbaranski Jan 10, 2025
b6ba8d0
fix: remove trailing newline
gbaranski Jan 10, 2025
5c02879
feat: add InsightsViaTunnel()
gbaranski Jan 13, 2025
6c594e1
feat: log error for actualIP()
gbaranski Jan 13, 2025
c2d42db
fix: mispelling of necessary
gbaranski Jan 13, 2025
d6c50d6
fix: make requests via api.doWithClient()
gbaranski Jan 13, 2025
0b0fb1a
style: remove trailing newline
gbaranski Jan 13, 2025
341aeb7
fix: make to actually use InsightsViaTunnel
gbaranski Jan 13, 2025
0262e66
fix: do not print error if it is ctx cancellation
gbaranski Jan 13, 2025
03e034a
test: TestInsightsIPUntilSuccess
gbaranski Jan 13, 2025
8cbf8c3
test: TestJobActualIP
gbaranski Jan 13, 2025
30915b3
fix: use errors.Is instead weird of comparsions
gbaranski Jan 14, 2025
8f3afa5
fix: utilize assert library for tests
gbaranski Jan 14, 2025
a40b324
fix: continue retrying after ip parse error
gbaranski Jan 14, 2025
8538a44
fix: call api in a goroutine
gbaranski Jan 14, 2025
c33d1f5
fix: put each attempt for api in goroutine
gbaranski Jan 14, 2025
8425660
fix: reorganize the job actual ip related functions
gbaranski Jan 15, 2025
a7d425b
feat: notifying about actual ip update through protobuf
gbaranski Jan 16, 2025
af4e60e
test: TestJobUpdateActualIP
gbaranski Jan 16, 2025
0f27891
test: testing ip status in qa tests
gbaranski Jan 16, 2025
07956a2
refactor: move JobActualIP out of jobs
gbaranski Jan 22, 2025
f2aa444
refactor: ActualIP -> ActualIPResolver
gbaranski Jan 22, 2025
e4171da
fix: handle context cancellation
gbaranski Jan 22, 2025
bda07b3
refactor: now call new fn name
gbaranski Jan 23, 2025
10fc237
fix: cleaner behaviour of update fn
gbaranski Jan 23, 2025
db6775c
fix: stop actual ip update if ctx cancelled
gbaranski Jan 23, 2025
b3513e8
fix: use a buffered channel for actual ip result
gbaranski Jan 24, 2025
60bb1b8
fix: simpliy if else
gbaranski Jan 24, 2025
84deb23
Merge branch 'main' into show-public-ip
gbaranski Jan 28, 2025
63ff929
chore: regenerate protobuf
gbaranski Jan 28, 2025
6230a7b
fix: skip connect events that are attempts
gbaranski Jan 28, 2025
84a37d4
test: add EventStatus to DataConnect
gbaranski Jan 28, 2025
89cdd98
Merge branch 'main' into show-public-ip
gbaranski Jan 31, 2025
e3d4d46
fix test
mariusSincovici Jan 31, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: cleaner behaviour of update fn
  • Loading branch information
gbaranski committed Jan 23, 2025
commit 10fc23702b78fe59aea2347ec7c35db538601523
35 changes: 13 additions & 22 deletions daemon/actual_ip_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,45 +68,36 @@ func insightsIPUntilSuccess(ctx context.Context, api core.InsightsAPI, backoff f
}
}

func updateActualIP(dm *DataManager, api core.InsightsAPI, ctx context.Context, isConnected bool) error {
func updateActualIP(statePublisher *state.StatePublisher, dm *DataManager, api core.InsightsAPI, ctx context.Context, isConnected bool) {
var newIP netip.Addr
defer func() {
dm.SetActualIP(newIP)
err := statePublisher.NotifyActualIPUpdate()
if err != nil {
log.Println(internal.ErrorPrefix, "notify about actual ip update failed: ", err)
}
}()

if !isConnected {
return nil
return
}

insightsIP, err := insightsIPUntilSuccess(ctx, api, network.ExponentialBackoff)
if err != nil {
return err
if err == context.Canceled {
return
}
log.Println(internal.ErrorPrefix, "actual ip job error: ", err)
return
}
if insightsIP.IsValid() {
newIP = insightsIP
}

return nil
}

// ActualIPResolver is a long-running function that will update the actual IP address indefinitely
// it reacts to state updates from the statePublisher
func ActualIPResolver(statePublisher *state.StatePublisher, dm *DataManager, api core.InsightsAPI) {
call := func(ctx context.Context, isConnected bool) {
err := updateActualIP(dm, api, ctx, isConnected)
if err == nil {
err := statePublisher.NotifyActualIPUpdate()
if err != nil {
log.Println(internal.ErrorPrefix, "notify about actual ip update failed: ", err)
}
} else {
if err == context.Canceled {
return
}
log.Println(internal.ErrorPrefix, "actual ip job error: ", err)
}
}

stateChan, _ := statePublisher.AddSubscriber()
var cancel context.CancelFunc

Expand All @@ -123,9 +114,9 @@ func ActualIPResolver(statePublisher *state.StatePublisher, dm *DataManager, api
ctx, cancel = context.WithCancel(context.Background())

if isConnect {
go call(ctx, true)
go updateActualIP(statePublisher, dm, api, ctx, true)
} else {
call(ctx, false) // should finish immediately, that's why it's not a separate goroutine
updateActualIP(statePublisher, dm, api, ctx, false) // should finish immediately, that's why it's not a separate goroutine
}
}
}
Expand Down
61 changes: 0 additions & 61 deletions daemon/actual_ip_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/NordSecurity/nordvpn-linux/core"
daemonEvents "github.com/NordSecurity/nordvpn-linux/daemon/events"
"github.com/NordSecurity/nordvpn-linux/daemon/pb"
"github.com/NordSecurity/nordvpn-linux/daemon/state"
"github.com/NordSecurity/nordvpn-linux/events"
Expand Down Expand Up @@ -129,66 +128,6 @@ func TestInsightsIPUntilSuccess(t *testing.T) {
}
}

func TestUpdateActualIP(t *testing.T) {
tests := []struct {
name string
isConnected bool
insightsAPI func() (*core.Insights, error)
expectedIP string
expectedErr error
}{
{
name: "Successful IP update",
isConnected: true,
insightsAPI: func() (*core.Insights, error) {
return &core.Insights{IP: "192.168.1.2"}, nil
},
expectedIP: "192.168.1.2",
expectedErr: nil,
},
{
name: "Not connected",
isConnected: false,
insightsAPI: func() (*core.Insights, error) {
return &core.Insights{IP: "192.168.1.2"}, nil
},
expectedIP: "invalid IP",
expectedErr: nil,
},
{
name: "Error from Insights API",
isConnected: true,
insightsAPI: func() (*core.Insights, error) {
return nil, errors.New("API error")
},
expectedIP: "invalid IP",
expectedErr: context.DeadlineExceeded,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

dm := NewDataManager("", "", "", "", daemonEvents.NewDataUpdateEvents())
api := &mockInsights{
insightsFunc: tt.insightsAPI,
}

err := updateActualIP(dm, api, ctx, tt.isConnected)

assert.ErrorIs(t, err, tt.expectedErr)

dm.mu.Lock()
actualIP := dm.actualIP
dm.mu.Unlock()

assert.Equal(t, actualIP.String(), tt.expectedIP)
})
}
}

func receiveWithTimeout(t *testing.T, ch <-chan interface{}) interface{} {
const TIMEOUT time.Duration = time.Second * 5
select {
Expand Down
Loading