PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/whois.rb

https://github.com/pablouesc/Snorby
Ruby | 159 lines | 119 code | 19 blank | 21 comment | 13 complexity | f928c615e08133aee066b8499538014d MD5 | raw file
  1. $:.unshift File.dirname(__FILE__)
  2. require 'socket'
  3. require 'resolv'
  4. require 'ipaddr'
  5. require 'yaml'
  6. require 'whois/server/server'
  7. # Module for manage all Whois Class
  8. module Whois
  9. # Base exception of Whois
  10. class WhoisException < Exception
  11. end
  12. # Exception of Whois who made report a bug
  13. class WhoisExceptionError < WhoisException
  14. def initialize (i)
  15. WhoisException.initialize("Report a bug with error #{i} to http://rubyforge.org/projects/whois/")
  16. end
  17. end
  18. # Class to get all information about Host or IP with the Whois request
  19. class Whois
  20. attr_reader :all
  21. attr_reader :server
  22. attr_reader :ip
  23. attr_reader :host
  24. attr_accessor :host_search
  25. # Initialize with a request. The request must be an IPv4, or a host string
  26. #
  27. # The first params now is :
  28. # * a string which a Ipv4, Ipv6 or a host.
  29. # * A IPAddr instance
  30. #
  31. # A second param, host_search is optionnal. By default he is false.
  32. # If this value is true, a resolv host is made for know the host to this IPv4
  33. def initialize(request, host_search=false)
  34. @host_search = host_search
  35. @host = nil
  36. if request.instance_of? IPAddr
  37. if request.ipv4?
  38. @ip = request
  39. @server = server_ipv4
  40. elsif request.ipv6?
  41. @ip = request
  42. @server = server_ipv6
  43. else
  44. raise WhoisExceptionError.new(1)
  45. end
  46. elsif Resolv::IPv4::Regex =~ request
  47. ipv4_init request
  48. unless @server
  49. raise WhoisException.new("no server found for this IPv4 : #{request}")
  50. end
  51. elsif Resolv::IPv6::Regex =~ request
  52. ipv6_init request
  53. unless @server
  54. raise WhoisException.new("no server found for this Ipv6 : #{request}")
  55. end
  56. else
  57. # Test if the request is an host or not
  58. begin
  59. ip = Resolv.getaddress request
  60. @ip = IPAddr.new ip
  61. @server = server_ipv4
  62. @host = request
  63. rescue Resolv::ResolvError
  64. raise WhoisException.new("host #{request} has no DNS result")
  65. end
  66. end
  67. search_host unless @host
  68. end
  69. # Ask of whois server
  70. def search_whois
  71. s = TCPsocket.open(@server.server, 43)
  72. s.write("#{self.ip.to_s}\n")
  73. ret = ''
  74. while s.gets do ret += $_ end
  75. s.close
  76. @all = ret
  77. end
  78. # Search the host for this IPv4, if the value host_search is true, else host = nil
  79. def search_host
  80. begin
  81. if @host_search
  82. @host = Resolv.getname self.ip.to_s
  83. else
  84. @host = nil
  85. end
  86. rescue Resolv::ResolvError
  87. @host = nil
  88. end
  89. end
  90. private
  91. # Init value for a ipv4 request
  92. def ipv4_init (ip)
  93. @ip = IPAddr.new ip
  94. @server = server_ipv4
  95. end
  96. # Init value for a ipv6 request
  97. def ipv6_init (ip)
  98. @ip = IPAddr.new ip
  99. @server = server_ipv6
  100. end
  101. # Return the Server with the hash of mask
  102. def server_with_hash(ip_hash)
  103. # Sort by mask of Ip Range
  104. arr_tmp = ip_hash.sort{|b,c| c[0][/\/(.+)/, 1].to_i <=> b[0][/\/(.+)/, 1].to_i}
  105. arr_tmp.each do |l|
  106. ip_range = IPAddr.new l[0]
  107. if ip_range.include? self.ip
  108. if l[1].nil? or l[1].empty?
  109. raise WhoisException.new("no server of Whois is define for the IP #{self.ip}. Sorry")
  110. end
  111. return Object.instance_eval("Server::#{l[1]}.new")
  112. end
  113. end
  114. end
  115. # Define the server of Whois in IPC6 list of YAML
  116. def server_ipv6
  117. ipv6_list = YAML::load_file(File.dirname(__FILE__) + '/whois/data/ipv6.yaml')
  118. server = server_with_hash(ipv6_list)
  119. unless server.kind_of? Server::Server
  120. raise WhoisException.new("no server found for this IPv6 : #{self.ip}")
  121. else
  122. return server
  123. end
  124. end
  125. # Define the server of Whois in IPV4 list of YAML
  126. def server_ipv4
  127. ipv4_list = YAML::load_file(File.dirname(__FILE__) + '/whois/data/ipv4.yaml')
  128. server = server_with_hash(ipv4_list)
  129. unless server.kind_of? Server::Server
  130. raise WhoisException.new("no server found for this IPv4 : #{self.ip}")
  131. else
  132. return server
  133. end
  134. end
  135. end
  136. end
  137. if $0 == __FILE__
  138. w = Whois::Whois.new '218.14.221.147'
  139. puts w.search_whois
  140. end