diff --git a/Cargo.lock b/Cargo.lock index 4992c6d..a515d86 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", ] diff --git a/zns-daemon/Cargo.toml b/zns-daemon/Cargo.toml index 6ee1d11..240765d 100644 --- a/zns-daemon/Cargo.toml +++ b/zns-daemon/Cargo.toml @@ -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"] } diff --git a/zns/diesel.toml b/zns-daemon/diesel.toml similarity index 100% rename from zns/diesel.toml rename to zns-daemon/diesel.toml diff --git a/zns/migrations/.keep b/zns-daemon/migrations/.keep similarity index 100% rename from zns/migrations/.keep rename to zns-daemon/migrations/.keep diff --git a/zns/migrations/2024-03-03-220459_create_records/down.sql b/zns-daemon/migrations/2024-03-03-220459_create_records/down.sql similarity index 100% rename from zns/migrations/2024-03-03-220459_create_records/down.sql rename to zns-daemon/migrations/2024-03-03-220459_create_records/down.sql diff --git a/zns/migrations/2024-03-03-220459_create_records/up.sql b/zns-daemon/migrations/2024-03-03-220459_create_records/up.sql similarity index 100% rename from zns/migrations/2024-03-03-220459_create_records/up.sql rename to zns-daemon/migrations/2024-03-03-220459_create_records/up.sql diff --git a/zns/src/config.rs b/zns-daemon/src/config.rs similarity index 100% rename from zns/src/config.rs rename to zns-daemon/src/config.rs diff --git a/zns/src/db/lib.rs b/zns-daemon/src/db/lib.rs similarity index 100% rename from zns/src/db/lib.rs rename to zns-daemon/src/db/lib.rs diff --git a/zns/src/db/mod.rs b/zns-daemon/src/db/mod.rs similarity index 100% rename from zns/src/db/mod.rs rename to zns-daemon/src/db/mod.rs diff --git a/zns/src/db/models.rs b/zns-daemon/src/db/models.rs similarity index 95% rename from zns/src/db/models.rs rename to zns-daemon/src/db/models.rs index 32b0383..b62be9f 100644 --- a/zns/src/db/models.rs +++ b/zns-daemon/src/db/models.rs @@ -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() { diff --git a/zns/src/handlers/mod.rs b/zns-daemon/src/handlers/mod.rs similarity index 98% rename from zns/src/handlers/mod.rs rename to zns-daemon/src/handlers/mod.rs index 9cc1243..0b3ddcc 100644 --- a/zns/src/handlers/mod.rs +++ b/zns-daemon/src/handlers/mod.rs @@ -1,6 +1,6 @@ use diesel::PgConnection; -use crate::{ +use zns::{ errors::ZNSError, structs::{Message, Opcode}, }; diff --git a/zns/src/handlers/query.rs b/zns-daemon/src/handlers/query.rs similarity index 90% rename from zns/src/handlers/query.rs rename to zns-daemon/src/handlers/query.rs index f372cc7..24035d3 100644 --- a/zns/src/handlers/query.rs +++ b/zns-daemon/src/handlers/query.rs @@ -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, diff --git a/zns/src/handlers/update/pubkeys/mod.rs b/zns-daemon/src/handlers/update/pubkeys/mod.rs similarity index 95% rename from zns/src/handlers/update/pubkeys/mod.rs rename to zns-daemon/src/handlers/update/pubkeys/mod.rs index 9373294..b3788cc 100644 --- a/zns/src/handlers/update/pubkeys/mod.rs +++ b/zns-daemon/src/handlers/update/pubkeys/mod.rs @@ -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; diff --git a/zns/src/handlers/update/pubkeys/rsa.rs b/zns-daemon/src/handlers/update/pubkeys/rsa.rs similarity index 95% rename from zns/src/handlers/update/pubkeys/rsa.rs rename to zns-daemon/src/handlers/update/pubkeys/rsa.rs index 8b4728b..da5776b 100644 --- a/zns/src/handlers/update/pubkeys/rsa.rs +++ b/zns-daemon/src/handlers/update/pubkeys/rsa.rs @@ -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}; diff --git a/zns/src/handlers/update/sig.rs b/zns-daemon/src/handlers/update/sig.rs similarity index 99% rename from zns/src/handlers/update/sig.rs rename to zns-daemon/src/handlers/update/sig.rs index 65b1d97..5c5af34 100644 --- a/zns/src/handlers/update/sig.rs +++ b/zns-daemon/src/handlers/update/sig.rs @@ -1,7 +1,7 @@ use base64::prelude::*; use int_enum::IntEnum; -use crate::{ +use zns::{ errors::ZNSError, parser::FromBytes, reader::Reader, diff --git a/zns-daemon/src/main.rs b/zns-daemon/src/main.rs index dbd51ea..51fa2d3 100644 --- a/zns-daemon/src/main.rs +++ b/zns-daemon/src/main.rs @@ -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> { diff --git a/zns/src/resolver.rs b/zns-daemon/src/resolver.rs similarity index 95% rename from zns/src/resolver.rs rename to zns-daemon/src/resolver.rs index 3846ff6..53936c3 100644 --- a/zns/src/resolver.rs +++ b/zns-daemon/src/resolver.rs @@ -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> { #[cfg(test)] mod tests { - use crate::structs::{Class, Question, RRClass, RRType, Type}; + use zns::structs::{Class, Question, RRClass, RRType, Type}; use super::*; diff --git a/zns/src/schema.rs b/zns-daemon/src/schema.rs similarity index 100% rename from zns/src/schema.rs rename to zns-daemon/src/schema.rs diff --git a/zns/src/utils.rs b/zns-daemon/src/utils.rs similarity index 100% rename from zns/src/utils.rs rename to zns-daemon/src/utils.rs diff --git a/zns/Cargo.toml b/zns/Cargo.toml index 01575cd..30efe72 100644 --- a/zns/Cargo.toml +++ b/zns/Cargo.toml @@ -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"] } + diff --git a/zns/src/errors.rs b/zns/src/errors.rs index 1af0326..a0ad105 100644 --- a/zns/src/errors.rs +++ b/zns/src/errors.rs @@ -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, diff --git a/zns/src/lib.rs b/zns/src/lib.rs index 14a9512..ff13bcd 100644 --- a/zns/src/lib.rs +++ b/zns/src/lib.rs @@ -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; diff --git a/zns/src/parser.rs b/zns/src/parser.rs index cae9a07..376febf 100644 --- a/zns/src/parser.rs +++ b/zns/src/parser.rs @@ -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 { diff --git a/zns/src/test_utils.rs b/zns/src/test_utils.rs new file mode 100644 index 0000000..aa75068 --- /dev/null +++ b/zns/src/test_utils.rs @@ -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()], + } +}