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

def get_domain_root (host)
	puts "Retrieve the root domain for host: #{host}" if @verbose
	begin
     # Comnplete Top Level Domain List - loading once
     @tlds=file_2_hash(File_tld) if @tlds.nil?
		# Generic Top Level Domain List - loading once
		@gtld=file_2_hash(File_gtld) if @gtld.nil?
		# Country code top-level domain list - loading once
		@cctld=file_2_hash(File_cctld) if @cctld.nil?
		# Country code second level domain - loading once
		@ccsld=load_ccsld_from_file(File_ccsld) if @ccsld.nil?

		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
		found_tld=false
		found_cctld=false
		# search the  top level domain list first
		root_domain=""
		dn=host.split(".")
		if @tlds.key?(dn.last)
			cc_found=false
			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
			found_tld=true
		end
		# search the country code top level domain list secondly
		if @cctld.key?(dn.last)
			found=false
			# reverse search of general top level domain
			if @gtld.key?(dn[dn.length-2])
				found=true
			end
			# 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 )
						found=true
						break
					end
				end
				# 1/8/2015: additional logic to handle invalid ccsld string: reserved gtld string
				#unless found
				#	if @gtld.key?(dn[dn.length-2])
				#		puts "Invalid ccsld: #{dn[dn.length-2]} for host: #{host}"
				#		return nil
				#	end
				#end
			end
			if found
				root_domain=dn[dn.length-3] + "." + dn[dn.length-2] + "." + dn.last
			else
				root_domain=dn[dn.length-2] + "." + dn.last
			end
			found_cctld=true
		end
		unless (found_tld or found_cctld)
			puts "#{host} - the top level domain is unknown. Please check out your record #{root_domain} " if @verbose
			return nil
		else
			puts "Domain root found: #{root_domain}" if @verbose
			return root_domain
		end
	rescue => ee
		puts "Exception on method #{__method__}: #{ee}" if @verbose
		return nil
	end
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”



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/wmap/utils/domain_root.rb', line 147

def get_sub_domain (host)
	puts "Retrieve sub-domain from host: #{host}" if @verbose
	begin
		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
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)


132
133
134
135
136
137
138
139
140
141
# File 'lib/wmap/utils/domain_root.rb', line 132

def is_domain_root? (domain)
	puts "Validate the domain name is valid: #{domain}" if @verbose
	begin
		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
end

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



180
181
182
# File 'lib/wmap/utils/domain_root.rb', line 180

def print_ccsld
	puts @ccsld
end

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



175
176
177
# File 'lib/wmap/utils/domain_root.rb', line 175

def print_cctld
	puts @cctld
end

Function to print instance variable - General top level domain list



170
171
172
# File 'lib/wmap/utils/domain_root.rb', line 170

def print_gtld
	puts @gtld
end