Exemplo n.º 1
0
            // mods…
            // text…

            public static AlfredItem FromLocation(ExpressVPNClient client, ExpressVPNClient.Location loc, bool withUid = true)
            {
                var prefix = "";
                var action = $"connect {loc.id}";

                var connectedId = client.LatestStatus.current_location.id;

                if (loc.id == connectedId)
                {
                    prefix = "⚡️ ";
                    action = "disconnect";
                }
                else
                {
                    if (loc.favorite)
                    {
                        prefix += "❤️";
                    }
                    if (client.RecentLocationIds.Contains(loc.id))
                    {
                        prefix += "🕙";
                    }
                    if (client.DefaultLocationId == loc.id)
                    {
                        prefix += "👍";
                    }
                    if (prefix.Length > 0)
                    {
                        prefix = prefix + " ";
                    }
                }

                string uid = null;

                if (withUid)
                {
                    uid = loc.id;
                }

                return(new AlfredItem()
                {
                    uid = uid,
                    title = loc.name,
                    subtitle = $"{prefix}{loc.region} - {loc.country}",
                    arg = action,
                    icon = new AlfredIcon()
                    {
                        path = $"./flags/{loc.country_code}.png",
                    },
                    valid = true,
                    match = $"{loc.name} {loc.region} {loc.country_code}",
                });
            }
Exemplo n.º 2
0
        async Task Connect()
        {
            await client.GetLocations();

            if (client.Locations == null)
            {
                throw new Exception("Could not load location list");
            }

            var args = new ExpressVPNClient.ConnectArgs();

            // No location -> Connect to default location
            if (string.IsNullOrEmpty(location))
            {
                if (string.IsNullOrEmpty(client.DefaultLocationId))
                {
                    throw new Exception("No default location returned");
                }

                var defaultLoc = client.GetLocation(client.DefaultLocationId);
                if (defaultLoc == null)
                {
                    throw new Exception($"Default location with id {client.DefaultLocationId} not found in locations list");
                }

                Logger.LogInformation($"Connecting to default location '{defaultLoc.Value.name}'");
                args.id         = defaultLoc.Value.id;
                args.name       = defaultLoc.Value.name;
                args.is_default = true;
            }
            else
            {
                // Connect to any location in a country
                var countryLoc = client.Locations.FirstOrDefault(l => l.country == location || l.country_code == location);
                if (countryLoc.country != null)
                {
                    Logger.LogInformation($"Connecting to best location in country '{countryLoc.country}'");
                    args.country = countryLoc.country;

                    // Look up specific location
                }
                else
                {
                    ExpressVPNClient.Location selected = default;
                    int selectedPriority = 0;
                    foreach (var loc in client.Locations)
                    {
                        // ID-match has the highest priority
                        if (selectedPriority < 2 && location == loc.id)
                        {
                            selected         = loc;
                            selectedPriority = 2;
                            // Name-match has lower priority
                        }
                        else if (selectedPriority < 1 && loc.name.IndexOf(location, StringComparison.OrdinalIgnoreCase) >= 0)
                        {
                            selected         = loc;
                            selectedPriority = 1;
                        }
                    }
                    if (selected.name == null)
                    {
                        throw new Exception($"Could not find a location for the query '{location}'");
                    }
                    Logger.LogInformation($"Connecting to location '{selected.name}'");
                    args.id   = selected.id;
                    args.name = selected.name;
                }
            }

            await client.Connect(args, timeout);

            await client.UpdateStatus();

            Console.WriteLine($"Connected to '{client.LatestStatus.current_location.name}'");
        }
Exemplo n.º 3
0
        async Task Connect()
        {
            await client.GetLocations();

            if (client.Locations == null)
            {
                throw new Exception("Could not load location list");
            }

            var args = new ExpressVPNClient.ConnectArgs();

            // No location -> Connect to default location
            ExpressVPNClient.Location connectCountry = default;
            if (string.IsNullOrEmpty(location))
            {
                if (string.IsNullOrEmpty(client.DefaultLocationId))
                {
                    throw new Exception("No default location returned");
                }

                var defaultLoc = client.GetLocation(client.DefaultLocationId);
                if (defaultLoc == null)
                {
                    throw new Exception($"Default location with id {client.DefaultLocationId} not found in locations list");
                }

                Logger.LogInformation($"Connecting to default location '{defaultLoc.Value.name}'");
                args.id         = defaultLoc.Value.id;
                args.name       = defaultLoc.Value.name;
                args.is_default = true;
            }
            else
            {
                // Connect to any location in a country
                var countryLocs = client.Locations.Where(l =>
                                                         string.Equals(l.country, location, StringComparison.OrdinalIgnoreCase) ||
                                                         string.Equals(l.country_code, location, StringComparison.OrdinalIgnoreCase)
                                                         );
                if (countryLocs.Any())
                {
                    connectCountry = countryLocs.Skip(new Random().Next(countryLocs.Count() - 1)).First();
                    if (randomize)
                    {
                        Logger.LogInformation($"Connecting to random location in country '{connectCountry.country}'");
                        args.id   = connectCountry.id;
                        args.name = connectCountry.name;
                    }
                    else
                    {
                        Logger.LogInformation($"Connecting to default location in country '{connectCountry.country}'");
                        args.country = connectCountry.country;
                    }

                    // Look up specific location
                }
                else
                {
                    ExpressVPNClient.Location selected = default;
                    int selectedPriority = 0;
                    foreach (var loc in client.Locations)
                    {
                        // ID-match has the highest priority
                        if (selectedPriority < 2 && location == loc.id)
                        {
                            selected         = loc;
                            selectedPriority = 2;
                            // Name-match has lower priority
                        }
                        else if (selectedPriority < 1 && loc.name.IndexOf(location, StringComparison.OrdinalIgnoreCase) >= 0)
                        {
                            selected         = loc;
                            selectedPriority = 1;
                        }
                    }
                    if (selected.name == null)
                    {
                        throw new Exception($"Could not find a location for the query '{location}'");
                    }
                    Logger.LogInformation($"Connecting to location '{selected.name}'");
                    args.id   = selected.id;
                    args.name = selected.name;
                }
            }

            if (client.LatestStatus.state == ExpressVPNClient.State.connected)
            {
                var current = client.LatestStatus.current_location;

                if (toggle && (!changeConnection || current.id == args.id || (connectCountry.country != null && current.country == connectCountry.country)))
                {
                    // Toggle: Disconnect instead of already connected
                    Logger.LogInformation($"Already connected to '{current.name}', disconnecting instead");
                    await Disconnect();

                    return;
                }

                if (!changeConnection)
                {
                    throw new Exception($"Already connected to '{current.name}'.\n"
                                        + $"Use --change or -c to change the currently connected location.");
                }
                else
                {
                    args.change_connected_location = true;
                }
            }

            await client.Connect(args, timeout);

            await client.UpdateStatus();

            Console.WriteLine($"Connected to '{client.LatestStatus.current_location.name}'");
        }