chore(all): memory and thread safe storage

- settings: get filter choices from storage for settings validation
- updater: update servers to the storage
- storage: minimal deep copying and data duplication
- storage: add merged servers mutex for thread safety
- connection: filter servers in storage
- formatter: format servers to Markdown in storage
- PIA: get server by name from storage directly
- Updater: get servers count from storage directly
- Updater: equality check done in storage, fix #882
This commit is contained in:
Quentin McGaw
2022-06-05 14:58:46 +00:00
parent 1e6b4ed5eb
commit 36b504609b
84 changed files with 1267 additions and 877 deletions

View File

@@ -2,6 +2,7 @@ package privateinternetaccess
import (
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/constants/providers"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider/privateinternetaccess/presets"
"github.com/qdm12/gluetun/internal/provider/utils"
@@ -20,5 +21,6 @@ func (p *Provider) GetConnection(selection settings.ServerSelection) (
defaults.OpenVPNUDPPort = 1197
}
return utils.GetConnection(p.servers, selection, defaults, p.randSource)
return utils.GetConnection(providers.PrivateInternetAccess,
p.storage, selection, defaults, p.randSource)
}

View File

@@ -15,25 +15,24 @@ import (
"strings"
"time"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/constants/providers"
"github.com/qdm12/gluetun/internal/provider/utils"
"github.com/qdm12/golibs/format"
)
var (
ErrGatewayIPIsNil = errors.New("gateway IP address is nil")
ErrServerNameEmpty = errors.New("server name is empty")
ErrServerNameNotFound = errors.New("server name not found in servers")
ErrGatewayIPIsNil = errors.New("gateway IP address is nil")
ErrServerNameEmpty = errors.New("server name is empty")
)
// PortForward obtains a VPN server side port forwarded from PIA.
func (p *Provider) PortForward(ctx context.Context, client *http.Client,
logger utils.Logger, gateway net.IP, serverName string) (
port uint16, err error) {
var server models.Server
for _, server = range p.servers {
if server.ServerName == serverName {
break
}
server, ok := p.storage.GetServerByName(providers.PrivateInternetAccess, serverName)
if !ok {
return 0, fmt.Errorf("%w: %s", ErrServerNameNotFound, serverName)
}
if !server.PortForward {

View File

@@ -5,11 +5,11 @@ import (
"time"
"github.com/qdm12/gluetun/internal/constants/openvpn"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider/common"
)
type Provider struct {
servers []models.Server
storage common.Storage
randSource rand.Source
timeNow func() time.Time
// Port forwarding
@@ -17,11 +17,11 @@ type Provider struct {
authFilePath string
}
func New(servers []models.Server, randSource rand.Source,
func New(storage common.Storage, randSource rand.Source,
timeNow func() time.Time) *Provider {
const jsonPortForwardPath = "/gluetun/piaportforward.json"
return &Provider{
servers: servers,
storage: storage,
timeNow: timeNow,
randSource: randSource,
portForwardPath: jsonPortForwardPath,