8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DNSでズンドコ

Last updated at Posted at 2016-03-30

ズンドコウェーブにのってインフラエンジニアらしくズンドコネームサーバを作ってみました。

#仕様
*.zunの応答は127.0.0.1
*.dokoの応答は
・直前の.zunドメインへの問い合わせが4回以上連続した場合CNAMEとして*.kiyosiを返す
・それ以外127.0.0.1

$ dig foo.zun
127.0.0.1

$ dig foo.doko
foo.kiyoshi

みたいな感じ

#準備
rubyの標準クラスではDNSサーバ機能がないためrubydnsを利用します。

$ gem install rubydns

コード

zundokoNameSystem.rb
require 'resolv'
require 'rubydns'

Interfaces = [
    [:udp, "0.0.0.0", 5300],
    [:tcp, "0.0.0.0", 5300]
]
zun_counter = 0

RubyDNS::run_server(:listen =>Interfaces) do
  match(/\.zun$/, Resolv::DNS::Resource::IN::A) do |transaction|
    zun_counter += 1
    transaction.respond!("127.0.0.1", resource_class: Resolv::DNS::Resource::IN::A)
  end

  match(/.doko$/, Resolv::DNS::Resource::IN::A) do |transaction|
    cname= transaction.name.gsub(/doko$/, "kiyoshi")
    if zun_counter >= 4
       transaction.respond!(Resolv::DNS::Name.create(cname), resource_class: Resolv::DNS::Resource::IN::CNAME)
    else
       transaction.respond!("127.0.0.1", resource_class: Resolv::DNS::Resource::IN::A)
    end
    zun_counter = 0
  end

  otherwise do |transaction|
    transaction.fail!(:NXDomain)
    zun_counter = 0
  end
end

実行

サーバ起動

$ ruby zundokoNameSystem.rb

5300ポートで起動させているのでクライアント側でポートを指定してます。

$ dig @127.0.0.1 -p 5300 +short foo.zun
127.0.0.1
$ dig @127.0.0.1 -p 5300 +short foo.zun
127.0.0.1
$ dig @127.0.0.1 -p 5300 +short foo.zun
127.0.0.1
$ dig @127.0.0.1 -p 5300 +short foo.zun
127.0.0.1
$ dig @127.0.0.1 -p 5300 +short foo.zun
127.0.0.1
$ dig @127.0.0.1 -p 5300 +short foo.doko
foo.kiyoshi.

日本語ドメイン対応は春休みの宿題ですw

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?