feat(surfshark): update API endpoint and servers data (#1560)
This commit is contained in:
@@ -10,7 +10,6 @@ import (
|
|||||||
"github.com/qdm12/gluetun/internal/provider/surfshark/servers"
|
"github.com/qdm12/gluetun/internal/provider/surfshark/servers"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Note: no multi-hop and some OpenVPN servers are missing from their API.
|
|
||||||
func addServersFromAPI(ctx context.Context, client *http.Client,
|
func addServersFromAPI(ctx context.Context, client *http.Client,
|
||||||
hts hostToServers) (err error) {
|
hts hostToServers) (err error) {
|
||||||
data, err := fetchAPI(ctx, client)
|
data, err := fetchAPI(ctx, client)
|
||||||
@@ -52,31 +51,38 @@ type serverData struct {
|
|||||||
|
|
||||||
func fetchAPI(ctx context.Context, client *http.Client) (
|
func fetchAPI(ctx context.Context, client *http.Client) (
|
||||||
servers []serverData, err error) {
|
servers []serverData, err error) {
|
||||||
const url = "https://my.surfshark.com/vpn/api/v4/server/clusters"
|
const url = "https://api.surfshark.com/v4/server/clusters"
|
||||||
|
|
||||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
for _, clustersType := range [...]string{"generic", "double", "static", "obfuscated"} {
|
||||||
if err != nil {
|
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url+"/"+clustersType, nil)
|
||||||
return nil, err
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
response, err := client.Do(request)
|
response, err := client.Do(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
|
|
||||||
if response.StatusCode != http.StatusOK {
|
if response.StatusCode != http.StatusOK {
|
||||||
return nil, fmt.Errorf("%w: %d %s", ErrHTTPStatusCodeNotOK,
|
return nil, fmt.Errorf("%w: %d %s", ErrHTTPStatusCodeNotOK,
|
||||||
response.StatusCode, response.Status)
|
response.StatusCode, response.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
decoder := json.NewDecoder(response.Body)
|
decoder := json.NewDecoder(response.Body)
|
||||||
if err := decoder.Decode(&servers); err != nil {
|
var newServers []serverData
|
||||||
return nil, fmt.Errorf("decoding response body: %w", err)
|
err = decoder.Decode(&newServers)
|
||||||
}
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("decoding response body: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
if err := response.Body.Close(); err != nil {
|
err = response.Body.Close()
|
||||||
return nil, err
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
servers = append(servers, newServers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return servers, nil
|
return servers, nil
|
||||||
|
|||||||
@@ -14,30 +14,53 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type httpExchange struct {
|
||||||
|
requestURL string
|
||||||
|
responseStatus int
|
||||||
|
responseBody io.ReadCloser
|
||||||
|
}
|
||||||
|
|
||||||
func Test_addServersFromAPI(t *testing.T) {
|
func Test_addServersFromAPI(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
testCases := map[string]struct {
|
testCases := map[string]struct {
|
||||||
hts hostToServers
|
hts hostToServers
|
||||||
responseStatus int
|
exchanges []httpExchange
|
||||||
responseBody io.ReadCloser
|
expected hostToServers
|
||||||
expected hostToServers
|
err error
|
||||||
err error
|
|
||||||
}{
|
}{
|
||||||
"fetch API error": {
|
"fetch API error": {
|
||||||
responseStatus: http.StatusNoContent,
|
exchanges: []httpExchange{{
|
||||||
err: errors.New("HTTP status code not OK: 204 No Content"),
|
requestURL: "https://api.surfshark.com/v4/server/clusters/generic",
|
||||||
|
responseStatus: http.StatusNoContent,
|
||||||
|
}},
|
||||||
|
err: errors.New("HTTP status code not OK: 204 No Content"),
|
||||||
},
|
},
|
||||||
"success": {
|
"success": {
|
||||||
hts: hostToServers{
|
hts: hostToServers{
|
||||||
"existinghost": []models.Server{{Hostname: "existinghost"}},
|
"existinghost": []models.Server{{Hostname: "existinghost"}},
|
||||||
},
|
},
|
||||||
responseStatus: http.StatusOK,
|
exchanges: []httpExchange{{
|
||||||
responseBody: io.NopCloser(strings.NewReader(`[
|
requestURL: "https://api.surfshark.com/v4/server/clusters/generic",
|
||||||
|
responseStatus: http.StatusOK,
|
||||||
|
responseBody: io.NopCloser(strings.NewReader(`[
|
||||||
{"connectionName":"host1","region":"region1","country":"country1","location":"location1"},
|
{"connectionName":"host1","region":"region1","country":"country1","location":"location1"},
|
||||||
{"connectionName":"host1","region":"region1","country":"country1","location":"location1","pubkey":"pubKeyValue"},
|
{"connectionName":"host1","region":"region1","country":"country1","location":"location1","pubkey":"pubKeyValue"},
|
||||||
{"connectionName":"host2","region":"region2","country":"country1","location":"location2"}
|
{"connectionName":"host2","region":"region2","country":"country1","location":"location2"}
|
||||||
]`)),
|
]`)),
|
||||||
|
}, {
|
||||||
|
requestURL: "https://api.surfshark.com/v4/server/clusters/double",
|
||||||
|
responseStatus: http.StatusOK,
|
||||||
|
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
||||||
|
}, {
|
||||||
|
requestURL: "https://api.surfshark.com/v4/server/clusters/static",
|
||||||
|
responseStatus: http.StatusOK,
|
||||||
|
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
||||||
|
}, {
|
||||||
|
requestURL: "https://api.surfshark.com/v4/server/clusters/obfuscated",
|
||||||
|
responseStatus: http.StatusOK,
|
||||||
|
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
||||||
|
}},
|
||||||
expected: map[string][]models.Server{
|
expected: map[string][]models.Server{
|
||||||
"existinghost": {{Hostname: "existinghost"}},
|
"existinghost": {{Hostname: "existinghost"}},
|
||||||
"host1": {{
|
"host1": {{
|
||||||
@@ -75,14 +98,18 @@ func Test_addServersFromAPI(t *testing.T) {
|
|||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
|
currentExchangeIndex := 0
|
||||||
|
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
||||||
assert.Equal(t, http.MethodGet, r.Method)
|
assert.Equal(t, http.MethodGet, r.Method)
|
||||||
assert.Equal(t, r.URL.String(), "https://my.surfshark.com/vpn/api/v4/server/clusters")
|
exchange := testCase.exchanges[currentExchangeIndex]
|
||||||
|
currentExchangeIndex++
|
||||||
|
assert.Equal(t, exchange.requestURL, r.URL.String())
|
||||||
return &http.Response{
|
return &http.Response{
|
||||||
StatusCode: testCase.responseStatus,
|
StatusCode: exchange.responseStatus,
|
||||||
Status: http.StatusText(testCase.responseStatus),
|
Status: http.StatusText(exchange.responseStatus),
|
||||||
Body: testCase.responseBody,
|
Body: exchange.responseBody,
|
||||||
}, nil
|
}, nil
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
@@ -104,30 +131,64 @@ func Test_fetchAPI(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
testCases := map[string]struct {
|
testCases := map[string]struct {
|
||||||
responseStatus int
|
exchanges []httpExchange
|
||||||
responseBody io.ReadCloser
|
data []serverData
|
||||||
data []serverData
|
err error
|
||||||
err error
|
|
||||||
}{
|
}{
|
||||||
"http response status not ok": {
|
"http response status not ok": {
|
||||||
responseStatus: http.StatusNoContent,
|
exchanges: []httpExchange{{
|
||||||
err: errors.New("HTTP status code not OK: 204 No Content"),
|
requestURL: "https://api.surfshark.com/v4/server/clusters/generic",
|
||||||
|
responseStatus: http.StatusNoContent,
|
||||||
|
}},
|
||||||
|
err: errors.New("HTTP status code not OK: 204 No Content"),
|
||||||
},
|
},
|
||||||
"nil body": {
|
"nil body": {
|
||||||
responseStatus: http.StatusOK,
|
exchanges: []httpExchange{{
|
||||||
err: errors.New("decoding response body: EOF"),
|
requestURL: "https://api.surfshark.com/v4/server/clusters/generic",
|
||||||
|
responseStatus: http.StatusOK,
|
||||||
|
}},
|
||||||
|
err: errors.New("decoding response body: EOF"),
|
||||||
},
|
},
|
||||||
"no server": {
|
"no server": {
|
||||||
responseStatus: http.StatusOK,
|
exchanges: []httpExchange{{
|
||||||
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
requestURL: "https://api.surfshark.com/v4/server/clusters/generic",
|
||||||
data: []serverData{},
|
responseStatus: http.StatusOK,
|
||||||
|
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
||||||
|
}, {
|
||||||
|
requestURL: "https://api.surfshark.com/v4/server/clusters/double",
|
||||||
|
responseStatus: http.StatusOK,
|
||||||
|
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
||||||
|
}, {
|
||||||
|
requestURL: "https://api.surfshark.com/v4/server/clusters/static",
|
||||||
|
responseStatus: http.StatusOK,
|
||||||
|
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
||||||
|
}, {
|
||||||
|
requestURL: "https://api.surfshark.com/v4/server/clusters/obfuscated",
|
||||||
|
responseStatus: http.StatusOK,
|
||||||
|
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
||||||
|
}},
|
||||||
},
|
},
|
||||||
"success": {
|
"success": {
|
||||||
responseStatus: http.StatusOK,
|
exchanges: []httpExchange{{
|
||||||
responseBody: io.NopCloser(strings.NewReader(`[
|
requestURL: "https://api.surfshark.com/v4/server/clusters/generic",
|
||||||
{"connectionName":"host1","region":"region1","country":"country1","location":"location1"},
|
responseStatus: http.StatusOK,
|
||||||
{"connectionName":"host2","region":"region2","country":"country1","location":"location2"}
|
responseBody: io.NopCloser(strings.NewReader(`[
|
||||||
]`)),
|
{"connectionName":"host1","region":"region1","country":"country1","location":"location1"},
|
||||||
|
{"connectionName":"host2","region":"region2","country":"country1","location":"location2"}
|
||||||
|
]`)),
|
||||||
|
}, {
|
||||||
|
requestURL: "https://api.surfshark.com/v4/server/clusters/double",
|
||||||
|
responseStatus: http.StatusOK,
|
||||||
|
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
||||||
|
}, {
|
||||||
|
requestURL: "https://api.surfshark.com/v4/server/clusters/static",
|
||||||
|
responseStatus: http.StatusOK,
|
||||||
|
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
||||||
|
}, {
|
||||||
|
requestURL: "https://api.surfshark.com/v4/server/clusters/obfuscated",
|
||||||
|
responseStatus: http.StatusOK,
|
||||||
|
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
||||||
|
}},
|
||||||
data: []serverData{
|
data: []serverData{
|
||||||
{
|
{
|
||||||
Region: "region1",
|
Region: "region1",
|
||||||
@@ -151,14 +212,18 @@ func Test_fetchAPI(t *testing.T) {
|
|||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
|
currentExchangeIndex := 0
|
||||||
|
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
||||||
assert.Equal(t, http.MethodGet, r.Method)
|
assert.Equal(t, http.MethodGet, r.Method)
|
||||||
assert.Equal(t, r.URL.String(), "https://my.surfshark.com/vpn/api/v4/server/clusters")
|
exchange := testCase.exchanges[currentExchangeIndex]
|
||||||
|
currentExchangeIndex++
|
||||||
|
assert.Equal(t, exchange.requestURL, r.URL.String())
|
||||||
return &http.Response{
|
return &http.Response{
|
||||||
StatusCode: testCase.responseStatus,
|
StatusCode: exchange.responseStatus,
|
||||||
Status: http.StatusText(testCase.responseStatus),
|
Status: http.StatusText(exchange.responseStatus),
|
||||||
Body: testCase.responseBody,
|
Body: exchange.responseBody,
|
||||||
}, nil
|
}, nil
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user