package provider import ( "fmt" "strings" "github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/gluetun/internal/models" "github.com/qdm12/golibs/network" ) type cyberghost struct { servers []models.CyberghostServer } func newCyberghost(servers []models.CyberghostServer) *cyberghost { return &cyberghost{ servers: servers, } } 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]) } } 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) } 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}) } } } if selection.TargetIP != nil { return nil, fmt.Errorf("target IP %s not found in IP addresses", selection.TargetIP) } if len(connections) > 64 { connections = connections[:64] } return connections, nil } func (c *cyberghost) BuildConf(connections []models.OpenVPNConnection, verbosity, uid, gid int, root bool, cipher, auth string, extras models.ExtraConfigOptions) (lines []string) { if len(cipher) == 0 { cipher = aes256cbc } if len(auth) == 0 { auth = "SHA256" } lines = []string{ "client", "dev tun", "nobind", "persist-key", "persist-tun", "remote-cert-tls server", // Cyberghost specific // "redirect-gateway def1", "ncp-disable", "ping 5", "explicit-exit-notify 2", "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", 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, connection.Port)) } lines = append(lines, []string{ "", "-----BEGIN CERTIFICATE-----", constants.CyberghostCertificate, "-----END CERTIFICATE-----", "", }...) lines = append(lines, []string{ "", "-----BEGIN CERTIFICATE-----", constants.CyberghostClientCertificate, "-----END CERTIFICATE-----", "", }...) lines = append(lines, []string{ "", "-----BEGIN PRIVATE KEY-----", extras.ClientKey, "-----END PRIVATE KEY-----", "", "", }...) return lines } func (c *cyberghost) GetPortForward(client network.Client) (port uint16, err error) { panic("port forwarding is not supported for cyberghost") }