2to3port #2

Merged
benjamyn merged 12 commits from 2to3port into master 2020-10-10 18:48:33 -04:00
3 changed files with 110 additions and 96 deletions
Showing only changes of commit a09941ccae - Show all commits

View File

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

View File

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

View File

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