region-proxy 1.2.5

A CLI tool to create a SOCKS proxy through AWS EC2 in any region
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
mod aws;
mod cli;
mod config;
mod proxy;
mod state;

use anyhow::{bail, Context, Result};
use chrono::Utc;
use clap::Parser;
use cli::{Cli, Commands, ConfigAction, UnsetOption};
use config::{find_region, Preferences, REGIONS};
use state::ProxyState;
use std::fs;
use tokio::task::JoinSet;
use tracing::{error, info, warn, Level};
use tracing_subscriber::FmtSubscriber;

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();

    let level = if cli.verbose {
        Level::DEBUG
    } else {
        Level::INFO
    };
    FmtSubscriber::builder()
        .with_max_level(level)
        .with_target(false)
        .without_time()
        .init();

    match cli.command {
        Commands::Start {
            region,
            port,
            instance_type,
            no_system_proxy,
        } => {
            cmd_start(region, port, instance_type, no_system_proxy).await?;
        }
        Commands::Stop { force } => {
            cmd_stop(force).await?;
        }
        Commands::Status => {
            cmd_status().await?;
        }
        Commands::ListRegions { detailed } => {
            cmd_list_regions(detailed);
        }
        Commands::Cleanup { region } => {
            cmd_cleanup(region.as_deref()).await?;
        }
        Commands::Config { action } => {
            cmd_config(action)?;
        }
    }

    Ok(())
}

async fn cmd_start(
    region: Option<String>,
    port: Option<u16>,
    instance_type: Option<String>,
    no_system_proxy: bool,
) -> Result<()> {
    let prefs = Preferences::load()?;

    let region = match region {
        Some(r) => r,
        None => match prefs.default_region {
            Some(r) => {
                info!("Using default region from config: {}", r);
                r
            }
            None => {
                bail!(
                    "No region specified. Use --region or set a default with:\n  region-proxy config set-region <REGION>\n\nUse 'region-proxy list-regions' to see available regions."
                );
            }
        },
    };

    let port = port.or(prefs.default_port).unwrap_or(1080);
    let instance_type = instance_type.or(prefs.default_instance_type);
    let enable_system_proxy = !no_system_proxy && !prefs.no_system_proxy.unwrap_or(false);

    if ProxyState::is_running()? {
        bail!("A proxy is already running. Use 'region-proxy stop' first.");
    }

    let region_info = find_region(&region).with_context(|| {
        format!(
            "Unknown region: {}. Use 'region-proxy list-regions' to see available regions.",
            region
        )
    })?;

    let instance_type = instance_type
        .as_deref()
        .unwrap_or(region_info.default_instance_type());
    let is_arm = instance_type.starts_with("t4g")
        || instance_type.starts_with("m7g")
        || instance_type.starts_with("c7g");

    info!("🚀 Starting proxy in {} ({})", region_info.name, region);
    info!("   Instance type: {}", instance_type);
    info!("   Local port: {}", port);

    let ec2 = aws::Ec2Manager::new(&region).await?;

    info!("📦 Finding latest Amazon Linux 2023 AMI...");
    let ami_id = ec2.find_latest_ami(is_arm).await?;

    info!("🔒 Creating security group...");
    let sg_id = ec2.create_security_group().await?;

    info!("🔑 Creating key pair...");
    let (key_name, private_key) = ec2.create_key_pair().await?;

    let keys_dir = ProxyState::keys_dir()?;
    let key_path = keys_dir.join(format!("{}.pem", key_name));
    fs::write(&key_path, &private_key)?;

    info!("🖥️  Launching EC2 instance...");
    let instance_id = ec2
        .launch_instance(&ami_id, instance_type, &sg_id, &key_name)
        .await?;

    info!("⏳ Waiting for instance to be ready...");
    let public_ip = match ec2.wait_for_instance(&instance_id).await {
        Ok(ip) => ip,
        Err(e) => {
            error!("Failed to wait for instance: {}", e);
            warn!("Cleaning up resources...");
            let _ = ec2.terminate_instance(&instance_id).await;
            let _ = ec2.delete_security_group(&sg_id).await;
            let _ = ec2.delete_key_pair(&key_name).await;
            let _ = fs::remove_file(&key_path);
            return Err(e);
        }
    };

    info!("🔗 Starting SSH tunnel...");
    let ssh_pid = proxy::start_ssh_tunnel(&public_ip, &key_path, port, "ec2-user")?;

    proxy::wait_for_tunnel(port).await?;

    if enable_system_proxy {
        info!("🌐 Configuring system proxy...");
        proxy::enable_socks_proxy(port)?;
    }

    let state = ProxyState {
        instance_id: instance_id.clone(),
        region: region.to_string(),
        public_ip: public_ip.clone(),
        security_group_id: sg_id,
        key_pair_name: key_name,
        key_path,
        local_port: port,
        ssh_pid: Some(ssh_pid),
        started_at: Utc::now(),
    };
    state.save()?;

    println!();
    println!("✅ Proxy is ready!");
    println!();
    println!("   Region:    {} ({})", region_info.name, region);
    println!("   Public IP: {}", public_ip);
    println!("   SOCKS:     localhost:{}", port);
    println!();
    println!("   To stop:   region-proxy stop");
    println!();

    Ok(())
}

async fn cmd_stop(force: bool) -> Result<()> {
    let state = match ProxyState::load()? {
        Some(s) => s,
        None => {
            if force {
                warn!("No active proxy found, but --force was specified. Skipping.");
                return Ok(());
            }
            bail!("No active proxy found. Nothing to stop.");
        }
    };

    info!("🛑 Stopping proxy...");

    info!("🌐 Disabling system proxy...");
    if let Err(e) = proxy::disable_socks_proxy() {
        if force {
            warn!("Failed to disable system proxy: {}", e);
        } else {
            return Err(e);
        }
    }

    info!("🔗 Stopping SSH tunnel...");
    if let Some(pid) = state.ssh_pid {
        if let Err(e) = proxy::stop_ssh_tunnel(pid) {
            if force {
                warn!("Failed to stop SSH tunnel: {}", e);
            } else {
                let _ = proxy::stop_ssh_tunnel_by_port(state.local_port);
            }
        }
    } else {
        let _ = proxy::stop_ssh_tunnel_by_port(state.local_port);
    }

    info!("🖥️  Terminating EC2 instance...");
    let ec2 = aws::Ec2Manager::new(&state.region).await?;
    if let Err(e) = ec2.terminate_instance(&state.instance_id).await {
        if force {
            warn!("Failed to terminate instance: {}", e);
        } else {
            return Err(e);
        }
    }

    info!("🔒 Deleting security group...");
    if let Err(e) = ec2.delete_security_group(&state.security_group_id).await {
        if force {
            warn!("Failed to delete security group: {}", e);
        } else {
            return Err(e);
        }
    }

    info!("🔑 Deleting key pair...");
    if let Err(e) = ec2.delete_key_pair(&state.key_pair_name).await {
        if force {
            warn!("Failed to delete key pair: {}", e);
        } else {
            return Err(e);
        }
    }

    match fs::remove_file(&state.key_path) {
        Ok(()) => {}
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
        Err(e) => warn!("Failed to remove key file: {}", e),
    }

    ProxyState::delete()?;

    println!();
    println!("✅ Proxy stopped and cleaned up!");
    println!();

    Ok(())
}

async fn cmd_status() -> Result<()> {
    let state = match ProxyState::load()? {
        Some(s) => s,
        None => {
            println!("No active proxy.");
            return Ok(());
        }
    };

    let region_info = find_region(&state.region);
    let region_name = region_info.map(|r| r.name).unwrap_or("Unknown");

    let duration = Utc::now().signed_duration_since(state.started_at);
    let hours = duration.num_hours();
    let minutes = duration.num_minutes() % 60;

    let ssh_running = proxy::find_ssh_pid(state.local_port)?.is_some();
    let proxy_enabled = proxy::is_socks_proxy_enabled().unwrap_or(false);

    println!();
    println!("📊 Proxy Status");
    println!();
    println!("   Region:      {} ({})", region_name, state.region);
    println!("   Instance:    {}", state.instance_id);
    println!("   Public IP:   {}", state.public_ip);
    println!("   SOCKS:       localhost:{}", state.local_port);
    println!(
        "   SSH tunnel:  {}",
        if ssh_running {
            "✅ Running"
        } else {
            "❌ Not running"
        }
    );
    println!(
        "   System proxy: {}",
        if proxy_enabled {
            "✅ Enabled"
        } else {
            "❌ Disabled"
        }
    );
    println!("   Running for: {}h {}m", hours, minutes);
    println!();

    Ok(())
}

fn cmd_list_regions(detailed: bool) {
    println!();
    println!("Available AWS Regions:");
    println!();

    if detailed {
        println!("{:<20} {:<20} Default Instance", "Code", "Name");
        println!("{}", "-".repeat(55));
        for region in REGIONS {
            println!(
                "{:<20} {:<20} {}",
                region.code,
                region.name,
                region.default_instance_type()
            );
        }
    } else {
        for region in REGIONS {
            println!("  {} ({})", region.code, region.name);
        }
    }
    println!();
}

async fn cmd_cleanup(region: Option<&str>) -> Result<()> {
    let regions: Vec<&str> = match region {
        Some(r) => vec![r],
        None => REGIONS.iter().map(|r| r.code).collect(),
    };

    let mut set: JoinSet<Result<u32>> = JoinSet::new();
    for region_code in regions {
        let region_code = region_code.to_string();
        set.spawn(async move {
            info!("Checking region: {}", region_code);
            let ec2 = aws::Ec2Manager::new(&region_code).await?;
            let orphaned = ec2.find_orphaned_resources().await?;
            if orphaned.is_empty() {
                return Ok(0);
            }

            println!("Found orphaned resources in {}:", region_code);
            let mut cleaned = 0u32;

            for id in &orphaned.instance_ids {
                println!("  Terminating instance: {}", id);
                if let Err(e) = ec2.terminate_instance(id).await {
                    warn!("Failed to terminate instance {}: {}", id, e);
                } else {
                    cleaned += 1;
                }
            }

            for id in &orphaned.security_group_ids {
                println!("  Deleting security group: {}", id);
                if let Err(e) = ec2.delete_security_group(id).await {
                    warn!("Failed to delete security group {}: {}", id, e);
                } else {
                    cleaned += 1;
                }
            }

            for name in &orphaned.key_pair_names {
                println!("  Deleting key pair: {}", name);
                if let Err(e) = ec2.delete_key_pair(name).await {
                    warn!("Failed to delete key pair {}: {}", name, e);
                } else {
                    cleaned += 1;
                }
            }

            Ok(cleaned)
        });
    }

    let mut total_cleaned = 0u32;
    while let Some(res) = set.join_next().await {
        match res {
            Ok(Ok(n)) => total_cleaned += n,
            Ok(Err(e)) => warn!("Region cleanup failed: {}", e),
            Err(e) => warn!("Task join error: {}", e),
        }
    }

    if total_cleaned == 0 {
        println!("No orphaned resources found.");
    } else {
        println!();
        println!("Cleaned up {} resource(s).", total_cleaned);
    }

    Ok(())
}

fn cmd_config(action: ConfigAction) -> Result<()> {
    match action {
        ConfigAction::Show => {
            let prefs = Preferences::load()?;
            println!();
            println!("⚙️  Configuration");
            println!();

            if prefs.is_empty() {
                println!("   No configuration set.");
                println!();
                println!("   Set defaults with:");
                println!("     region-proxy config set-region <REGION>");
                println!("     region-proxy config set-port <PORT>");
            } else {
                if let Some(ref region) = prefs.default_region {
                    let region_name = find_region(region).map(|r| r.name).unwrap_or("Unknown");
                    println!("   Default region:        {} ({})", region, region_name);
                }
                if let Some(port) = prefs.default_port {
                    println!("   Default port:          {}", port);
                }
                if let Some(ref instance_type) = prefs.default_instance_type {
                    println!("   Default instance type: {}", instance_type);
                }
                if let Some(no_system_proxy) = prefs.no_system_proxy {
                    println!("   Skip system proxy:     {}", no_system_proxy);
                }
            }

            println!();
            println!(
                "   Config file: {}",
                Preferences::config_file_path()?.display()
            );
            println!();
        }

        ConfigAction::SetRegion { region } => {
            let region_info = find_region(&region).with_context(|| {
                format!(
                    "Unknown region: {}. Use 'region-proxy list-regions' to see available regions.",
                    region
                )
            })?;

            let mut prefs = Preferences::load()?;
            prefs.default_region = Some(region.clone());
            prefs.save()?;

            println!(
                "✅ Default region set to: {} ({})",
                region, region_info.name
            );
        }

        ConfigAction::SetPort { port } => {
            if port == 0 {
                bail!("Port must be greater than 0");
            }

            let mut prefs = Preferences::load()?;
            prefs.default_port = Some(port);
            prefs.save()?;

            println!("✅ Default port set to: {}", port);
        }

        ConfigAction::SetInstanceType { instance_type } => {
            let mut prefs = Preferences::load()?;
            prefs.default_instance_type = Some(instance_type.clone());
            prefs.save()?;

            println!("✅ Default instance type set to: {}", instance_type);
        }

        ConfigAction::SetNoSystemProxy { value } => {
            let mut prefs = Preferences::load()?;
            prefs.no_system_proxy = Some(value);
            prefs.save()?;

            if value {
                println!("✅ System proxy configuration will be skipped by default");
            } else {
                println!("✅ System proxy will be configured by default");
            }
        }

        ConfigAction::Unset { option } => {
            let mut prefs = Preferences::load()?;

            match option {
                UnsetOption::Region => {
                    prefs.default_region = None;
                    println!("✅ Default region cleared");
                }
                UnsetOption::Port => {
                    prefs.default_port = None;
                    println!("✅ Default port cleared");
                }
                UnsetOption::InstanceType => {
                    prefs.default_instance_type = None;
                    println!("✅ Default instance type cleared");
                }
                UnsetOption::NoSystemProxy => {
                    prefs.no_system_proxy = None;
                    println!("✅ System proxy preference cleared");
                }
            }

            prefs.save()?;
        }

        ConfigAction::Reset => {
            let path = Preferences::config_file_path()?;
            match fs::remove_file(&path) {
                Ok(()) => println!("✅ Configuration reset to defaults"),
                Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                    println!("No configuration file to reset.");
                }
                Err(e) => return Err(e.into()),
            }
        }
    }

    Ok(())
}