Add ipinfo command

This commit is contained in:
JeDaYoshi 2021-10-17 21:53:25 -04:00
parent 2e5836ae22
commit 37aa85e601
No known key found for this signature in database
GPG key ID: 8060B288C274219D
2 changed files with 46 additions and 2 deletions

View file

@ -94,3 +94,6 @@ bitly-api-key =
# https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line # https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line
github-token = github-token =
# https://ipinfo.io/account/token
ipinfo-token =

View file

@ -5,6 +5,7 @@ from src import ModuleManager, utils
import dns.resolver import dns.resolver
URL_GEOIP = "http://ip-api.com/json/%s" URL_GEOIP = "http://ip-api.com/json/%s"
URL_IPINFO = "https://ipinfo.io/%s/json"
REGEX_IPv6 = r"(?:(?:[a-f0-9]{1,4}:){2,}|[a-f0-9:]*::)[a-f0-9:]*" REGEX_IPv6 = r"(?:(?:[a-f0-9]{1,4}:){2,}|[a-f0-9:]*::)[a-f0-9:]*"
REGEX_IPv4 = r"(?:\d{1,3}\.){3}\d{1,3}" REGEX_IPv4 = r"(?:\d{1,3}\.){3}\d{1,3}"
REGEX_IP = re.compile("%s|%s" % (REGEX_IPv4, REGEX_IPv6), re.I) REGEX_IP = re.compile("%s|%s" % (REGEX_IPv4, REGEX_IPv6), re.I)
@ -32,6 +33,14 @@ class Module(ModuleManager.BaseModule):
return ip return ip
def _ipinfo_get(self, url):
access_token = self.bot.config.get("ipinfo-token", None)
headers = {}
if not access_token == None:
headers["Authorization"] = "Bearer %s" % access_token
request = utils.http.Request(url, headers=headers)
return utils.http.request(request)
@utils.hook("received.command.dig", alias_of="dns") @utils.hook("received.command.dig", alias_of="dns")
@utils.hook("received.command.dns", min_args=1) @utils.hook("received.command.dns", min_args=1)
def dns(self, event): def dns(self, event):
@ -108,8 +117,7 @@ class Module(ModuleManager.BaseModule):
pass pass
data = page["query"] data = page["query"]
if hostname: data += " (%s)" % hostname if hostname else ""
data += " (%s)" % hostname
data += " | Organisation: %s" % page["org"] data += " | Organisation: %s" % page["org"]
data += " | City: %s" % page["city"] data += " | City: %s" % page["city"]
data += " | Region: %s (%s)" % ( data += " | Region: %s (%s)" % (
@ -123,6 +131,39 @@ class Module(ModuleManager.BaseModule):
else: else:
raise utils.EventResultsError() raise utils.EventResultsError()
@utils.hook("received.command.ipinfo")
def ipinfo(self, event):
"""
:help: Get IPinfo.io data on a given IPv4/IPv6 address
:usage: <IP>
:prefix: IPinfo
"""
ip = self._get_ip(event)
page = self._ipinfo_get(URL_IPINFO % ip).json()
if page:
if not page.get("error", None):
hostname = page.get("hostname", None)
if not hostname:
try:
hostname, alias, ips = socket.gethostbyaddr(page["ip"])
except (socket.herror, socket.gaierror):
pass
data = page["ip"]
data += " (%s)" % hostname if hostname else ""
data += " | ISP: %s" % page["org"]
data += " | Location: %s, %s, %s" % (
page["city"], page["region"], page["country"])
data += " (Anycast)" if page.get("anycast", False) == True else ""
data += " | Lon/Lat: %s" % page["loc"]
data += " | Timezone: %s" % page["timezone"]
event["stdout"].write(data)
else:
event["stderr"].write(page["error"]["message"])
else:
raise utils.EventResultsError()
@utils.hook("received.command.rdns") @utils.hook("received.command.rdns")
def rdns(self, event): def rdns(self, event):
""" """