diff --git a/Cargo.toml b/Cargo.toml index ec384d6..a68aa72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,15 +14,17 @@ exclude = [ ] [features] -disable-tls = [] -default =[] +tls=["native-tls"] +tor=["tor-stream"] +default =["tls", "tor"] [profile.release] -lto = true +panic = 'abort' +lto = true codegen-units = 1 -panic = 'abort' opt-level = 'z' # Optimize for size. +# Change v1.4.1 -> v1.4.2 in README on `cargo release` [package.metadata.release] pre-release-replacements = [ {file="README.md", search="phetch-v\\d+\\.\\d+\\.\\d+-", replace="{{crate_name}}-v{{version}}-"}, @@ -32,7 +34,8 @@ dev-version-ext = "dev" [dependencies] termion = "1.5.3" -native-tls = "0.2" libc = "0.2.66" -tor-stream = "0.2.0" atty = "0.2.14" + +tor-stream = { version = "0.2.0", optional = true } +native-tls = { version = "0.2", optional = true } \ No newline at end of file diff --git a/README.md b/README.md index 6f66ba2..fc00f47 100644 --- a/README.md +++ b/README.md @@ -89,13 +89,19 @@ To build with TLS support on **Linux**, you need `openssl` and sudo apt install -y pkg-config libssl-dev -To build without TLS support, build with the `disable-tls` feature: +phetch builds with TLS and Tor support by default. To disable these +features, or to enable only one of them, use the +`--no-default-features` flag: - cargo build --features disable-tls + cargo build --no-default-features You can check whether TLS is enabled by visiting the About page: - cargo run --features disable-tls -- gopher://phetch/about + cargo run --no-default-features -- gopher://phetch/about + +To enable just TLS support, or just Tor support, use `--features`: + + cargo run --no-default-features --features tor -- gopher://phetch/about ## screenies diff --git a/src/args.rs b/src/args.rs index ddd2d01..988f0a4 100644 --- a/src/args.rs +++ b/src/args.rs @@ -132,7 +132,7 @@ pub fn parse>(args: &[T]) -> Result { } set_tls = true; cfg.tls = true; - if cfg!(feature = "disable-tls") { + if cfg!(not(feature = "tls")) { return Err(ArgError::new("phetch was compiled without TLS support")); } } @@ -147,6 +147,9 @@ pub fn parse>(args: &[T]) -> Result { if set_notor { return Err(ArgError::new("can't set both --tor and --no-tor")); } + if cfg!(not(feature = "tor")) { + return Err(ArgError::new("phetch was compiled without Tor support")); + } set_tor = true; cfg.tor = true; } diff --git a/src/gopher.rs b/src/gopher.rs index bdef0a0..df978ef 100644 --- a/src/gopher.rs +++ b/src/gopher.rs @@ -5,7 +5,6 @@ //! IPv6 addresses. use std::{ - env, io::{Read, Result, Write}, net::TcpStream, net::ToSocketAddrs, @@ -13,9 +12,11 @@ use std::{ time::Duration, }; use termion::input::TermRead; + +#[cfg(feature = "tor")] use tor_stream::TorStream; -#[cfg(not(feature = "disable-tls"))] +#[cfg(feature = "tls")] use native_tls::TlsConnector; mod r#type; @@ -141,7 +142,7 @@ pub fn request(host: &str, port: &str, selector: &str, tls: bool, tor: bool) -> // attempt tls connection if tls { - #[cfg(not(feature = "disable-tls"))] + #[cfg(feature = "tls")] { { if let Ok(connector) = TlsConnector::new() { @@ -161,29 +162,33 @@ pub fn request(host: &str, port: &str, selector: &str, tls: bool, tor: bool) -> // tls didn't work or wasn't selected, try Tor or default if tor { - let proxy = env::var("TOR_PROXY") - .unwrap_or("127.0.0.1:9050".into()) - .to_socket_addrs()? - .nth(0) - .unwrap(); - let mut stream = match TorStream::connect_with_address(proxy, sock) { - Ok(s) => s, - Err(e) => return Err(error!("Tor error: {}", e)), - }; - stream.write(format!("{}\r\n", selector).as_ref())?; - Ok(Stream { - io: Box::new(stream), - tls: false, - }) - } else { - let mut stream = TcpStream::connect_timeout(&sock, TCP_TIMEOUT_DURATION)?; - stream.set_read_timeout(Some(TCP_TIMEOUT_DURATION))?; - stream.write(format!("{}\r\n", selector).as_ref())?; - Ok(Stream { - io: Box::new(stream), - tls: false, - }) + #[cfg(feature = "tor")] + { + let proxy = std::env::var("TOR_PROXY") + .unwrap_or("127.0.0.1:9050".into()) + .to_socket_addrs()? + .nth(0) + .unwrap(); + let mut stream = match TorStream::connect_with_address(proxy, sock) { + Ok(s) => s, + Err(e) => return Err(error!("Tor error: {}", e)), + }; + stream.write(format!("{}\r\n", selector).as_ref())?; + return Ok(Stream { + io: Box::new(stream), + tls: false, + }); + } } + + // no tls or tor, try regular connection + let mut stream = TcpStream::connect_timeout(&sock, TCP_TIMEOUT_DURATION)?; + stream.set_read_timeout(Some(TCP_TIMEOUT_DURATION))?; + stream.write(format!("{}\r\n", selector).as_ref())?; + Ok(Stream { + io: Box::new(stream), + tls: false, + }) } /// Parses gopher URL into parts. diff --git a/src/help.rs b/src/help.rs index 3265272..5ba0fd4 100644 --- a/src/help.rs +++ b/src/help.rs @@ -15,20 +15,35 @@ pub fn lookup(name: &str) -> Option { "help/types" => format!("{}{}", HEADER, TYPES), "help/bookmarks" => format!("{}{}", HEADER, BOOKMARKS), "help/history" => format!("{}{}", HEADER, HISTORY), - "about" => format!( + "help" | "help/" => format!( "{}{}", HEADER, - ABOUT - .replace("{build-date}", crate::BUILD_DATE) - .replace("{git-ref}", crate::GIT_REF) - .replace("{tls-support}", crate::TLS_SUPPORT) + HELP.replace("{platform}", crate::PLATFORM) .replace("{version}", crate::VERSION) ), - "help" | "help/" => format!( + "about" => format!( "{}{}", HEADER, - HELP.replace("{platform}", crate::PLATFORM) + ABOUT + .replace("{build-date}", crate::BUILD_DATE) + .replace("{git-ref}", crate::GIT_REF) .replace("{version}", crate::VERSION) + .replace( + "{tls-support}", + if crate::TLS_SUPPORT { + "supported" + } else { + "not supported" + } + ) + .replace( + "{tor-support}", + if crate::TOR_SUPPORT { + "supported" + } else { + "not supported" + } + ) ), _ => return None, }) @@ -281,7 +296,8 @@ i i ~ * ~ i itls: {tls-support} -iref: {git-ref} -ibuilt: {build-date} +itor: {tor-support} +igit ref: {git-ref} +ibuilt on: {build-date} i "; diff --git a/src/lib.rs b/src/lib.rs index 4f7e49c..bccaaf1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,7 +28,13 @@ pub const BUILD_DATE: &str = env!("BUILD_DATE"); pub const BUG_URL: &str = "https://github.com/xvxx/phetch/issues/new"; /// Whether we compiled with TLS support. -#[cfg(not(feature = "disable-tls"))] -pub const TLS_SUPPORT: &str = "enabled"; -#[cfg(feature = "disable-tls")] -pub const TLS_SUPPORT: &str = "not enabled"; +#[cfg(feature = "tls")] +pub const TLS_SUPPORT: bool = true; +#[cfg(not(feature = "tls"))] +pub const TLS_SUPPORT: bool = false; + +/// Whether we compiled with Tor support. +#[cfg(feature = "tor")] +pub const TOR_SUPPORT: bool = true; +#[cfg(not(feature = "tor"))] +pub const TOR_SUPPORT: bool = false;