Initial commit default tip

Thu, 22 Jun 2023 12:14:31 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 22 Jun 2023 12:14:31 +0100
changeset 0
b8706b1b3d0f

Initial commit

Dockerfile file | annotate | diff | comparison | revisions
app.py file | annotate | diff | comparison | revisions
justfile file | annotate | diff | comparison | revisions
requirements.txt file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Dockerfile	Thu Jun 22 12:14:31 2023 +0100
@@ -0,0 +1,16 @@
+# syntax=docker/dockerfile:1.4
+FROM python:3.10-alpine
+
+WORKDIR /app
+
+RUN apk update \
+ && apk add git
+
+COPY requirements.txt /app
+RUN pip3 install -r requirements.txt \
+ && rm -rf /root/.cache/pip
+
+COPY . /app
+
+ENTRYPOINT ["python3"]
+CMD ["app.py"]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app.py	Thu Jun 22 12:14:31 2023 +0100
@@ -0,0 +1,53 @@
+from ipwhois import IPWhois
+
+from flask import Flask
+app = Flask(__name__)
+
+@app.route('/ip/<ip_str>')
+def ip(ip_str):
+    whois = IPWhois(ip_str).lookup_rdap(depth=1,get_asn_description=False)
+
+    contacts = []
+    existing_contacts = set()
+
+    for name in whois["objects"]:
+        org = whois["objects"][name]
+        email_contacts = org["contact"]["email"]
+        if email_contacts:
+            candidate_contacts = []
+            found_abuse_contact = False
+            for contact in email_contacts:
+                if contact["value"] in existing_contacts:
+                    continue
+                elif contact["type"] == "abuse" or ("abuse" in contact["value"]):
+                    found_abuse_contact = True
+                    existing_contacts.add(contact["value"])
+                    contacts.append({
+                        "name": org["contact"]["name"],
+                        "email": contact["value"]
+                    })
+                elif org["roles"] is not None and ("abuse" in org["roles"]):
+                    candidate_contacts.append({
+                        "name": org["contact"]["name"],
+                        "email": contact["value"]
+                    })
+            if not found_abuse_contact:
+                contacts.extend(candidate_contacts)
+
+    return {
+        "asn": whois["asn"],
+        "asn_info": {
+            "cidr": whois["asn_cidr"],
+            "country_code": whois["asn_country_code"],
+        },
+        "abuse_contacts": contacts,
+    }
+
+
+@app.route('/ip-raw/<ip_str>')
+def ip_raw(ip_str):
+    whois = IPWhois(ip_str).lookup_rdap(depth=1,get_asn_description=False)
+    return whois
+
+if __name__ == '__main__':
+	app.run(host='0.0.0.0', port=8008)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/justfile	Thu Jun 22 12:14:31 2023 +0100
@@ -0,0 +1,5 @@
+build:
+	docker build -t abuse-contact-api .
+
+run:
+	docker run --rm --network=host abuse-contact-api
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/requirements.txt	Thu Jun 22 12:14:31 2023 +0100
@@ -0,0 +1,3 @@
+flask
+ipwhois
+#git+https://github.com/certsocietegenerale/abuse_finder.git

mercurial