Skip to content

Commit

Permalink
feat: add noise security protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed May 1, 2022
1 parent 6950fff commit b30bfed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const QUIC: u32 = 460;
const SCTP: u32 = 132;
const TCP: u32 = 6;
const TLS: u32 = 448;
const NOISE: u32 = 454;
const UDP: u32 = 273;
const UDT: u32 = 301;
const UNIX: u32 = 400;
Expand Down Expand Up @@ -90,6 +91,7 @@ pub enum Protocol<'a> {
Sctp(u16),
Tcp(u16),
Tls,
Noise,
Udp(u16),
Udt,
Unix(Cow<'a, str>),
Expand Down Expand Up @@ -119,6 +121,7 @@ impl<'a> Protocol<'a> {
Ok(Protocol::Tcp(s.parse()?))
}
"tls" => Ok(Protocol::Tls),
"noise" => Ok(Protocol::Noise),
"udp" => {
let s = iter.next().ok_or(Error::InvalidProtocolString)?;
Ok(Protocol::Udp(s.parse()?))
Expand Down Expand Up @@ -308,6 +311,7 @@ impl<'a> Protocol<'a> {
Ok((Protocol::Tcp(num), rest))
}
TLS => Ok((Protocol::Tls, input)),
NOISE => Ok((Protocol::Noise, input)),
UDP => {
let (data, rest) = split_at(2, input)?;
let mut rdr = Cursor::new(data);
Expand Down Expand Up @@ -357,6 +361,7 @@ impl<'a> Protocol<'a> {
w.write_u16::<BigEndian>(*port)?
}
Protocol::Tls => w.write_all(encode::u32(TLS, &mut buf))?,
Protocol::Noise => w.write_all(encode::u32(NOISE, &mut buf))?,
Protocol::Udp(port) => {
w.write_all(encode::u32(UDP, &mut buf))?;
w.write_u16::<BigEndian>(*port)?
Expand Down Expand Up @@ -471,6 +476,7 @@ impl<'a> Protocol<'a> {
Sctp(a) => Sctp(a),
Tcp(a) => Tcp(a),
Tls => Tls,
Noise => Noise,
Udp(a) => Udp(a),
Udt => Udt,
Unix(cow) => Unix(Cow::Owned(cow.into_owned())),
Expand Down Expand Up @@ -512,6 +518,7 @@ impl<'a> fmt::Display for Protocol<'a> {
Sctp(port) => write!(f, "/sctp/{}", port),
Tcp(port) => write!(f, "/tcp/{}", port),
Tls => write!(f, "/tls"),
Noise => write!(f, "/noise"),
Udp(port) => write!(f, "/udp/{}", port),
Udt => f.write_str("/udt"),
Unix(s) => write!(f, "/unix/{}", s),
Expand Down
6 changes: 6 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@ fn construct_success() {
"047F00000106007FC003DD03",
vec![Ip4(local.clone()), Tcp(127), Tls, Ws("/".into())],
);

ma_valid(
"/ip4/127.0.0.1/tcp/127/noise",
"047F00000106007FC603",
vec![Ip4(local.clone()), Tcp(127), Noise],
);
}

#[test]
Expand Down

0 comments on commit b30bfed

Please sign in to comment.