Module: Wmap::Utils::DomainRoot

Extended by:
DomainRoot
Included in:
Wmap::Utils, DomainRoot
Defined in:
lib/wmap/utils/domain_root.rb

Overview

Module to validate and retrieve the top or second level domain name from a host-name (FQDN).

Constant Summary collapse

File_ccsld =

Internet Domain Architecture Definitions

File.dirname(__FILE__)+'/../../../dicts/ccsld.txt'
File_cctld =
File.dirname(__FILE__)+'/../../../dicts/cctld.txt'
File_gtld =
File.dirname(__FILE__)+'/../../../dicts/gtld.txt'
File_tld =
File.dirname(__FILE__)+'/../../../dicts/tlds.txt'

Instance Method Summary collapse

Instance Method Details

#get_domain_root(host) ⇒ Object Also known as: get_root_domain, root_domain, domain_root, host_2_domain

Main function to retrieve the registered domain (‘domain root’ from the ‘registrant’ perspective) from a hostname, for example, “www.telegraph.co.uk” -> “telegraph.co.uk”



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
# File 'lib/wmap/utils/domain_root.rb', line 22

def get_domain_root (host)
	puts "Retrieve the root domain for host: #{host}" if @verbose
	if host.strip.nil?
		puts "Error: empty record found. Please check your input and remove any empty line." if @verbose
		return nil
	else
		host=host.downcase.strip
	end
   # First order - search country code second level domain list
   root_domain = get_domain_root_by_ccsld(host)
   if root_domain.nil?
 		# Second order - search the country code top level domain list
     root_domain = get_domain_root_by_cctld(host)
     if root_domain.nil?
       # Third order - search top level domain list
       root_domain = get_domain_root_by_tlds(host)
       if root_domain.nil?
         # do nothing - no further search
       else
         return root_domain
       end
     else
       return root_domain
     end
   else
     return root_domain
   end
   puts "#{host} - the top level domain is unknown. Please check out your record #{root_domain} " if @verbose
   return nil
#rescue => ee
#	puts "Exception on method #{__method__}: #{ee}" if @verbose
#	return nil
end

#get_domain_root_by_ccsld(host) ⇒ Object

get domain root by lookup Country Code Second Level Domain list



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/wmap/utils/domain_root.rb', line 61

def get_domain_root_by_ccsld(host)
  puts "First order search - domain root lookup by Country Code Second Level Domain list ..." if @verbose
  root_domain = nil
  dn = host.split(".")
  # Country code second level domain - loading once
@ccsld=load_ccsld_from_file(File_ccsld) if @ccsld.nil?
  # search country code second level domain list
  if @ccsld.key?(dn.last)
    @ccsld[dn.last].each do |v|
      if ( v =~ /#{dn[dn.length-2]}/i )
        return dn[dn.length-3] + "." + dn[dn.length-2] + "." + dn.last
      end
    end
  end
  return root_domain
#rescue => ee
	#	puts "Exception on method #{__method__}: #{ee}" if @verbose
	#	return nil
end

#get_domain_root_by_cctld(host) ⇒ Object

get domain root by lookup Country Code Top Level Domain list



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/wmap/utils/domain_root.rb', line 82

def get_domain_root_by_cctld(host)
  puts "Second order search - domain root lookup by Country Code Top Level Domain list ..." if @verbose
  root_domain = nil
  dn = host.split(".")
  # Country code top-level domain list - loading once
  @cctld=file_2_hash(File_cctld) if @cctld.nil?
  # Generic Top Level Domain List - loading once
  @gtld=file_2_hash(File_gtld) if @gtld.nil?
  # Country code second level domain - loading once
@ccsld=load_ccsld_from_file(File_ccsld) if @ccsld.nil?
  # search the country code top level domain list
  if @cctld.key?(dn.last)
    # reverse search of general top level domain
    if @gtld.key?(dn[dn.length-2])
      root_domain=dn[dn.length-3] + "." + dn[dn.length-2] + "." + dn.last
    else
      root_domain=dn[dn.length-2] + "." + dn.last
    end
  end
  return root_domain
#rescue => ee
	#	puts "Exception on method #{__method__}: #{ee}" if @verbose
	#	return nil
end

#get_domain_root_by_tlds(host) ⇒ Object

get domain root by lookup Top Level Domain list



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/wmap/utils/domain_root.rb', line 108

def get_domain_root_by_tlds(host)
  puts "Third order search - domain root lookup by Top Level Domain list ..." if @verbose
  root_domain = nil
  dn = host.split(".")
  # Comnplete Top Level Domain List - loading once
  @tlds=file_2_hash(File_tld) if @tlds.nil?
  # Country code top-level domain list - loading once
  @cctld=file_2_hash(File_cctld) if @cctld.nil?
  cc_found=false
  if @tlds.key?(dn.last)
    if @cctld.key?(dn[dn.length-2])
      cc_found=true
    end
    if cc_found
      root_domain=dn[dn.length-3] + "." + dn[dn.length-2] + "." + dn.last
    else
      root_domain=dn[dn.length-2] + "." + dn.last
    end
  end
  return root_domain
  #rescue => ee
	#	puts "Exception on method #{__method__}: #{ee}" if @verbose
	#	return nil
end

#get_sub_domain(host) ⇒ Object Also known as: get_subdomain

Function to retrieve the sub-domain from a Fully Qualified Domain Name(FQDN), for example, “www.secure.telegraph.co.uk” -> “secure.telegraph.co.uk”



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/wmap/utils/domain_root.rb', line 172

def get_sub_domain (host)
	puts "Retrieve sub-domain from host: #{host}" if @verbose
	subdomain=String.new
	host=host.strip.downcase
	domain=get_domain_root(host)
	record_h=host.split(".")
	record_d=domain.split(".")
	if (record_h.length - record_d.length) >= 2
		subdomain=record_h[record_h.length-record_d.length-1]+"."+domain
		puts "Sub domain found: #{subdomain}" if @verbose
		return subdomain
	else
		return nil
	end
rescue Exception => ee
	puts "Exception on method #{__method__} for #{host}: #{ee}" if @verbose
	return nil
end

#is_domain_root?(domain) ⇒ Boolean Also known as: is_root_domain?, is_domain?, is_root?

Test a host string to see if it’s a valid Internet root domain

Returns:

  • (Boolean)


159
160
161
162
163
164
165
166
# File 'lib/wmap/utils/domain_root.rb', line 159

def is_domain_root? (domain)
  puts "Validate the domain name is valid: #{domain}" if @verbose
	domain=domain.strip.downcase
	return domain == get_domain_root(domain)
rescue => ee
	puts "Exception on method #{__method__} for #{domain}: #{ee}" if @verbose
	return false
end

Function to print instance variable - Country code second-level domain list



205
206
207
208
# File 'lib/wmap/utils/domain_root.rb', line 205

def print_ccsld
	puts @ccsld
   return @ccsld
end

Function to print instance variable - Country code top-level domain list



199
200
201
202
# File 'lib/wmap/utils/domain_root.rb', line 199

def print_cctld
	puts @cctld
   return @cctld
end

Function to print instance variable - General top level domain list



193
194
195
196
# File 'lib/wmap/utils/domain_root.rb', line 193

def print_gtld
	puts @gtld
   return @gtld 
end