Some performance improvements
This commit is contained in:
parent
3877234037
commit
c11c87254c
@ -2,6 +2,11 @@ cmake_minimum_required(VERSION 3.15)
|
||||
project(TeaWebDNS)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
if (CONFIG_FILE)
|
||||
message("Including config file ${CONFIG_FILE}")
|
||||
include(${CONFIG_FILE})
|
||||
message("${spdlog_DIR}")
|
||||
endif ()
|
||||
|
||||
#Setup the compiler (Cant be done within a function!)
|
||||
if (MSVC)
|
||||
|
@ -1,11 +1,9 @@
|
||||
project(TeaWebDNS)
|
||||
|
||||
# Require libevent
|
||||
set(LIBEVENT_STATIC_LINK TRUE)
|
||||
find_package(Libevent REQUIRED)
|
||||
include_directories(${LIBEVENT_INCLUDE_DIRS})
|
||||
message(${LIBEVENT_INCLUDE_DIRS})
|
||||
|
||||
find_package(spdlog REQUIRED)
|
||||
|
||||
add_executable(TeaWebDNS ./main.cpp src/server.cpp src/handler.cpp src/logger.cpp)
|
||||
target_link_libraries(TeaWebDNS ${LIBEVENT_STATIC_LIBRARIES} stdc++fs pthread teadns::parser spdlog::spdlog)
|
||||
target_link_libraries(TeaWebDNS libevent::core libevent::pthreads stdc++fs pthread teadns::parser spdlog::spdlog)
|
@ -80,7 +80,7 @@ void WebDNSHandler::handle_message(const std::shared_ptr<DNSServerBinding>& bind
|
||||
#define MAX_IPV6_ADDRESS_STR_LEN 39
|
||||
|
||||
//Address at least one char long!
|
||||
bool parse_v6(uint8_t(result)[16], const std::string_view& address, char colon = ':') {
|
||||
inline bool parse_v6(uint8_t(result)[16], const std::string_view& address, char colon = ':') {
|
||||
uint16_t accumulator{0};
|
||||
uint8_t colon_count{0}, pos{0};
|
||||
|
||||
@ -126,38 +126,40 @@ bool parse_v6(uint8_t(result)[16], const std::string_view& address, char colon =
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool handle_ipv4_proxy_request(DNSBuilder& response, const ClientAddress & client_address, const parser::DNSQuery& query) {
|
||||
auto dn = query.qname();
|
||||
uint8_t resp[4];
|
||||
|
||||
size_t index{0};
|
||||
char* data_ptr = dn.data(), *next_ptr{nullptr};
|
||||
for(; index < 4 && *data_ptr; index++) {
|
||||
resp[index] = strtol(data_ptr, &next_ptr, 10);
|
||||
if(data_ptr == next_ptr)
|
||||
return false;
|
||||
|
||||
if(*next_ptr != '-')
|
||||
break;
|
||||
|
||||
data_ptr = ++next_ptr;
|
||||
}
|
||||
if(index != 3 || *next_ptr != '.') return false;
|
||||
|
||||
log_dns()->info("[{}] Sending requested IPv4 ({}): {}.{}.{}.{}", client_address.str, dn, resp[0], resp[1], resp[2], resp[3]);
|
||||
auto& a = response.push_answer(query.qname());
|
||||
a.set_class(query.qclass());
|
||||
a.set_type(rrtype::A);
|
||||
a.set_ttl(120);
|
||||
a.builder<rrbuilder::A>().set_address(resp);
|
||||
return true;
|
||||
}
|
||||
|
||||
void create_answer(DNSBuilder& response, const ClientAddress & client_address, const parser::DNSQuery& query) {
|
||||
if(query.qclass() != rrclass::IN) return;
|
||||
|
||||
if(query.qtype() == rrtype::A) {
|
||||
auto dn = query.qname();
|
||||
uint8_t resp[4];
|
||||
{
|
||||
size_t index = 0;
|
||||
size_t aindex = 0;
|
||||
do {
|
||||
auto found = dn.find('-', index);
|
||||
auto length = index == -1 ? dn.length() - index : found - index;
|
||||
|
||||
try {
|
||||
resp[aindex] = std::stoul(dn.substr(index, length));
|
||||
} catch(std::exception& ex) {
|
||||
break;
|
||||
}
|
||||
aindex++;
|
||||
index = found;
|
||||
} while(index++ && aindex < 4);
|
||||
if(aindex != 4)
|
||||
return;
|
||||
}
|
||||
|
||||
log_dns()->info("[{}] Sending requested IPv4 ({}): {}.{}.{}.{}", client_address.str, dn, resp[0], resp[1], resp[2], resp[3]);
|
||||
auto& a = response.push_answer(query.qname());
|
||||
a.set_class(query.qclass());
|
||||
a.set_type(rrtype::A);
|
||||
a.set_ttl(120);
|
||||
a.builder<rrbuilder::A>().set_address(resp);
|
||||
if(handle_ipv4_proxy_request(response, client_address, query))
|
||||
return;
|
||||
//Could not find IPv4 address. Lookup for domain?
|
||||
} else if(query.qtype() == rrtype::AAAA) {
|
||||
auto dn = query.qname();
|
||||
const auto parts = parse_dn(dn);
|
||||
|
@ -46,6 +46,7 @@ bool logger::setup(std::string& error) {
|
||||
spdlog::apply_all([](const std::shared_ptr<spdlog::logger>& l) {
|
||||
l->set_level(spdlog::level::trace);
|
||||
});
|
||||
logger_dns->set_level(spdlog::level::info);
|
||||
spdlog::flush_every(std::chrono::seconds(5));
|
||||
return true;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user