Fixed AU and UK

This commit is contained in:
Benjamyn Love 2020-05-19 09:48:54 -04:00
parent 772c0d5779
commit a09941ccae
3 changed files with 110 additions and 96 deletions

View File

@ -1,5 +1,5 @@
server = { server = {
"host": "whois.auda.ltd", "host": "whois.auda.org.au",
"redirect": "\s+Whois Server: (.*)", # Should never apply to .au domains "redirect": "\s+Whois Server: (.*)", # Should never apply to .au domains
} }
@ -17,6 +17,7 @@ parse = {
"EligibilityName": "Eligibility Name:(.+)", "EligibilityName": "Eligibility Name:(.+)",
"EligibilityType": "Eligibility Type:(.+)", "EligibilityType": "Eligibility Type:(.+)",
"EligibilityID": "Eligibility ID:(.+)", "EligibilityID": "Eligibility ID:(.+)",
"RegistrantID": "Registrant ID:(.+)",
"AdminID": "Registrant Contact ID:(.+)", "AdminID": "Registrant Contact ID:(.+)",
"AdminName": "Registrant Contact Name:(.+)", "AdminName": "Registrant Contact Name:(.+)",
"TechName": "Tech Contact Name:(.+)", "TechName": "Tech Contact Name:(.+)",

View File

@ -8,7 +8,7 @@ parse = {
"NotFound": "No match for", "NotFound": "No match for",
"DomainName": "Domain name:\s+(.+|\n)", "DomainName": "Domain name:\s+(.+|\n)",
"Registrar": "Registrar:\s+(.+)", "Registrar": "Registrar:\s+(.+)",
"NameServer": "Name servers:\s+(.+)", "NameServer": "Name servers:\s+(.+?\n\n)",
"Status": "Registration status:\s+(.+)", "Status": "Registration status:\s+(.+)",
"UpdatedDate": "Last updated:\s+(.+)", "UpdatedDate": "Last updated:\s+(.+)",
"CreationDate": "Registered on:\s+(.+)", "CreationDate": "Registered on:\s+(.+)",

View File

@ -1,8 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# __ __ __ __ ______ __ ______ # __ __ __ __ ______ __ ______
#/\ \ _ \ \ /\ \_\ \ /\ __ \ /\ \ /\ ___\ # /\ \ _ \ \ /\ \_\ \ /\ __ \ /\ \ /\ ___\
#\ \ \/ ".\ \\ \ __ \\ \ \/\ \\ \ \\ \___ \ # \ \ \/ ".\ \\ \ __ \\ \ \/\ \\ \ \\ \___ \
# \ \__/".~\_\\ \_\ \_\\ \_____\\ \_\\/\_____\ # \ \__/".~\_\\ \_\ \_\\ \_____\\ \_\\/\_____\
# \/_/ \/_/ \/_/\/_/ \/_____/ \/_/ \/_____/ # \/_/ \/_/ \/_/\/_/ \/_____/ \/_/ \/_____/
@ -11,127 +11,140 @@ import os
import socket import socket
import re import re
import logging import logging
import urllib.request, urllib.parse, urllib.error import urllib.request
import urllib.parse
import urllib.error
from . import flags from . import flags
#import error #import error
#import flags # import flags
class Whois(object): class Whois(object):
def __init__(self, domain, debug=False): def __init__(self, domain, debug=False):
if debug: if debug:
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
logging.debug("__init__: DEBUG is set to True") logging.debug("__init__: DEBUG is set to True")
self.domain = domain self.domain = domain
self.tld = self.domain.split(".")[-1] self.tld = self.domain.split(".")[-1]
self.currPath = os.path.dirname(os.path.realpath(__file__)) self.currPath = os.path.dirname(os.path.realpath(__file__))
self.tldPath = os.path.join(self.currPath, "tlds") self.tldPath = os.path.join(self.currPath, "tlds")
self.tldList = os.listdir(self.tldPath) self.tldList = os.listdir(self.tldPath)
logging.debug("__init__: Setting initial variables.. self.currPath = %s / self.tldPath = %s / self.tldList = %s" logging.debug("__init__: Setting initial variables.. self.currPath = %s / self.tldPath = %s / self.tldList = %s"
%(self.currPath, self.tldPath, self.tldList)) % (self.currPath, self.tldPath, self.tldList))
self.settings = {} self.settings = {}
if self.tld in self.tldList: if self.tld in self.tldList:
logging.debug("__init__: Loading tld configuration file...") logging.debug("__init__: Loading tld configuration file...")
_settings = {} _settings = {}
exec(compile(open(os.path.join(self.tldPath, self.tld)).read(), os.path.join(self.tldPath, self.tld), 'exec'), {}, _settings) exec(compile(open(os.path.join(self.tldPath, self.tld)).read(),
os.path.join(self.tldPath, self.tld), 'exec'), {}, _settings)
if "server" in _settings: if "server" in _settings:
logging.debug("__init__: Settings: %s"%(_settings["server"])) logging.debug("__init__: Settings: %s" % (_settings["server"]))
self.settings.update(_settings["server"]) self.settings.update(_settings["server"])
else: else:
logging.debug("__init__: No server settings found") logging.debug("__init__: No server settings found")
def chooseServer(self): def chooseServer(self):
'''Choose whois server by detecting tld of given domain.''' '''Choose whois server by detecting tld of given domain.'''
if "host" in self.settings: if "host" in self.settings:
logging.debug("chooseServer: Whois server addr: %s"%(self.settings["host"])) logging.debug("chooseServer: Whois server addr: %s" %
return self.settings["host"] (self.settings["host"]))
else: return self.settings["host"]
logging.debug("chooseServer: Whois server addr: %s"%(self.tld + ".whois-servers.net")) else:
return self.tld + ".whois-servers.net" logging.debug("chooseServer: Whois server addr: %s" %
(self.tld + ".whois-servers.net"))
return self.tld + ".whois-servers.net"
def sendHTTPQuery(self, whoisServer): def sendHTTPQuery(self, whoisServer):
param = urllib.parse.urlencode({self.settings["http-arg"]: self.domain}) param = urllib.parse.urlencode(
{self.settings["http-arg"]: self.domain})
if self.settings.get("http-method").lower() == "post": if self.settings.get("http-method").lower() == "post":
logging.debug("sendHTTPQuery: Connecting to whois server using POST") logging.debug(
req = urllib.request.Request(whoisServer, param) "sendHTTPQuery: Connecting to whois server using POST")
else: # GET req = urllib.request.Request(whoisServer, param)
logging.debug("sendHTTPQuery: Connecting to whois server using GET") else: # GET
req = urllib.request.Request((whoisServer.endswith("?") and whoisServer or whoisServer+"?") + param) logging.debug(
"sendHTTPQuery: Connecting to whois server using GET")
req = urllib.request.Request((whoisServer.endswith(
"?") and whoisServer or whoisServer+"?") + param)
data = urllib.request.urlopen(req).read() data = urllib.request.urlopen(req).read()
print(data) print(data)
return data return data
def sendQuery(self, whoisServer): def sendQuery(self, whoisServer):
'''Send query to whois server.''' '''Send query to whois server.'''
logging.debug("sendQuery: Connecting to whois server") logging.debug("sendQuery: Connecting to whois server")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try: try:
s.connect((whoisServer, 43)) s.connect((whoisServer, 43))
except: except:
# FIXME: Create a exception class for this # FIXME: Create a exception class for this
logging.error("sendQuery: Error connecting to whois server %s"%(whoisServer)) logging.error(
return False "sendQuery: Error connecting to whois server %s" % (whoisServer))
return False
try: try:
msg = self.settings['format'][whoisServer].replace("%DOMAIN%", self.domain) + "\r\n" msg = self.settings['format'][whoisServer].replace(
"%DOMAIN%", self.domain) + "\r\n"
except: except:
msg = self.domain + "\r\n" msg = self.domain + "\r\n"
logging.debug("sendQuery: Sending data.. %s"%(msg)) logging.debug("sendQuery: Sending data.. %s" % (msg))
# print(msg.encode()) # print(msg.encode())
s.send(msg.encode('utf-8')) s.send(msg.encode('utf-8'))
result = "" result = ""
while True: while True:
buffer = s.recv(512) buffer = s.recv(512)
if not buffer: if not buffer:
break break
result += buffer.decode() result += buffer.decode()
finalResult = result.replace("\r\n", "\n") finalResult = result.replace("\r\n", "\n")
logging.debug("sendQuery: result: %s"%(finalResult)) logging.debug("sendQuery: result: %s" % (finalResult))
return finalResult return finalResult
def query(self, redirect=True, return_type=flags.RETURN_TYPE_LIST): def query(self, redirect=True, return_type=flags.RETURN_TYPE_LIST):
'''Start whole process of whois query. This method will do them all.''' '''Start whole process of whois query. This method will do them all.'''
whoisServer = self.chooseServer() whoisServer = self.chooseServer()
if self.settings.get("method") == "http": if self.settings.get("method") == "http":
result = self.sendHTTPQuery(whoisServer) result = self.sendHTTPQuery(whoisServer)
else: else:
result = self.sendQuery(whoisServer) result = self.sendQuery(whoisServer)
if redirect and "redirect" in self.settings: if redirect and "redirect" in self.settings:
logging.debug("query: Redirection found. Connecting to given server address") logging.debug(
"query: Redirection found. Connecting to given server address")
redirection = re.findall(self.settings["redirect"], result, re.MULTILINE) redirection = re.findall(
self.settings["redirect"], result, re.MULTILINE)
while redirection and len(redirection) >= 1: while redirection and len(redirection) >= 1:
whoisServer = redirection[0] whoisServer = redirection[0]
result = self.sendQuery(whoisServer) result = self.sendQuery(whoisServer)
redirection = re.findall(self.settings["redirect"], result) redirection = re.findall(self.settings["redirect"], result)
if return_type == flags.RETURN_TYPE_LIST:
if return_type == flags.RETURN_TYPE_LIST: return whoisServer, result
return whoisServer, result else:
else: return {"whoisServer": whoisServer, "result": result}
return {"whoisServer": whoisServer, "result": result}