`--no-default-features` to disable tls, tor

pull/14/head
chris west 4 years ago
parent 2bd48f7a2d
commit 75ac066caa

@ -14,15 +14,17 @@ exclude = [
] ]
[features] [features]
disable-tls = [] tls=["native-tls"]
default =[] tor=["tor-stream"]
default =["tls", "tor"]
[profile.release] [profile.release]
lto = true panic = 'abort'
lto = true
codegen-units = 1 codegen-units = 1
panic = 'abort'
opt-level = 'z' # Optimize for size. opt-level = 'z' # Optimize for size.
# Change v1.4.1 -> v1.4.2 in README on `cargo release`
[package.metadata.release] [package.metadata.release]
pre-release-replacements = [ pre-release-replacements = [
{file="README.md", search="phetch-v\\d+\\.\\d+\\.\\d+-", replace="{{crate_name}}-v{{version}}-"}, {file="README.md", search="phetch-v\\d+\\.\\d+\\.\\d+-", replace="{{crate_name}}-v{{version}}-"},
@ -32,7 +34,8 @@ dev-version-ext = "dev"
[dependencies] [dependencies]
termion = "1.5.3" termion = "1.5.3"
native-tls = "0.2"
libc = "0.2.66" libc = "0.2.66"
tor-stream = "0.2.0"
atty = "0.2.14" atty = "0.2.14"
tor-stream = { version = "0.2.0", optional = true }
native-tls = { version = "0.2", optional = true }

@ -89,13 +89,19 @@ To build with TLS support on **Linux**, you need `openssl` and
sudo apt install -y pkg-config libssl-dev 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: 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 ## screenies

@ -132,7 +132,7 @@ pub fn parse<T: AsRef<str>>(args: &[T]) -> Result<Config, ArgError> {
} }
set_tls = true; set_tls = true;
cfg.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")); return Err(ArgError::new("phetch was compiled without TLS support"));
} }
} }
@ -147,6 +147,9 @@ pub fn parse<T: AsRef<str>>(args: &[T]) -> Result<Config, ArgError> {
if set_notor { if set_notor {
return Err(ArgError::new("can't set both --tor and --no-tor")); 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; set_tor = true;
cfg.tor = true; cfg.tor = true;
} }

@ -5,7 +5,6 @@
//! IPv6 addresses. //! IPv6 addresses.
use std::{ use std::{
env,
io::{Read, Result, Write}, io::{Read, Result, Write},
net::TcpStream, net::TcpStream,
net::ToSocketAddrs, net::ToSocketAddrs,
@ -13,9 +12,11 @@ use std::{
time::Duration, time::Duration,
}; };
use termion::input::TermRead; use termion::input::TermRead;
#[cfg(feature = "tor")]
use tor_stream::TorStream; use tor_stream::TorStream;
#[cfg(not(feature = "disable-tls"))] #[cfg(feature = "tls")]
use native_tls::TlsConnector; use native_tls::TlsConnector;
mod r#type; mod r#type;
@ -141,7 +142,7 @@ pub fn request(host: &str, port: &str, selector: &str, tls: bool, tor: bool) ->
// attempt tls connection // attempt tls connection
if tls { if tls {
#[cfg(not(feature = "disable-tls"))] #[cfg(feature = "tls")]
{ {
{ {
if let Ok(connector) = TlsConnector::new() { 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 // tls didn't work or wasn't selected, try Tor or default
if tor { if tor {
let proxy = env::var("TOR_PROXY") #[cfg(feature = "tor")]
.unwrap_or("127.0.0.1:9050".into()) {
.to_socket_addrs()? let proxy = std::env::var("TOR_PROXY")
.nth(0) .unwrap_or("127.0.0.1:9050".into())
.unwrap(); .to_socket_addrs()?
let mut stream = match TorStream::connect_with_address(proxy, sock) { .nth(0)
Ok(s) => s, .unwrap();
Err(e) => return Err(error!("Tor error: {}", e)), let mut stream = match TorStream::connect_with_address(proxy, sock) {
}; Ok(s) => s,
stream.write(format!("{}\r\n", selector).as_ref())?; Err(e) => return Err(error!("Tor error: {}", e)),
Ok(Stream { };
io: Box::new(stream), stream.write(format!("{}\r\n", selector).as_ref())?;
tls: false, return Ok(Stream {
}) io: Box::new(stream),
} else { tls: false,
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,
})
} }
// 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. /// Parses gopher URL into parts.

@ -15,20 +15,35 @@ pub fn lookup(name: &str) -> Option<String> {
"help/types" => format!("{}{}", HEADER, TYPES), "help/types" => format!("{}{}", HEADER, TYPES),
"help/bookmarks" => format!("{}{}", HEADER, BOOKMARKS), "help/bookmarks" => format!("{}{}", HEADER, BOOKMARKS),
"help/history" => format!("{}{}", HEADER, HISTORY), "help/history" => format!("{}{}", HEADER, HISTORY),
"about" => format!( "help" | "help/" => format!(
"{}{}", "{}{}",
HEADER, HEADER,
ABOUT HELP.replace("{platform}", crate::PLATFORM)
.replace("{build-date}", crate::BUILD_DATE)
.replace("{git-ref}", crate::GIT_REF)
.replace("{tls-support}", crate::TLS_SUPPORT)
.replace("{version}", crate::VERSION) .replace("{version}", crate::VERSION)
), ),
"help" | "help/" => format!( "about" => format!(
"{}{}", "{}{}",
HEADER, HEADER,
HELP.replace("{platform}", crate::PLATFORM) ABOUT
.replace("{build-date}", crate::BUILD_DATE)
.replace("{git-ref}", crate::GIT_REF)
.replace("{version}", crate::VERSION) .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, _ => return None,
}) })
@ -281,7 +296,8 @@ i
i ~ * ~ i ~ * ~
i i
itls: {tls-support} itls: {tls-support}
iref: {git-ref} itor: {tor-support}
ibuilt: {build-date} igit ref: {git-ref}
ibuilt on: {build-date}
i i
"; ";

@ -28,7 +28,13 @@ pub const BUILD_DATE: &str = env!("BUILD_DATE");
pub const BUG_URL: &str = "https://github.com/xvxx/phetch/issues/new"; pub const BUG_URL: &str = "https://github.com/xvxx/phetch/issues/new";
/// Whether we compiled with TLS support. /// Whether we compiled with TLS support.
#[cfg(not(feature = "disable-tls"))] #[cfg(feature = "tls")]
pub const TLS_SUPPORT: &str = "enabled"; pub const TLS_SUPPORT: bool = true;
#[cfg(feature = "disable-tls")] #[cfg(not(feature = "tls"))]
pub const TLS_SUPPORT: &str = "not enabled"; 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;

Loading…
Cancel
Save