10
0
Fork 0
mirror of https://github.com/ZeusWPI/ZNS.git synced 2024-11-23 22:11:10 +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"
version = "0.1.0"
dependencies = [
"asn1",
"base64",
"diesel",
"dotenvy",
"int-enum",
"reqwest",
"ring",
"thiserror",
"tokio",
"zns",
]
[[package]]
@ -1669,6 +1664,13 @@ dependencies = [
name = "zns-daemon"
version = "0.1.0"
dependencies = [
"asn1",
"base64",
"diesel",
"dotenvy",
"int-enum",
"reqwest",
"ring",
"tokio",
"zns",
]

View file

@ -2,10 +2,22 @@
name = "zns-daemon"
version = "0.1.0"
edition = "2021"
resolver = "2"
[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]
version = "0.1.0"
version = "*"
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,
structs::{Class, Type, RR},
};
use diesel::prelude::*;
use diesel::sql_types::Text;
use self::schema::records::{self};
@ -100,7 +100,7 @@ pub fn insert_into_database(rr: &RR, connection: &mut PgConnection) -> Result<()
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(),
})?;
@ -119,7 +119,7 @@ pub fn get_from_database(
_type.map(|t| t.into()),
class.into(),
)
.map_err(|e| ZNSError::Database {
.map_err(|e| ZNSError::Servfail {
message: e.to_string(),
})?;
@ -157,9 +157,12 @@ pub fn delete_from_database(
#[cfg(test)]
mod tests {
use zns::test_utils::get_rr;
use super::*;
use crate::{db::lib::tests::get_test_connection, parser::tests::get_rr};
use crate::db::lib::tests::get_test_connection;
#[test]
fn test() {

View file

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

View file

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

View file

@ -1,9 +1,9 @@
use diesel::PgConnection;
use reqwest::header::ACCEPT;
use crate::{
config::Config,
db::models::get_from_database,
use crate::{config::Config, db::models::get_from_database};
use zns::{
errors::ZNSError,
parser::FromBytes,
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;
/// https://datatracker.ietf.org/doc/html/rfc4034#section-2
#[derive(Debug)]
#[allow(dead_code)]
pub struct DNSKeyRData {
pub flags: u16,
pub protocol: u8,

View file

@ -3,11 +3,12 @@ use diesel::PgConnection;
use crate::{
config::Config,
db::models::{delete_from_database, insert_into_database},
errors::ZNSError,
structs::{Class, Message, RRClass, RRType, Type},
utils::vec_equal,
};
use zns::errors::ZNSError;
use zns::structs::{Class, Message, RRClass, RRType, Type};
use self::sig::Sig;
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 crate::handlers::update::sig::Algorithm;
use ring::signature;
use zns::{errors::ZNSError, reader::Reader};
pub struct Ed25519PublicKey {
data: Vec<u8>,

View file

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

View file

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

View file

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

View file

@ -1,9 +1,14 @@
use std::{error::Error, net::SocketAddr};
use config::Config;
use zns::config;
mod 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]
async fn main() -> Result<(), Box<dyn Error>> {

View file

@ -4,13 +4,13 @@ use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
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::errors::ZNSError;
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;
@ -102,7 +102,7 @@ pub async fn tcp_listener_loop(addr: SocketAddr) -> Result<(), Box<dyn Error>> {
#[cfg(test)]
mod tests {
use crate::structs::{Class, Question, RRClass, RRType, Type};
use zns::structs::{Class, Question, RRClass, RRType, Type};
use super::*;

View file

@ -1,15 +1,17 @@
[package]
name = "zns"
resolver = "2"
version = "0.1.0"
edition = "2021"
[features]
test-utils = []
[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"
reqwest = {version = "0.12.4", features = ["json","default"]}
asn1 = "0.16.2"
int-enum = "1.1"
thiserror = "1.0"
[dev-dependencies]
zns = { path = ".", features = ["test-utils"] }

View file

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

View file

@ -284,47 +284,10 @@ impl ToBytes for Message {
#[cfg(test)]
pub mod tests {
use crate::test_utils::{get_message, get_rr};
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]
fn test_parse_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()],
}
}