1#!/usr/bin/env python
2# Copyright 2009 Google Inc. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""Tool for checking a lot of DNS servers from stdin for possible inclusion."""
17
18__author__ = 'tstromberg@google.com (Thomas Stromberg)'
19
20
21import sys
22import pygeoip
23sys.path.append('..')
24sys.path.append('/Users/tstromberg/namebench')
25import third_party
26from libnamebench import nameserver_list
27from libnamebench import config
28from libnamebench import addr_util
29
30import check_nameserver_popularity
31
32(options, supplied_ns, global_ns, regional_ns) = config.GetConfiguration()
33has_ip = [ x[0] for x in regional_ns ]
34has_ip.extend([ x[0] for x in global_ns ])
35check_ns = []
36
37for line in sys.stdin:
38  ips = addr_util.ExtractIPsFromString(line)
39  for ip in ips:
40    print ip
41    # disable IPV6 by default
42    if ':' in ip:
43      continue
44    if ip not in has_ip:
45      check_ns.append((ip, ip))
46
47if not check_ns:
48  print "no new servers to check"
49  sys.exit(1)
50else:
51  print "%s servers to check" % len(check_ns)
52print '-' * 80
53
54nameservers = nameserver_list.NameServers(
55    check_ns,
56    timeout=8,
57    health_timeout=8,
58    threads=60,
59    skip_cache_collusion_checks=True,
60)
61nameservers.min_healthy_percent = 0
62(primary_checks, secondary_checks, censor_tests) = config.GetLatestSanityChecks()
63try:
64  nameservers.CheckHealth(primary_checks, secondary_checks)
65except nameserver_list.TooFewNameservers:
66  pass
67print '-' * 80
68geo_city = pygeoip.GeoIP('/usr/local/share/GeoLiteCity.dat')
69
70for ns in nameservers:
71  if ':' in ns.ip:
72    details = {}
73  else:
74    try:
75      details = geo_city.record_by_addr(ns.ip)
76    except:
77      pass
78
79  if not details:
80    details = {}
81  city = details.get('city', '')
82  country = details.get('country_name', '')
83  country_code = details.get('country_code', '')
84  region = details.get('region_name', '')
85  results = check_nameserver_popularity.CheckPopularity(ns.ip)
86  urls = [ x['Url'] for x in results ]
87  if urls:
88    print "%s=%s %s %s # %s: %s %s" % (ns.ip, ns.hostname, country_code, city, len(urls),
89                                       ns.warnings_comment, urls[:2])
90