2021-02-06 11:05:50 -05:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
|
|
import (
|
2021-08-17 15:44:11 +00:00
|
|
|
"errors"
|
2021-02-06 11:05:50 -05:00
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/qdm12/gluetun/internal/constants"
|
|
|
|
|
"github.com/qdm12/golibs/params"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Provider contains settings specific to a VPN provider.
|
|
|
|
|
type Provider struct {
|
2021-08-17 15:44:11 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
|
ServerSelection ServerSelection `json:"server_selection"`
|
|
|
|
|
PortForwarding PortForwarding `json:"port_forwarding"`
|
2021-02-06 11:05:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (settings *Provider) lines() (lines []string) {
|
2021-08-17 16:54:22 +00:00
|
|
|
if settings.Name == "" { // custom OpenVPN configuration
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-06 18:31:14 +00:00
|
|
|
lines = append(lines, lastIndent+strings.Title(settings.Name)+" settings:")
|
2021-02-06 11:05:50 -05:00
|
|
|
|
2021-08-19 17:41:37 +00:00
|
|
|
for _, line := range settings.ServerSelection.toLines() {
|
|
|
|
|
lines = append(lines, indent+line)
|
2021-02-06 11:05:50 -05:00
|
|
|
}
|
|
|
|
|
|
2021-08-19 17:41:37 +00:00
|
|
|
if settings.PortForwarding.Enabled { // PIA
|
|
|
|
|
lines = append(lines, indent+lastIndent+"Port forwarding:")
|
|
|
|
|
for _, line := range settings.PortForwarding.lines() {
|
|
|
|
|
lines = append(lines, indent+indent+line)
|
|
|
|
|
}
|
2021-02-06 11:05:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return lines
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 15:44:11 +00:00
|
|
|
var (
|
|
|
|
|
ErrInvalidVPNProvider = errors.New("invalid VPN provider")
|
|
|
|
|
)
|
|
|
|
|
|
2021-08-19 14:09:41 +00:00
|
|
|
func (settings *Provider) read(r reader, vpnType string) error {
|
2021-08-22 14:58:39 -07:00
|
|
|
err := settings.readVPNServiceProvider(r, vpnType)
|
2021-08-17 15:44:11 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch settings.Name {
|
|
|
|
|
case constants.Cyberghost:
|
2021-08-19 14:09:41 +00:00
|
|
|
err = settings.readCyberghost(r)
|
2021-08-17 15:44:11 +00:00
|
|
|
case constants.Fastestvpn:
|
2021-08-19 14:09:41 +00:00
|
|
|
err = settings.readFastestvpn(r)
|
2021-08-17 15:44:11 +00:00
|
|
|
case constants.HideMyAss:
|
2021-08-19 14:09:41 +00:00
|
|
|
err = settings.readHideMyAss(r)
|
2021-08-17 15:44:11 +00:00
|
|
|
case constants.Ipvanish:
|
2021-08-19 14:09:41 +00:00
|
|
|
err = settings.readIpvanish(r)
|
2021-08-17 15:44:11 +00:00
|
|
|
case constants.Ivpn:
|
2021-08-19 14:09:41 +00:00
|
|
|
err = settings.readIvpn(r)
|
2021-08-17 15:44:11 +00:00
|
|
|
case constants.Mullvad:
|
2021-08-19 14:09:41 +00:00
|
|
|
err = settings.readMullvad(r)
|
2021-08-17 15:44:11 +00:00
|
|
|
case constants.Nordvpn:
|
2021-08-19 14:09:41 +00:00
|
|
|
err = settings.readNordvpn(r)
|
2021-08-17 15:44:11 +00:00
|
|
|
case constants.Privado:
|
2021-08-19 14:09:41 +00:00
|
|
|
err = settings.readPrivado(r)
|
2021-08-17 15:44:11 +00:00
|
|
|
case constants.PrivateInternetAccess:
|
2021-08-19 14:09:41 +00:00
|
|
|
err = settings.readPrivateInternetAccess(r)
|
2021-08-17 15:44:11 +00:00
|
|
|
case constants.Privatevpn:
|
2021-08-19 14:09:41 +00:00
|
|
|
err = settings.readPrivatevpn(r)
|
2021-08-17 15:44:11 +00:00
|
|
|
case constants.Protonvpn:
|
2021-08-19 14:09:41 +00:00
|
|
|
err = settings.readProtonvpn(r)
|
2021-08-17 15:44:11 +00:00
|
|
|
case constants.Purevpn:
|
2021-08-19 14:09:41 +00:00
|
|
|
err = settings.readPurevpn(r)
|
2021-08-17 15:44:11 +00:00
|
|
|
case constants.Surfshark:
|
2021-08-19 14:09:41 +00:00
|
|
|
err = settings.readSurfshark(r)
|
2021-08-17 15:44:11 +00:00
|
|
|
case constants.Torguard:
|
2021-08-19 14:09:41 +00:00
|
|
|
err = settings.readTorguard(r)
|
2021-08-17 15:44:11 +00:00
|
|
|
case constants.VPNUnlimited:
|
2021-08-19 14:09:41 +00:00
|
|
|
err = settings.readVPNUnlimited(r)
|
2021-08-17 15:44:11 +00:00
|
|
|
case constants.Vyprvpn:
|
2021-08-19 14:09:41 +00:00
|
|
|
err = settings.readVyprvpn(r)
|
2021-08-17 15:44:11 +00:00
|
|
|
case constants.Windscribe:
|
2021-08-19 14:09:41 +00:00
|
|
|
err = settings.readWindscribe(r)
|
2021-08-17 15:44:11 +00:00
|
|
|
default:
|
|
|
|
|
return fmt.Errorf("%w: %s", ErrInvalidVPNProvider, settings.Name)
|
|
|
|
|
}
|
2021-08-19 14:09:41 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
settings.ServerSelection.VPN = vpnType
|
|
|
|
|
return nil
|
2021-08-17 15:44:11 +00:00
|
|
|
}
|
|
|
|
|
|
2021-08-22 14:58:39 -07:00
|
|
|
func (settings *Provider) readVPNServiceProvider(r reader, vpnType string) (err error) {
|
|
|
|
|
var allowedVPNServiceProviders []string
|
|
|
|
|
switch vpnType {
|
|
|
|
|
case constants.OpenVPN:
|
|
|
|
|
allowedVPNServiceProviders = []string{
|
|
|
|
|
"cyberghost", "fastestvpn", "hidemyass", "ipvanish", "ivpn", "mullvad", "nordvpn",
|
|
|
|
|
"privado", "pia", "private internet access", "privatevpn", "protonvpn",
|
|
|
|
|
"purevpn", "surfshark", "torguard", constants.VPNUnlimited, "vyprvpn", "windscribe"}
|
|
|
|
|
case constants.Wireguard:
|
|
|
|
|
allowedVPNServiceProviders = []string{constants.Mullvad, constants.Windscribe}
|
|
|
|
|
}
|
2021-08-17 15:44:11 +00:00
|
|
|
|
|
|
|
|
vpnsp, err := r.env.Inside("VPNSP", allowedVPNServiceProviders,
|
|
|
|
|
params.Default("private internet access"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("environment variable VPNSP: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if vpnsp == "pia" { // retro compatibility
|
|
|
|
|
vpnsp = "private internet access"
|
|
|
|
|
}
|
|
|
|
|
settings.Name = vpnsp
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-06 11:05:50 -05:00
|
|
|
func commaJoin(slice []string) string {
|
|
|
|
|
return strings.Join(slice, ", ")
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 18:18:12 +00:00
|
|
|
func protoToString(tcp bool) string {
|
|
|
|
|
if tcp {
|
|
|
|
|
return constants.TCP
|
|
|
|
|
}
|
|
|
|
|
return constants.UDP
|
2021-02-06 11:05:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func readTargetIP(env params.Env) (targetIP net.IP, err error) {
|
2021-07-23 02:34:15 +00:00
|
|
|
targetIP, err = readIP(env, "OPENVPN_TARGET_IP")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("environment variable OPENVPN_TARGET_IP: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return targetIP, nil
|
2021-02-06 11:05:50 -05:00
|
|
|
}
|
|
|
|
|
|
2021-08-22 14:58:39 -07:00
|
|
|
func readOpenVPNCustomPort(env params.Env, tcp bool,
|
2021-02-06 11:05:50 -05:00
|
|
|
allowedTCP, allowedUDP []uint16) (port uint16, err error) {
|
|
|
|
|
port, err = readPortOrZero(env, "PORT")
|
|
|
|
|
if err != nil {
|
2021-07-23 02:34:15 +00:00
|
|
|
return 0, fmt.Errorf("environment variable PORT: %w", err)
|
2021-02-06 11:05:50 -05:00
|
|
|
} else if port == 0 {
|
|
|
|
|
return 0, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 18:18:12 +00:00
|
|
|
if tcp {
|
2021-02-06 11:05:50 -05:00
|
|
|
for i := range allowedTCP {
|
|
|
|
|
if allowedTCP[i] == port {
|
|
|
|
|
return port, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-22 14:58:39 -07:00
|
|
|
return 0, fmt.Errorf(
|
|
|
|
|
"environment variable PORT: %w: port %d for TCP protocol, can only be one of %s",
|
|
|
|
|
ErrInvalidPort, port, portsToString(allowedTCP))
|
2021-05-10 18:18:12 +00:00
|
|
|
}
|
|
|
|
|
for i := range allowedUDP {
|
|
|
|
|
if allowedUDP[i] == port {
|
|
|
|
|
return port, nil
|
2021-02-06 11:05:50 -05:00
|
|
|
}
|
|
|
|
|
}
|
2021-08-22 14:58:39 -07:00
|
|
|
return 0, fmt.Errorf(
|
|
|
|
|
"environment variable PORT: %w: port %d for UDP protocol, can only be one of %s",
|
|
|
|
|
ErrInvalidPort, port, portsToString(allowedUDP))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func readWireguardCustomPort(env params.Env, allowed []uint16) (port uint16, err error) {
|
|
|
|
|
port, err = readPortOrZero(env, "WIREGUARD_PORT")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, fmt.Errorf("environment variable WIREGUARD_PORT: %w", err)
|
|
|
|
|
} else if port == 0 {
|
|
|
|
|
return 0, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := range allowed {
|
|
|
|
|
if allowed[i] == port {
|
|
|
|
|
return port, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0, fmt.Errorf(
|
|
|
|
|
"environment variable WIREGUARD_PORT: %w: port %d, can only be one of %s",
|
|
|
|
|
ErrInvalidPort, port, portsToString(allowed))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func portsToString(ports []uint16) string {
|
|
|
|
|
slice := make([]string, len(ports))
|
|
|
|
|
for i := range ports {
|
|
|
|
|
slice[i] = fmt.Sprint(ports[i])
|
|
|
|
|
}
|
|
|
|
|
return strings.Join(slice, ", ")
|
2021-02-06 11:05:50 -05:00
|
|
|
}
|