feat(nord): support multi-server NordLynx outbounds (#6311)

* feat(nord): support multi-server NordLynx outbounds

* fix(nord): address verified PR review findings

Tighten the NordVPN multi-outbound implementation and its regression
coverage based on the verified review feedback.

- remove the redundant Xray validation test that duplicated the base
  branch and did not exercise multiple outbounds
- make NordModal tests wait for server loading and assert the modal close
  callback, duplicate-server state, and endpoint behavior
- add coverage for resolving the NordLynx public key from technology
  metadata instead of a numeric technology ID
- use a real httptest server for Nord integration tests through an
  injectable API base URL
- represent the All Cities sentinel consistently as null and reset it
  when a country changes

The existing NordVPN API contracts and persisted outbound schema remain
unchanged.
This commit is contained in:
Masterain
2026-09-03 02:20:10 +08:00
committed by GitHub
parent f727d04f65
commit f9cfd87cb2
23 changed files with 1186 additions and 215 deletions
+7 -25
View File
@@ -18,11 +18,14 @@ type NordService struct {
var nordHTTPClient = &http.Client{Timeout: 15 * time.Second}
// nordAPIBase is a var so integration tests can use a local HTTP server.
var nordAPIBase = "https://api.nordvpn.com"
// maxResponseSize limits the maximum size of NordVPN API responses (10 MB).
const maxResponseSize = 10 << 20
func (s *NordService) GetCountries() (string, error) {
req, reqErr := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://api.nordvpn.com/v1/countries", nil)
req, reqErr := http.NewRequestWithContext(context.Background(), http.MethodGet, nordAPIBase+"/v1/servers/countries?filters[servers_technologies][identifier]=wireguard_udp", nil)
if reqErr != nil {
return "", reqErr
}
@@ -48,7 +51,7 @@ func (s *NordService) GetServers(countryId string) (string, error) {
return "", common.NewError("invalid country ID")
}
}
url := fmt.Sprintf("https://api.nordvpn.com/v2/servers?limit=0&filters[servers_technologies][id]=35&filters[country_id]=%s", countryId)
url := fmt.Sprintf("%s/v2/servers?limit=0&filters[servers_technologies][identifier]=wireguard_udp&filters[country_id]=%s", nordAPIBase, countryId)
req, reqErr := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
if reqErr != nil {
return "", reqErr
@@ -65,28 +68,7 @@ func (s *NordService) GetServers(countryId string) (string, error) {
if err != nil {
return "", err
}
var data map[string]any
if err := json.Unmarshal(body, &data); err != nil {
return string(body), nil
}
servers, ok := data["servers"].([]any)
if !ok {
return string(body), nil
}
var filtered []any
for _, s := range servers {
if server, ok := s.(map[string]any); ok {
if load, ok := server["load"].(float64); ok && load > 7 {
filtered = append(filtered, s)
}
}
}
data["servers"] = filtered
result, _ := json.Marshal(data)
return string(result), nil
return string(body), nil
}
func (s *NordService) SetKey(privateKey string) (string, error) {
@@ -106,7 +88,7 @@ func (s *NordService) SetKey(privateKey string) (string, error) {
}
func (s *NordService) GetCredentials(token string) (string, error) {
url := "https://api.nordvpn.com/v1/users/services/credentials"
url := nordAPIBase + "/v1/users/services/credentials"
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
if err != nil {
return "", err
@@ -0,0 +1,74 @@
package integration
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func stubNordAPI(t *testing.T, handler http.HandlerFunc) {
t.Helper()
previous := nordAPIBase
server := httptest.NewServer(handler)
nordAPIBase = server.URL
t.Cleanup(func() {
nordAPIBase = previous
server.Close()
})
}
func TestNordCountriesOnlyRequestsNordLynxServerCountries(t *testing.T) {
stubNordAPI(t, func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/v1/servers/countries" {
t.Errorf("country path = %q", req.URL.Path)
}
if got := req.URL.Query().Get("filters[servers_technologies][identifier]"); got != "wireguard_udp" {
t.Errorf("NordLynx technology filter = %q", got)
}
w.Header().Set("Content-Type", "application/json")
_, _ = io.WriteString(w, `[{"id":228,"name":"United States","code":"US"}]`)
})
got, err := (&NordService{}).GetCountries()
if err != nil {
t.Fatal(err)
}
if !strings.Contains(got, `"code":"US"`) {
t.Fatalf("countries = %s", got)
}
}
func TestNordServersPreserveLowLoadServers(t *testing.T) {
stubNordAPI(t, func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/v2/servers" {
t.Errorf("server path = %q", req.URL.Path)
}
if got := req.URL.Query().Get("filters[country_id]"); got != "225" {
t.Errorf("country filter = %q", got)
}
if got := req.URL.Query().Get("filters[servers_technologies][identifier]"); got != "wireguard_udp" {
t.Errorf("NordLynx technology filter = %q", got)
}
w.Header().Set("Content-Type", "application/json")
_, _ = io.WriteString(w, `{"servers":[{"id":1,"load":0},{"id":2,"load":4}]}`)
})
got, err := (&NordService{}).GetServers("225")
if err != nil {
t.Fatal(err)
}
var payload struct {
Servers []struct {
Load int `json:"load"`
} `json:"servers"`
}
if err := json.Unmarshal([]byte(got), &payload); err != nil {
t.Fatal(err)
}
if len(payload.Servers) != 2 || payload.Servers[0].Load != 0 || payload.Servers[1].Load != 4 {
t.Fatalf("servers = %+v", payload.Servers)
}
}