Skip to content

Commit

Permalink
feat(ci): paid-tunnels opt-in (#83)
Browse files Browse the repository at this point in the history
Resolves DO-1379

If PAID_TUNNELS is set, run paid tunnels, otherwise, free.

---------

Co-authored-by: Jonas Lindström <[email protected]>
  • Loading branch information
charlottea98 and jonaslindstr authored May 16, 2024
1 parent 1059096 commit 44ee9ad
Show file tree
Hide file tree
Showing 5 changed files with 446 additions and 6 deletions.
72 changes: 72 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions linkup-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ serde_json = "1"
serde_yaml = "0.9"
thiserror = "1"
url = { version = "2.5", features = ["serde"] }
mockall = "0.12.1"
1 change: 1 addition & 0 deletions linkup-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod signal;
mod start;
mod status;
mod stop;
mod tunnel;
mod worker_client;

use completion::completion;
Expand Down
54 changes: 48 additions & 6 deletions linkup-cli/src/start.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,63 @@
use std::{
fs,
env, fs,
path::{Path, PathBuf},
};

use crate::env_files::write_to_env_file;
use crate::local_config::{config_path, get_config};
use crate::LINKUP_LOCALDNS_INSTALL;
use crate::{
background_booting::boot_background_services,
env_files::write_to_env_file,
linkup_file_path,
local_config::{config_to_state, LocalState},
local_config::{config_path, config_to_state, get_config},
LINKUP_LOCALDNS_INSTALL,
};
use crate::{
local_config::LocalState,
status::{server_status, ServerStatus},
CliError,
};

use crate::tunnel::{create_dns_record, create_tunnel, get_tunnel_id};

fn file_exists(file_path: &str) -> bool {
Path::new(file_path).exists()
}

pub fn start(config_arg: &Option<String>, no_tunnel: bool) -> Result<(), CliError> {
if env::var("PAID_TUNNELS").is_ok() {
start_paid_tunnel()?;
} else {
start_free_tunnel(config_arg, no_tunnel)?;
}
Ok(())
}

fn start_paid_tunnel() -> Result<(), CliError> {
println!("Starting paid tunnel");
let tunnel_name = "happy-lion".to_string();
let tunnel_id = match get_tunnel_id(&tunnel_name) {
Ok(Some(id)) => id,
Ok(None) => "".to_string(),
Err(e) => return Err(e),
};

// If there exists a /$ENV_HOME/.cloudflared/<Tunnel-UUID>.json file, skip creating a tunnel
let file_path = format!(
"{}/.cloudflared/{}.json",
env::var("HOME").expect("HOME is not set"),
tunnel_id
);
if file_exists(&file_path) {
println!("File exists: {}", file_path);
} else {
println!("File does not exist: {}", file_path);
let tunnel_id = create_tunnel(&tunnel_name)?;
create_dns_record(&tunnel_id, &tunnel_name)?;
}
Ok(())
}

fn start_free_tunnel(config_arg: &Option<String>, no_tunnel: bool) -> Result<(), CliError> {
println!("Starting free tunnel");
let previous_state = LocalState::load();
let config_path = config_path(config_arg)?;
let input_config = get_config(&config_path)?;
Expand Down Expand Up @@ -51,7 +94,6 @@ pub fn start(config_arg: &Option<String>, no_tunnel: bool) -> Result<(), CliErro
boot_background_services()?;

check_local_not_started()?;

Ok(())
}

Expand Down
Loading

0 comments on commit 44ee9ad

Please sign in to comment.