Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency Updates, Modernize Rust Version + Misc Enhancements #18

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "i2p"
version = "0.1.1"
version = "0.2.0"
authors = ["Jack Grigg <[email protected]>"]
description = "I2P client library with a std::net-like API"
homepage = "https://github.com/i2p/i2p-rs"
Expand All @@ -9,25 +9,32 @@ readme = "README.md"
categories = ["network-programming"]
keywords = ["i2p", "net", "network", "sam"]
license = "MIT"
edition = "2018"
edition = "2021"

[features]
default = ["public-conn"]
public-conn = []
[badges]
travis-ci = { repository = "i2p/i2p-rs" }

[dependencies]
data-encoding = "2.1.2"
failure = "0.1"
failure_derive = "0.1"
lazy_static = "1.3.0"
log = "0.4.6"
nom = "^4.2"
rand = "0.5"
serde = "1"
serde_derive = "1"
sha2 = "0.8.0"

[dependencies.data-encoding]
version = "2"
[dependencies.serde]
version = "1"
features = ["derive"]
[dependencies.anyhow]
version = "1"
[dependencies.thiserror]
version = "1"
[dependencies.lazy_static]
version = "1"
[dependencies.log]
version = "0.4"
[dependencies.nom]
version = "6"
[dependencies.rand]
version = "0.8"
[dependencies.sha2]
version = "0.10"
[dev-dependencies]
env_logger = "0.5"
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.PHONY: fmt
fmt:
find -type f -name "*.rs" -not -path "*target*" -exec rustfmt --edition 2018 {} \;
find -type f -name "*.rs" -not -path "*target*" -exec rustfmt --edition 2021 {} \;

.PHONY: lint
lint:
cargo +nightly clippy --fix -Z unstable-options --release --all
2 changes: 1 addition & 1 deletion examples/eepget/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
i2p = {path = "../../", version = "0.1.0"}
i2p = {path = "../../", version = "0.2.0"}
env_logger = "0.5"

[[bin]]
Expand Down
2 changes: 1 addition & 1 deletion examples/gen_dest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
i2p = {path = "../../", version = "0.1.0"}
i2p = {path = "../../", version = "0.2.0"}
env_logger = "0.5"

[[bin]]
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
i2p = {path = "../../", version = "0.1.0"}
i2p = {path = "../../", version = "0.2.0"}
env_logger = "0.5"
log = "0.4"
crossbeam = "0.8"
Expand Down
2 changes: 1 addition & 1 deletion examples/selfserve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
i2p = {path = "../../", version = "0.1.0"}
i2p = {path = "../../", version = "0.2.0"}
env_logger = "0.5"
log = "0.4.6"

Expand Down
2 changes: 1 addition & 1 deletion examples/session_watcher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
i2p = {path = "../../", version = "0.1.0"}
i2p = {path = "../../", version = "0.2.0"}
env_logger = "0.5"
log = "0.4"
crossbeam = "0.8"
Expand Down
5 changes: 3 additions & 2 deletions examples/session_watcher/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ async fn main() {
DEFAULT_API,
&seckey,
SessionStyle::Stream,
Default::default()
).unwrap();
Default::default(),
)
.unwrap();

loop {
match watcher.accept() {
Expand Down
114 changes: 24 additions & 90 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,117 +1,51 @@
use std::fmt::{self, Display};
use std::io;

use failure::{Backtrace, Context, Fail};
use nom;

/// I2P/SAM error definition
#[derive(Debug, Fail)]
pub struct Error {
inner: Context<ErrorKind>,
}
use std::io;
use thiserror::Error as ThisError;

/// Kinds of I2P/SAM errors
#[derive(Clone, Eq, PartialEq, Debug, Fail)]
pub enum ErrorKind {
#[derive(Clone, Eq, PartialEq, Debug, ThisError)]
pub enum I2PError {
/// Wraps io errors
#[fail(display = "IO error occurred (is i2p running?): {}", _0)]
#[error("IO error occurred (is i2p running?): {0}")]
Io(String),
/// Wraps nom parser errors
#[fail(display = "Failed to parse an I2P/SAM message")]
#[error("Failed to parse an I2P/SAM message")]
MessageParsing,
#[fail(display = "Failed to parse an I2P/SAM message")]
#[error("Failed to parse an I2P/SAM message")]
UnresolvableAddress,
#[fail(display = "Invalid or unrecognized I2P/SAM message: {}", _0)]
#[error("Invalid or unrecognized I2P/SAM message: {0}")]
SAMInvalidMessage(String),
#[fail(display = "Can't reach peer: {}", _0)]
#[error("Can't reach peer: {0}")]
SAMCantReachPeer(String),
#[fail(display = "Destination key not found: {}", _0)]
#[error("Destination key not found: {0}")]
SAMKeyNotFound(String),
#[fail(display = "Peer not found: {}", _0)]
#[error("Peer not found: {0}")]
SAMPeerNotFound(String),
#[fail(display = "Duplicate peer destination: {}", _0)]
#[error("Duplicate peer destination: {0}")]
SAMDuplicatedDest(String),
#[fail(display = "Invalid destination key: {}", _0)]
#[error("Invalid destination key: {0}")]
SAMInvalidKey(String),
#[fail(display = "Invalid stream id: {}", _0)]
#[error("Invalid stream id: {0}")]
SAMInvalidId(String),
#[fail(display = "I2P/SAM Timeout: {}", _0)]
#[error("I2P/SAM Timeout: {0}")]
SAMTimeout(String),
#[fail(display = "Unknown I2P/SAM error: {}", _0)]
#[error("Unknown I2P/SAM error: {0}")]
SAMI2PError(String),
#[fail(display = "I2P address isn't a valid b32 or b64 encoding: {}", _0)]
#[error("I2P address isn't a valid b32 or b64 encoding: {0}")]
BadAddressEncoding(String),
#[fail(display = "Accept encountered error, and session was recreated. try operation again")]
#[error("Accept encountered error, and session was recreated. try operation again")]
SessionRecreated,
}

impl ErrorKind {
pub fn to_err(self) -> Error {
Error {
inner: Context::new(self),
}
}
}

impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let cause = match self.cause() {
Some(c) => format!("{}", c),
None => String::from("Unknown"),
};
let backtrace = match self.backtrace() {
Some(b) => format!("{}", b),
None => String::from("Unknown"),
};
let output = format!(
"{} \n Cause: {} \n Backtrace: {}",
self.inner, cause, backtrace
);
Display::fmt(&output, f)
}
}

impl Error {
/// get kind
pub fn kind(&self) -> ErrorKind {
self.inner.get_context().clone()
}
/// get cause
pub fn cause(&self) -> Option<&dyn Fail> {
self.inner.cause()
}
/// get backtrace
pub fn backtrace(&self) -> Option<&Backtrace> {
self.inner.backtrace()
}
}

impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Error {
Error {
inner: Context::new(kind),
}
}
}

impl From<Context<ErrorKind>> for Error {
fn from(inner: Context<ErrorKind>) -> Error {
Error { inner: inner }
}
}

impl From<io::Error> for Error {
fn from(err: io::Error) -> Error {
Error {
inner: Context::new(ErrorKind::Io(err.to_string())),
}
impl From<io::Error> for I2PError {
fn from(err: io::Error) -> I2PError {
Self::Io(err.to_string())
}
}

impl<I, E> From<nom::Err<I, E>> for Error {
fn from(_err: nom::Err<I, E>) -> Error {
Error {
inner: Context::new(ErrorKind::MessageParsing),
}
impl<E> From<nom::Err<E>> for I2PError {
fn from(_err: nom::Err<E>) -> I2PError {
Self::MessageParsing
}
}
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ pub mod session_watcher;

mod parsers;

pub use crate::error::{Error, ErrorKind};
pub mod utils;

pub use crate::error::I2PError;
pub use crate::sam::{SamConnection, Session};
44 changes: 18 additions & 26 deletions src/net/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::option;
use std::slice;
use std::vec;

use serde_derive::{Deserialize, Serialize};
use serde::{Deserialize, Serialize};

use crate::net::i2p::I2pAddr;

Expand All @@ -28,10 +28,7 @@ impl I2pSocketAddr {
/// assert_eq!(socket.port(), 8080);
/// ```
pub fn new(dest: I2pAddr, port: u16) -> I2pSocketAddr {
I2pSocketAddr {
port: port,
dest: dest,
}
I2pSocketAddr { port, dest }
}

/// Returns the I2P address associated with this socket address.
Expand Down Expand Up @@ -132,24 +129,19 @@ impl fmt::Display for I2pSocketAddr {
/// Some examples:
///
/// ```no_run
/// use i2p::net::{I2pSocketAddr, I2pStream, I2pDatagramSocket, I2pListener, I2pAddr};
///
/// fn main() {
/// let dest = I2pAddr::new("example.i2p");
/// let port = 12345;
///
/// // The following lines are equivalent
/// let i2p_s = I2pStream::connect(I2pSocketAddr::new(dest.clone(), port));
/// let i2p_s = I2pStream::connect((dest.clone(), port));
/// let i2p_s = I2pStream::connect(("example.i2p", port));
/// let i2p_s = I2pStream::connect("example.i2p:12345");
///
/// // I2pListener::bind(), I2pDatagramSocket::bind() and I2pDatagramSocket::send_to()
/// // behave similarly
/// let i2p_l = I2pListener::bind();
///
/// let mut i2p_dg_s = I2pDatagramSocket::bind(("127.0.0.1", port)).unwrap();
/// i2p_dg_s.send_to(&[7], (dest, 23451)).unwrap();
/// fn execute() {
/// use i2p::net::{I2pSocketAddr, I2pStream, I2pDatagramSocket, I2pListener, I2pAddr};
/// let dest = I2pAddr::new("example.i2p");
/// let port = 12345;
/// // The following lines are equivalent
/// let i2p_s = I2pStream::connect(I2pSocketAddr::new(dest.clone(), port));
/// let i2p_s = I2pStream::connect((dest.clone(), port));
/// let i2p_s = I2pStream::connect(("example.i2p", port));
/// let i2p_s = I2pStream::connect("example.i2p:12345");
/// // I2pListener::bind(), I2pDatagramSocket::bind() and I2pDatagramSocket::send_to() behave similarly
/// let i2p_l = I2pListener::bind();
/// let mut i2p_dg_s = I2pDatagramSocket::bind(("127.0.0.1", port)).unwrap();
/// i2p_dg_s.send_to(&[7], (dest, 23451)).unwrap();
/// }
/// ```
pub trait ToI2pSocketAddrs {
Expand Down Expand Up @@ -235,7 +227,7 @@ impl<'a, T: ToI2pSocketAddrs + ?Sized> ToI2pSocketAddrs for &'a T {
impl ToI2pSocketAddrs for String {
type Iter = vec::IntoIter<I2pSocketAddr>;
fn to_socket_addrs(&self) -> io::Result<vec::IntoIter<I2pSocketAddr>> {
(&**self).to_socket_addrs()
(**self).to_socket_addrs()
}
}

Expand Down Expand Up @@ -279,7 +271,7 @@ mod tests {
);
assert_eq!(
Ok(vec![a.clone()]),
tsa(&format!("{}:{}", "example.i2p", "24352"))
tsa(format!("{}:{}", "example.i2p", "24352"))
);
assert_eq!(
Ok(vec![a.clone()]),
Expand All @@ -294,7 +286,7 @@ mod tests {
#[test]
fn set_dest() {
fn i2p(low: u8) -> I2pAddr {
I2pAddr::new(&format!("example{}.i2p", low))
I2pAddr::new(&format!("example{low}.i2p"))
}

let mut addr = I2pSocketAddr::new(i2p(12), 80);
Expand Down
Loading