10
0
Fork 0
mirror of https://github.com/ZeusWPI/ZNS.git synced 2024-10-29 21:14:27 +01:00

refactor for less dependencies in cli

This commit is contained in:
Xander Bil 2024-08-16 23:39:38 +02:00
parent 223988741f
commit f7122891c6
No known key found for this signature in database
GPG key ID: EC9706B54A278598
28 changed files with 127 additions and 106 deletions

14
Cargo.lock generated
View file

@ -1643,15 +1643,10 @@ checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde"
name = "zns" name = "zns"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"asn1",
"base64", "base64",
"diesel",
"dotenvy",
"int-enum", "int-enum",
"reqwest",
"ring",
"thiserror", "thiserror",
"tokio", "zns",
] ]
[[package]] [[package]]
@ -1669,6 +1664,13 @@ dependencies = [
name = "zns-daemon" name = "zns-daemon"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"asn1",
"base64",
"diesel",
"dotenvy",
"int-enum",
"reqwest",
"ring",
"tokio", "tokio",
"zns", "zns",
] ]

View file

@ -2,10 +2,22 @@
name = "zns-daemon" name = "zns-daemon"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
resolver = "2"
[dependencies] [dependencies]
tokio = {version = "1.36.0"} tokio = {version = "1.36.0", features = ["macros","rt-multi-thread","net"]}
diesel = { version = "2.1.4", features = ["postgres"] }
dotenvy = "0.15"
ring = "0.17.8"
reqwest = {version = "0.12.4", features = ["json","default"]}
asn1 = "0.16.2"
base64 = "0.22.0"
int-enum = "1.1"
[dependencies.zns] [dependencies.zns]
version = "0.1.0" version = "*"
path = "../zns" path = "../zns"
[dev-dependencies]
zns = { path = "../zns", features = ["test-utils"] }

View file

@ -1,9 +1,9 @@
use crate::{ use diesel::prelude::*;
use diesel::sql_types::Text;
use zns::{
errors::ZNSError, errors::ZNSError,
structs::{Class, Type, RR}, structs::{Class, Type, RR},
}; };
use diesel::prelude::*;
use diesel::sql_types::Text;
use self::schema::records::{self}; use self::schema::records::{self};
@ -100,7 +100,7 @@ pub fn insert_into_database(rr: &RR, connection: &mut PgConnection) -> Result<()
rdata: rr.rdata.clone(), rdata: rr.rdata.clone(),
}; };
Record::create(connection, record).map_err(|e| ZNSError::Database { Record::create(connection, record).map_err(|e| ZNSError::Servfail {
message: e.to_string(), message: e.to_string(),
})?; })?;
@ -119,7 +119,7 @@ pub fn get_from_database(
_type.map(|t| t.into()), _type.map(|t| t.into()),
class.into(), class.into(),
) )
.map_err(|e| ZNSError::Database { .map_err(|e| ZNSError::Servfail {
message: e.to_string(), message: e.to_string(),
})?; })?;
@ -157,9 +157,12 @@ pub fn delete_from_database(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use zns::test_utils::get_rr;
use super::*; use super::*;
use crate::{db::lib::tests::get_test_connection, parser::tests::get_rr}; use crate::db::lib::tests::get_test_connection;
#[test] #[test]
fn test() { fn test() {

View file

@ -1,6 +1,6 @@
use diesel::PgConnection; use diesel::PgConnection;
use crate::{ use zns::{
errors::ZNSError, errors::ZNSError,
structs::{Message, Opcode}, structs::{Message, Opcode},
}; };

View file

@ -1,11 +1,12 @@
use diesel::PgConnection; use diesel::PgConnection;
use crate::{ use zns::{
db::models::get_from_database,
errors::ZNSError, errors::ZNSError,
structs::{Message, Question, RR}, structs::{Message, Question, RR},
}; };
use crate::db::models::get_from_database;
use super::ResponseHandler; use super::ResponseHandler;
pub struct QueryHandler {} pub struct QueryHandler {}
@ -42,7 +43,7 @@ impl ResponseHandler for QueryHandler {
response.answer.extend(rrs) response.answer.extend(rrs)
} }
Err(e) => { Err(e) => {
return Err(ZNSError::Database { return Err(ZNSError::Servfail {
message: e.to_string(), message: e.to_string(),
}) })
} }
@ -79,12 +80,11 @@ fn try_wildcard(question: &Question, connection: &mut PgConnection) -> Result<Ve
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::parser::tests::get_message;
use crate::structs::*;
use crate::{ use crate::db::{lib::tests::get_test_connection, models::insert_into_database};
db::{lib::tests::get_test_connection, models::insert_into_database}, use zns::{
parser::{tests::get_rr, ToBytes}, parser::ToBytes,
test_utils::{get_message, get_rr},
}; };
#[tokio::test] #[tokio::test]

View file

@ -1,9 +1,9 @@
use diesel::PgConnection; use diesel::PgConnection;
use reqwest::header::ACCEPT; use reqwest::header::ACCEPT;
use crate::{ use crate::{config::Config, db::models::get_from_database};
config::Config,
db::models::get_from_database, use zns::{
errors::ZNSError, errors::ZNSError,
parser::FromBytes, parser::FromBytes,
reader::Reader, reader::Reader,

View file

@ -1,9 +1,10 @@
use crate::{errors::ZNSError, parser::FromBytes, reader::Reader}; use zns::{errors::ZNSError, parser::FromBytes, reader::Reader};
use super::sig::Algorithm; use super::sig::Algorithm;
/// https://datatracker.ietf.org/doc/html/rfc4034#section-2 /// https://datatracker.ietf.org/doc/html/rfc4034#section-2
#[derive(Debug)] #[derive(Debug)]
#[allow(dead_code)]
pub struct DNSKeyRData { pub struct DNSKeyRData {
pub flags: u16, pub flags: u16,
pub protocol: u8, pub protocol: u8,

View file

@ -3,11 +3,12 @@ use diesel::PgConnection;
use crate::{ use crate::{
config::Config, config::Config,
db::models::{delete_from_database, insert_into_database}, db::models::{delete_from_database, insert_into_database},
errors::ZNSError,
structs::{Class, Message, RRClass, RRType, Type},
utils::vec_equal, utils::vec_equal,
}; };
use zns::errors::ZNSError;
use zns::structs::{Class, Message, RRClass, RRType, Type};
use self::sig::Sig; use self::sig::Sig;
use super::ResponseHandler; use super::ResponseHandler;

View file

@ -1,8 +1,7 @@
use ring::signature;
use crate::{errors::ZNSError, handlers::update::sig::Algorithm, reader::Reader};
use super::{PublicKey, SSH_ED25519}; use super::{PublicKey, SSH_ED25519};
use crate::handlers::update::sig::Algorithm;
use ring::signature;
use zns::{errors::ZNSError, reader::Reader};
pub struct Ed25519PublicKey { pub struct Ed25519PublicKey {
data: Vec<u8>, data: Vec<u8>,

View file

@ -2,8 +2,7 @@ mod ed25519;
mod rsa; mod rsa;
use std::str::from_utf8; use std::str::from_utf8;
use crate::errors::ZNSError; use zns::{errors::ZNSError, reader::Reader};
use crate::reader::Reader;
pub use self::ed25519::Ed25519PublicKey; pub use self::ed25519::Ed25519PublicKey;
pub use self::rsa::RsaPublicKey; pub use self::rsa::RsaPublicKey;

View file

@ -1,6 +1,6 @@
use crate::handlers::update::sig::Algorithm;
use ring::signature; use ring::signature;
use zns::{errors::ZNSError, reader::Reader};
use crate::{errors::ZNSError, handlers::update::sig::Algorithm, reader::Reader};
use super::{PublicKey, SSH_RSA}; use super::{PublicKey, SSH_RSA};

View file

@ -1,7 +1,7 @@
use base64::prelude::*; use base64::prelude::*;
use int_enum::IntEnum; use int_enum::IntEnum;
use crate::{ use zns::{
errors::ZNSError, errors::ZNSError,
parser::FromBytes, parser::FromBytes,
reader::Reader, reader::Reader,

View file

@ -1,9 +1,14 @@
use std::{error::Error, net::SocketAddr}; use std::{error::Error, net::SocketAddr};
use config::Config; mod config;
use zns::config; mod resolver;
mod db;
mod handlers;
mod utils;
use zns::resolver::{tcp_listener_loop, udp_listener_loop}; use config::Config;
use crate::resolver::{tcp_listener_loop, udp_listener_loop};
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> { async fn main() -> Result<(), Box<dyn Error>> {

View file

@ -4,13 +4,13 @@ use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpSocket, UdpSocket}; use tokio::net::{TcpSocket, UdpSocket};
use zns::errors::ZNSError;
use zns::parser::{FromBytes, ToBytes};
use zns::reader::Reader;
use zns::structs::{Header, Message, RCODE};
use crate::db::lib::get_connection; use crate::db::lib::get_connection;
use crate::errors::ZNSError;
use crate::handlers::{Handler, ResponseHandler}; use crate::handlers::{Handler, ResponseHandler};
use crate::parser::{FromBytes, ToBytes};
use crate::reader::Reader;
use crate::structs::{Header, Message, RCODE};
const MAX_DATAGRAM_SIZE: usize = 512; const MAX_DATAGRAM_SIZE: usize = 512;
@ -102,7 +102,7 @@ pub async fn tcp_listener_loop(addr: SocketAddr) -> Result<(), Box<dyn Error>> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::structs::{Class, Question, RRClass, RRType, Type}; use zns::structs::{Class, Question, RRClass, RRType, Type};
use super::*; use super::*;

View file

@ -1,15 +1,17 @@
[package] [package]
name = "zns" name = "zns"
resolver = "2"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
[features]
test-utils = []
[dependencies] [dependencies]
diesel = { version = "2.1.4", features = ["postgres"] }
dotenvy = "0.15"
tokio = {version = "1.36.0", features = ["macros","rt-multi-thread","net"], default-features = false}
ring = "0.17.8"
base64 = "0.22.0" base64 = "0.22.0"
reqwest = {version = "0.12.4", features = ["json","default"]}
asn1 = "0.16.2"
int-enum = "1.1" int-enum = "1.1"
thiserror = "1.0" thiserror = "1.0"
[dev-dependencies]
zns = { path = ".", features = ["test-utils"] }

View file

@ -6,15 +6,12 @@ use crate::structs::{Type, RCODE};
pub enum ZNSError { pub enum ZNSError {
#[error("Parse Error for {object:?}: {message:?}")] #[error("Parse Error for {object:?}: {message:?}")]
Parse { object: String, message: String }, Parse { object: String, message: String },
#[error("Database Error: {message:?}")]
Database { message: String },
#[error("Reader Error: {message:?}")] #[error("Reader Error: {message:?}")]
Reader { message: String }, Reader { message: String },
#[error("Key Error: {message:?}")] #[error("Key Error: {message:?}")]
Key { message: String }, Key { message: String },
#[error("Reqwest error")] #[error("Server error")]
Reqwest(#[from] reqwest::Error), Servfail { message: String },
#[error("DNS Query Format Error: {message:?}")] #[error("DNS Query Format Error: {message:?}")]
Formerr { message: String }, Formerr { message: String },
#[error("Domain name does not exist: {qtype:?} {domain:?}")] #[error("Domain name does not exist: {qtype:?} {domain:?}")]
@ -33,8 +30,7 @@ impl ZNSError {
ZNSError::Formerr { .. } | ZNSError::Parse { .. } | ZNSError::Reader { .. } => { ZNSError::Formerr { .. } | ZNSError::Parse { .. } | ZNSError::Reader { .. } => {
RCODE::FORMERR RCODE::FORMERR
} }
ZNSError::Database { .. } | ZNSError::Reqwest(_) => RCODE::SERVFAIL, ZNSError::Servfail { .. } => RCODE::SERVFAIL,
ZNSError::NotAuth { .. } => RCODE::NOTAUTH, ZNSError::NotAuth { .. } => RCODE::NOTAUTH,
ZNSError::NXDomain { .. } => RCODE::NXDOMAIN, ZNSError::NXDomain { .. } => RCODE::NXDOMAIN,
ZNSError::NotImp { .. } => RCODE::NOTIMP, ZNSError::NotImp { .. } => RCODE::NOTIMP,

View file

@ -1,11 +1,7 @@
mod db;
mod handlers;
mod message;
mod structs;
mod utils;
pub mod config;
pub mod errors; pub mod errors;
pub mod message;
pub mod parser; pub mod parser;
pub mod resolver;
pub mod reader; pub mod reader;
pub mod structs;
pub mod test_utils;

View file

@ -284,47 +284,10 @@ impl ToBytes for Message {
#[cfg(test)] #[cfg(test)]
pub mod tests { pub mod tests {
use crate::test_utils::{get_message, get_rr};
use super::*; use super::*;
pub fn get_rr() -> RR {
RR {
name: vec![String::from("example"), String::from("org")],
_type: Type::Type(RRType::A),
class: Class::Class(RRClass::IN),
ttl: 10,
rdlength: 4,
rdata: vec![1, 2, 3, 4],
}
}
pub fn get_message() -> Message {
Message {
header: Header {
id: 1,
flags: 288,
qdcount: 2,
ancount: 1,
nscount: 1,
arcount: 1,
},
question: vec![
Question {
qname: vec![String::from("example"), String::from("org")],
qtype: Type::Type(RRType::A),
qclass: Class::Class(RRClass::IN),
},
Question {
qname: vec![String::from("example"), String::from("org")],
qtype: Type::Type(RRType::A),
qclass: Class::Class(RRClass::IN),
},
],
answer: vec![get_rr()],
authority: vec![get_rr()],
additional: vec![get_rr()],
}
}
#[test] #[test]
fn test_parse_header() { fn test_parse_header() {
let header = Header { let header = Header {

42
zns/src/test_utils.rs Normal file
View file

@ -0,0 +1,42 @@
#![cfg(feature = "test-utils")]
use crate::structs::*;
#[cfg(feature = "test-utils")]
pub fn get_rr() -> RR {
RR {
name: vec![String::from("example"), String::from("org")],
_type: Type::Type(RRType::A),
class: Class::Class(RRClass::IN),
ttl: 10,
rdlength: 4,
rdata: vec![1, 2, 3, 4],
}
}
pub fn get_message() -> Message {
Message {
header: Header {
id: 1,
flags: 288,
qdcount: 2,
ancount: 1,
nscount: 1,
arcount: 1,
},
question: vec![
Question {
qname: vec![String::from("example"), String::from("org")],
qtype: Type::Type(RRType::A),
qclass: Class::Class(RRClass::IN),
},
Question {
qname: vec![String::from("example"), String::from("org")],
qtype: Type::Type(RRType::A),
qclass: Class::Class(RRClass::IN),
},
],
answer: vec![get_rr()],
authority: vec![get_rr()],
additional: vec![get_rr()],
}
}