Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 108 lines (94 sloc) 3.654 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
namespace :server do
  
  desc "server:convert_file_tlds"
  task :convert_file_tlds do
    list = parse_list_tld
    File.open("lib/whois/definitions/tlds.txt", "w+") do |f|
      f.write(list)
    end
    puts "Created file with #{list.size} servers."
  end

  desc "server:convert_file_ipv4"
  task :convert_file_ipv4 do
    list = parse_list_ipv4
    File.open("lib/whois/definitions/ipv4.txt", "w+") do |f|
      f.write(list)
    end
    puts "Created file with #{list.size} servers."
  end

  desc "server:convert_file_ipv6"
  task :convert_file_ipv6 do
    list = parse_list_ipv6
    File.open("lib/whois/definitions/ipv6.txt", "w+") do |f|
      f.write(list)
    end
    puts "Created file with #{list.size} servers."
  end


  def parse_list_tld
    File.readlines("data/tld_serv_list").map do |line|
      line.chomp!
      line.gsub!(/^\s*(.*)\s*$/, '\1')
      line.gsub!(/\s*#.*$/, '')
      next if line =~ /^$/;
      abort("format error #{line}") unless line =~ /^([\w\d\.-]+)\s+([\w\d\.:-]+|[A-Z]+\s+.*)$/

      extension, instructions = $1, $2
      server, options = case instructions
        when "NONE" then [nil, { :adapter => Whois::Server::Adapters::None }]
        when "ARPA" then [nil, { :adapter => Whois::Server::Adapters::Arpa }]
        when /^WEB (.*)$/ then [nil, { :adapter => Whois::Server::Adapters::Web, :web => $1 }]
        when "CRSNIC" then ["whois.crsnic.net", { :adapter => Whois::Server::Adapters::Verisign }]
        when "PIR" then ["whois.publicinterestregistry.net", { :adapter => Whois::Server::Adapters::Pir }]
        when "AFILIAS" then ["whois.afilias-grs.info", { :adapter => Whois::Server::Adapters::Afilias }]
        when "NICCC" then ["whois.nic.cc", { :adapter => Whois::Server::Adapters::Verisign }]
        else [instructions]
      end

      <<-RUBY
Whois::Server.define :tld, #{extension.inspect}, \
#{server.inspect}\
#{options.nil? ? "" : ", " + options.inspect}
RUBY
    end
  end

  def parse_list_ipv4
    File.readlines("data/ip_del_list").map do |line|
      line.chomp!
      line.gsub!(/^\s*(.*)\s*$/, '\1')
      line.gsub!(/\s*#.*$/, '')
      next if line =~ /^$/;
      abort("format error #{line}") unless line =~ /^([\d\.]+)\/(\d+)\s+([\w\.]+)$/

      range, server = line.split(/[ \t]/)
      server, options = case server
        when /\./ then [server]
        when "UNALLOCATED" then [nil, { :adapter => Whois::Server::Adapters::None }]
        else ["whois.#{server}.net"]
      end

      <<-RUBY
Whois::Server.define :ipv4, #{range.inspect}, \
#{server.inspect}\
#{options.nil? ? "" : ", " + options.inspect}
RUBY
    end
  end

  def parse_list_ipv6
    File.readlines("data/ip6_del_list").map do |line|
      line.chomp!
      line.gsub!(/^\s*(.*)\s*$/, '\1')
      line.gsub!(/\s*#.*$/, '')
      next if line =~ /^$/;
      abort("format error #{line}") unless line =~ %r{^([\da-fA-F]{4}):([\da-fA-F]{1,4})::/(\d+)\s+([\w\.]+)$}

      range = "#{$1}:#{$2}::/#{$3}"
      server = $4
      server, options = case server
        when /\./ then [server]
        when "UNALLOCATED" then [nil, { :adapter => Whois::Server::Adapters::None }]
        when "6to4" then ["6to4", { :adapter => Whois::Server::Adapters::NotImplemented }]
        when "teredo" then ["teredo", { :adapter => Whois::Server::Adapters::NotImplemented }]
        else ["whois.#{server}.net"]
      end

      <<-RUBY
Whois::Server.define :ipv6, #{range.inspect}, \
#{server.inspect}\
#{options.nil? ? "" : ", " + options.inspect}
RUBY
    end
  end

end
Something went wrong with that request. Please try again.