@@ -19,9 +19,15 @@ class Query:
1919 refer_regex = r"refer: *(.+)"
2020 whois_server_regex = r".+ whois server: *(.+)"
2121
22- def __init__ (self , proxy_url : str = None , timeout : int = 10 ):
22+ def __init__ (
23+ self ,
24+ proxy_url : str = None ,
25+ timeout : int = 10 ,
26+ find_authoritative_server : bool = True ,
27+ ):
2328 self .proxy_url = proxy_url
2429 self .timeout = timeout
30+ self .find_authoritative_server = find_authoritative_server
2531
2632 @staticmethod
2733 def _find_match (regex : str , blob : str ) -> str :
@@ -117,6 +123,16 @@ def run(self, search_term: str, server: str = None) -> list[str]:
117123 server_regex = self .whois_server_regex
118124 return self ._do_query (server , data , server_regex , [])
119125
126+ @staticmethod
127+ def _continue_querying (current_server : str , next_server : str ) -> bool :
128+ next_server = next_server .lower ()
129+ return (
130+ next_server
131+ and next_server != current_server
132+ and not next_server .startswith ("http" )
133+ and not next_server .startswith ("www." )
134+ )
135+
120136 async def aio_run (self , search_term : str , server : str = None ) -> list [str ]:
121137 data = search_term + "\r \n "
122138 if not server :
@@ -141,19 +157,16 @@ def _do_query(
141157 query_output = self ._send_and_recv (conn , data )
142158 # save query chain
143159 chain .append (query_output )
144- # parse response for the referred WHOIS server name
145- whois_server = self ._find_match (regex , query_output )
146- whois_server = whois_server .lower ()
147- if (
148- whois_server
149- and whois_server != server
150- and not whois_server .startswith ("http" )
151- and not whois_server .startswith ("www." )
152- ):
153- # recursive call to find more authoritative server
154- chain = self ._do_query (
155- whois_server , data , self .whois_server_regex , chain
156- )
160+ # if we should find the authoritative response,
161+ # then parse the response for the next server
162+ if self .find_authoritative_server :
163+ # parse response for the referred WHOIS server name
164+ whois_server = self ._find_match (regex , query_output )
165+ if self ._continue_querying (server , whois_server ):
166+ # recursive call to find more authoritative server
167+ chain = self ._do_query (
168+ whois_server , data , self .whois_server_regex , chain
169+ )
157170 # return the WHOIS query chain
158171 return chain
159172
@@ -171,20 +184,16 @@ async def _aio_do_query(
171184 self ._aio_send_and_recv (reader , writer , data ), self .timeout
172185 )
173186 chain .append (query_output )
174- # parse response for the referred WHOIS server name
175- whois_server = self ._find_match (regex , query_output )
176- whois_server = whois_server .lower ()
177- # check for another legitimate server name
178- if (
179- whois_server
180- and whois_server != server
181- and not whois_server .startswith ("http" )
182- and not whois_server .startswith ("www." )
183- ):
184- # recursive call to find the authoritative server
185- chain = await self ._aio_do_query (
186- whois_server , data , self .whois_server_regex , chain
187- )
187+ # if we should find the authoritative response,
188+ # then parse the response for the next server
189+ if self .find_authoritative_server :
190+ # parse response for the referred WHOIS server name
191+ whois_server = self ._find_match (regex , query_output )
192+ if self ._continue_querying (server , whois_server ):
193+ # recursive call to find more authoritative server
194+ chain = self ._do_query (
195+ whois_server , data , self .whois_server_regex , chain
196+ )
188197 # return the WHOIS query chain
189198 return chain
190199
@@ -195,8 +204,9 @@ def __init__(
195204 server : str = None ,
196205 proxy_url : str = None ,
197206 timeout : int = 10 ,
207+ find_authoritative_server : bool = True ,
198208 ):
199- super ().__init__ (proxy_url , timeout )
209+ super ().__init__ (proxy_url , timeout , find_authoritative_server )
200210 self .server = server
201211
202212 @staticmethod
0 commit comments