DNSLib
A DNS client library for Dart with full support for UDP, TCP, and DNS-over-HTTPS (DoH) queries.
DNSLib enables direct DNS resolution from Dart applications without relying on platform-native libraries or system commands. It’s ideal for CLI tools, Flutter desktop apps, or network-sensitive environments where low-level DNS control is required.
Features
- Supports both IPv4 and IPv6.
- Works over UDP, TCP, and DoH.
- Fully asynchronous API.
- Minimal and dependency-free core.
- Built-in support for AXFR zone transfers.
- Compatible with Dart CLI, Flutter Desktop, and Flutter Web (DoH only).
Supported DNS record types
A, AAAA, AFSDB, APL, CAA, CDS, CERT, CNAME, DHCID, DLV, DNSKEY, EUI48, EUI64, HINFO, HIP, HTTPS, IPSECKEY, KEY, KX, LOC, MX, NAPTR, NS, NSEC, NSEC3PARAM, RP, SMIMEA, SOA, SRV, SSHFP, SVCB, TA, TXT, URI.
Installation
From the pub.dev repository using
command dart pub add dnslib.
Short example
import 'package:dnslib/dnslib.dart';
// Create query
DNSClient // Returns a Future<List<DNSResponseRecord>>
.query(
domain: 'example.com',
dnsRecordType: DNSRecordTypes.findByName('A'),
dnsServer: DNSServer(host: '8.8.8.8'),
)
.then((records) {
for (DNSResponseRecord record in records)
print(record); // By default print in json format
})
.catchError((error) { // Catch any error here
throw error;
});
You can use a sync method:
final List<DNSResponseRecord> records = await DNSClient.query( // ...
You can see more examples in the example directory and execute this using:
dart run example/tcp.dart
DNSServer
The definition object and properties are:
const DNSServer({
required this.host,
this.port = 53, // Default port
this.protocol = DNSProtocol.udp, // Default protocol
this.path = '/dns-query', // Default path for DoH
this.headers = const { // Default headers for DoH
'Accept': 'application/dns-message',
'Content-Type': 'application/dns-message',
'Connection': 'close',
},
});
Protocols supported by DNSProtocol are udp, tcp and doh.
DNSClient
The definition object and properties are:
class DNSClient {
static Future<List<DNSResponseRecord>> query({
required String domain,
required DNSRecordType dnsRecordType,
required DNSServer dnsServer,
int timeout = 5000, // 5 seconds by default (in milliseconds)
}) async { ...
Notes & Considerations
- DoH (DNS over HTTPS) uses port
443and HTTPS protocol. You must setprotocol: DNSProtocol.dohandport: 443explicitly when using DoH. - AXFR (zone transfer) is supported only over TCP. UDP and DoH do not support AXFR.
- All DNS queries use OPT records for extended responses and support for large payloads over TCP and UDP.
Contributions
This project is open source and under active development. Contributions, bug reports, and suggestions are welcome via GitHub.
Your donation can help sustain this project.
Libraries
- dnslib
- domain/dns_protocol
- domain/dns_record_type
- domain/dns_response_record
- domain/dns_response_record_a
- domain/dns_response_record_aaaa
- domain/dns_response_record_afsdb
- domain/dns_response_record_apl
- domain/dns_response_record_caa
- domain/dns_response_record_cds
- domain/dns_response_record_cert
- domain/dns_response_record_cname
- domain/dns_response_record_dhcid
- domain/dns_response_record_dlv
- domain/dns_response_record_dnskey
- domain/dns_response_record_eui48
- domain/dns_response_record_eui64
- domain/dns_response_record_hinfo
- domain/dns_response_record_hip
- domain/dns_response_record_https
- domain/dns_response_record_ipseckey
- domain/dns_response_record_key
- domain/dns_response_record_kx
- domain/dns_response_record_loc
- domain/dns_response_record_mx
- domain/dns_response_record_naptr
- domain/dns_response_record_ns
- domain/dns_response_record_nsec
- domain/dns_response_record_nsec3param
- domain/dns_response_record_rp
- domain/dns_response_record_smimea
- domain/dns_response_record_soa
- domain/dns_response_record_srv
- domain/dns_response_record_sshfp
- domain/dns_response_record_svcb
- domain/dns_response_record_ta
- domain/dns_response_record_txt
- domain/dns_response_record_uri
- domain/dns_server
- helper/dns
- repository/dns_record_types
- repository/dns_servers
- service/dns_client