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

Fix connection to a DIP server #645

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 5 additions & 6 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
type DedicatedIPService struct {
ExpiresAt string
// ServerID will be set to NoServerSelected if server was not selected by the user
ServerID int64
ServerIDs []int64
}

// Checker provides information about current authentication.
Expand All @@ -37,7 +37,6 @@ type Checker interface {
const (
VPNServiceID = 1
DedicatedIPServiceID = 11
NoServerSelected = -1
)

type expirationChecker interface {
Expand Down Expand Up @@ -172,12 +171,12 @@ func (r *RenewingChecker) GetDedicatedIPServices() ([]DedicatedIPService, error)
dipServices := []DedicatedIPService{}
for _, service := range services {
if service.Service.ID == DedicatedIPServiceID && !r.expChecker.isExpired(service.ExpiresAt) {
var serverID int64 = NoServerSelected
if len(service.Details.Servers) != 0 {
serverID = service.Details.Servers[0].ID
serverIDs := []int64{}
for _, server := range service.Details.Servers {
serverIDs = append(serverIDs, server.ID)
}
dipServices = append(dipServices,
DedicatedIPService{ExpiresAt: service.ExpiresAt, ServerID: serverID})
DedicatedIPService{ExpiresAt: service.ExpiresAt, ServerIDs: serverIDs})
}
}

Expand Down
45 changes: 34 additions & 11 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,29 +250,43 @@ func TestGetDedicatedIPServices(t *testing.T) {
category.Set(t, category.Unit)

dipService1ExpDate := "2050-06-04 00:00:00"
var dipSercice1ServerID int64 = 11111
var dipService1ServerID int64 = 11111
dipService1 := core.ServiceData{
ExpiresAt: dipService1ExpDate,
Service: core.Service{
ID: DedicatedIPServiceID,
},
Details: core.ServiceDetails{
Servers: []core.ServiceServer{
{ID: dipSercice1ServerID},
{ID: dipService1ServerID},
},
},
}

dipService2ExpDate := "2050-08-22 00:00:00"
var dipSercice2ServerID int64 = 11111
dipService2ExpDate := "2050-01-25 00:00:00"
var dipService2ServerID int64 = 222222
dipService2 := core.ServiceData{
ExpiresAt: dipService2ExpDate,
Service: core.Service{
ID: DedicatedIPServiceID,
},
Details: core.ServiceDetails{
Servers: []core.ServiceServer{
{ID: dipSercice2ServerID},
{ID: dipService2ServerID},
},
},
}

dipService3ExpDate := "2043-05-10 00:00:00"
dipService3 := core.ServiceData{
ExpiresAt: dipService3ExpDate,
Service: core.Service{
ID: DedicatedIPServiceID,
},
Details: core.ServiceDetails{
Servers: []core.ServiceServer{
{ID: dipService1ServerID},
{ID: dipService2ServerID},
},
},
}
Expand Down Expand Up @@ -328,7 +342,7 @@ func TestGetDedicatedIPServices(t *testing.T) {
dipService1,
},
expectedDIPSerivces: []DedicatedIPService{
{ExpiresAt: dipService1ExpDate, ServerID: dipSercice1ServerID},
{ExpiresAt: dipService1ExpDate, ServerIDs: []int64{dipService1ServerID}},
},
},
{
Expand All @@ -338,8 +352,17 @@ func TestGetDedicatedIPServices(t *testing.T) {
dipService2,
},
expectedDIPSerivces: []DedicatedIPService{
{ExpiresAt: dipService1ExpDate, ServerID: dipSercice1ServerID},
{ExpiresAt: dipService2ExpDate, ServerID: dipSercice2ServerID},
{ExpiresAt: dipService1ExpDate, ServerIDs: []int64{dipService1ServerID}},
{ExpiresAt: dipService2ExpDate, ServerIDs: []int64{dipService2ServerID}},
},
},
{
name: "multiple dip servers in single service",
servicesResponse: []core.ServiceData{
dipService3,
},
expectedDIPSerivces: []DedicatedIPService{
{ExpiresAt: dipService3ExpDate, ServerIDs: []int64{dipService1ServerID, dipService2ServerID}},
},
},
{
Expand All @@ -356,7 +379,7 @@ func TestGetDedicatedIPServices(t *testing.T) {
dipService1,
},
expectedDIPSerivces: []DedicatedIPService{
{ExpiresAt: dipService1ExpDate, ServerID: dipSercice1ServerID},
{ExpiresAt: dipService1ExpDate, ServerIDs: []int64{dipService1ServerID}},
},
},
{
Expand All @@ -368,7 +391,7 @@ func TestGetDedicatedIPServices(t *testing.T) {
dipService1,
},
expectedDIPSerivces: []DedicatedIPService{
{ExpiresAt: dipService1ExpDate, ServerID: dipSercice1ServerID},
{ExpiresAt: dipService1ExpDate, ServerIDs: []int64{dipService1ServerID}},
},
},
{
Expand Down Expand Up @@ -396,7 +419,7 @@ func TestGetDedicatedIPServices(t *testing.T) {
dipServiceNoServer,
},
expectedDIPSerivces: []DedicatedIPService{
{ExpiresAt: dipServiceNoServerExpirationDate, ServerID: NoServerSelected},
{ExpiresAt: dipServiceNoServerExpirationDate, ServerIDs: []int64{}},
},
},
}
Expand Down
76 changes: 38 additions & 38 deletions daemon/pb/account.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion daemon/rpc_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func dipServicesToProtobuf(dipServices []auth.DedicatedIPService) []*pb.Dedidcat
dipServicesProtobuf := []*pb.DedidcatedIPService{}
for _, dipService := range dipServices {
dipServicesProtobuf = append(dipServicesProtobuf, &pb.DedidcatedIPService{
ServerId: dipService.ServerID,
ServerIds: dipService.ServerIDs,
DedicatedIpExpiresAt: dipService.ExpiresAt,
})
}
Expand Down
24 changes: 20 additions & 4 deletions daemon/rpc_connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func TestRpcConnect(t *testing.T) {
rpc.ac = &workingLoginChecker{
isDedicatedIPExpired: false,
dedicatedIPService: []auth.DedicatedIPService{
{ExpiresAt: "", ServerID: 7},
{ExpiresAt: "", ServerIDs: []int64{7}},
},
}
},
Expand All @@ -189,7 +189,23 @@ func TestRpcConnect(t *testing.T) {
rpc.ac = &workingLoginChecker{
isDedicatedIPExpired: false,
dedicatedIPService: []auth.DedicatedIPService{
{ExpiresAt: "", ServerID: 7},
{ExpiresAt: "", ServerIDs: []int64{7}},
},
}
},
resp: internal.CodeConnected,
},
{
name: "Dedicated IP with server name multiple servers in service works",
serverTag: "lt7",
factory: func(config.Technology) (vpn.VPN, error) {
return &mock.WorkingVPN{}, nil
},
setup: func(rpc *RPC) {
rpc.ac = &workingLoginChecker{
isDedicatedIPExpired: false,
dedicatedIPService: []auth.DedicatedIPService{
{ExpiresAt: "", ServerIDs: []int64{7, 8}},
},
}
},
Expand Down Expand Up @@ -229,7 +245,7 @@ func TestRpcConnect(t *testing.T) {
rpc.ac = &workingLoginChecker{
isDedicatedIPExpired: false,
dedicatedIPService: []auth.DedicatedIPService{
{ExpiresAt: "", ServerID: 7},
{ExpiresAt: "", ServerIDs: []int64{7}},
},
}
},
Expand All @@ -245,7 +261,7 @@ func TestRpcConnect(t *testing.T) {
rpc.ac = &workingLoginChecker{
isDedicatedIPExpired: false,
dedicatedIPService: []auth.DedicatedIPService{
{ExpiresAt: "", ServerID: auth.NoServerSelected},
{ExpiresAt: "", ServerIDs: []int64{}},
},
}
},
Expand Down
8 changes: 5 additions & 3 deletions daemon/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ func selectFilter(tag string, group config.ServerGroup, obfuscated bool) core.Pr
// isAnyDIPServersAvailable returns true if dedicated IP server is selected for any of the provided services
func isAnyDIPServersAvailable(dedicatedIPServices []auth.DedicatedIPService) bool {
index := slices.IndexFunc(dedicatedIPServices, func(dipService auth.DedicatedIPService) bool {
return dipService.ServerID != auth.NoServerSelected
return len(dipService.ServerIDs) > 0
})

return index != -1
Expand Down Expand Up @@ -516,7 +516,8 @@ func selectServer(r *RPC, insights *core.Insights, cfg config.Config, tag string
}

index := slices.IndexFunc(dedicatedIPServices, func(s auth.DedicatedIPService) bool {
return s.ServerID == server.ID
index := slices.Index(s.ServerIDs, server.ID)
return index != -1
})
if index == -1 {
log.Println(internal.ErrorPrefix, "server is not in the DIP servers list")
Expand Down Expand Up @@ -561,7 +562,8 @@ func selectDedicatedIPServer(authChecker auth.Checker, servers core.Servers) (*c
}

service := dedicatedIPServices[rand.Intn(len(dedicatedIPServices))]
server, err := getServerByID(servers, service.ServerID)
serverID := service.ServerIDs[rand.Intn(len(service.ServerIDs))]
server, err := getServerByID(servers, serverID)
if err != nil {
log.Println(internal.ErrorPrefix, "DIP server not found:", err)
return nil, internal.ErrServerIsUnavailable
Expand Down
2 changes: 1 addition & 1 deletion protobuf/daemon/account.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ option go_package = "github.com/NordSecurity/nordvpn-linux/daemon/pb";
import "common.proto";

message DedidcatedIPService {
int64 server_id = 1;
repeated int64 server_ids = 1;
string dedicated_ip_expires_at = 2;
}

Expand Down
Loading