diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b18af2..5ed154b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 4308352..5757d50 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -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) \ No newline at end of file +target_link_libraries(TeaWebDNS libevent::core libevent::pthreads stdc++fs pthread teadns::parser spdlog::spdlog) \ No newline at end of file diff --git a/server/src/handler.cpp b/server/src/handler.cpp index 28e51e0..04d73e2 100644 --- a/server/src/handler.cpp +++ b/server/src/handler.cpp @@ -80,7 +80,7 @@ void WebDNSHandler::handle_message(const std::shared_ptr& 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().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().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); diff --git a/server/src/logger.cpp b/server/src/logger.cpp index 0ed2766..48ee2c3 100644 --- a/server/src/logger.cpp +++ b/server/src/logger.cpp @@ -46,6 +46,7 @@ bool logger::setup(std::string& error) { spdlog::apply_all([](const std::shared_ptr& l) { l->set_level(spdlog::level::trace); }); + logger_dns->set_level(spdlog::level::info); spdlog::flush_every(std::chrono::seconds(5)); return true; }