Compare commits

..

1 Commits

Author SHA1 Message Date
13d9cc9dc9 Merge pull request 'Change site to Light Mode for now' (#6) from lightmode into main
All checks were successful
Flask Run Test / Flask-Run-Test (push) Successful in 13s
Reviewed-on: #6
2026-01-12 11:49:15 +09:00
2 changed files with 30 additions and 45 deletions

44
app.py
View File

@@ -39,42 +39,30 @@ app.logger.addHandler(handler)
@app.route('/get-location') @app.route('/get-location')
def get_location(): def get_location():
# Since you said /check-ip shows the correct IP, we use it directly # 1. Try to get the forwarded IP first (for Docker/Production)
user_ip = request.headers.get('X-Forwarded-For', request.remote_addr) user_ip = request.headers.get('X-Forwarded-For', request.remote_addr)
# Clean the IP if it's a list # 2. If it's a list (e.g. "1.2.3.4, 172.17.0.1"), take the first one
if user_ip and ',' in user_ip: if user_ip and ',' in user_ip:
user_ip = user_ip.split(',')[0].strip() user_ip = user_ip.split(',')[0].strip()
# 3. Clean up any weird local addresses
if user_ip in ['127.0.0.1', 'localhost', '::1'] or user_ip.startswith('172.'):
# Fallback for testing only
return jsonify({"status": "success", "city": "Seoul", "country": "South Korea", "isp": "Test ISP"})
try: try:
# 1. We MUST send a User-Agent header or the API will block the request # Use HTTPS to be safer
headers = { response = requests.get(f'https://ipapi.co/{user_ip}/json/', timeout=5)
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0.0.0'
}
# 2. Use ip-api.com (it's the most 'forgiving' for free testing)
response = requests.get(
f'http://ip-api.com/json/{user_ip}',
headers=headers,
timeout=5
)
data = response.json() data = response.json()
return jsonify({
if data.get('status') == 'success': "status": "success",
return jsonify({ "city": data.get('city', 'Unknown'),
"status": "success", "country": data.get('country_name', 'Unknown'),
"city": data.get('city'), "isp": data.get('org', 'Unknown ISP')
"country": data.get('country') })
})
else:
# This helps you debug: what did the API actually say?
print(f"API Error Message: {data.get('message')}")
except Exception as e: except Exception as e:
print(f"Python Request Failed: {e}") return jsonify({"status": "fail", "error": str(e)}), 500
return jsonify({"status": "fail", "city": "Unknown", "country": "Location"})
@app.route('/check-ip') @app.route('/check-ip')
def check_ip(): def check_ip():

View File

@@ -226,24 +226,21 @@
</script> </script>
<script> <script>
async function loadLocation() { async function loadLocation() {
try { try {
// This calls the API from the USER'S browser. const response = await fetch('/get-location');
// The API will see the User's Real IP directly. const data = await response.json();
const response = await fetch('https://ipapi.co/json/');
const data = await response.json(); if (data.status === 'success') {
document.getElementById('location-display').innerHTML =
if (data.city && data.country_name) { `${data.city}, ${data.country} <small>(${data.isp})</small>`;
document.getElementById('location-display').innerText = document.getElementById('location-display').classList.remove('loading');
`${data.city}, ${data.country_name}`; } else {
document.getElementById('location-display').classList.remove('loading'); throw new Error();
} else { }
throw new Error("Missing data"); } catch (err) {
document.getElementById('location-display').innerText = "Unknown Location";
}
} }
} catch (err) {
console.error("Location Error:", err);
document.getElementById('location-display').innerText = "Unknown Location";
}
}
loadLocation(); loadLocation();
</script> </script>