[Pharo-users] DNS over HTTPS (DoH) in Pharo

Sven Van Caekenberghe sven at stfx.eu
Tue Feb 25 15:12:06 EST 2020


Hi,

Today Firefox switched over to using 'DNS over HTTPS (DoH)' by default.

  https://blog.mozilla.org/netpolicy/2020/02/25/the-facts-mozillas-dns-over-https-doh/

We can do this in Pharo as well, even out of the box (minus the interpretation of the results, but still).

First, what is this ? A good description is:

  https://developers.cloudflare.com/1.1.1.1/dns-over-https/

Using the Cloudflare server, we can do the following in Pharo, using the JSON wire format.

ZnClient new
  url: 'https://cloudflare-dns.com/dns-query';
  accept: 'application/dns-json';
  queryAt: #name put: 'pharo.org';
  queryAt: #type put: 'A';
  contentReader: [ :entity | STONJSON fromString: entity contents ];
  get.

The actual address can be accessed inside the returned result.

SocketAddress fromDottedString: (((ZnClient new
  url: 'https://cloudflare-dns.com/dns-query';
  accept: 'application/dns-json';
  queryAt: #name put: 'pharo.org';
  queryAt: #type put: 'A';
  contentReader: [ :entity | STONJSON fromString: entity contents ];
  get) at: #Answer) first at: #data).

If you load the following code,

  https://github.com/svenvc/NeoDNS

it is just as easy to use the binary UDP wire format.

ZnClient new
  url: 'https://cloudflare-dns.com/dns-query';
  accept: 'application/dns-message';
  contentWriter: [ :message | 
    ZnEntity with: message asByteArray type: 'application/dns-message' ];
  contentReader: [ :entity | 
    DNSMessage readFrom: entity readStream ];
  contents: (DNSMessage addressByName: 'pharo.org');
  post.

Again, the actual address can be accessed inside the returned object.

(ZnClient new
   url: 'https://cloudflare-dns.com/dns-query';
   accept: 'application/dns-message';
   contentWriter: [ :message |
     ZnEntity with: message asByteArray type: 'application/dns-message' ];
   contentReader: [ :entity |
     DNSMessage readFrom: entity readStream ];
   contents: (DNSMessage addressByName: 'pharo.org');
   post) answers first address.

Incidentally, a more robust answer can be got as follows:

  NeoSimplifiedDNSClient default addressForName: 'pharo.org'.

Sven




More information about the Pharo-users mailing list