new: switch to return emails only on whois query

pull/564/head
Raphaël Vinot 2022-12-26 23:01:44 +01:00
parent 479fcb39e5
commit 1abd97f37a
2 changed files with 29 additions and 8 deletions

View File

@ -1,8 +1,10 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import logging import logging
import re
import socket import socket
from typing import Any, Dict
from typing import Any, Dict, overload, Literal, List, Union
from har2tree import CrawledTree, Har2TreeError, HostNode from har2tree import CrawledTree, Har2TreeError, HostNode
@ -35,9 +37,11 @@ class UniversalWhois():
def query_whois_hostnode(self, hostnode: HostNode) -> None: def query_whois_hostnode(self, hostnode: HostNode) -> None:
if hasattr(hostnode, 'resolved_ips'): if hasattr(hostnode, 'resolved_ips'):
ip: str
for ip in hostnode.resolved_ips: for ip in hostnode.resolved_ips:
self.whois(ip) self.whois(ip)
if hasattr(hostnode, 'cnames'): if hasattr(hostnode, 'cnames'):
cname: str
for cname in hostnode.cnames: for cname in hostnode.cnames:
self.whois(cname) self.whois(cname)
self.whois(hostnode.name) self.whois(hostnode.name)
@ -58,7 +62,19 @@ class UniversalWhois():
for n in hostnode.get_ancestors(): for n in hostnode.get_ancestors():
self.query_whois_hostnode(n) self.query_whois_hostnode(n)
def whois(self, query: str) -> str: @overload
def whois(self, query: str, contact_email_only: Literal[True]) -> List[str]:
...
@overload
def whois(self, query: str, contact_email_only: Literal[False]) -> str:
...
@overload
def whois(self, query: str, contact_email_only: bool=False) -> Union[str, List[str]]:
...
def whois(self, query: str, contact_email_only: bool=False) -> Union[str, List[str]]:
if not self.available: if not self.available:
return '' return ''
bytes_whois = b'' bytes_whois = b''
@ -70,5 +86,7 @@ class UniversalWhois():
if not data: if not data:
break break
bytes_whois += data bytes_whois += data
to_return = bytes_whois.decode() if not contact_email_only:
return to_return return bytes_whois.decode()
emails = list(set(re.findall(rb'[\w\.-]+@[\w\.-]+', bytes_whois)))
return [e.decode() for e in sorted(emails)]

View File

@ -1062,10 +1062,13 @@ def statsfull():
@app.route('/whois/<string:query>', methods=['GET']) @app.route('/whois/<string:query>', methods=['GET'])
def whois(query: str): @app.route('/whois/<string:query>/<int:email_only>', methods=['GET'])
to_return = lookyloo.uwhois.whois(query) def whois(query: str, email_only: int=0):
return send_file(BytesIO(to_return.encode()), to_return = lookyloo.uwhois.whois(query, bool(email_only))
mimetype='test/plain', as_attachment=True, download_name=f'whois.{query}.txt') if isinstance(to_return, str):
return send_file(BytesIO(to_return.encode()),
mimetype='test/plain', as_attachment=True, download_name=f'whois.{query}.txt')
return jsonify(to_return)
# ##### Methods related to a specific URLNode ##### # ##### Methods related to a specific URLNode #####