async Task Setup() { enableColors = Environment.GetEnvironmentVariable("CLICOLORS") != "0"; var level = LogLevel.Warning; if (quiet) { level = LogLevel.Error; } else if (verbose >= 3) { level = LogLevel.Trace; } else if (verbose == 2) { level = LogLevel.Debug; } else if (verbose == 1) { level = LogLevel.Information; } LoggerFactory = new LoggerFactory() .AddNiceConsole(level, false); Logger = LoggerFactory.CreateLogger <ExpressoCLI>(); Logger.LogInformation($"{PROGRAM_NAME} v{GetVersion()}"); if (level != LogLevel.Warning) { Logger.LogInformation($"Log level set to {level}"); } client = new ExpressVPNClient(LoggerFactory.CreateLogger <ExpressVPNClient>()); await client.WaitForConnection(); Logger.LogInformation($"Connected to ExpressVPN version {client.AppVersion}"); await client.UpdateStatus(); }
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}'"); }
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}'"); }