-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
107 lines (93 loc) · 3.59 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import socket
import pymongo
import requests
import streamlit as st
def get_domain(url: str) -> str:
url = url.replace("https://", "").replace("http://", "").replace("www.", "")
domain = url.split("/", 1)[0].lower()
return domain
@st.cache_resource
def get_mongo_collection(uri: str):
# Connect to MongoDB
client = pymongo.MongoClient(uri)
# Create database
db = client["ipdb"]
# Create collection
collection = db["ipdb"]
# Return collection
return collection
def get_location(ip_address: str) -> dict:
url = requests.get(f"http://ip-api.com/json/{ip_address}")
return url.json()
st.set_page_config(
page_title="IP DB",
page_icon=":mag_right:",
menu_items={
"Get Help": "https://github.com/Siddhesh-Agarwal/IP-DB/issues",
"Report a bug": "https://github.com/Siddhesh-Agarwal/IP-DB/issues",
"About": open("./README.md").read(),
},
)
st.title(":mag_right: IP DB")
tabs = st.tabs(["Add", "Search"])
with st.spinner("Connecting to database..."):
collection = get_mongo_collection(st.secrets["uri"])
with tabs[0]:
url = st.text_input(
label="Enter URL",
placeholder="https://www.google.com",
).strip()
if st.button("Find"):
with st.spinner("Finding IP Address..."):
# get domain
domain = get_domain(url)
try:
# get ip address
ip_address = socket.gethostbyname(domain)
# show ip address
st.info(ip_address)
with st.spinner("Checking database..."):
data = {"domain": domain, "ip_address": ip_address}
if collection.find_one(data):
st.info(f"Data already exists in the database")
else:
collection.insert_one(data)
st.success(f"Data added to the database", icon="✅")
except socket.gaierror:
st.error("Invalid URL", icon="❌")
except Exception as e:
st.error(e, icon="❌")
st.info(
"Refresh the site. If the problem persists, report this issue [here](https://github.com/Siddhesh-Agarwal/IP-DB/issues)"
)
st.stop()
with tabs[1]:
# get IP address
ip_address = st.text_input("Enter IP Address").strip()
if st.button("Search"):
if ip_address:
with st.spinner("Searching..."):
# number of times ip_address exists
res = collection.find({"ip_address": ip_address})
if res:
domains = list(set(i["domain"] for i in res))
count = len(domains)
location = get_location(ip_address)
st.success(
f"IP Address {ip_address} exists {count} times", icon="ℹ️"
)
st.info(
body=f"**Location:** {location['city']}, {location['country']} ({location['countryCode']})",
icon="📌",
)
if count == 1:
st.info(f"**Domain:** {domains[0]}", icon="🔗")
if count > 1:
with st.expander("All domains"):
st.write(domains)
with st.expander("More details"):
st.write(location)
else:
st.error(f"IP Address {ip_address} does not exist", icon="❌")
else:
st.error("Please enter an IP Address")