Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrise committed May 11, 2024
1 parent a2340dc commit e8e176f
Show file tree
Hide file tree
Showing 13 changed files with 1,235 additions and 7 deletions.
776 changes: 769 additions & 7 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"modules/cloud",
"modules/image",
"modules/migration",
"modules/omnius",
"modules/testkit",
]
resolver = "2"
Expand All @@ -17,6 +18,7 @@ core-base = { path = "./modules/base" }
core-cloud = { path = "./modules/cloud", features = ["aws"] }
core-image = { path = "./modules/image" }
core-migration = { path = "./modules/migration", features = ["postgres"] }
core-omnius = { path = "./modules/omnius" }
core-testkit = { path = "./modules/testkit" }

aws-config = "0.56.1"
Expand Down Expand Up @@ -74,3 +76,17 @@ serial_test = "2.0.0"
proc-macro2 = "1.0.70"
reqwest = { version = "0.11.23", features = ["json"] }
testresult = "0.4.0"
base64 = "0.21.7"
ciborium = "0.2.2"
bitflags = { version = "2.4.2", features = ["serde"] }
tempfile = "3.10.0"
crc = "3.0.1"
sha3 = "0.10.8"
ed25519-dalek = { version = "2.1.0", features = ["rand_core", "serde"] }
rand_core = "0.6.4"
nom = "7.1.3"
fast-socks5 = "0.9.2"
futures = "0.3.30"
futures-util = "0.3.28"
local-ip-address = "0.5.7"
pin-utils = "0.1.0"
50 changes: 50 additions & 0 deletions modules/omnius/Cargo.toml
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 }
5 changes: 5 additions & 0 deletions modules/omnius/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod model;
mod service;

pub use model::*;
pub use service::*;
7 changes: 7 additions & 0 deletions modules/omnius/src/model.rs
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::*;
51 changes: 51 additions & 0 deletions modules/omnius/src/model/omni_address.rs
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());
}
}
30 changes: 30 additions & 0 deletions modules/omnius/src/model/omni_hash.rs
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))
}
}
138 changes: 138 additions & 0 deletions modules/omnius/src/model/omni_signature.rs
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());
}
}
4 changes: 4 additions & 0 deletions modules/omnius/src/service.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod connection;
mod util;

pub use connection::*;
3 changes: 3 additions & 0 deletions modules/omnius/src/service/connection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod stream;

pub use stream::*;
Loading

0 comments on commit e8e176f

Please sign in to comment.