2020-06-13 14:08:29 -04:00
|
|
|
package provider
|
2020-06-13 10:43:47 -04:00
|
|
|
|
|
|
|
|
import (
|
2020-06-13 14:08:29 -04:00
|
|
|
"context"
|
2020-06-13 10:43:47 -04:00
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/qdm12/golibs/files"
|
|
|
|
|
"github.com/qdm12/private-internet-access-docker/internal/constants"
|
|
|
|
|
"github.com/qdm12/private-internet-access-docker/internal/models"
|
|
|
|
|
)
|
|
|
|
|
|
2020-06-13 14:08:29 -04:00
|
|
|
type cyberghost struct {
|
|
|
|
|
fileManager files.FileManager
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newCyberghost(fileManager files.FileManager) *cyberghost {
|
|
|
|
|
return &cyberghost{fileManager: fileManager}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *cyberghost) GetOpenVPNConnections(selection models.ServerSelection) (connections []models.OpenVPNConnection, err error) {
|
2020-06-13 10:43:47 -04:00
|
|
|
var IPs []net.IP
|
|
|
|
|
for _, server := range constants.CyberghostServers() {
|
2020-06-13 14:08:29 -04:00
|
|
|
if strings.EqualFold(server.Region, selection.Region) && strings.EqualFold(server.Group, selection.Group) {
|
2020-06-13 10:43:47 -04:00
|
|
|
IPs = server.IPs
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if len(IPs) == 0 {
|
2020-06-13 14:08:29 -04:00
|
|
|
return nil, fmt.Errorf("no IP found for group %q and region %q", selection.Group, selection.Region)
|
2020-06-13 10:43:47 -04:00
|
|
|
}
|
2020-06-13 14:08:29 -04:00
|
|
|
if selection.TargetIP != nil {
|
2020-06-13 10:43:47 -04:00
|
|
|
found := false
|
|
|
|
|
for i := range IPs {
|
2020-06-13 14:08:29 -04:00
|
|
|
if IPs[i].Equal(selection.TargetIP) {
|
2020-06-13 10:43:47 -04:00
|
|
|
found = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !found {
|
2020-06-13 14:08:29 -04:00
|
|
|
return nil, fmt.Errorf("target IP address %q not found in IP addresses", selection.TargetIP)
|
2020-06-13 10:43:47 -04:00
|
|
|
}
|
2020-06-13 14:08:29 -04:00
|
|
|
IPs = []net.IP{selection.TargetIP}
|
2020-06-13 10:43:47 -04:00
|
|
|
}
|
|
|
|
|
for _, IP := range IPs {
|
2020-06-13 14:08:29 -04:00
|
|
|
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: 1443, Protocol: selection.Protocol})
|
2020-06-13 10:43:47 -04:00
|
|
|
}
|
|
|
|
|
return connections, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-13 14:08:29 -04:00
|
|
|
func (c *cyberghost) BuildConf(connections []models.OpenVPNConnection, verbosity, uid, gid int, root bool, cipher, auth string, extras models.ExtraConfigOptions) (err error) {
|
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"
|
|
|
|
|
}
|
|
|
|
|
lines := []string{
|
|
|
|
|
"client",
|
|
|
|
|
"dev tun",
|
|
|
|
|
"nobind",
|
|
|
|
|
"persist-key",
|
|
|
|
|
"persist-tun",
|
|
|
|
|
"remote-cert-tls server",
|
|
|
|
|
|
|
|
|
|
// Cyberghost specific
|
|
|
|
|
"resolv-retry infinite",
|
|
|
|
|
"redirect-gateway def1",
|
|
|
|
|
"ncp-disable",
|
|
|
|
|
"ping 5",
|
|
|
|
|
"ping-exit 60",
|
|
|
|
|
"ping-timer-rem",
|
|
|
|
|
"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),
|
|
|
|
|
fmt.Sprintf("proto %s", string(connections[0].Protocol)),
|
|
|
|
|
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 {
|
|
|
|
|
lines = append(lines, fmt.Sprintf("remote %s %d", connection.IP.String(), connection.Port))
|
|
|
|
|
}
|
|
|
|
|
lines = append(lines, []string{
|
|
|
|
|
"<ca>",
|
|
|
|
|
"-----BEGIN CERTIFICATE-----",
|
|
|
|
|
constants.CyberghostCertificate,
|
|
|
|
|
"-----END CERTIFICATE-----",
|
|
|
|
|
"</ca>",
|
|
|
|
|
}...)
|
|
|
|
|
lines = append(lines, []string{
|
|
|
|
|
"<crt>",
|
|
|
|
|
"-----BEGIN CERTIFICATE-----",
|
|
|
|
|
constants.CyberghostClientCertificate,
|
|
|
|
|
"-----END CERTIFICATE-----",
|
|
|
|
|
"</crt>",
|
|
|
|
|
}...)
|
|
|
|
|
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>",
|
|
|
|
|
"",
|
|
|
|
|
}...)
|
|
|
|
|
return c.fileManager.WriteLinesToFile(string(constants.OpenVPNConf), lines, files.Ownership(uid, gid), files.Permissions(0400))
|
|
|
|
|
}
|
2020-06-13 14:08:29 -04:00
|
|
|
|
|
|
|
|
func (c *cyberghost) GetPortForward() (port uint16, err error) {
|
|
|
|
|
panic("port forwarding is not supported for cyberghost")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *cyberghost) WritePortForward(filepath models.Filepath, port uint16, uid, gid int) (err error) {
|
|
|
|
|
panic("port forwarding is not supported for cyberghost")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *cyberghost) AllowPortForwardFirewall(ctx context.Context, device models.VPNDevice, port uint16) (err error) {
|
|
|
|
|
panic("port forwarding is not supported for cyberghost")
|
|
|
|
|
}
|