Skip to content

Commit

Permalink
handful of small cli fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
agrinman committed May 16, 2021
1 parent c688277 commit d497594
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 108 deletions.
11 changes: 11 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@
"isDefault": true
},
"label": "cargo check"
},
{
"type": "cargo",
"command": "run",
"args": [
"--bin tunnelto"
],
"problemMatcher": [
"$rustc"
],
"label": "Run client"
}
]
}
42 changes: 38 additions & 4 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 fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ kill_timeout = 5
NET_PORT = 10002
BLOCKED_SUB_DOMAINS = "wormhole,dashboard,2,myapp"
RUST_LOG="tunnelto_server=debug"
TUNNEL_HOST = "tunnelto.dev"

[experimental]
private_network=true
Expand Down
5 changes: 3 additions & 2 deletions tunnelto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "tunnelto"
description = "expose your local web server to the internet with a public url"
version = "0.1.17"
version = "0.1.18"
authors = ["Alex Grinman <[email protected]>"]
edition = "2018"
license = "MIT"
Expand All @@ -13,7 +13,7 @@ name = "tunnelto"
path = "src/main.rs"

[dependencies]
tunnelto_lib = { version = "0.1.16", path = "../tunnelto_lib" }
tunnelto_lib = { version = "0.1.17", path = "../tunnelto_lib" }
tokio = { version = "1.0", features = ["full"] }
futures = "0.3"
serde = { version = "1.0", features = ["derive"] }
Expand Down Expand Up @@ -41,3 +41,4 @@ http-body = "0.3.1"
serde_urlencoded = "0.6.1"
reqwest = { version = "0.11", features = ["json"] }
cli-table = "0.4"
semver = "0.11"
4 changes: 2 additions & 2 deletions tunnelto/src/cli_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ impl CliInterface {
}
}

pub fn did_connect(&self, sub_domain: &str) {
pub fn did_connect(&self, sub_domain: &str, full_hostname: &str) {
self.spinner
.finish_with_message("Success! Remote tunnel is now open.\n".green().as_ref());

if !self.config.first_run {
return;
}

let public_url = self.config.activation_url(&sub_domain).bold().green();
let public_url = self.config.activation_url(&full_hostname).bold().green();
let forward_url = self.config.forward_url();
let inspect = format!(
"http://localhost:{}",
Expand Down
8 changes: 2 additions & 6 deletions tunnelto/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,14 @@ impl Config {
})
}

pub fn activation_url(&self, server_chosen_sub_domain: &str) -> String {
pub fn activation_url(&self, full_hostname: &str) -> String {
format!(
"{}://{}",
if self.tls_off { "http" } else { "https" },
self.activation_host(server_chosen_sub_domain)
full_hostname
)
}

pub fn activation_host(&self, server_chosen_sub_domain: &str) -> String {
format!("{}.{}", &server_chosen_sub_domain, &self.host)
}

pub fn forward_url(&self) -> String {
format!(
"{}://{}:{}",
Expand Down
2 changes: 1 addition & 1 deletion tunnelto/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub enum Error {
#[error("Failed to connect to control server: {0}.")]
WebSocketError(#[from] tokio_tungstenite::tungstenite::error::Error),

#[error("Server denied the connection. Please check your authentication key.")]
#[error("Server denied the connection.")]
AuthenticationFailed,

#[error("Server sent a malformed message.")]
Expand Down
52 changes: 43 additions & 9 deletions tunnelto/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ async fn main() {
error!("Control error: {:?}. Retrying in 5 seconds.", e);
tokio::time::sleep(Duration::from_secs(5)).await;
}
Error::AuthenticationFailed => {
if config.secret_key.is_none() {
eprintln!(
">> {}",
"Please use an access key with the `--key` option".yellow()
);
eprintln!(
">> {}{}",
"You can get your access key here: ".yellow(),
"https://dashboard.tunnelto.dev".yellow().underline()
);
} else {
eprintln!(
">> {}{}",
"Please check your access key at ".yellow(),
"https://dashboard.tunnelto.dev".yellow().underline()
);
}
eprintln!("\nError: {}", format!("{}", e).red());
return;
}
_ => {
eprintln!("Error: {}", format!("{}", e).red());
return;
Expand All @@ -92,8 +113,13 @@ async fn run_wormhole(
) -> Result<(), Error> {
let interface = CliInterface::start(config.clone(), introspect.clone());
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
let (websocket, sub_domain) = connect_to_wormhole(&config).await?;
interface.did_connect(&sub_domain);
let Wormhole {
websocket,
sub_domain,
hostname,
} = connect_to_wormhole(&config).await?;

interface.did_connect(&sub_domain, &hostname);

// split reading and writing
let (mut ws_sink, mut ws_stream) = websocket.split();
Expand Down Expand Up @@ -156,9 +182,13 @@ async fn run_wormhole(
}
}

async fn connect_to_wormhole(
config: &Config,
) -> Result<(WebSocketStream<MaybeTlsStream<TcpStream>>, String), Error> {
struct Wormhole {
websocket: WebSocketStream<MaybeTlsStream<TcpStream>>,
sub_domain: String,
hostname: String,
}

async fn connect_to_wormhole(config: &Config) -> Result<Wormhole, Error> {
let (mut websocket, _) = tokio_tungstenite::connect_async(&config.control_url).await?;

// send our Client Hello message
Expand Down Expand Up @@ -196,14 +226,14 @@ async fn connect_to_wormhole(
Error::ServerReplyInvalid
})?;

let sub_domain = match server_hello {
let (sub_domain, hostname) = match server_hello {
ServerHello::Success {
sub_domain,
client_id,
..
hostname,
} => {
info!("Server accepted our connection. I am client_{}", client_id);
sub_domain
(sub_domain, hostname)
}
ServerHello::AuthFailed => {
return Err(Error::AuthenticationFailed);
Expand All @@ -217,7 +247,11 @@ async fn connect_to_wormhole(
ServerHello::Error(error) => return Err(Error::ServerError(error)),
};

Ok((websocket, sub_domain))
Ok(Wormhole {
websocket,
sub_domain,
hostname,
})
}

async fn process_control_flow_message(
Expand Down
7 changes: 6 additions & 1 deletion tunnelto/src/update.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::str::FromStr;

use colored::Colorize;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -37,7 +39,10 @@ async fn check_inner() -> Result<Option<Update>, Box<dyn std::error::Error>> {
.json()
.await?;

if update.name.as_str() != CURRENT_VERSION {
let cur = semver::Version::from_str(CURRENT_VERSION)?;
let remote = semver::Version::from_str(&update.name)?;

if remote > cur {
Ok(Some(update))
} else {
Ok(None)
Expand Down
2 changes: 1 addition & 1 deletion tunnelto_lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "tunnelto_lib"
description = "expose your local web server to the internet with a public url"
version = "0.1.16"
version = "0.1.17"
authors = ["Alex Grinman <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down
10 changes: 1 addition & 9 deletions tunnelto_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct ReconnectToken(pub String);
pub enum ServerHello {
Success {
sub_domain: String,
hostname: String,
client_id: ClientId,
},
SubDomainInUse,
Expand Down Expand Up @@ -66,15 +67,6 @@ pub struct ClientHello {
pub reconnect_token: Option<ReconnectToken>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ClientHelloV1 {
pub id: ClientId,
pub sub_domain: Option<String>,
pub is_anonymous: bool,
unix_seconds: i64,
signature: String,
}

impl ClientHello {
pub fn generate(sub_domain: Option<String>, typ: ClientType) -> Self {
ClientHello {
Expand Down
Loading

0 comments on commit d497594

Please sign in to comment.