2020-06-13 14:08:29 -04:00
|
|
|
package provider
|
2020-06-13 10:43:47 -04:00
|
|
|
|
|
|
|
|
import (
|
2020-10-12 10:55:08 -04:00
|
|
|
"context"
|
2020-06-13 10:43:47 -04:00
|
|
|
"fmt"
|
2020-10-12 15:29:58 -04:00
|
|
|
"math/rand"
|
2020-10-12 10:55:08 -04:00
|
|
|
"net"
|
|
|
|
|
"net/http"
|
2020-06-13 10:43:47 -04:00
|
|
|
"strings"
|
|
|
|
|
|
2020-07-26 12:07:06 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/constants"
|
2020-10-12 10:55:08 -04:00
|
|
|
"github.com/qdm12/gluetun/internal/firewall"
|
2020-07-26 12:07:06 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/models"
|
2020-10-12 10:55:08 -04:00
|
|
|
"github.com/qdm12/golibs/files"
|
|
|
|
|
"github.com/qdm12/golibs/logging"
|
2020-06-13 10:43:47 -04:00
|
|
|
)
|
|
|
|
|
|
2020-08-25 19:38:50 -04:00
|
|
|
type cyberghost struct {
|
2020-10-12 15:29:58 -04:00
|
|
|
servers []models.CyberghostServer
|
|
|
|
|
randSource rand.Source
|
2020-08-25 19:38:50 -04:00
|
|
|
}
|
2020-06-13 14:08:29 -04:00
|
|
|
|
2020-10-12 15:29:58 -04:00
|
|
|
func newCyberghost(servers []models.CyberghostServer, timeNow timeNowFunc) *cyberghost {
|
2020-08-25 19:38:50 -04:00
|
|
|
return &cyberghost{
|
2020-10-12 15:29:58 -04:00
|
|
|
servers: servers,
|
|
|
|
|
randSource: rand.NewSource(timeNow().UnixNano()),
|
2020-08-25 19:38:50 -04:00
|
|
|
}
|
2020-06-13 14:08:29 -04:00
|
|
|
}
|
|
|
|
|
|
2020-10-18 17:15:42 -04:00
|
|
|
func (c *cyberghost) filterServers(regions []string, group string) (servers []models.CyberghostServer) {
|
|
|
|
|
for _, server := range c.servers {
|
|
|
|
|
switch {
|
2020-10-28 22:09:58 +00:00
|
|
|
case len(group) > 0 && !strings.EqualFold(group, server.Group),
|
2020-10-18 17:15:42 -04:00
|
|
|
filterByPossibilities(server.Region, regions):
|
|
|
|
|
default:
|
|
|
|
|
servers = append(servers, server)
|
2020-06-13 10:43:47 -04:00
|
|
|
}
|
|
|
|
|
}
|
2020-07-23 01:46:28 +00:00
|
|
|
return servers
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-20 02:45:28 +00:00
|
|
|
func (c *cyberghost) GetOpenVPNConnection(selection models.ServerSelection) (
|
|
|
|
|
connection models.OpenVPNConnection, err error) {
|
|
|
|
|
const httpsPort = 443
|
2020-10-12 20:21:26 +00:00
|
|
|
if selection.TargetIP != nil {
|
2020-10-20 02:45:28 +00:00
|
|
|
return models.OpenVPNConnection{IP: selection.TargetIP, Port: httpsPort, Protocol: selection.Protocol}, nil
|
2020-10-12 20:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
2020-10-18 17:15:42 -04:00
|
|
|
servers := c.filterServers(selection.Regions, selection.Group)
|
2020-07-23 01:46:28 +00:00
|
|
|
if len(servers) == 0 {
|
2020-10-20 02:45:28 +00:00
|
|
|
return connection,
|
|
|
|
|
fmt.Errorf("no server found for regions %s and group %q", commaJoin(selection.Regions), selection.Group)
|
2020-06-13 10:43:47 -04:00
|
|
|
}
|
2020-07-23 01:46:28 +00:00
|
|
|
|
2020-10-12 15:29:58 -04:00
|
|
|
var connections []models.OpenVPNConnection
|
2020-07-23 01:46:28 +00:00
|
|
|
for _, server := range servers {
|
|
|
|
|
for _, IP := range server.IPs {
|
2020-10-20 02:45:28 +00:00
|
|
|
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: httpsPort, Protocol: selection.Protocol})
|
2020-06-13 10:43:47 -04:00
|
|
|
}
|
|
|
|
|
}
|
2020-07-23 01:46:28 +00:00
|
|
|
|
2020-10-12 15:29:58 -04:00
|
|
|
return pickRandomConnection(connections, c.randSource), nil
|
2020-06-13 10:43:47 -04:00
|
|
|
}
|
|
|
|
|
|
2020-10-20 02:45:28 +00:00
|
|
|
func (c *cyberghost) BuildConf(connection models.OpenVPNConnection, verbosity,
|
|
|
|
|
uid, gid int, root bool, cipher, auth string, extras models.ExtraConfigOptions) (lines []string) {
|
2020-06-13 10:43:47 -04:00
|
|
|
if len(cipher) == 0 {
|
2020-06-13 14:08:29 -04:00
|
|
|
cipher = aes256cbc
|
2020-06-13 10:43:47 -04:00
|
|
|
}
|
|
|
|
|
if len(auth) == 0 {
|
2020-11-08 20:56:49 -05:00
|
|
|
auth = sha256
|
2020-06-13 10:43:47 -04:00
|
|
|
}
|
2020-07-13 23:34:03 +00:00
|
|
|
lines = []string{
|
2020-06-13 10:43:47 -04:00
|
|
|
"client",
|
|
|
|
|
"dev tun",
|
|
|
|
|
"nobind",
|
|
|
|
|
"persist-key",
|
|
|
|
|
"persist-tun",
|
|
|
|
|
"remote-cert-tls server",
|
|
|
|
|
|
|
|
|
|
// Cyberghost specific
|
2020-07-12 14:47:37 +00:00
|
|
|
// "redirect-gateway def1",
|
2020-06-13 10:43:47 -04:00
|
|
|
"ncp-disable",
|
|
|
|
|
"ping 5",
|
2020-07-12 14:47:37 +00:00
|
|
|
"explicit-exit-notify 2",
|
2020-06-13 10:43:47 -04:00
|
|
|
"script-security 2",
|
|
|
|
|
"route-delay 5",
|
|
|
|
|
|
|
|
|
|
// Added constant values
|
|
|
|
|
"auth-nocache",
|
|
|
|
|
"mute-replay-warnings",
|
|
|
|
|
"pull-filter ignore \"auth-token\"", // prevent auth failed loops
|
|
|
|
|
"auth-retry nointeract",
|
|
|
|
|
"suppress-timestamps",
|
|
|
|
|
|
|
|
|
|
// Modified variables
|
|
|
|
|
fmt.Sprintf("verb %d", verbosity),
|
|
|
|
|
fmt.Sprintf("auth-user-pass %s", constants.OpenVPNAuthConf),
|
2020-10-12 15:29:58 -04:00
|
|
|
fmt.Sprintf("proto %s", connection.Protocol),
|
|
|
|
|
fmt.Sprintf("remote %s %d", connection.IP, connection.Port),
|
2020-06-13 10:43:47 -04:00
|
|
|
fmt.Sprintf("cipher %s", cipher),
|
|
|
|
|
fmt.Sprintf("auth %s", auth),
|
|
|
|
|
}
|
|
|
|
|
if strings.HasSuffix(cipher, "-gcm") {
|
|
|
|
|
lines = append(lines, "ncp-ciphers AES-256-GCM:AES-256-CBC:AES-128-GCM")
|
|
|
|
|
}
|
|
|
|
|
if !root {
|
|
|
|
|
lines = append(lines, "user nonrootuser")
|
|
|
|
|
}
|
|
|
|
|
lines = append(lines, []string{
|
|
|
|
|
"<ca>",
|
|
|
|
|
"-----BEGIN CERTIFICATE-----",
|
|
|
|
|
constants.CyberghostCertificate,
|
|
|
|
|
"-----END CERTIFICATE-----",
|
|
|
|
|
"</ca>",
|
|
|
|
|
}...)
|
|
|
|
|
lines = append(lines, []string{
|
2020-07-05 16:20:40 +00:00
|
|
|
"<cert>",
|
2020-06-13 10:43:47 -04:00
|
|
|
"-----BEGIN CERTIFICATE-----",
|
2020-11-19 08:50:55 -05:00
|
|
|
extras.ClientCertificate,
|
2020-06-13 10:43:47 -04:00
|
|
|
"-----END CERTIFICATE-----",
|
2020-07-05 16:20:40 +00:00
|
|
|
"</cert>",
|
2020-06-13 10:43:47 -04:00
|
|
|
}...)
|
|
|
|
|
lines = append(lines, []string{
|
|
|
|
|
"<key>",
|
|
|
|
|
"-----BEGIN PRIVATE KEY-----",
|
2020-06-13 14:08:29 -04:00
|
|
|
extras.ClientKey,
|
2020-06-13 10:43:47 -04:00
|
|
|
"-----END PRIVATE KEY-----",
|
|
|
|
|
"</key>",
|
|
|
|
|
"",
|
|
|
|
|
}...)
|
2020-07-13 23:34:03 +00:00
|
|
|
return lines
|
2020-06-13 10:43:47 -04:00
|
|
|
}
|
2020-06-13 14:08:29 -04:00
|
|
|
|
2020-10-12 10:55:08 -04:00
|
|
|
func (c *cyberghost) PortForward(ctx context.Context, client *http.Client,
|
|
|
|
|
fileManager files.FileManager, pfLogger logging.Logger, gateway net.IP, fw firewall.Configurator,
|
|
|
|
|
syncState func(port uint16) (pfFilepath models.Filepath)) {
|
2020-06-13 14:08:29 -04:00
|
|
|
panic("port forwarding is not supported for cyberghost")
|
|
|
|
|
}
|