diff --git a/etc/smartdns/smartdns.conf b/etc/smartdns/smartdns.conf index 4f82ea2063f..58ab2261527 100644 --- a/etc/smartdns/smartdns.conf +++ b/etc/smartdns/smartdns.conf @@ -310,6 +310,8 @@ log-level info # [-d] -dualstack-ip-selection [yes|no]: same as dualstack-ip-selection option # -no-serve-expired: ignore expired domain # -delete: delete domain rule +# -no-ip-alias: ignore ip alias +# -no-cache: ignore cache # collection of domains # the domain-set can be used with /domain/ for address, nameserver, ipset, etc. diff --git a/src/dns_conf.c b/src/dns_conf.c index be4bdaf2e21..915152ed252 100644 --- a/src/dns_conf.c +++ b/src/dns_conf.c @@ -3031,6 +3031,11 @@ static int _conf_domain_rule_no_cache(const char *domain) return _config_domain_rule_flag_set(domain, DOMAIN_FLAG_NO_CACHE, 0); } +static int _conf_domain_rule_no_ipalias(const char *domain) +{ + return _config_domain_rule_flag_set(domain, DOMAIN_FLAG_NO_IPALIAS, 0); +} + static int _conf_domain_rules(void *data, int argc, char *argv[]) { int opt = 0; @@ -3056,6 +3061,7 @@ static int _conf_domain_rules(void *data, int argc, char *argv[]) {"no-serve-expired", no_argument, NULL, 254}, {"delete", no_argument, NULL, 255}, {"no-cache", no_argument, NULL, 256}, + {"no-ip-alias", no_argument, NULL, 257}, {NULL, no_argument, NULL, 0} }; /* clang-format on */ @@ -3211,6 +3217,14 @@ static int _conf_domain_rules(void *data, int argc, char *argv[]) break; } + case 257: { + if (_conf_domain_rule_no_ipalias(domain) != 0) { + tlog(TLOG_ERROR, "set no-ipalias rule failed."); + goto errout; + } + + break; + } default: break; } diff --git a/src/dns_conf.h b/src/dns_conf.h index 8167814f522..553ff71150e 100644 --- a/src/dns_conf.h +++ b/src/dns_conf.h @@ -117,6 +117,7 @@ typedef enum { #define DOMAIN_FLAG_NO_SERVE_EXPIRED (1 << 15) #define DOMAIN_FLAG_CNAME_IGN (1 << 16) #define DOMAIN_FLAG_NO_CACHE (1 << 17) +#define DOMAIN_FLAG_NO_IPALIAS (1 << 18) #define SERVER_FLAG_EXCLUDE_DEFAULT (1 << 0) #define SERVER_FLAG_HITCHHIKING (1 << 1) diff --git a/src/dns_server.c b/src/dns_server.c index d29d6f30475..02e54f08e46 100644 --- a/src/dns_server.c +++ b/src/dns_server.c @@ -321,6 +321,7 @@ struct dns_request { int no_select_possible_ip; int no_cache_cname; int no_cache; + int no_ipalias; int has_cname_loop; }; @@ -2861,7 +2862,7 @@ static int _dns_server_process_ip_rule(struct dns_request *request, unsigned cha return ret; } - if (rule->ip_alias_enable && alias != NULL) { + if (rule->ip_alias_enable && alias != NULL && request->no_ipalias == 0) { *alias = rule->ip_alias; if (alias == NULL) { return 0; @@ -2871,7 +2872,7 @@ static int _dns_server_process_ip_rule(struct dns_request *request, unsigned cha return -1; } - return 0; + return -1; } static int _dns_server_is_adblock_ipv6(const unsigned char addr[16]) @@ -4137,6 +4138,10 @@ static int _dns_server_pre_process_rule_flags(struct dns_request *request) request->no_cache = 1; } + if (flags & DOMAIN_FLAG_NO_IPALIAS) { + request->no_ipalias = 1; + } + if (flags & DOMAIN_FLAG_ADDR_IGN) { /* ignore this domain */ goto skip_soa_out; diff --git a/test/cases/test-ip-alias.cc b/test/cases/test-ip-alias.cc index 4d50761d37d..35c0b4d1aa3 100644 --- a/test/cases/test-ip-alias.cc +++ b/test/cases/test-ip-alias.cc @@ -246,3 +246,73 @@ cache-persist no)"""); EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com"); EXPECT_EQ(client.GetAnswer()[0].GetData(), "ffff::1"); } + +TEST(IPAlias, no_ip_alias) +{ + smartdns::MockServer server_upstream; + smartdns::Server server; + + server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) { + std::string domain = request->domain; + if (request->domain.length() == 0) { + return smartdns::SERVER_REQUEST_ERROR; + } + + if (request->qtype == DNS_T_A) { + unsigned char addr[][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; + dns_add_A(request->response_packet, DNS_RRS_AN, domain.c_str(), 61, addr[0]); + dns_add_A(request->response_packet, DNS_RRS_AN, domain.c_str(), 61, addr[1]); + dns_add_A(request->response_packet, DNS_RRS_AN, domain.c_str(), 61, addr[2]); + } else if (request->qtype == DNS_T_AAAA) { + unsigned char addr[][16] = {{1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {10, 11, 12, 13, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; + dns_add_AAAA(request->response_packet, DNS_RRS_AN, domain.c_str(), 61, addr[0]); + dns_add_AAAA(request->response_packet, DNS_RRS_AN, domain.c_str(), 61, addr[1]); + dns_add_AAAA(request->response_packet, DNS_RRS_AN, domain.c_str(), 61, addr[2]); + } else { + return smartdns::SERVER_REQUEST_ERROR; + } + + request->response_packet->head.rcode = DNS_RC_NOERROR; + return smartdns::SERVER_REQUEST_OK; + }); + + server.MockPing(PING_TYPE_ICMP, "1.2.3.4", 60, 100); + server.MockPing(PING_TYPE_ICMP, "5.6.7.8", 60, 110); + server.MockPing(PING_TYPE_ICMP, "9.10.11.12", 60, 140); + server.MockPing(PING_TYPE_ICMP, "10.10.10.10", 60, 120); + server.MockPing(PING_TYPE_ICMP, "11.11.11.11", 60, 150); + server.MockPing(PING_TYPE_ICMP, "0102:0304:0500::", 60, 100); + server.MockPing(PING_TYPE_ICMP, "0506:0708:0900::", 60, 110); + server.MockPing(PING_TYPE_ICMP, "0a0b:0c0d:0e00::", 60, 140); + server.MockPing(PING_TYPE_ICMP, "ffff::1", 60, 120); + server.MockPing(PING_TYPE_ICMP, "ffff::2", 60, 150); + + server.Start(R"""(bind [::]:60053 +server 127.0.0.1:61053 +log-num 0 +log-console yes +log-level debug +dualstack-ip-selection no +ip-alias 1.2.3.4 10.10.10.10 +ip-alias 5.6.7.8/32 11.11.11.11 +ip-alias 0102::/16 ffff::1 +ip-alias 0506::/16 ffff::2 +domain-rules /a.com/ -no-ip-alias +cache-persist no)"""); + smartdns::Client client; + ASSERT_TRUE(client.Query("a.com", 60053)); + std::cout << client.GetResult() << std::endl; + ASSERT_EQ(client.GetAnswerNum(), 1); + EXPECT_EQ(client.GetStatus(), "NOERROR"); + EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com"); + EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4"); + + ASSERT_TRUE(client.Query("a.com AAAA", 60053)); + std::cout << client.GetResult() << std::endl; + ASSERT_EQ(client.GetAnswerNum(), 1); + EXPECT_EQ(client.GetStatus(), "NOERROR"); + EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com"); + EXPECT_EQ(client.GetAnswer()[0].GetData(), "102:304:500::"); +} \ No newline at end of file