-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
337 lines (267 loc) · 12.5 KB
/
app.py
File metadata and controls
337 lines (267 loc) · 12.5 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
from flask import Flask, render_template, request, jsonify, send_file, redirect, url_for, flash
import sqlite3
import geoip2.database
import geoip2.errors
import os
import re
import threading
import time
from werkzeug.utils import secure_filename
import tempfile
from pathlib import Path
import requests
import hashlib
import tarfile
import sys
from file_parser import extract_ips_from_file, preview_file
app = Flask(__name__)
app.secret_key = 'who_dat_secret_key'
# Global variables for progress tracking
progress = {'current': 0, 'total': 0, 'status': 'idle', 'message': ''}
processing_thread = None
def validate_ip(ip):
"""Validate IP address format"""
pattern = r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
return re.match(pattern, ip) is not None
def count_lines(filename):
"""Count lines in file"""
with open(filename, 'r') as f:
return sum(1 for _ in f)
def update_progress(current, total, status, message=""):
global progress
progress['current'] = current
progress['total'] = total
progress['status'] = status
progress['message'] = message
def process_ips_async(ip_file, db_file, batch_size=1000):
"""Process IPs asynchronously"""
try:
update_progress(0, 0, 'loading', 'Extracting IPs from file...')
# Extract IPs from file (supports multiple formats)
ips, file_type, confidence = extract_ips_from_file(ip_file)
if not ips:
update_progress(0, 0, 'error', f'No valid IPs found in {file_type} file')
return
total_ips = len(ips)
update_progress(0, total_ips, 'loading', f'Found {total_ips} IPs in {file_type} (confidence: {confidence:.1f}%)')
# Open MaxMind databases
geoip_asn = geoip2.database.Reader("GeoLite2-ASN.mmdb")
geoip_city = geoip2.database.Reader("GeoLite2-City.mmdb")
# Connect to SQLite
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
# Ensure table structure
cursor.execute("""
CREATE TABLE IF NOT EXISTS ip_info (
ip TEXT PRIMARY KEY,
isp TEXT,
city TEXT,
region TEXT,
country TEXT,
latitude REAL,
longitude REAL
)
""")
# Check if 'region' column exists
cursor.execute("PRAGMA table_info(ip_info);")
columns = [row[1] for row in cursor.fetchall()]
if "region" not in columns:
cursor.execute("ALTER TABLE ip_info ADD COLUMN region TEXT;")
conn.commit()
# Optimize database
cursor.execute("PRAGMA journal_mode = WAL;")
cursor.execute("PRAGMA synchronous = OFF;")
# Count total IPs
update_progress(0, total_ips, 'processing', f'Processing {total_ips} IP addresses from {file_type}...')
# Process IPs from extracted list
batch = []
processed = 0
for ip in ips:
try:
city_resp = geoip_city.city(ip)
city = city_resp.city.name or "N/A"
country = city_resp.country.name or "N/A"
region = city_resp.subdivisions.most_specific.name or "N/A"
latitude = city_resp.location.latitude or 0.0
longitude = city_resp.location.longitude or 0.0
except geoip2.errors.AddressNotFoundError:
city, country, region, latitude, longitude = "N/A", "N/A", "N/A", 0.0, 0.0
try:
asn_resp = geoip_asn.asn(ip)
isp = asn_resp.autonomous_system_organization or "N/A"
except geoip2.errors.AddressNotFoundError:
isp = "N/A"
batch.append((ip, isp, city, region, country, latitude, longitude))
# Insert in batches
if len(batch) >= batch_size:
cursor.executemany("INSERT OR IGNORE INTO ip_info VALUES (?, ?, ?, ?, ?, ?, ?)", batch)
conn.commit()
batch = []
processed += 1
if processed % 10 == 0: # Update progress every 10 IPs
update_progress(processed, total_ips, 'processing', f'Processed {processed}/{total_ips} IPs...')
# Final batch insert
if batch:
cursor.executemany("INSERT OR IGNORE INTO ip_info VALUES (?, ?, ?, ?, ?, ?, ?)", batch)
conn.commit()
# Close resources
geoip_asn.close()
geoip_city.close()
conn.close()
update_progress(total_ips, total_ips, 'completed', f'Completed! Processed {total_ips} IP addresses.')
# Auto-reset progress after 5 seconds to prevent continuous polling
threading.Timer(5.0, lambda: update_progress(0, 0, 'idle', '')).start()
except Exception as e:
update_progress(0, 0, 'error', f'Error: {str(e)}')
def update_databases_async():
"""Update MaxMind databases asynchronously"""
try:
from config import license_key
if license_key == "your_license_key_here":
update_progress(0, 0, 'error', 'Please update config.py with your license key')
return
def download_and_verify(db_name, db_url, sha256_url):
try:
update_progress(0, 0, 'downloading', f'Downloading {db_name} database...')
# Use session for connection reuse and better error handling
session = requests.Session()
session.headers.update({'User-Agent': 'Who-Dat/1.0'})
db_response = session.get(db_url, timeout=300)
db_response.raise_for_status()
tar_filename = f"{db_name}.tar.gz"
with open(tar_filename, "wb") as db_file:
db_file.write(db_response.content)
update_progress(0, 0, 'downloading', f'Downloading {db_name} checksum...')
sha256_response = session.get(sha256_url, timeout=300)
sha256_response.raise_for_status()
sha256_filename = f"{db_name}.tar.gz.sha256"
with open(sha256_filename, "wb") as sha256_file:
sha256_file.write(sha256_response.content)
# Verify SHA256
with open(tar_filename, "rb") as db_file:
db_content = db_file.read()
actual_sha256 = hashlib.sha256(db_content).hexdigest()
with open(sha256_filename, "r") as sha256_file:
sha256_content = sha256_file.read().strip()
# Extract just the hash (first part before any whitespace)
expected_sha256 = sha256_content.split()[0] if sha256_content else ""
if actual_sha256 != expected_sha256:
print(f"SHA256 checksum verification failed for {db_name}")
print(f"Expected: {expected_sha256}")
print(f"Actual: {actual_sha256}")
raise Exception(f"SHA256 checksum verification failed for {db_name}")
# Extract
update_progress(0, 0, 'extracting', f'Extracting {db_name} database...')
with tarfile.open(tar_filename, 'r:gz') as tar:
for member in tar.getmembers():
if member.name.endswith('.mmdb'):
member.name = Path(member.name).name
tar.extract(member, filter='data')
break
# Cleanup
os.remove(tar_filename)
os.remove(sha256_filename)
return True
except Exception as e:
# Cleanup partial files on error
for filename in [f"{db_name}.tar.gz", f"{db_name}.tar.gz.sha256"]:
try:
os.remove(filename)
except:
pass
raise Exception(f"Error processing {db_name}: {str(e)}")
# Update City database
city_db_url = f"https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key={license_key}&suffix=tar.gz"
city_sha256_url = f"https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key={license_key}&suffix=tar.gz.sha256"
if not download_and_verify("GeoLite2-City", city_db_url, city_sha256_url):
return
# Update ASN database
asn_db_url = f"https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN&license_key={license_key}&suffix=tar.gz"
asn_sha256_url = f"https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN&license_key={license_key}&suffix=tar.gz.sha256"
if not download_and_verify("GeoLite2-ASN", asn_db_url, asn_sha256_url):
return
update_progress(100, 100, 'completed', 'Database update completed successfully!')
# Auto-reset progress after 5 seconds to prevent continuous polling
threading.Timer(5.0, lambda: update_progress(0, 0, 'idle', '')).start()
except Exception as e:
update_progress(0, 0, 'error', f'Update error: {str(e)}')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/preview', methods=['POST'])
def preview_file_route():
"""Preview uploaded file structure"""
if 'file' not in request.files:
return jsonify({'error': 'No file uploaded'})
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No file selected'})
# Save file temporarily
filename = secure_filename(file.filename)
filepath = os.path.join(tempfile.gettempdir(), filename)
file.save(filepath)
# Get preview
preview = preview_file(filepath)
# Clean up
try:
os.remove(filepath)
except:
pass
return jsonify(preview)
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
flash('No file selected')
return redirect(url_for('index'))
file = request.files['file']
if file.filename == '':
flash('No file selected')
return redirect(url_for('index'))
if file:
filename = secure_filename(file.filename)
filepath = os.path.join(tempfile.gettempdir(), filename)
file.save(filepath)
# Extract IPs from file first to validate
ips, file_type, confidence = extract_ips_from_file(filepath)
if not ips:
flash(f'No valid IP addresses found in {file_type} file')
return redirect(url_for('index'))
# Start processing in background
global processing_thread
processing_thread = threading.Thread(target=process_ips_async, args=(filepath, 'output.db'))
processing_thread.start()
flash(f'File uploaded successfully! Found {len(ips)} IPs in {file_type} file.')
return redirect(url_for('index'))
@app.route('/update_databases', methods=['POST'])
def update_databases():
global processing_thread
processing_thread = threading.Thread(target=update_databases_async)
processing_thread.start()
flash('Database update started...')
return redirect(url_for('index'))
@app.route('/progress')
def get_progress():
return jsonify(progress)
@app.route('/results')
def show_results():
try:
conn = sqlite3.connect('output.db')
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM ip_info")
total_count = cursor.fetchone()[0]
cursor.execute("SELECT ip, isp, city, region, country FROM ip_info ORDER BY ip LIMIT 100")
results = cursor.fetchall()
conn.close()
return render_template('results.html', total_count=total_count, results=results)
except Exception as e:
flash(f'Error reading results: {str(e)}')
return redirect(url_for('index'))
@app.route('/download')
def download_results():
try:
return send_file('output.db', as_attachment=True, download_name='ip_geolocation.db')
except Exception as e:
flash(f'Error downloading file: {str(e)}')
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5001)