Maint: make VPN connection not specific to OpenVPN
- Add VPN field to ServerSelection struct - Set VPN type to server selection at start using VPN_TYPE - Change OpenVPNConnection to Connection with Type field - Rename Provider GetOpenVPNConnection to GetConnection - Rename GetTargetIPOpenVPNConnection to GetTargetIPConnection - Rename PickRandomOpenVPNConnection to PickRandomConnection - Add 'OpenVPN' prefix to OpenVPN specific methods on connection
This commit is contained in:
@@ -14,7 +14,7 @@ var (
|
||||
)
|
||||
|
||||
func BuildConfig(settings configuration.OpenVPN) (
|
||||
lines []string, connection models.OpenVPNConnection, err error) {
|
||||
lines []string, connection models.Connection, err error) {
|
||||
lines, err = readCustomConfigLines(settings.Config)
|
||||
if err != nil {
|
||||
return nil, connection, fmt.Errorf("%w: %s", ErrReadCustomConfig, err)
|
||||
|
||||
@@ -54,7 +54,7 @@ func Test_BuildConfig(t *testing.T) {
|
||||
}
|
||||
assert.Equal(t, expectedLines, lines)
|
||||
|
||||
expectedConnection := models.OpenVPNConnection{
|
||||
expectedConnection := models.Connection{
|
||||
IP: net.IPv4(1, 9, 8, 7),
|
||||
Port: 1194,
|
||||
Protocol: constants.UDP,
|
||||
|
||||
@@ -17,7 +17,7 @@ var (
|
||||
|
||||
// extractConnectionFromLines always takes the first remote line only.
|
||||
func extractConnectionFromLines(lines []string) (
|
||||
connection models.OpenVPNConnection, err error) {
|
||||
connection models.Connection, err error) {
|
||||
for i, line := range lines {
|
||||
newConnectionData, err := extractConnectionFromLine(line)
|
||||
if err != nil {
|
||||
@@ -54,7 +54,7 @@ var (
|
||||
)
|
||||
|
||||
func extractConnectionFromLine(line string) (
|
||||
connection models.OpenVPNConnection, err error) {
|
||||
connection models.Connection, err error) {
|
||||
switch {
|
||||
case strings.HasPrefix(line, "proto "):
|
||||
connection.Protocol, err = extractProto(line)
|
||||
|
||||
@@ -16,12 +16,12 @@ func Test_extractConnectionFromLines(t *testing.T) {
|
||||
|
||||
testCases := map[string]struct {
|
||||
lines []string
|
||||
connection models.OpenVPNConnection
|
||||
connection models.Connection
|
||||
err error
|
||||
}{
|
||||
"success": {
|
||||
lines: []string{"bla bla", "proto tcp", "remote 1.2.3.4 1194 tcp"},
|
||||
connection: models.OpenVPNConnection{
|
||||
connection: models.Connection{
|
||||
IP: net.IPv4(1, 2, 3, 4),
|
||||
Port: 1194,
|
||||
Protocol: constants.TCP,
|
||||
@@ -33,7 +33,7 @@ func Test_extractConnectionFromLines(t *testing.T) {
|
||||
},
|
||||
"only use first values found": {
|
||||
lines: []string{"proto udp", "proto tcp", "remote 1.2.3.4 443 tcp", "remote 5.2.3.4 1194 udp"},
|
||||
connection: models.OpenVPNConnection{
|
||||
connection: models.Connection{
|
||||
IP: net.IPv4(1, 2, 3, 4),
|
||||
Port: 443,
|
||||
Protocol: constants.UDP,
|
||||
@@ -41,14 +41,14 @@ func Test_extractConnectionFromLines(t *testing.T) {
|
||||
},
|
||||
"no IP found": {
|
||||
lines: []string{"proto tcp"},
|
||||
connection: models.OpenVPNConnection{
|
||||
connection: models.Connection{
|
||||
Protocol: constants.TCP,
|
||||
},
|
||||
err: errRemoteLineNotFound,
|
||||
},
|
||||
"default TCP port": {
|
||||
lines: []string{"remote 1.2.3.4", "proto tcp"},
|
||||
connection: models.OpenVPNConnection{
|
||||
connection: models.Connection{
|
||||
IP: net.IPv4(1, 2, 3, 4),
|
||||
Port: 443,
|
||||
Protocol: constants.TCP,
|
||||
@@ -56,7 +56,7 @@ func Test_extractConnectionFromLines(t *testing.T) {
|
||||
},
|
||||
"default UDP port": {
|
||||
lines: []string{"remote 1.2.3.4", "proto udp"},
|
||||
connection: models.OpenVPNConnection{
|
||||
connection: models.Connection{
|
||||
IP: net.IPv4(1, 2, 3, 4),
|
||||
Port: 1194,
|
||||
Protocol: constants.UDP,
|
||||
@@ -88,7 +88,7 @@ func Test_extractConnectionFromLine(t *testing.T) {
|
||||
|
||||
testCases := map[string]struct {
|
||||
line string
|
||||
connection models.OpenVPNConnection
|
||||
connection models.Connection
|
||||
isErr error
|
||||
}{
|
||||
"irrelevant line": {
|
||||
@@ -100,7 +100,7 @@ func Test_extractConnectionFromLine(t *testing.T) {
|
||||
},
|
||||
"extract proto success": {
|
||||
line: "proto tcp",
|
||||
connection: models.OpenVPNConnection{
|
||||
connection: models.Connection{
|
||||
Protocol: constants.TCP,
|
||||
},
|
||||
},
|
||||
@@ -110,7 +110,7 @@ func Test_extractConnectionFromLine(t *testing.T) {
|
||||
},
|
||||
"extract remote success": {
|
||||
line: "remote 1.2.3.4 1194 udp",
|
||||
connection: models.OpenVPNConnection{
|
||||
connection: models.Connection{
|
||||
IP: net.IPv4(1, 2, 3, 4),
|
||||
Port: 1194,
|
||||
Protocol: constants.UDP,
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func modifyCustomConfig(lines []string, settings configuration.OpenVPN,
|
||||
connection models.OpenVPNConnection) (modified []string) {
|
||||
connection models.Connection) (modified []string) {
|
||||
// Remove some lines
|
||||
for _, line := range lines {
|
||||
switch {
|
||||
@@ -33,8 +33,8 @@ func modifyCustomConfig(lines []string, settings configuration.OpenVPN,
|
||||
}
|
||||
|
||||
// Add values
|
||||
modified = append(modified, connection.ProtoLine())
|
||||
modified = append(modified, connection.RemoteLine())
|
||||
modified = append(modified, connection.OpenVPNProtoLine())
|
||||
modified = append(modified, connection.OpenVPNRemoteLine())
|
||||
modified = append(modified, "mute-replay-warnings")
|
||||
modified = append(modified, "auth-nocache")
|
||||
modified = append(modified, "pull-filter ignore \"auth-token\"") // prevent auth failed loop
|
||||
|
||||
@@ -16,7 +16,7 @@ func Test_modifyCustomConfig(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
lines []string
|
||||
settings configuration.OpenVPN
|
||||
connection models.OpenVPNConnection
|
||||
connection models.Connection
|
||||
modified []string
|
||||
}{
|
||||
"mixed": {
|
||||
@@ -36,7 +36,7 @@ func Test_modifyCustomConfig(t *testing.T) {
|
||||
MSSFix: 1000,
|
||||
ProcUser: "procuser",
|
||||
},
|
||||
connection: models.OpenVPNConnection{
|
||||
connection: models.Connection{
|
||||
IP: net.IPv4(1, 2, 3, 4),
|
||||
Port: 1194,
|
||||
Protocol: constants.UDP,
|
||||
|
||||
Reference in New Issue
Block a user