Switch to default feature tls

This commit is contained in:
Yash Karandikar 2021-11-11 20:12:57 -06:00
parent 5d47cf680b
commit 6ddf52915e
Signed by: karx
GPG key ID: A794DA2529474BA5
4 changed files with 24 additions and 39 deletions

View file

@ -15,7 +15,12 @@ exclude = ["src/main.rs", "config.toml"]
toml = "0.5" toml = "0.5"
serde_derive = "1.0" serde_derive = "1.0"
serde = "1.0" serde = "1.0"
native-tls = "0.2"
[dependencies.native-tls]
version= "0.2"
optional = true
[features] [features]
tls = [] default = ["tls"]
tls = ["native-tls"]

View file

@ -1,5 +1,5 @@
channels = ["#main", "#circe"] channels = ["#main", "#circe"]
host = "192.168.178.100" host = "192.168.1.28"
mode = "+B" mode = "+B"
nickname = "circe" nickname = "circe"
port = 6667 port = 6667

View file

@ -22,7 +22,7 @@
#![warn(missing_docs)] #![warn(missing_docs)]
#[cfg(not(feature = "not-tls"))] #[cfg(feature = "tls")]
use native_tls::TlsConnector; use native_tls::TlsConnector;
use std::borrow::Cow; use std::borrow::Cow;
@ -39,9 +39,9 @@ pub mod commands;
/// An IRC client /// An IRC client
pub struct Client { pub struct Client {
config: Config, config: Config,
#[cfg(feature = "no-tls")] #[cfg(not(feature = "tls"))]
stream: TcpStream, stream: TcpStream,
#[cfg(not(feature = "not-tls"))] #[cfg(feature = "tls")]
stream: native_tls::TlsStream<TcpStream>, stream: native_tls::TlsStream<TcpStream>,
} }
@ -68,23 +68,23 @@ impl Client {
/// Returns error if the client could not connect to the host. /// Returns error if the client could not connect to the host.
pub fn new(config: Config) -> Result<Self, Error> { pub fn new(config: Config) -> Result<Self, Error> {
let stream = TcpStream::connect(format!("{}:{}", config.host, config.port))?; let stream = TcpStream::connect(format!("{}:{}", config.host, config.port))?;
#[cfg(not(feature = "not-tls"))] #[cfg(feature = "tls")]
let sslstream: native_tls::TlsStream<TcpStream>; let sslstream: native_tls::TlsStream<TcpStream>;
#[cfg(not(feature = "not-tls"))] #[cfg(feature = "tls")]
{ {
let connector = TlsConnector::new().unwrap(); let connector = TlsConnector::new().unwrap();
sslstream = connector.connect(config.host.as_str(), stream).unwrap(); sslstream = connector.connect(config.host.as_str(), stream).unwrap();
} };
#[cfg(not(feature = "not-tls"))] #[cfg(feature = "tls")]
return Ok(Self { return Ok(Self {
config, config,
stream: sslstream, stream: sslstream,
}); });
#[cfg(feature = "no-tls")] #[cfg(not(feature = "tls"))]
return Self { config, stream }; return Ok(Self { config, stream });
} }
/// Identify user and joins the in the [`Config`] specified channels. /// Identify user and joins the in the [`Config`] specified channels.
@ -119,6 +119,7 @@ impl Client {
self.write_command(commands::Command::PONG(code.to_string()))?; self.write_command(commands::Command::PONG(code.to_string()))?;
} }
commands::Command::OTHER(line) => { commands::Command::OTHER(line) => {
println!("{}", line);
if line.contains("001") { if line.contains("001") {
break; break;
} }
@ -140,18 +141,11 @@ impl Client {
fn read_string(&mut self) -> Option<String> { fn read_string(&mut self) -> Option<String> {
let mut buffer = [0u8; 512]; let mut buffer = [0u8; 512];
#[cfg(not(feature = "not-tls"))]
match self.stream.read(&mut buffer) { match self.stream.read(&mut buffer) {
Ok(_) => {} Ok(_) => {}
Err(_) => return None, Err(_) => return None,
}; };
#[cfg(feature = "no-tls")]
match self.stream.as_mut().unwrap().read(&mut buffer) {
Ok(_) => {}
Err(_) => return None,
};
Some(String::from_utf8_lossy(&buffer).into()) Some(String::from_utf8_lossy(&buffer).into())
} }
@ -193,11 +187,7 @@ impl Client {
Cow::Owned(new) as Cow<str> Cow::Owned(new) as Cow<str>
}; };
#[cfg(not(feature = "not-tls"))] self.stream.write(formatted.as_bytes())?;
self.stream.write(formatted.as_bytes()).unwrap();
#[cfg(feature = "no-tls")]
self.stream.as_mut().unwrap().write(formatted.as_bytes())?;
Ok(()) Ok(())
} }
@ -506,15 +496,13 @@ impl Client {
)))?; )))?;
} }
#[cfg(not(feature = "not-tls"))] #[cfg(not(feature = "tls"))]
self.stream.shutdown().unwrap();
#[cfg(feature = "no-tls")]
self.stream self.stream
.as_mut()
.unwrap()
.shutdown(std::net::Shutdown::Both)?; .shutdown(std::net::Shutdown::Both)?;
#[cfg(feature = "tls")]
self.stream.shutdown()?;
Ok(()) Ok(())
} }
} }

View file

@ -10,15 +10,7 @@ macro_rules! loop_n {
} }
fn main() -> Result<(), std::io::Error> { fn main() -> Result<(), std::io::Error> {
//let config = Config::from_toml("config.toml")?; let config = Config::from_toml("config.toml")?;
let config = Config::new(
vec!["#main", "#no-normies"],
"karx.xyz",
Some("+B"),
Some("circe"),
6697,
"circe",
);
let mut client = Client::new(config).unwrap(); let mut client = Client::new(config).unwrap();
client.identify().unwrap(); client.identify().unwrap();