Add linters and fix lint issues
This commit is contained in:
@@ -3,10 +3,8 @@ package updater
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type (
|
||||
httpGetFunc func(url string) (r *http.Response, err error)
|
||||
lookupIPFunc func(ctx context.Context, host string) (ips []net.IP, err error)
|
||||
)
|
||||
|
||||
@@ -70,10 +70,16 @@ func (l *looper) SetPeriod(period time.Duration) {
|
||||
|
||||
func (l *looper) logAndWait(ctx context.Context, err error) {
|
||||
l.logger.Error(err)
|
||||
l.logger.Info("retrying in 5 minutes")
|
||||
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
|
||||
defer cancel() // just for the linter
|
||||
<-ctx.Done()
|
||||
const waitTime = 5 * time.Minute
|
||||
l.logger.Info("retrying in %s", waitTime)
|
||||
timer := time.NewTimer(waitTime)
|
||||
select {
|
||||
case <-timer.C:
|
||||
case <-ctx.Done():
|
||||
if !timer.Stop() {
|
||||
<-timer.C
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup) {
|
||||
|
||||
@@ -1,18 +1,19 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"sort"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/golibs/network"
|
||||
)
|
||||
|
||||
func (u *updater) updateMullvad() (err error) {
|
||||
servers, err := findMullvadServers(u.httpGet)
|
||||
func (u *updater) updateMullvad(ctx context.Context) (err error) {
|
||||
servers, err := findMullvadServers(ctx, u.client)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot update Mullvad servers: %w", err)
|
||||
}
|
||||
@@ -24,19 +25,14 @@ func (u *updater) updateMullvad() (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func findMullvadServers(httpGet httpGetFunc) (servers []models.MullvadServer, err error) {
|
||||
func findMullvadServers(ctx context.Context, client network.Client) (servers []models.MullvadServer, err error) {
|
||||
const url = "https://api.mullvad.net/www/relays/openvpn/"
|
||||
response, err := httpGet(url)
|
||||
bytes, status, err := client.Get(ctx, url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf(response.Status)
|
||||
}
|
||||
bytes, err := ioutil.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if status != http.StatusOK {
|
||||
return nil, fmt.Errorf("HTTP status code %d", status)
|
||||
}
|
||||
var data []struct {
|
||||
Country string `json:"country_name"`
|
||||
@@ -89,7 +85,6 @@ func findMullvadServers(httpGet httpGetFunc) (servers []models.MullvadServer, er
|
||||
return servers, nil
|
||||
}
|
||||
|
||||
//nolint:goconst
|
||||
func stringifyMullvadServers(servers []models.MullvadServer) (s string) {
|
||||
s = "func MullvadServers() []models.MullvadServer {\n"
|
||||
s += " return []models.MullvadServer{\n"
|
||||
|
||||
@@ -18,6 +18,7 @@ func Test_stringifyMullvadServers(t *testing.T) {
|
||||
IPs: []net.IP{{1, 1, 1, 1}},
|
||||
IPsV6: []net.IP{{1, 1, 1, 1}},
|
||||
}}
|
||||
//nolint:lll
|
||||
expected := `
|
||||
func MullvadServers() []models.MullvadServer {
|
||||
return []models.MullvadServer{
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"sort"
|
||||
@@ -11,10 +11,11 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/golibs/network"
|
||||
)
|
||||
|
||||
func (u *updater) updateNordvpn() (err error) {
|
||||
servers, warnings, err := findNordvpnServers(u.httpGet)
|
||||
func (u *updater) updateNordvpn(ctx context.Context) (err error) {
|
||||
servers, warnings, err := findNordvpnServers(ctx, u.client)
|
||||
if u.options.CLI {
|
||||
for _, warning := range warnings {
|
||||
u.logger.Warn("Nordvpn: %s", warning)
|
||||
@@ -31,19 +32,15 @@ func (u *updater) updateNordvpn() (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func findNordvpnServers(httpGet httpGetFunc) (servers []models.NordvpnServer, warnings []string, err error) {
|
||||
func findNordvpnServers(ctx context.Context, client network.Client) (
|
||||
servers []models.NordvpnServer, warnings []string, err error) {
|
||||
const url = "https://nordvpn.com/api/server"
|
||||
response, err := httpGet(url)
|
||||
bytes, status, err := client.Get(ctx, url)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return nil, nil, fmt.Errorf(response.Status)
|
||||
}
|
||||
bytes, err := ioutil.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
if status != http.StatusOK {
|
||||
return nil, nil, fmt.Errorf("HTTP status code %d", status)
|
||||
}
|
||||
var data []struct {
|
||||
IPAddress string `json:"ip_address"`
|
||||
@@ -71,7 +68,9 @@ func findNordvpnServers(httpGet httpGetFunc) (servers []models.NordvpnServer, wa
|
||||
}
|
||||
ip := net.ParseIP(jsonServer.IPAddress)
|
||||
if ip == nil || ip.To4() == nil {
|
||||
return nil, nil, fmt.Errorf("IP address %q is not a valid IPv4 address for server %q", jsonServer.IPAddress, jsonServer.Name)
|
||||
return nil, nil,
|
||||
fmt.Errorf("IP address %q is not a valid IPv4 address for server %q",
|
||||
jsonServer.IPAddress, jsonServer.Name)
|
||||
}
|
||||
i := strings.IndexRune(jsonServer.Name, '#')
|
||||
if i < 0 {
|
||||
@@ -94,7 +93,6 @@ func findNordvpnServers(httpGet httpGetFunc) (servers []models.NordvpnServer, wa
|
||||
return servers, warnings, nil
|
||||
}
|
||||
|
||||
//nolint:goconst
|
||||
func stringifyNordvpnServers(servers []models.NordvpnServer) (s string) {
|
||||
s = "func NordvpnServers() []models.NordvpnServer {\n"
|
||||
s += " return []models.NordvpnServer{\n"
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
func (u *updater) updatePIAOld(ctx context.Context) (err error) {
|
||||
const zipURL = "https://www.privateinternetaccess.com/openvpn/openvpn.zip"
|
||||
contents, err := fetchAndExtractFiles(ctx, zipURL)
|
||||
contents, err := fetchAndExtractFiles(ctx, u.client, zipURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@ package updater
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"sort"
|
||||
@@ -13,18 +13,14 @@ import (
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
func (u *updater) updatePIA() (err error) {
|
||||
func (u *updater) updatePIA(ctx context.Context) (err error) {
|
||||
const url = "https://serverlist.piaservers.net/vpninfo/servers/v4"
|
||||
response, err := u.httpGet(url)
|
||||
b, status, err := u.client.Get(ctx, url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
b, err := ioutil.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if response.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("%s: %s", response.Status, strings.ReplaceAll(string(b), "\n", ""))
|
||||
if status != http.StatusOK {
|
||||
return fmt.Errorf("HTTP status code %d: %s", status, strings.ReplaceAll(string(b), "\n", ""))
|
||||
}
|
||||
|
||||
// remove key/signature at the bottom
|
||||
@@ -58,7 +54,8 @@ func (u *updater) updatePIA() (err error) {
|
||||
}
|
||||
for _, udpServer := range region.Servers.UDP {
|
||||
if len(server.OpenvpnUDP.CN) > 0 && server.OpenvpnUDP.CN != udpServer.CN {
|
||||
return fmt.Errorf("CN is different for UDP for region %q: %q and %q", region.Name, server.OpenvpnUDP.CN, udpServer.CN)
|
||||
return fmt.Errorf("CN is different for UDP for region %q: %q and %q",
|
||||
region.Name, server.OpenvpnUDP.CN, udpServer.CN)
|
||||
}
|
||||
if udpServer.IP != nil {
|
||||
server.OpenvpnUDP.IPs = append(server.OpenvpnUDP.IPs, udpServer.IP)
|
||||
@@ -67,7 +64,8 @@ func (u *updater) updatePIA() (err error) {
|
||||
}
|
||||
for _, tcpServer := range region.Servers.TCP {
|
||||
if len(server.OpenvpnTCP.CN) > 0 && server.OpenvpnTCP.CN != tcpServer.CN {
|
||||
return fmt.Errorf("CN is different for TCP for region %q: %q and %q", region.Name, server.OpenvpnTCP.CN, tcpServer.CN)
|
||||
return fmt.Errorf("CN is different for TCP for region %q: %q and %q",
|
||||
region.Name, server.OpenvpnTCP.CN, tcpServer.CN)
|
||||
}
|
||||
if tcpServer.IP != nil {
|
||||
server.OpenvpnTCP.IPs = append(server.OpenvpnTCP.IPs, tcpServer.IP)
|
||||
|
||||
@@ -4,16 +4,16 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/golibs/network"
|
||||
)
|
||||
|
||||
func (u *updater) updatePurevpn(ctx context.Context) (err error) {
|
||||
servers, warnings, err := findPurevpnServers(ctx, u.httpGet, u.lookupIP)
|
||||
servers, warnings, err := findPurevpnServers(ctx, u.client, u.lookupIP)
|
||||
if u.options.CLI {
|
||||
for _, warning := range warnings {
|
||||
u.logger.Warn("PureVPN: %s", warning)
|
||||
@@ -30,20 +30,15 @@ func (u *updater) updatePurevpn(ctx context.Context) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func findPurevpnServers(ctx context.Context, httpGet httpGetFunc, lookupIP lookupIPFunc) (
|
||||
func findPurevpnServers(ctx context.Context, client network.Client, lookupIP lookupIPFunc) (
|
||||
servers []models.PurevpnServer, warnings []string, err error) {
|
||||
const url = "https://support.purevpn.com/vpn-servers"
|
||||
response, err := httpGet(url)
|
||||
bytes, status, err := client.Get(ctx, url)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return nil, nil, fmt.Errorf(response.Status)
|
||||
}
|
||||
bytes, err := ioutil.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
if status != http.StatusOK {
|
||||
return nil, nil, fmt.Errorf("HTTP status code %d", status)
|
||||
}
|
||||
const jsonPrefix = "<script>var servers = "
|
||||
const jsonSuffix = "</script>"
|
||||
@@ -82,11 +77,13 @@ func findPurevpnServers(ctx context.Context, httpGet httpGetFunc, lookupIP looku
|
||||
return nil, warnings, err
|
||||
}
|
||||
if jsonServer.UDP == "" && jsonServer.TCP == "" {
|
||||
warnings = append(warnings, fmt.Sprintf("server %s %s %s does not support TCP and UDP for openvpn", jsonServer.Region, jsonServer.Country, jsonServer.City))
|
||||
warnings = append(warnings, fmt.Sprintf("server %s %s %s does not support TCP and UDP for openvpn",
|
||||
jsonServer.Region, jsonServer.Country, jsonServer.City))
|
||||
continue
|
||||
}
|
||||
if jsonServer.UDP == "" || jsonServer.TCP == "" {
|
||||
warnings = append(warnings, fmt.Sprintf("server %s %s %s does not support TCP or UDP for openvpn", jsonServer.Region, jsonServer.Country, jsonServer.City))
|
||||
warnings = append(warnings, fmt.Sprintf("server %s %s %s does not support TCP or UDP for openvpn",
|
||||
jsonServer.Region, jsonServer.Country, jsonServer.City))
|
||||
continue
|
||||
}
|
||||
host := jsonServer.UDP
|
||||
|
||||
@@ -4,16 +4,17 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/golibs/network"
|
||||
)
|
||||
|
||||
func (u *updater) updateSurfshark(ctx context.Context) (err error) {
|
||||
servers, warnings, err := findSurfsharkServersFromZip(ctx, u.lookupIP)
|
||||
servers, warnings, err := findSurfsharkServersFromZip(ctx, u.client, u.lookupIP)
|
||||
if u.options.CLI {
|
||||
for _, warning := range warnings {
|
||||
u.logger.Warn("Surfshark: %s", warning)
|
||||
@@ -31,16 +32,14 @@ func (u *updater) updateSurfshark(ctx context.Context) (err error) {
|
||||
}
|
||||
|
||||
//nolint:deadcode,unused
|
||||
func findSurfsharkServersFromAPI(ctx context.Context, lookupIP lookupIPFunc, httpGet httpGetFunc) (servers []models.SurfsharkServer, warnings []string, err error) {
|
||||
func findSurfsharkServersFromAPI(ctx context.Context, client network.Client, lookupIP lookupIPFunc) (
|
||||
servers []models.SurfsharkServer, warnings []string, err error) {
|
||||
const url = "https://my.surfshark.com/vpn/api/v1/server/clusters"
|
||||
response, err := httpGet(url)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
b, err := ioutil.ReadAll(response.Body)
|
||||
b, status, err := client.Get(ctx, url)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
} else if status != http.StatusOK {
|
||||
return nil, nil, fmt.Errorf("HTTP status code %d", status)
|
||||
}
|
||||
var jsonServers []struct {
|
||||
Host string `json:"connectionName"`
|
||||
@@ -70,9 +69,10 @@ func findSurfsharkServersFromAPI(ctx context.Context, lookupIP lookupIPFunc, htt
|
||||
return servers, warnings, nil
|
||||
}
|
||||
|
||||
func findSurfsharkServersFromZip(ctx context.Context, lookupIP lookupIPFunc) (servers []models.SurfsharkServer, warnings []string, err error) {
|
||||
func findSurfsharkServersFromZip(ctx context.Context, client network.Client, lookupIP lookupIPFunc) (
|
||||
servers []models.SurfsharkServer, warnings []string, err error) {
|
||||
const zipURL = "https://my.surfshark.com/vpn/api/v1/server/configurations"
|
||||
contents, err := fetchAndExtractFiles(ctx, zipURL)
|
||||
contents, err := fetchAndExtractFiles(ctx, client, zipURL)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@@ -154,7 +154,8 @@ func getRemainingServers(ctx context.Context, mapping map[string]string, lookupI
|
||||
warnings = append(warnings, warning)
|
||||
continue
|
||||
} else if len(IPs) == 0 {
|
||||
warning := fmt.Sprintf("subdomain %q for region %q from mapping did not resolve to any IP address", subdomain, region)
|
||||
warning := fmt.Sprintf("subdomain %q for region %q from mapping did not resolve to any IP address",
|
||||
subdomain, region)
|
||||
warnings = append(warnings, warning)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/golibs/logging"
|
||||
"github.com/qdm12/golibs/network"
|
||||
)
|
||||
|
||||
type Updater interface {
|
||||
@@ -25,8 +26,8 @@ type updater struct {
|
||||
logger logging.Logger
|
||||
timeNow func() time.Time
|
||||
println func(s string)
|
||||
httpGet httpGetFunc
|
||||
lookupIP lookupIPFunc
|
||||
client network.Client
|
||||
}
|
||||
|
||||
func New(options Options, httpClient *http.Client, currentServers models.AllServers, logger logging.Logger) Updater {
|
||||
@@ -34,18 +35,19 @@ func New(options Options, httpClient *http.Client, currentServers models.AllServ
|
||||
options.DNSAddress = "1.1.1.1"
|
||||
}
|
||||
resolver := newResolver(options.DNSAddress)
|
||||
const clientTimeout = 10 * time.Second
|
||||
return &updater{
|
||||
logger: logger,
|
||||
timeNow: time.Now,
|
||||
println: func(s string) { fmt.Println(s) },
|
||||
httpGet: httpClient.Get,
|
||||
lookupIP: newLookupIP(resolver),
|
||||
client: network.NewClient(clientTimeout),
|
||||
options: options,
|
||||
servers: currentServers,
|
||||
}
|
||||
}
|
||||
|
||||
// TODO parallelize DNS resolution
|
||||
// TODO parallelize DNS resolution.
|
||||
func (u *updater) UpdateServers(ctx context.Context) (allServers models.AllServers, err error) { //nolint:gocognit
|
||||
if u.options.Cyberghost {
|
||||
u.logger.Info("updating Cyberghost servers...")
|
||||
@@ -59,7 +61,7 @@ func (u *updater) UpdateServers(ctx context.Context) (allServers models.AllServe
|
||||
|
||||
if u.options.Mullvad {
|
||||
u.logger.Info("updating Mullvad servers...")
|
||||
if err := u.updateMullvad(); err != nil {
|
||||
if err := u.updateMullvad(ctx); err != nil {
|
||||
u.logger.Error(err)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
@@ -70,7 +72,7 @@ func (u *updater) UpdateServers(ctx context.Context) (allServers models.AllServe
|
||||
if u.options.Nordvpn {
|
||||
// TODO support servers offering only TCP or only UDP
|
||||
u.logger.Info("updating NordVPN servers...")
|
||||
if err := u.updateNordvpn(); err != nil {
|
||||
if err := u.updateNordvpn(ctx); err != nil {
|
||||
u.logger.Error(err)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
@@ -80,7 +82,7 @@ func (u *updater) UpdateServers(ctx context.Context) (allServers models.AllServe
|
||||
|
||||
if u.options.PIA {
|
||||
u.logger.Info("updating Private Internet Access (v4) servers...")
|
||||
if err := u.updatePIA(); err != nil {
|
||||
if err := u.updatePIA(ctx); err != nil {
|
||||
u.logger.Error(err)
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
|
||||
@@ -8,10 +8,11 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/golibs/network"
|
||||
)
|
||||
|
||||
func (u *updater) updateVyprvpn(ctx context.Context) (err error) {
|
||||
servers, err := findVyprvpnServers(ctx, u.lookupIP)
|
||||
servers, err := findVyprvpnServers(ctx, u.client, u.lookupIP)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot update Vyprvpn servers: %w", err)
|
||||
}
|
||||
@@ -23,9 +24,10 @@ func (u *updater) updateVyprvpn(ctx context.Context) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func findVyprvpnServers(ctx context.Context, lookupIP lookupIPFunc) (servers []models.VyprvpnServer, err error) {
|
||||
func findVyprvpnServers(ctx context.Context, client network.Client, lookupIP lookupIPFunc) (
|
||||
servers []models.VyprvpnServer, err error) {
|
||||
const zipURL = "https://support.vyprvpn.com/hc/article_attachments/360052617332/Vypr_OpenVPN_20200320.zip"
|
||||
contents, err := fetchAndExtractFiles(ctx, zipURL)
|
||||
contents, err := fetchAndExtractFiles(ctx, client, zipURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -9,13 +9,12 @@ import (
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/qdm12/golibs/network"
|
||||
)
|
||||
|
||||
func fetchAndExtractFiles(ctx context.Context, urls ...string) (contents map[string][]byte, err error) {
|
||||
client := network.NewClient(10 * time.Second)
|
||||
func fetchAndExtractFiles(ctx context.Context, client network.Client, urls ...string) (
|
||||
contents map[string][]byte, err error) {
|
||||
contents = make(map[string][]byte)
|
||||
for _, url := range urls {
|
||||
zipBytes, status, err := client.Get(ctx, url)
|
||||
|
||||
Reference in New Issue
Block a user