package provider import ( "context" "fmt" "math/rand" "net" "net/http" "strings" "github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/gluetun/internal/firewall" "github.com/qdm12/gluetun/internal/models" "github.com/qdm12/golibs/files" "github.com/qdm12/golibs/logging" ) type windscribe struct { servers []models.WindscribeServer randSource rand.Source } func newWindscribe(servers []models.WindscribeServer, timeNow timeNowFunc) *windscribe { return &windscribe{ servers: servers, randSource: rand.NewSource(timeNow().UnixNano()), } } func (w *windscribe) filterServers(region string) (servers []models.WindscribeServer) { if len(region) == 0 { return w.servers } for _, server := range w.servers { if strings.EqualFold(server.Region, region) { return []models.WindscribeServer{server} } } return nil } func (w *windscribe) GetOpenVPNConnection(selection models.ServerSelection) (connection models.OpenVPNConnection, err error) { servers := w.filterServers(selection.Region) if len(servers) == 0 { return connection, fmt.Errorf("no server found for region %q", selection.Region) } var port uint16 switch { case selection.CustomPort > 0: port = selection.CustomPort case selection.Protocol == constants.TCP: port = 1194 case selection.Protocol == constants.UDP: port = 443 default: return connection, fmt.Errorf("protocol %q is unknown", selection.Protocol) } var connections []models.OpenVPNConnection for _, server := range servers { for _, IP := range server.IPs { if selection.TargetIP != nil { if selection.TargetIP.Equal(IP) { return models.OpenVPNConnection{IP: IP, Port: port, Protocol: selection.Protocol}, nil } } else { connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: selection.Protocol}) } } } if selection.TargetIP != nil { return connection, fmt.Errorf("target IP %s not found in IP addresses", selection.TargetIP) } return pickRandomConnection(connections, w.randSource), nil } func (w *windscribe) BuildConf(connection 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 = "sha512" } lines = []string{ "client", "dev tun", "nobind", "persist-key", "remote-cert-tls server", // Windscribe specific "comp-lzo", "key-direction 1", "script-security 2", // 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", connection.Protocol), fmt.Sprintf("remote %s %d", connection.IP, connection.Port), 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{ "", "-----BEGIN CERTIFICATE-----", constants.WindscribeCertificate, "-----END CERTIFICATE-----", "", }...) lines = append(lines, []string{ "", "-----BEGIN OpenVPN Static key V1-----", constants.WindscribeOpenvpnStaticKeyV1, "-----END OpenVPN Static key V1-----", "", "", }...) return lines } func (w *windscribe) 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)) { panic("port forwarding is not supported for windscribe") }