2020-06-13 14:08:29 -04:00
|
|
|
package provider
|
2020-06-13 10:43:47 -04:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2020-07-13 23:34:03 +00:00
|
|
|
"github.com/qdm12/golibs/network"
|
2020-06-13 10:43:47 -04:00
|
|
|
"github.com/qdm12/private-internet-access-docker/internal/constants"
|
|
|
|
|
"github.com/qdm12/private-internet-access-docker/internal/models"
|
|
|
|
|
)
|
|
|
|
|
|
2020-07-13 23:34:03 +00:00
|
|
|
type cyberghost struct{}
|
2020-06-13 14:08:29 -04:00
|
|
|
|
2020-07-13 23:34:03 +00:00
|
|
|
func newCyberghost() *cyberghost {
|
|
|
|
|
return &cyberghost{}
|
2020-06-13 14:08:29 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-23 01:46:28 +00:00
|
|
|
func (c *cyberghost) filterServers(region, group string) (servers []models.CyberghostServer) {
|
|
|
|
|
allServers := constants.CyberghostServers()
|
|
|
|
|
for i, server := range allServers {
|
|
|
|
|
if len(region) == 0 {
|
|
|
|
|
server.Region = ""
|
|
|
|
|
}
|
|
|
|
|
if len(group) == 0 {
|
|
|
|
|
server.Group = ""
|
|
|
|
|
}
|
|
|
|
|
if strings.EqualFold(server.Region, region) && strings.EqualFold(server.Group, group) {
|
|
|
|
|
servers = append(servers, allServers[i])
|
2020-06-13 10:43:47 -04:00
|
|
|
}
|
|
|
|
|
}
|
2020-07-23 01:46:28 +00:00
|
|
|
return servers
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *cyberghost) GetOpenVPNConnections(selection models.ServerSelection) (connections []models.OpenVPNConnection, err error) {
|
|
|
|
|
servers := c.filterServers(selection.Region, selection.Group)
|
|
|
|
|
if len(servers) == 0 {
|
|
|
|
|
return nil, fmt.Errorf("no server found for region %q and group %q", selection.Region, selection.Group)
|
2020-06-13 10:43:47 -04:00
|
|
|
}
|
2020-07-23 01:46:28 +00:00
|
|
|
|
|
|
|
|
for _, server := range servers {
|
|
|
|
|
for _, IP := range server.IPs {
|
|
|
|
|
if selection.TargetIP != nil {
|
|
|
|
|
if selection.TargetIP.Equal(IP) {
|
|
|
|
|
return []models.OpenVPNConnection{{IP: IP, Port: 443, Protocol: selection.Protocol}}, nil
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: 443, Protocol: selection.Protocol})
|
2020-06-13 10:43:47 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-23 01:46:28 +00:00
|
|
|
|
|
|
|
|
if selection.TargetIP != nil {
|
|
|
|
|
return nil, fmt.Errorf("target IP %s not found in IP addresses", selection.TargetIP)
|
2020-06-13 10:43:47 -04:00
|
|
|
}
|
2020-07-23 01:46:28 +00:00
|
|
|
|
|
|
|
|
if len(connections) > 64 {
|
|
|
|
|
connections = connections[:64]
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-13 10:43:47 -04:00
|
|
|
return connections, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 23:34:03 +00:00
|
|
|
func (c *cyberghost) BuildConf(connections []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 {
|
|
|
|
|
auth = "SHA256"
|
|
|
|
|
}
|
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",
|
|
|
|
|
"remote-random",
|
|
|
|
|
"suppress-timestamps",
|
|
|
|
|
|
|
|
|
|
// Modified variables
|
|
|
|
|
fmt.Sprintf("verb %d", verbosity),
|
|
|
|
|
fmt.Sprintf("auth-user-pass %s", constants.OpenVPNAuthConf),
|
2020-07-12 19:15:05 +00:00
|
|
|
fmt.Sprintf("proto %s", connections[0].Protocol),
|
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")
|
|
|
|
|
}
|
|
|
|
|
for _, connection := range connections {
|
2020-07-12 19:15:05 +00:00
|
|
|
lines = append(lines, fmt.Sprintf("remote %s %d", connection.IP, connection.Port))
|
2020-06-13 10:43:47 -04:00
|
|
|
}
|
|
|
|
|
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-----",
|
|
|
|
|
constants.CyberghostClientCertificate,
|
|
|
|
|
"-----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-07-13 23:34:03 +00:00
|
|
|
func (c *cyberghost) GetPortForward(client network.Client) (port uint16, err error) {
|
2020-06-13 14:08:29 -04:00
|
|
|
panic("port forwarding is not supported for cyberghost")
|
|
|
|
|
}
|