Giter VIP home page Giter VIP logo

durian.cr's Introduction

Durian.cr - Domain Name System Resolver

Description

  • Because Crystal Domain Name Resolver uses C.getaddrinfo, It seems to have serious problems.
  • Of course, Crystal official is always busy, it will not help you solve these problems.
    • Thanks to rdp for the help, this made me sure that the problem was caused by C.getaddrinfo.
  • Then I discovered the CrDNS repository, but it too broken, so I gave up the idea.
    • I started looking at many documents and started researching how to build a DNS resolver.
    • It took me some time to make it, it's not troublesome, it's not easy.
  • That's it, Thanks for using, If you encounter any problems, please let me know.

Features

  • If sending fails, it will try to resend through the next DNS server.
  • It supports Querying / Receiving multiple DNS record Types.
    • AAAA
    • A
    • NS
    • PTR
    • SOA
    • TXT
    • MX
    • CNAME
    • DNAME
    • SRV
  • It supports TCP and UDP protocols DNS server.
  • It does not contain any C.getaddrinfo.
  • It supports simple DNS caching (non-LRUCache).
    • (tapCount + updatedAt) custom Cache.
  • You can send / receive packets via Resolver.
    • or you can process packets via Packet.from_io, Packet.to_io.
  • ...

Tips

  • If the connection fails or there is no response packet, it will try to use the next server.
  • C.getaddrinfo is incompatible with green threads, It may cause your program to pause.
    • C.getaddrinfo is too bad, you should not use it in green thread.
    • (libuv uv_getaddrinfo, libevent evdns_getaddrinfo) is too complicated, you may encounter many problems.

Next

  • Support Alias & Mapping and Special DNS Server.
  • Support response packet to_io operation.
  • More exception handling.
  • Support DNS server features.
  • Better performance, Better DNS cache.
  • Supported DNS over TLS (DoT) feature.

Usage

  • Client | Http - Testing DNS resolution for IP availability.
require "durian"

servers = [] of Durian::Resolver::Server
servers << Durian::Resolver::Server.new ipAddress: Socket::IPAddress.new("8.8.8.8", 53_i32), protocol: Durian::Protocol::UDP
servers << Durian::Resolver::Server.new ipAddress: Socket::IPAddress.new("1.1.1.1", 53_i32), protocol: Durian::Protocol::UDP

buffer = uninitialized UInt8[4096_i32]
resolver = Durian::Resolver.new servers
resolver.ip_cache = Durian::Cache::IPAddress.new

begin
  socket = Durian::TCPSocket.connect "www.example.com", 80_i32, resolver, 5_i32
  socket.read_timeout = 5_i32
  socket.write_timeout = 5_i32
rescue
  abort "Connect Failed"
end

begin
  socket << "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: close\r\n\r\n"
rescue
  abort "Write Failed"
end

begin
  length = socket.read buffer.to_slice
rescue
  abort "Read Failed"
end

STDOUT.puts [length, String.new buffer.to_slice[0_i32, length]]
  • Client | Query - A similar React Proc usage.
require "durian"

servers = [] of Durian::Resolver::Server
servers << Durian::Resolver::Server.new ipAddress: Socket::IPAddress.new("8.8.8.8", 53_i32), protocol: Durian::Protocol::UDP
servers << Durian::Resolver::Server.new ipAddress: Socket::IPAddress.new("1.1.1.1", 53_i32), protocol: Durian::Protocol::UDP

resolver = Durian::Resolver.new servers
resolver.record_cache = Durian::Cache::Record.new

resolver.resolve "google.com", [Durian::RecordFlag::A, Durian::RecordFlag::AAAA] do |response|
  STDOUT.puts [:Google, Time.utc, response]
end

resolver.resolve "twitter.com", Durian::RecordFlag::SOA do |response|
  STDOUT.puts [:Twitter, Time.utc, response]
end

resolver.resolve "facebook.com", [Durian::RecordFlag::A, Durian::RecordFlag::AAAA] do |response|
  STDOUT.puts [:FaceBook, Time.utc, response]
end

resolver.resolve "twitter.com", Durian::RecordFlag::SOA do |response|
  STDOUT.puts [:Twitter, Time.utc, response]
end

resolver.run
  • Client | Packet - from_io, to_io usage.
require "durian"

buffer = uninitialized UInt8[4096_i32]

request = Durian::Packet.new Durian::Protocol::UDP, Durian::Packet::QRFlag::Query
request.add_query "www.example.com", Durian::RecordFlag::A

_request = IO::Memory.new request.to_slice
STDOUT.puts [:Request, Durian::Packet.from_io Durian::Protocol::UDP, _request]

udp_socket = UDPSocket.new
udp_socket.connect Socket::IPAddress.new "8.8.8.8", 53_i32
udp_socket.send _request.to_slice
length, ip_address = udp_socket.receive buffer.to_slice

_response = IO::Memory.new buffer.to_slice[0_i32, length]
STDOUT.puts [:Response, Durian::Packet.from_io Durian::Protocol::UDP, _response]

Used as Shard

Add this to your application's shard.yml:

dependencies:
  durian:
    github: 636f7374/durian.cr

Installation

$ git clone https://github.com/636f7374/durian.cr.git

Development

$ make test

References

Related

Credit

Contributors

Name Creator Maintainer Contributor
636f7374
rdp
teknomunk
ilmanzo
yunixon
z64

License

  • MIT License

durian.cr's People

Contributors

636f7374 avatar ilmanzo avatar kalinon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

durian.cr's Issues

Cannot resolve specific host

I have run into an issue attempting to use durian to resolve api.cognitive.microsoft.com. stdlib and other utilities can access and interact with this host fine, so it appears to be some issue with durian.. or perhaps I've misconfigured something?

I'm not that knowledgeable about DNS, so your help is appreciated.
I have example you can try here that fails: https://git.sr.ht/~z64/durian-issue-4/tree

Output is:

got std socket: #<TCPSocket:0x7fa845b91d10>
Unhandled exception: Invalid host address (Socket::Error)
  from lib/durian/src/durian/resolver.cr:223:17 in 'get_tcp_socket!'
  from src/durian-issue-4.cr:19:7 in '__crystal_main'
  from ../../../../../usr/lib/crystal/crystal/main.cr:105:5 in 'main_user_code'
  from ../../../../../usr/lib/crystal/crystal/main.cr:91:7 in 'main'
  from ../../../../../usr/lib/crystal/crystal/main.cr:114:3 in 'main'
  from __libc_start_main
  from _start
  from ???

Bug: Cache timeout under contention

Using the demo code here:

https://paste.sr.ht/~z64/05e1209fea4e2d465b75e232720fba1ca72c96f8

Produces output:

$ cr _test.cr google.com 5
...................................................
  0 | 48.33ms
  1 | 48.34ms
  2 | 5024.24ms
  3 | 5024.25ms
  4 | 5024.25ms

because the domain is never removed from the pending list once it is resolved.

The following patch resolves this problem: https://git.sr.ht/~z64/durian.cr/commit/663ca66477d80c2ab8757d8f51669d6dd0e7e328

$ cr _test.cr google.com 5
.
  0 | 51.47ms
  1 | 51.48ms
  2 | 51.48ms
  3 | 51.48ms
  4 | 51.48ms

Please feel free to apply this patch or resolve at your discretion.

Error: undefined method 'connect' for TCPSocket.class

In lib/durian/src/durian/extra/http/client.cr:26:15

26 | TCPSocket.connect hostname, @PORT, resolver, @connect_timeout
^------
Error: undefined method 'connect' for TCPSocket.class

crystal: 0.35.1

durian:
    git: https://github.com/636f7374/durian.cr.git
    version: 0.1.4

Rework for Durian.cr

Summary

  • Durian.cr Domain Name System Resolver features has been very complete.
  • However, the code of this repository is very messy and needs to be polished.
  • This repository has been two years old, and the processing logic has undergone tremendous changes in some places.
  • So, I think it is time to rework this repository.
  • There may be breaking changes. (Updated: Feb 10, 2021)

SRV record type support

SRV record resolving would help me a lot with a project I work on. I'm not sure how hard it is to implement, since I don't really have any idea how dns works below the surface level. Will you get to it at some point in the future or would it be more promising for me to read into it and try to implement it myself?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.