deanpowers commited on
Commit
5206c01
·
1 Parent(s): 08e9ab1

feat(alerts): include IP location in admin Telegram messages

Browse files
Files changed (1) hide show
  1. local_app.py +46 -2
local_app.py CHANGED
@@ -1308,6 +1308,43 @@ def whoami_api():
1308
  "email": (u.get("email") if u else None),
1309
  })
1310
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1311
  @app.route('/api/scan/latest')
1312
  def latest_scan_api():
1313
  from modules.storage import load_json
@@ -1322,8 +1359,8 @@ def latest_scan_api():
1322
  @app.route('/api/scan', methods=['GET', 'POST'])
1323
  def scan_api():
1324
  """Main scan endpoint (20-day lookback, 14-period RSI). Supports ?mode=conservative|risk"""
 
1325
  if not current_user():
1326
- ip = request.remote_addr or "unknown"
1327
  now_ts = time.time()
1328
  last = _ANON_SCAN_LIMIT.get(ip, 0)
1329
  elapsed = now_ts - last
@@ -1352,6 +1389,10 @@ def scan_api():
1352
  except Exception as e:
1353
  print(f"⚠️ admin_stats log failed: {e}")
1354
 
 
 
 
 
1355
  alert_on_buy_signals(result)
1356
 
1357
  payload = {
@@ -1700,11 +1741,14 @@ def _notify_admin_new_registration(user, ip):
1700
  if not token or not chat:
1701
  return
1702
  name = (user.get("first_name", "") + " " + user.get("last_name", "")).strip() or "(no name)"
 
 
 
1703
  text = (
1704
  "\U0001F195 New registration request\n\n"
1705
  "Name: " + name + "\n"
1706
  "Email: " + user.get("email", "") + "\n"
1707
- "IP: " + (ip or "unknown") + "\n"
1708
  "\nReview: /admin/users"
1709
  )
1710
  requests.post(
 
1308
  "email": (u.get("email") if u else None),
1309
  })
1310
 
1311
+ DBIP_KEY = os.environ.get("DBIP_KEY", "")
1312
+
1313
+
1314
+ def _geo_lookup(ip):
1315
+ """Look up city/state/country for an IP. Returns dict or None."""
1316
+ if not ip or ip in ("unknown", "127.0.0.1"):
1317
+ return None
1318
+ if ip.startswith("10.") or ip.startswith("192.168.") or ip.startswith("172.") or ip.startswith("169.254."):
1319
+ return None
1320
+ if not DBIP_KEY:
1321
+ return None
1322
+ try:
1323
+ r = requests.get(
1324
+ "http://api.db-ip.com/v2/" + DBIP_KEY + "/" + ip,
1325
+ timeout=3
1326
+ )
1327
+ if r.status_code == 200:
1328
+ d = r.json()
1329
+ return {
1330
+ "city": d.get("city", "") or "",
1331
+ "state": d.get("stateProv", "") or "",
1332
+ "country": d.get("countryName", "") or "",
1333
+ }
1334
+ except Exception as e:
1335
+ print("geo lookup failed for " + ip + ": " + str(e))
1336
+ return None
1337
+
1338
+
1339
+ def _geo_line(ip):
1340
+ """Return 'City, State, Country' string or empty."""
1341
+ geo = _geo_lookup(ip)
1342
+ if not geo:
1343
+ return ""
1344
+ parts = [x for x in [geo.get("city"), geo.get("state"), geo.get("country")] if x]
1345
+ return ", ".join(parts)
1346
+
1347
+
1348
  @app.route('/api/scan/latest')
1349
  def latest_scan_api():
1350
  from modules.storage import load_json
 
1359
  @app.route('/api/scan', methods=['GET', 'POST'])
1360
  def scan_api():
1361
  """Main scan endpoint (20-day lookback, 14-period RSI). Supports ?mode=conservative|risk"""
1362
+ ip = client_ip(request)
1363
  if not current_user():
 
1364
  now_ts = time.time()
1365
  last = _ANON_SCAN_LIMIT.get(ip, 0)
1366
  elapsed = now_ts - last
 
1389
  except Exception as e:
1390
  print(f"⚠️ admin_stats log failed: {e}")
1391
 
1392
+ try:
1393
+ result["geo"] = _geo_lookup(ip)
1394
+ except Exception as _e:
1395
+ print("geo attach failed: " + str(_e))
1396
  alert_on_buy_signals(result)
1397
 
1398
  payload = {
 
1741
  if not token or not chat:
1742
  return
1743
  name = (user.get("first_name", "") + " " + user.get("last_name", "")).strip() or "(no name)"
1744
+ ip_str = ip or "unknown"
1745
+ loc = _geo_line(ip_str)
1746
+ loc_part = ("\nLocation: " + loc) if loc else ""
1747
  text = (
1748
  "\U0001F195 New registration request\n\n"
1749
  "Name: " + name + "\n"
1750
  "Email: " + user.get("email", "") + "\n"
1751
+ "IP: " + ip_str + loc_part + "\n"
1752
  "\nReview: /admin/users"
1753
  )
1754
  requests.post(