-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,235 additions
and
7 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
[package] | ||
name = "core-omnius" | ||
version = "0.1.0" | ||
edition = { workspace = true } | ||
authors = { workspace = true } | ||
|
||
[features] | ||
stable-test = [] | ||
|
||
[dependencies] | ||
core-base = { workspace = true } | ||
|
||
anyhow = { workspace = true } | ||
async-trait = { workspace = true } | ||
serde = { workspace = true } | ||
serde_json = { workspace = true } | ||
tokio = { workspace = true } | ||
tokio-util = { workspace = true } | ||
tokio-stream = { workspace = true } | ||
tracing = { workspace = true } | ||
tracing-subscriber = { workspace = true } | ||
uuid = { workspace = true } | ||
sqlx = { workspace = true } | ||
config = { workspace = true } | ||
ring = { workspace = true } | ||
hex = { workspace = true } | ||
urlencoding = { workspace = true } | ||
thiserror = { workspace = true } | ||
once_cell = { workspace = true } | ||
base64 = { workspace = true } | ||
futures = { workspace = true } | ||
futures-util = { workspace = true } | ||
serial_test = { workspace = true } | ||
pin-utils = { workspace = true } | ||
nom = { workspace = true } | ||
fast-socks5 = { workspace = true } | ||
ed25519-dalek = { workspace = true } | ||
rand_core = { workspace = true } | ||
sha3 = { workspace = true } | ||
ciborium = { workspace = true } | ||
bitflags = { workspace = true } | ||
tempfile = { workspace = true } | ||
crc = { workspace = true } | ||
chrono = { workspace = true } | ||
rand = { workspace = true } | ||
rand_chacha = { workspace = true } | ||
|
||
[dev-dependencies] | ||
testcontainers = { workspace = true } | ||
testresult = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod model; | ||
mod service; | ||
|
||
pub use model::*; | ||
pub use service::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
mod omni_address; | ||
mod omni_hash; | ||
mod omni_signature; | ||
|
||
pub use omni_address::*; | ||
pub use omni_hash::*; | ||
pub use omni_signature::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use std::fmt; | ||
|
||
use nom::bytes::complete::{is_not, tag}; | ||
use nom::character::complete::{char, multispace0}; | ||
use nom::sequence::delimited; | ||
use nom::IResult; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] | ||
pub struct OmniAddress(String); | ||
|
||
impl OmniAddress { | ||
pub fn new(value: &str) -> OmniAddress { | ||
OmniAddress(value.to_owned()) | ||
} | ||
|
||
pub fn parse_tcp(&self) -> anyhow::Result<String> { | ||
let (_, addr) = Self::parse_tcp_sub(&self.0).map_err(|e| e.to_owned())?; | ||
Ok(addr.to_string()) | ||
} | ||
|
||
fn parse_tcp_sub(v: &str) -> IResult<&str, &str> { | ||
let (v, _) = tag("tcp")(v)?; | ||
let (v, addr) = delimited(char('('), delimited(multispace0, is_not(")"), multispace0), char(')'))(v)?; | ||
Ok((v, addr)) | ||
} | ||
} | ||
|
||
impl fmt::Display for OmniAddress { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "{}", self.0) | ||
} | ||
} | ||
|
||
impl From<String> for OmniAddress { | ||
fn from(value: String) -> Self { | ||
Self::new(value.as_str()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::model::OmniAddress; | ||
|
||
#[tokio::test] | ||
#[ignore] | ||
async fn add_port_mapping_test() { | ||
let addr = OmniAddress::new("tcp(127.0.0.1:8000)"); | ||
println!("{:?}", addr.parse_tcp()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use std::fmt; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] | ||
pub enum OmniHashAlgorithmType { | ||
Sha3_256, | ||
} | ||
|
||
impl fmt::Display for OmniHashAlgorithmType { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
let typ = match self { | ||
OmniHashAlgorithmType::Sha3_256 => "sha3-256", | ||
}; | ||
|
||
write!(f, "{}", typ) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] | ||
pub struct OmniHash { | ||
pub typ: OmniHashAlgorithmType, | ||
pub value: Vec<u8>, | ||
} | ||
|
||
impl fmt::Display for OmniHash { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "{}:{}", self.typ, hex::encode(&self.value)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
use std::fmt; | ||
|
||
use base64::{engine::general_purpose::URL_SAFE_NO_PAD as BASE64, Engine}; | ||
use ed25519_dalek::Signer; | ||
use rand_core::OsRng; | ||
use serde::{Deserialize, Serialize}; | ||
use sha3::{Digest, Sha3_256}; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
pub enum OmniSignType { | ||
Ed25519, | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
pub struct OmniSigner { | ||
typ: OmniSignType, | ||
name: String, | ||
key: Vec<u8>, | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
pub struct OmniSignature { | ||
typ: OmniSignType, | ||
name: String, | ||
public_key: Vec<u8>, | ||
value: Vec<u8>, | ||
} | ||
|
||
impl OmniSigner { | ||
pub fn new(typ: &OmniSignType, name: &str) -> Self { | ||
match typ { | ||
OmniSignType::Ed25519 => { | ||
let signing_key = ed25519_dalek::SigningKey::generate(&mut OsRng); | ||
|
||
let typ = typ.clone(); | ||
let name = name.to_string(); | ||
let key = signing_key.to_keypair_bytes().to_vec(); | ||
Self { typ, name, key } | ||
} | ||
} | ||
} | ||
|
||
pub fn sign(&self, msg: &[u8]) -> anyhow::Result<OmniSignature> { | ||
match self.typ { | ||
OmniSignType::Ed25519 => { | ||
let signing_key_bytes = self.key.as_slice(); | ||
if signing_key_bytes.len() != ed25519_dalek::KEYPAIR_LENGTH { | ||
anyhow::bail!("Invalid signing_key length"); | ||
} | ||
let signing_key_bytes = <&[u8; ed25519_dalek::KEYPAIR_LENGTH]>::try_from(signing_key_bytes)?; | ||
|
||
let signing_key = ed25519_dalek::SigningKey::from_keypair_bytes(signing_key_bytes)?; | ||
|
||
let typ = self.typ.clone(); | ||
let name = self.name.clone(); | ||
let public_key = signing_key.verifying_key().to_bytes().to_vec(); | ||
let value = signing_key.sign(msg).to_vec(); | ||
Ok(OmniSignature { | ||
typ, | ||
name, | ||
public_key, | ||
value, | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for OmniSigner { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self.typ { | ||
OmniSignType::Ed25519 => { | ||
let signing_key_bytes: [u8; ed25519_dalek::KEYPAIR_LENGTH] = self.key.clone().try_into().map_err(|_| fmt::Error)?; | ||
|
||
let signing_key = ed25519_dalek::SigningKey::from_keypair_bytes(&signing_key_bytes).map_err(|_| fmt::Error)?; | ||
let public_key = signing_key.verifying_key().to_bytes(); | ||
|
||
let mut hasher = Sha3_256::new(); | ||
hasher.update(public_key); | ||
let hash = hasher.finalize(); | ||
|
||
write!(f, "{}@{}", self.name, BASE64.encode(hash)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl OmniSignature { | ||
pub fn verify(&self, msg: &[u8]) -> anyhow::Result<()> { | ||
match self.typ { | ||
OmniSignType::Ed25519 => { | ||
let verifying_key_bytes: [u8; ed25519_dalek::PUBLIC_KEY_LENGTH] = self | ||
.public_key | ||
.clone() | ||
.try_into() | ||
.map_err(|_| anyhow::anyhow!("Invalid verifying_key length"))?; | ||
let signature_bytes: [u8; ed25519_dalek::SIGNATURE_LENGTH] = | ||
self.value.clone().try_into().map_err(|_| anyhow::anyhow!("Invalid signature length"))?; | ||
|
||
let verifying_key = ed25519_dalek::VerifyingKey::from_bytes(&verifying_key_bytes)?; | ||
let signature = ed25519_dalek::Signature::from_bytes(&signature_bytes); | ||
Ok(verifying_key.verify_strict(msg, &signature)?) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for OmniSignature { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self.typ { | ||
OmniSignType::Ed25519 => { | ||
let mut hasher = Sha3_256::new(); | ||
hasher.update(&self.public_key); | ||
let hash = hasher.finalize(); | ||
|
||
write!(f, "{}@{}", self.name, BASE64.encode(hash)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{OmniSignType, OmniSigner}; | ||
|
||
#[tokio::test] | ||
#[ignore] | ||
async fn simple_test() { | ||
let signer = OmniSigner::new(&OmniSignType::Ed25519, "test_user"); | ||
let signature = signer.sign(b"test").unwrap(); | ||
|
||
println!("{}", signer); | ||
println!("{}", signature); | ||
|
||
assert!(signature.verify(b"test").is_ok()); | ||
assert!(signature.verify(b"test_err").is_err()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
mod connection; | ||
mod util; | ||
|
||
pub use connection::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod stream; | ||
|
||
pub use stream::*; |
Oops, something went wrong.