class Wmap::DomainTracker

Class to track the known (trusted) Internet domains

Attributes

data_dir[RW]
domains_file[RW]
known_internet_domains[RW]
max_parallel[RW]
verbose[RW]

Public Class Methods

new(params = {}) click to toggle source

Set default instance variables

# File lib/wmap/domain_tracker.rb, line 20
def initialize (params = {})
        # Initialize the instance variables
        @verbose=params.fetch(:verbose, false)
        @data_dir=params.fetch(:data_dir, File.dirname(__FILE__)+'/../../data/')
        Dir.mkdir(@data_dir) unless Dir.exist?(@data_dir)
        @domains_file=params.fetch(:domains_file, @data_dir+'domains')
        @max_parallel=params.fetch(:max_parallel, 40)
        # Hash table to hold the trusted domains
        File.write(@domains_file, "") unless File.exist?(@domains_file)
        load_domains_from_file(@domains_file)
end

Public Instance Methods

add(host) click to toggle source

'setter' to add domain entry to the cache one at a time

# File lib/wmap/domain_tracker.rb, line 101
def add(host)
        puts "Add entry to the local domains cache table: #{host}" if @verbose
        return nil if host.nil? or host.empty?
        host=host.strip.downcase
        if @known_internet_domains.key?(host)
                puts "Domain is already exist. Skipping: #{host}"
        else
                root=get_domain_root(host)
                sub=get_subdomain(host)
                record=Hash.new
                if host == root
                        if zone_transferable?(root)
                                record[root]=true
                                #@known_internet_domains[root]=true
                        else
                                record[root]=false
                                #@known_internet_domains[root]=false
                        end
                        puts "Entry loaded: #{record}"
                        @known_internet_domains.merge!(record)
                        return record
                elsif sub.nil?                               # 2/10/2014, additional logic to support sub-domains
                        # do nothing
                elsif host != sub
                        if zone_transferable?(sub)
                                #@known_internet_domains[sub]=true
                                record[sub]=true
                        else
                                #@known_internet_domains[sub]=false
                                record[sub]=false
                        end
                        puts "Entry loaded: #{record}"
                        @known_internet_domains.merge!(record)
                        return record
                else
                        puts "Problem add domain #{host}: unknown domain format - please use legal root domain or sub domain only."
                end
        end
rescue => ee
        puts "Exception on method #{__method__}: #{ee}" if @verbose
end
adds(list, num=@max_parallel)
Alias for: bulk_add
bulk_add(list, num=@max_parallel) click to toggle source

'setter' to add domain entry to the cache in batch (from a list)

# File lib/wmap/domain_tracker.rb, line 155
def bulk_add(list, num=@max_parallel)
        puts "Add entries to the local domains cache table from list: #{list}" if @verbose
        results=Hash.new
        domains=list
        if domains.size > 0
                Parallel.map(list, :in_processes => num) { |target|
                        add(target)
                }.each do |process|
                        if process.nil?
                                next
                        elsif process.empty?
                                #do nothing
                        else
                                results.merge!(process)
                        end
                end
                @known_internet_domains.merge!(results)
                puts "Done loading domain entries."
                return results
        else
                puts "Error: no entry is loaded. Please check your list and try again."
        end
        return results
rescue => ee
        puts "Exception on method #{__method__}: #{ee}" if @verbose
end
Also aliased as: adds
bulk_delete(list) click to toggle source

'setter' to delete domain entry to the cache in batch (from a list)

# File lib/wmap/domain_tracker.rb, line 199
def bulk_delete(list)
        puts "Delete entries to the local domains cache table from list: #{list}" if @verbose
        domains=list
        changes=Array.new
        if domains.size > 0
                domains.map do |x|
                        domain=delete(x)
                        changes.push(domain) unless domain.nil?
                end
                puts "Done deleting domains from list: #{list}"
                return changes
        else
                puts "Exception on method bulk_delete: no entry is loaded. Please check your list and try again."
        end
rescue => ee
        puts "Exception on method #{__method__}: #{ee}" if @verbose
end
Also aliased as: dels
count() click to toggle source

Count numbers of entries in the domain cache table

# File lib/wmap/domain_tracker.rb, line 85
def count
        puts "Counting number of entries in the domain cache table ..."
        cnt=0
        @known_internet_domains.map do |key|
                unless key =~ /\w+\.\w+/
                        cnt=cnt+1
                end
        end
        puts "Current number of entries: #{cnt}"
        return cnt
rescue => ee
        puts "Exception on method #{__method__}: #{ee}" if @verbose
end
Also aliased as: size
delete(domain) click to toggle source

'setter' to remove entry from the cache one at a time

# File lib/wmap/domain_tracker.rb, line 184
def delete(domain)
        puts "Remove entry from the domains cache table: #{domain} " if @verbose
        domain=domain.strip.downcase
        if @known_internet_domains.key?(domain)
                @known_internet_domains.delete(domain)
                puts "Entry cleared: #{domain}"
                return domain
        else
                puts "Entry not fund. Skipping: #{domain}"
        end
rescue => ee
        puts "Exception on method #{__method__}: #{ee}" if @verbose
end
delete_all() click to toggle source

'setter' to remove all entries from the store

# File lib/wmap/domain_tracker.rb, line 229
def delete_all
        puts "Delete all entries in the domain store! " if @verbose
        @known_internet_domains.keys.map do |domain|
                delete(domain)
        end
rescue => ee
        puts "Exception on method #{__method__}: #{ee}" if @verbose
end
dels(list)
Alias for: bulk_delete
domain_known?(domain) click to toggle source

Simple method to check if a domain is already within the domain cache table

# File lib/wmap/domain_tracker.rb, line 256
def domain_known?(domain)
        #abort "Trusted Internet domain file not loaded properly! " if @known_internet_domains.nil? or @known_internet_sub_domains.nil?
        domain=domain.strip.downcase unless domain.nil?
        case self.class.name
        when "Wmap::DomainTracker"
                return @known_internet_domains.key?(domain)
        when "Wmap::DomainTracker::SubDomain"
                return @known_internet_sub_domains.key?(domain)
        else
                return false
        end
rescue => ee
        puts "Exception on method #{__method__}: #{ee}" if @verbose
        return false
end
Also aliased as: is_known?, is_domain_known?
dump()
Alias for: get_domains
dump_domains()
Alias for: get_domains
file_add(file) click to toggle source

'setter' to add domain entry to the cache in batch (from a file)

# File lib/wmap/domain_tracker.rb, line 144
def file_add(file)
        puts "Add entries to the local domains cache table from file: #{file}" if @verbose
        raise "File non-exist. Please check your file path and name again: #{file}" unless File.exist?(file)
        changes=Array.new
        domains=file_2_list(file)
        changes=bulk_add(domains)
rescue => ee
        puts "Exception on method #{__method__}: #{ee}" if @verbose
end
file_delete(file) click to toggle source

'setter' to delete domain entry to the cache in batch (from a file)

# File lib/wmap/domain_tracker.rb, line 219
def file_delete(file)
        puts "Delete entries to the local domains cache table from file: #{file}" if @verbose
        raise "File non-exist. Please check your file path and name again: #{file}" unless File.exist?(file)
        domains=file_2_list(file)
        changes=bulk_delete(domains)
rescue => ee
        puts "Exception on method #{__method__}: #{ee}" if @verbose
end
find(pattern)
Alias for: search
get_domains() click to toggle source

Dump out the list of known domains

# File lib/wmap/domain_tracker.rb, line 275
def get_domains
        puts "Retrieve a list of known domain ..." if @verbose
        return @known_internet_domains.keys
rescue Exception => ee
        puts "Exception on method #{__method__}: #{ee}" if @verbose
        return nil
end
Also aliased as: dump_domains, dump
is_domain_known?(domain)
Alias for: domain_known?
is_known?(domain)
Alias for: domain_known?
load_domains_from_file(file=@domains_file, lc=true) click to toggle source

'setter' to load the known Internet domains into an instance variable

# File lib/wmap/domain_tracker.rb, line 33
def load_domains_from_file (file=@domains_file, lc=true)
        puts "Loading trusted domain file: #{file}"   if @verbose
        @known_internet_domains=Hash.new
        f_domains=File.open(file, 'r')
        f_domains.each_line do |line|
                puts "Processing line: #{line}" if @verbose
                line=line.chomp.strip
                next if line.nil?
                next if line.empty?
                next if line =~ /^\s*#/
                line=line.downcase if lc==true
                entry=line.split(',')
                if @known_internet_domains.key?(entry[0])
                        next
                else
                        if entry[1] =~ /yes/i
                                @known_internet_domains[entry[0]]=true
                        else
                                @known_internet_domains[entry[0]]=false
                        end
                end

        end
        f_domains.close
        return @known_internet_domains
rescue => ee
        puts "Exception on method #{__method__}: #{ee}" if @verbose
        return nil
end
print()
Alias for: print_known_domains
print_known_domains() click to toggle source

Print summary report on all known / trust domains in the domain cache table

Also aliased as: print
refresh(domain) click to toggle source

Refresh the domain entry one at a time

# File lib/wmap/domain_tracker.rb, line 239
def refresh(domain)
        abort "Trusted Internet domain file not loaded properly! " if @known_internet_domains.nil?
        domain=domain.strip.downcase unless domain.nil?
        if domain_known?(domain)
                delete(domain)
                add(domain)
                return domain
        else
                puts "Unknown domain: #{domain}"
                return nil
        end
rescue => ee
        puts "Exception on method #{__method__} for #{domain}: #{ee}" if @verbose
        return nil
end
save!(file_domains=@domains_file, domains=@known_internet_domains)
save_domains_to_file!(file_domains=@domains_file, domains=@known_internet_domains) click to toggle source

Save the current domain hash table into a file

# File lib/wmap/domain_tracker.rb, line 64
def save_domains_to_file!(file_domains=@domains_file, domains=@known_internet_domains)
        puts "Saving the current domains cache table from memory to file: #{file_domains} ..." if @verbose
        timestamp=Time.now
        f=File.open(file_domains, 'w')
        f.write "# Local domains file created by class #{self.class} method #{__method__} at: #{timestamp}\n"
        f.write "# domain name, free zone transfer detected?\n"
        domains.keys.sort.map do |key|
                if domains[key]
                        f.write "#{key}, yes\n"
                else
                        f.write "#{key}, no\n"
                end
        end
        f.close
        puts "Domain cache table is successfully saved: #{file_domains}"
rescue => ee
        puts "Exception on method #{__method__}: #{ee}" if @verbose
end
Also aliased as: save!
size()
Alias for: count