Skip to content

Commit

Permalink
Bump version 0.1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Jun 16, 2024
1 parent 94818f7 commit ef4db4c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dns2socks"
version = "0.1.5"
version = "0.1.6"
edition = "2021"
license = "MIT"
repository = "https://github.com/ssrlive/dns2socks"
Expand All @@ -17,10 +17,10 @@ clap = { version = "4", features = ["derive", "wrap_help"] }
ctrlc2 = { version = "3", features = ["tokio", "termination"] }
dotenvy = "0.15"
env_logger = "0.11"
hickory-proto = "0.24"
log = "0.4"
moka = { version = "0.12", default-features = false, features = ["future"] }
rand = "0.8"
socks5-impl = "0.5"
tokio = { version = "1", features = ["full"] }
tokio-util = "0.7"
hickory-proto = "0.24"
31 changes: 17 additions & 14 deletions src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ use hickory_proto::{
op::{header::MessageType, op_code::OpCode, query::Query, Message, ResponseCode},
rr::{record_type::RecordType, Name, RData},
};
use std::io::{Error, ErrorKind};
use std::{net::IpAddr, str::FromStr};

#[allow(dead_code)]
pub fn build_dns_query(domain: &str, query_type: RecordType, used_by_tcp: bool) -> Result<Vec<u8>, String> {
pub fn build_dns_query(domain: &str, query_type: RecordType, used_by_tcp: bool) -> std::io::Result<Vec<u8>> {
use rand::{rngs::StdRng, Rng, SeedableRng};
let name = Name::from_str(domain).map_err(|e| e.to_string())?;
let name = Name::from_str(domain).map_err(|e| Error::new(ErrorKind::InvalidInput, e.to_string()))?;
let query = Query::query(name, query_type);
let mut msg = Message::new();
msg.add_query(query)
.set_id(StdRng::from_entropy().gen())
.set_op_code(OpCode::Query)
.set_message_type(MessageType::Query)
.set_recursion_desired(true);
let mut msg_buf = msg.to_vec().map_err(|e| e.to_string())?;
let mut msg_buf = msg.to_vec().map_err(|e| Error::new(ErrorKind::InvalidInput, e.to_string()))?;
if used_by_tcp {
let mut buf = (msg_buf.len() as u16).to_be_bytes().to_vec();
buf.append(&mut msg_buf);
Expand All @@ -25,13 +26,13 @@ pub fn build_dns_query(domain: &str, query_type: RecordType, used_by_tcp: bool)
}
}

pub fn extract_ipaddr_from_dns_message(message: &Message) -> Result<IpAddr, String> {
pub fn extract_ipaddr_from_dns_message(message: &Message) -> std::io::Result<IpAddr> {
if message.response_code() != ResponseCode::NoError {
return Err(format!("{:?}", message.response_code()));
return Err(Error::new(ErrorKind::InvalidData, format!("{:?}", message.response_code())));
}
let mut cname = None;
for answer in message.answers() {
match answer.data().ok_or("DNS response not contains answer data")? {
match answer.data().ok_or(Error::new(ErrorKind::InvalidData, "No answer data"))? {
RData::A(addr) => {
return Ok(IpAddr::V4((*addr).into()));
}
Expand All @@ -45,26 +46,28 @@ pub fn extract_ipaddr_from_dns_message(message: &Message) -> Result<IpAddr, Stri
}
}
if let Some(cname) = cname {
return Err(cname);
return Err(Error::new(ErrorKind::InvalidData, format!("CNAME: {}", cname)));
}
Err(format!("{:?}", message.answers()))
Err(Error::new(ErrorKind::InvalidData, format!("{:?}", message.answers())))
}

pub fn extract_domain_from_dns_message(message: &Message) -> Result<String, String> {
let query = message.queries().first().ok_or("DnsRequest no query body")?;
pub fn extract_domain_from_dns_message(message: &Message) -> std::io::Result<String> {
let err = Error::new(ErrorKind::InvalidData, "DnsRequest no query body");
let query = message.queries().first().ok_or(err)?;
let name = query.name().to_string();
Ok(name)
}

pub fn parse_data_to_dns_message(data: &[u8], used_by_tcp: bool) -> Result<Message, String> {
pub fn parse_data_to_dns_message(data: &[u8], used_by_tcp: bool) -> std::io::Result<Message> {
if used_by_tcp {
let err = Error::new(ErrorKind::InvalidData, "invalid dns data");
if data.len() < 2 {
return Err("invalid dns data".into());
return Err(err);
}
let len = u16::from_be_bytes([data[0], data[1]]) as usize;
let data = data.get(2..len + 2).ok_or("invalid dns data")?;
let data = data.get(2..len + 2).ok_or(err)?;
return parse_data_to_dns_message(data, false);
}
let message = Message::from_vec(data).map_err(|e| e.to_string())?;
let message = Message::from_vec(data).map_err(|e| Error::new(ErrorKind::InvalidData, e.to_string()))?;
Ok(message)
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ fn log_dns_message(prefix: &str, domain: &str, message: &Message) {
Ok(ipaddr) => {
format!("{:?}", ipaddr)
}
Err(e) => e,
Err(e) => e.to_string(),
};
log::trace!("{} {:?} <==> {:?}", prefix, domain, ipaddr);
}
Expand Down

0 comments on commit ef4db4c

Please sign in to comment.