Files
gluetun/internal/provider/privado.go

134 lines
3.6 KiB
Go
Raw Normal View History

2020-11-08 20:56:49 -05:00
package provider
import (
"context"
"fmt"
"math/rand"
"net"
"net/http"
2021-01-19 02:55:38 +00:00
"strconv"
2020-11-08 20:56:49 -05:00
2021-02-06 11:05:50 -05:00
"github.com/qdm12/gluetun/internal/configuration"
2020-11-08 20:56:49 -05:00
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/firewall"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/golibs/logging"
2021-01-02 01:57:00 +00:00
"github.com/qdm12/golibs/os"
2020-11-08 20:56:49 -05:00
)
type privado struct {
servers []models.PrivadoServer
randSource rand.Source
}
func newPrivado(servers []models.PrivadoServer, timeNow timeNowFunc) *privado {
return &privado{
servers: servers,
randSource: rand.NewSource(timeNow().UnixNano()),
}
}
func (s *privado) filterServers(hostnames []string) (servers []models.PrivadoServer) {
2020-11-08 20:56:49 -05:00
for _, server := range s.servers {
switch {
case filterByPossibilities(server.Hostname, hostnames):
2020-11-08 20:56:49 -05:00
default:
servers = append(servers, server)
}
}
return servers
}
2021-02-06 11:05:50 -05:00
func (s *privado) GetOpenVPNConnection(selection configuration.ServerSelection) (
2020-11-08 20:56:49 -05:00
connection models.OpenVPNConnection, err error) {
var port uint16 = 1194
switch selection.Protocol {
case constants.UDP:
default:
return connection, fmt.Errorf("protocol %q is not supported by Privado", selection.Protocol)
}
if selection.TargetIP != nil {
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil
}
servers := s.filterServers(selection.Hostnames)
2020-11-08 20:56:49 -05:00
if len(servers) == 0 {
return connection, fmt.Errorf("no server found for cities %s and server numbers %v",
commaJoin(selection.Cities), selection.Numbers)
}
connections := make([]models.OpenVPNConnection, len(servers))
for i := range servers {
connection := models.OpenVPNConnection{
IP: servers[i].IP,
Port: port,
Protocol: selection.Protocol,
Hostname: servers[i].Hostname,
2020-11-08 20:56:49 -05:00
}
2020-12-31 21:57:26 +00:00
connections[i] = connection
2020-11-08 20:56:49 -05:00
}
return pickRandomConnection(connections, s.randSource), nil
}
func (s *privado) BuildConf(connection models.OpenVPNConnection,
2021-02-06 11:05:50 -05:00
username string, settings configuration.OpenVPN) (lines []string) {
if len(settings.Cipher) == 0 {
settings.Cipher = aes256cbc
2020-11-08 20:56:49 -05:00
}
if len(settings.Auth) == 0 {
settings.Auth = sha256
2020-11-08 20:56:49 -05:00
}
lines = []string{
"client",
"dev tun",
"nobind",
"persist-key",
"ping 10",
"ping-exit 60",
"ping-timer-rem",
2021-01-22 13:36:56 +00:00
"tls-exit",
2020-11-08 20:56:49 -05:00
// Privado specific
"tls-cipher TLS-DHE-RSA-WITH-AES-256-CBC-SHA:TLS-DHE-DSS-WITH-AES-256-CBC-SHA:TLS-RSA-WITH-AES-256-CBC-SHA",
fmt.Sprintf("verify-x509-name %s name", connection.Hostname),
// Added constant values
"auth-nocache",
"mute-replay-warnings",
"pull-filter ignore \"auth-token\"", // prevent auth failed loops
`pull-filter ignore "ping-restart"`,
2020-11-08 20:56:49 -05:00
"auth-retry nointeract",
"suppress-timestamps",
// Modified variables
fmt.Sprintf("verb %d", settings.Verbosity),
2020-11-08 20:56:49 -05:00
fmt.Sprintf("auth-user-pass %s", constants.OpenVPNAuthConf),
fmt.Sprintf("proto %s", connection.Protocol),
fmt.Sprintf("remote %s %d", connection.IP, connection.Port),
fmt.Sprintf("cipher %s", settings.Cipher),
fmt.Sprintf("auth %s", settings.Auth),
2020-11-08 20:56:49 -05:00
}
if !settings.Root {
2020-12-27 00:36:39 +00:00
lines = append(lines, "user "+username)
2020-11-08 20:56:49 -05:00
}
2021-01-19 02:55:38 +00:00
if settings.MSSFix > 0 {
lines = append(lines, "mssfix "+strconv.Itoa(int(settings.MSSFix)))
}
2020-11-08 20:56:49 -05:00
lines = append(lines, []string{
"<ca>",
"-----BEGIN CERTIFICATE-----",
constants.PrivadoCertificate,
"-----END CERTIFICATE-----",
"</ca>",
}...)
return lines
}
func (s *privado) PortForward(ctx context.Context, client *http.Client,
openFile os.OpenFileFunc, pfLogger logging.Logger, gateway net.IP, fw firewall.Configurator,
2021-02-06 18:31:14 +00:00
syncState func(port uint16) (pfFilepath string)) {
2020-11-08 20:56:49 -05:00
panic("port forwarding is not supported for privado")
}