-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
71 lines (58 loc) · 2.58 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
from flask import Flask, request
import logging
import requests
app = Flask(__name__)
# Set up logging to both console and file
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(message)s',
handlers=[
logging.FileHandler('ip_log.txt'), # Log to file
logging.StreamHandler() # Log to console
]
)
# IPinfo API token (replace with your actual token)
IPINFO_TOKEN = 'a80f59fde431ea'
def get_location(ip):
"""Fetch location and privacy details using the IPinfo API."""
try:
logging.info(f"Fetching location for IP: {ip}") # Log to console and file
response = requests.get(f'https://ipinfo.io/{ip}?token={IPINFO_TOKEN}')
data = response.json()
logging.info(f"Location data: {data}") # Log to console and file
# Check if the IP is a VPN, proxy, or Tor
if 'privacy' in data:
if data['privacy'].get('vpn', False):
data['vpn'] = True
if data['privacy'].get('proxy', False):
data['proxy'] = True
if data['privacy'].get('tor', False):
data['tor'] = True
return data
except Exception as e:
logging.error(f"Error fetching location: {e}") # Log to console and file
return {"error": str(e)}
@app.route('/')
def index():
# Get the user's real IP address from the X-Forwarded-For header
user_ip = request.headers.get('X-Forwarded-For', request.remote_addr)
if ',' in user_ip:
user_ip = user_ip.split(',')[0].strip() # Handle multiple IPs in the header
logging.info(f"User IP: {user_ip}") # Log to console and file
# Get location and privacy details
location = get_location(user_ip)
# Log the IP address, location, and privacy details
logging.info(f"Logged IP: {user_ip}, Location: {location}") # Log to console and file
# Prepare the response message
response_message = f"Hello! Your IP has been logged. Location: {location}"
# Add VPN/proxy/Tor information to the response
if location.get('vpn', False):
response_message += "\nYou are using a VPN."
if location.get('proxy', False):
response_message += "\nYou are using a proxy."
if location.get('tor', False):
response_message += "\nYou are using Tor."
return response_message
if __name__ == '__main__':
logging.info("Starting the IP logger...") # Log to console and file
app.run(host='0.0.0.0', port=5000, debug=False) # Disable debug mode for production