Maintenance: split each provider in a package
- Fix VyprVPN port - Fix missing Auth overrides
This commit is contained in:
15
internal/provider/utils/filtering.go
Normal file
15
internal/provider/utils/filtering.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package utils
|
||||
|
||||
import "strings"
|
||||
|
||||
func FilterByPossibilities(value string, possibilities []string) (filtered bool) {
|
||||
if len(possibilities) == 0 {
|
||||
return false
|
||||
}
|
||||
for _, possibility := range possibilities {
|
||||
if strings.EqualFold(value, possibility) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
35
internal/provider/utils/filtering_test.go
Normal file
35
internal/provider/utils/filtering_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_FilterByPossibilities(t *testing.T) {
|
||||
t.Parallel()
|
||||
testCases := map[string]struct {
|
||||
value string
|
||||
possibilities []string
|
||||
filtered bool
|
||||
}{
|
||||
"no possibilities": {},
|
||||
"value not in possibilities": {
|
||||
value: "c",
|
||||
possibilities: []string{"a", "b"},
|
||||
filtered: true,
|
||||
},
|
||||
"value in possibilities": {
|
||||
value: "c",
|
||||
possibilities: []string{"a", "b", "c"},
|
||||
},
|
||||
}
|
||||
|
||||
for name, testCase := range testCases {
|
||||
testCase := testCase
|
||||
t.Run(name, func(t *testing.T) {
|
||||
filtered := FilterByPossibilities(testCase.value, testCase.possibilities)
|
||||
assert.Equal(t, testCase.filtered, filtered)
|
||||
})
|
||||
}
|
||||
}
|
||||
119
internal/provider/utils/formatting.go
Normal file
119
internal/provider/utils/formatting.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration"
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
)
|
||||
|
||||
func commaJoin(slice []string) string {
|
||||
return strings.Join(slice, ", ")
|
||||
}
|
||||
|
||||
var ErrNoServerFound = errors.New("no server found")
|
||||
|
||||
func NoServerFoundError(selection configuration.ServerSelection) (err error) {
|
||||
var messageParts []string
|
||||
|
||||
protocol := constants.UDP
|
||||
if selection.TCP {
|
||||
protocol = constants.TCP
|
||||
}
|
||||
messageParts = append(messageParts, "protocol "+protocol)
|
||||
|
||||
if selection.Group != "" {
|
||||
part := "group " + selection.Group
|
||||
messageParts = append(messageParts, part)
|
||||
}
|
||||
|
||||
switch len(selection.Countries) {
|
||||
case 0:
|
||||
case 1:
|
||||
part := "country " + selection.Countries[0]
|
||||
messageParts = append(messageParts, part)
|
||||
default:
|
||||
part := "countries " + commaJoin(selection.Countries)
|
||||
messageParts = append(messageParts, part)
|
||||
}
|
||||
|
||||
switch len(selection.Regions) {
|
||||
case 0:
|
||||
case 1:
|
||||
part := "region " + selection.Regions[0]
|
||||
messageParts = append(messageParts, part)
|
||||
default:
|
||||
part := "regions " + commaJoin(selection.Regions)
|
||||
messageParts = append(messageParts, part)
|
||||
}
|
||||
|
||||
switch len(selection.Cities) {
|
||||
case 0:
|
||||
case 1:
|
||||
part := "city " + selection.Cities[0]
|
||||
messageParts = append(messageParts, part)
|
||||
default:
|
||||
part := "cities " + commaJoin(selection.Cities)
|
||||
messageParts = append(messageParts, part)
|
||||
}
|
||||
|
||||
if selection.Owned {
|
||||
messageParts = append(messageParts, "owned servers only")
|
||||
}
|
||||
|
||||
switch len(selection.ISPs) {
|
||||
case 0:
|
||||
case 1:
|
||||
part := "ISP " + selection.ISPs[0]
|
||||
messageParts = append(messageParts, part)
|
||||
default:
|
||||
part := "ISPs " + commaJoin(selection.ISPs)
|
||||
messageParts = append(messageParts, part)
|
||||
}
|
||||
|
||||
switch len(selection.Hostnames) {
|
||||
case 0:
|
||||
case 1:
|
||||
part := "hostname " + selection.Hostnames[0]
|
||||
messageParts = append(messageParts, part)
|
||||
default:
|
||||
part := "hostnames " + commaJoin(selection.Hostnames)
|
||||
messageParts = append(messageParts, part)
|
||||
}
|
||||
|
||||
switch len(selection.Names) {
|
||||
case 0:
|
||||
case 1:
|
||||
part := "name " + selection.Names[0]
|
||||
messageParts = append(messageParts, part)
|
||||
default:
|
||||
part := "names " + commaJoin(selection.Names)
|
||||
messageParts = append(messageParts, part)
|
||||
}
|
||||
|
||||
switch len(selection.Numbers) {
|
||||
case 0:
|
||||
case 1:
|
||||
part := "server number " + strconv.Itoa(int(selection.Numbers[0]))
|
||||
messageParts = append(messageParts, part)
|
||||
default:
|
||||
serverNumbers := make([]string, len(selection.Numbers))
|
||||
for i := range selection.Numbers {
|
||||
serverNumbers[i] = strconv.Itoa(int(selection.Numbers[i]))
|
||||
}
|
||||
part := "server numbers " + commaJoin(serverNumbers)
|
||||
messageParts = append(messageParts, part)
|
||||
}
|
||||
|
||||
if selection.EncryptionPreset != "" {
|
||||
part := "encryption preset " + selection.EncryptionPreset
|
||||
messageParts = append(messageParts, part)
|
||||
}
|
||||
|
||||
message := "for " + strings.Join(messageParts, "; ")
|
||||
|
||||
return fmt.Errorf("%w: %s", ErrNoServerFound, message)
|
||||
}
|
||||
71
internal/provider/utils/openvpn.go
Normal file
71
internal/provider/utils/openvpn.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package utils
|
||||
|
||||
func WrapOpenvpnCA(certificate string) (lines []string) {
|
||||
return []string{
|
||||
"<ca>",
|
||||
"-----BEGIN CERTIFICATE-----",
|
||||
certificate,
|
||||
"-----END CERTIFICATE-----",
|
||||
"</ca>",
|
||||
}
|
||||
}
|
||||
|
||||
func WrapOpenvpnCert(clientCertificate string) (lines []string) {
|
||||
return []string{
|
||||
"<cert>",
|
||||
"-----BEGIN CERTIFICATE-----",
|
||||
clientCertificate,
|
||||
"-----END CERTIFICATE-----",
|
||||
"</cert>",
|
||||
}
|
||||
}
|
||||
|
||||
func WrapOpenvpnCRLVerify(x509CRL string) (lines []string) {
|
||||
return []string{
|
||||
"<crl-verify>",
|
||||
"-----BEGIN X509 CRL-----",
|
||||
x509CRL,
|
||||
"-----END X509 CRL-----",
|
||||
"</crl-verify>",
|
||||
}
|
||||
}
|
||||
|
||||
func WrapOpenvpnKey(clientKey string) (lines []string) {
|
||||
return []string{
|
||||
"<key>",
|
||||
"-----BEGIN PRIVATE KEY-----",
|
||||
clientKey,
|
||||
"-----END PRIVATE KEY-----",
|
||||
"</key>",
|
||||
}
|
||||
}
|
||||
|
||||
func WrapOpenvpnRSAKey(rsaPrivateKey string) (lines []string) {
|
||||
return []string{
|
||||
"<key>",
|
||||
"-----BEGIN RSA PRIVATE KEY-----",
|
||||
rsaPrivateKey,
|
||||
"-----END RSA PRIVATE KEY-----",
|
||||
"</key>",
|
||||
}
|
||||
}
|
||||
|
||||
func WrapOpenvpnTLSAuth(staticKeyV1 string) (lines []string) {
|
||||
return []string{
|
||||
"<tls-auth>",
|
||||
"-----BEGIN OpenVPN Static key V1-----",
|
||||
staticKeyV1,
|
||||
"-----END OpenVPN Static key V1-----",
|
||||
"</tls-auth>",
|
||||
}
|
||||
}
|
||||
|
||||
func WrapOpenvpnTLSCrypt(staticKeyV1 string) (lines []string) {
|
||||
return []string{
|
||||
"<tls-crypt>",
|
||||
"-----BEGIN OpenVPN Static key V1-----",
|
||||
staticKeyV1,
|
||||
"-----END OpenVPN Static key V1-----",
|
||||
"</tls-crypt>",
|
||||
}
|
||||
}
|
||||
12
internal/provider/utils/pick.go
Normal file
12
internal/provider/utils/pick.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
func PickRandomConnection(connections []models.OpenVPNConnection,
|
||||
source rand.Source) models.OpenVPNConnection {
|
||||
return connections[rand.New(source).Intn(len(connections))] //nolint:gosec
|
||||
}
|
||||
26
internal/provider/utils/pick_test.go
Normal file
26
internal/provider/utils/pick_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"testing"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_PickRandomConnection(t *testing.T) {
|
||||
t.Parallel()
|
||||
connections := []models.OpenVPNConnection{
|
||||
{Port: 1}, {Port: 2}, {Port: 3}, {Port: 4},
|
||||
}
|
||||
source := rand.NewSource(0)
|
||||
|
||||
connection := PickRandomConnection(connections, source)
|
||||
assert.Equal(t, models.OpenVPNConnection{Port: 3}, connection)
|
||||
|
||||
connection = PickRandomConnection(connections, source)
|
||||
assert.Equal(t, models.OpenVPNConnection{Port: 3}, connection)
|
||||
|
||||
connection = PickRandomConnection(connections, source)
|
||||
assert.Equal(t, models.OpenVPNConnection{Port: 2}, connection)
|
||||
}
|
||||
22
internal/provider/utils/targetip.go
Normal file
22
internal/provider/utils/targetip.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
var ErrTargetIPNotFound = errors.New("target IP address not found")
|
||||
|
||||
func GetTargetIPConnection(connections []models.OpenVPNConnection,
|
||||
targetIP net.IP) (connection models.OpenVPNConnection, err error) {
|
||||
for _, connection := range connections {
|
||||
if targetIP.Equal(connection.IP) {
|
||||
return connection, nil
|
||||
}
|
||||
}
|
||||
return connection, fmt.Errorf("%w: in %d filtered connections",
|
||||
ErrTargetIPNotFound, len(connections))
|
||||
}
|
||||
Reference in New Issue
Block a user