Class: NicInfo::Bootstrap

Inherits:
Object
  • Object
show all
Defined in:
lib/nicinfo/bootstrap.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Bootstrap

Returns a new instance of Bootstrap.



24
25
26
# File 'lib/nicinfo/bootstrap.rb', line 24

def initialize config
  @config = config
end

Instance Method Details

#find_url_by_as(as) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/nicinfo/bootstrap.rb', line 58

def find_url_by_as as
  if as.instance_of? String
    as = as.sub( /^AS/i, '')
    as = as.to_i
  end
  retval = nil
  file = File.join( @config.rdap_bootstrap_dir , NicInfo::ASN_BOOTSTRAP )
  @config.logger.trace "Looking up bootstrap from #{file}"
  data = JSON.parse( File.read( file ) )
  services = get_services data
  services.each do |service|
    service[ 0 ].each do |service_entry|
      numbers = service_entry.split( "-" )
      min = numbers[ 0 ].to_i
      max = numbers[ -1 ].to_i
      if (as >= min ) && (as <= max)
        retval = get_service_url( service[ 1 ] )
        break
      end
    end
  end if services
  retval = @config.config[ NicInfo::BOOTSTRAP ][ NicInfo::AS_ROOT_URL ] if retval == nil
  return retval
end

#find_url_by_domain(domain) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/nicinfo/bootstrap.rb', line 114

def find_url_by_domain domain
  retval = nil
  domain = domain.sub( /\.$/, '' ) #remove trailing dot if there is one
  if domain.end_with?( ".ip6.arpa" )
    addr = get_ip6_by_inaddr domain
    retval = find_url_by_ip addr
  elsif domain.end_with?( ".in-addr.arpa" )
    addr = get_ip4_by_inaddr domain
    retval = find_url_by_ip addr
  else
    retval = find_url_by_forward_domain domain
  end
  return retval
end

#find_url_by_entity(entity_name) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/nicinfo/bootstrap.rb', line 154

def find_url_by_entity entity_name
  retval = nil
  suffix = entity_name.downcase.split( '-' ).last
  if suffix
    file = File.join( @config.rdap_bootstrap_dir , NicInfo::ENTITY_BOOTSTRAP )
    @config.logger.trace "Looking up bootstrap from #{file}"
    data = JSON.parse( File.read( file ) )
    services = get_services data
    services.each do |service|
      service[ 0 ].each do |service_entry|
        if service_entry.downcase == suffix
          retval = get_service_url( service[ 1 ] )
          break
        end
      end
    end if services
  end
  retval = @config.config[ NicInfo::BOOTSTRAP ][ NicInfo::ENTITY_ROOT_URL ] if retval == nil
  return retval
end

#find_url_by_forward_domain(domain) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/nicinfo/bootstrap.rb', line 129

def find_url_by_forward_domain domain
  retval = nil
  file = File.join( @config.rdap_bootstrap_dir , NicInfo::DNS_BOOTSTRAP )
  @config.logger.trace "Looking up bootstrap from #{file}"
  data = JSON.parse( File.read( file ) )
  longest_domain = nil
  longest_urls = nil
  services = get_services data
  services.each do |service|
    service[ 0 ].each do |service_entry|
      if domain.end_with?( service_entry )
        if longest_domain == nil || longest_domain.length < service_entry.length
          longest_domain = service_entry
          longest_urls = service[ 1 ]
        end
      end
    end
  end if services
  if longest_urls != nil
    retval = get_service_url( longest_urls )
  end
  retval = @config.config[ NicInfo::BOOTSTRAP ][ NicInfo::DOMAIN_ROOT_URL ] if retval == nil
  return retval
end

#find_url_by_ip(addr) ⇒ Object



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
# File 'lib/nicinfo/bootstrap.rb', line 29

def find_url_by_ip addr
  retval = nil
  if ! addr.instance_of? IPAddr
    addr = IPAddr.new addr
  end
  file = File.join( @config.rdap_bootstrap_dir , NicInfo::IPV6_BOOTSTRAP ) if addr.ipv6?
  file = File.join( @config.rdap_bootstrap_dir , NicInfo::IPV4_BOOTSTRAP ) if addr.ipv4?
  @config.logger.trace "Looking up bootstrap from #{file}"
  data = JSON.parse( File.read( file ) )
  services = get_services data
  services.each do |service|
    service[ 0 ].each do |service_entry|
      if addr.ipv6?
        prefix = IPAddr.new( service_entry )
      else
        entry = service_entry.split( "/" )[0].to_i.to_s + ".0.0.0"
        prefix = IPAddr.new( entry )
        prefix = prefix.mask( 8 )
      end
      if prefix.include?( addr )
        retval = get_service_url( service[ 1 ] )
        break
      end
    end
  end if services
  retval = @config.config[ NicInfo::BOOTSTRAP ][ NicInfo::IP_ROOT_URL ] if retval == nil
  return retval
end

#get_ip4_by_inaddr(inaddr) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/nicinfo/bootstrap.rb', line 83

def get_ip4_by_inaddr inaddr
  inaddr = inaddr.sub( /\.in\-addr\.arpa\.?/, "")
  a = inaddr.split( "." ).reverse
  ip = Array.new( 4 ).fill( 0 )
  for i in 0..a.length-1 do
    ip[ i ] = a[ i ].to_i
  end
  return IPAddr.new( ip.join( "." ) )
end

#get_ip6_by_inaddr(inaddr) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/nicinfo/bootstrap.rb', line 93

def get_ip6_by_inaddr inaddr
  inaddr = inaddr.sub( /\.ip6\.arpa\.?/, "")
  a = inaddr.split( "." ).reverse.join
  ip = Array.new( 16 ).fill( 0 )
  i = 0
  while i <= a.length-1 do
    ip[ i/2 ] = ( a[ i..i+1 ].to_i(16) )
    i = i +2
  end
  ipstr = ""
  i = 0
  while i <= ip.length-1 do
    ipstr << ("%02X" % ip[i])
    if ((i+1) % 2) == 0
      ipstr << ":" if i != ip.length-1
    end
    i = i +1
  end
  return IPAddr.new( ipstr )
end

#get_service_url(service_url_array) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/nicinfo/bootstrap.rb', line 179

def get_service_url service_url_array
  http_url = nil
  https_url = nil
  service_url_array.each do |url|
    if url.start_with?( "https" )
      https_url = url
    elsif url.start_with?( "http" )
      http_url = url
    end
  end
  if https_url != nil
    return https_url
  end
  #else
  return http_url
end

#get_services(data) ⇒ Object



175
176
177
# File 'lib/nicinfo/bootstrap.rb', line 175

def get_services data
  data["services"]
end