-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1558 lines (1423 loc) · 63.6 KB
/
app.py
File metadata and controls
1558 lines (1423 loc) · 63.6 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# --- Imports ---
import os
import time
import json
import logging
import requests
import threading
import datetime
import urllib.parse
from datetime import datetime, timezone, timedelta
from functools import wraps
from typing import Dict, Any, List, Optional, Union, Tuple
from collections import defaultdict
# Third-party imports
from dotenv import load_dotenv
from flask import (
Flask,
render_template,
request,
jsonify,
session,
redirect,
url_for,
make_response,
abort,
send_from_directory
)
from pymongo import MongoClient, ASCENDING, DESCENDING
from bson import ObjectId, json_util
from bson.json_util import dumps
from discord_webhook import DiscordWebhook, DiscordEmbed
# --- Load Environment Variables EARLY ---
load_dotenv()
# Debug log for Mongo URI
print("MongoDB URI:", os.getenv("MONGO_URI"))
# Initialize Flask app
app = Flask(__name__)
app.config["SECRET_KEY"] = os.getenv("FLASK_SECRET_KEY", "default-secret-key")
app.config["PERMANENT_SESSION_LIFETIME"] = timedelta(minutes=30)
app.config["MAX_CONTENT_LENGTH"] = 16 * 1024 * 1024 # 16MB max upload size
# Import and register API Blueprints
from api.users import users_bp as users_api_blueprint
app.register_blueprint(users_api_blueprint)
# --- End of Imports ---
# --- Load Environment Variables EARLY ---
load_dotenv()
# Debug log for Mongo URI
print("MongoDB URI:", os.getenv("MONGO_URI"))
# --- Constants and Configuration ---
CONFIG = {
"MONGO_DB": "techtact",
"MONGO_COLLECTION": "visitors",
"ARCHIVE_COLLECTION": "archived_stats",
"MONTH_TRACKER_COLLECTION": "month_tracker",
"LOG_FORMAT": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
"REQUEST_TIMEOUT": 5,
"DISCORD_EMBED_COLOR": 0x2B2D31,
"DEFAULT_AVATAR_URL": "https://cdn.discordapp.com/embed/avatars/{}.png",
"CLEANUP_TIMEZONE": "UTC",
# Enhanced Anti-DDoS settings
"RATE_LIMIT": 30,
"RATE_LIMIT_WINDOW": 30,
"REFRESH_LIMIT": 5,
"REFRESH_WINDOW": 30,
"BLOCK_THRESHOLD": 3,
"BLOCK_DURATION": 7200,
"USER_AGENT_BLACKLIST": [
"python-requests", "curl", "wget", "nikto", "sqlmap", "nmap",
"hydra", "metasploit", "scrapy", "phantomjs", "selenium",
"headless", "bot", "spider", "crawler", "zgrab", "masscan",
"nessus", "openvas", "burp", "arachni", "skipfish", "w3af",
"owasp", "zap", "fiddler", "postman", "insomnia", "httrack",
"grabber", "harvest", "extract", "collect", "scan", "exploit",
"dirbuster", "gobuster", "ffuf", "nikto", "sqlmap", "wpscan",
"acunetix", "netsparker", "appscan", "nessus", "openvas",
"nessus", "openvas", "burp", "arachni", "skipfish", "w3af",
"owasp", "zap", "fiddler", "postman", "insomnia", "httrack",
"grabber", "harvest", "extract", "collect", "scan", "exploit",
"dirbuster", "gobuster", "ffuf", "nikto", "sqlmap", "wpscan",
"acunetix", "netsparker", "appscan", "nessus", "openvas",
"nessus", "openvas", "burp", "arachni", "skipfish", "w3af",
"owasp", "zap", "fiddler", "postman", "insomnia", "httrack",
"grabber", "harvest", "extract", "collect", "scan", "exploit",
"dirbuster", "gobuster", "ffuf", "nikto", "sqlmap", "wpscan",
"acunetix", "netsparker", "appscan", "nessus", "openvas"
],
"IP_BLACKLIST": [],
"REQUEST_SIZE_LIMIT": 512 * 1024,
"LOADING_DELAY": 3, # 3 second loading delay for anti-DDoS
# Enhanced Human Verification settings
"HUMAN_VERIFICATION_ENABLED": True,
"VERIFICATION_SESSION_EXPIRY": 86400, # 24 hours
"VERIFICATION_SPAM_TIMEOUT": 20, # 20 seconds for spamming
"VERIFICATION_INACTIVE_TIMEOUT": 3600, # 1 hour for inactive
"VERIFICATION_FAIL_LIMIT": 3,
"VERIFICATION_FAIL_BAN": 300,
"VERIFICATION_QUESTIONS": [
{"question":"What do you wear on your feet?","answer":"Shoes","options":["Gloves","Shoes","Hat","Scarf"]},
{"question":"Which shape is round?","answer":"Circle","options":["Triangle","Square","Circle","Rectangle"]},
{"question":"What do you use to write?","answer":"Pen","options":["Ruler","Pen","Scissors","Eraser"]},
{"question":"Which animal has a long neck?","answer":"Giraffe","options":["Elephant","Lion","Giraffe","Zebra"]},
{"question":"How many letters are in the word 'cat'?","answer":"3","options":["2","3","4","5"]},
{"question":"Which fruit is red?","answer":"Apple","options":["Banana","Apple","Pear","Plum"]},
{"question":"What color are strawberries?","answer":"Red","options":["Blue","Green","Red","Yellow"]},
{"question":"What does a cow give us?","answer":"Milk","options":["Wool","Milk","Eggs","Honey"]},
{"question":"What is 5 + 3?","answer":"8","options":["6","7","8","9"]},
{"question":"Which one is used to call someone?","answer":"Phone","options":["Tablet","Phone","TV","Microwave"]},
{"question":"Which is a type of insect?","answer":"Ant","options":["Frog","Bird","Ant","Snake"]},
{"question":"Which one do you wear on your head?","answer":"Hat","options":["Hat","Shirt","Shoes","Belt"]},
{"question":"What color is the sky on a clear day?","answer":"Blue","options":["Green","Red","Blue","Black"]},
{"question":"Which is the largest planet?","answer":"Jupiter","options":["Earth","Mars","Jupiter","Venus"]},
{"question":"How many legs does a cat have?","answer":"4","options":["2","3","4","5"]},
{"question":"What do you drink when you're thirsty?","answer":"Water","options":["Milk","Water","Juice","Oil"]},
{"question":"Which is used to open a door?","answer":"Key","options":["Knife","Pen","Key","Screw"]},
{"question":"Which of these is a fruit?","answer":"Banana","options":["Potato","Banana","Carrot","Onion"]},
{"question":"Which one is a body part?","answer":"Arm","options":["Spoon","Arm","Pencil","Book"]},
{"question":"What shape is a pizza?","answer":"Circle","options":["Square","Circle","Triangle","Oval"]},
{"question":"What is 6 − 2?","answer":"4","options":["3","4","5","6"]},
{"question":"What do you use to cut paper?","answer":"Scissors","options":["Fork","Ruler","Scissors","Marker"]},
{"question":"What comes after the number 9?","answer":"10","options":["8","9","10","11"]},
{"question":"Which one can fly?","answer":"Plane","options":["Car","Plane","Boat","Train"]},
{"question":"Which of these is a mode of communication?","answer":"Email","options":["Email","Knife","Broom","Ladder"]},
{"question":"Which organ do you use to hear?","answer":"Ear","options":["Nose","Ear","Eye","Mouth"]},
{"question":"Which season comes after summer?","answer":"Autumn","options":["Spring","Autumn","Winter","Rainy"]},
{"question":"Which number is greater: 7 or 5?","answer":"7","options":["5","6","7","4"]},
{"question":"Which animal barks?","answer":"Dog","options":["Cat","Dog","Bird","Duck"]},
{"question":"Which object can float on water?","answer":"Boat","options":["Car","Rock","Boat","Bike"]},
{"question":"What color are lemons?","answer":"Yellow","options":["Red","Green","Blue","Yellow"]},
{"question":"Which one is used to clean floors?","answer":"Broom","options":["Fork","Broom","Pan","Towel"]},
{"question":"What do chickens lay?","answer":"Eggs","options":["Milk","Eggs","Bread","Wool"]},
{"question":"Which is a liquid?","answer":"Juice","options":["Rock","Paper","Juice","Sand"]},
{"question":"Which one is NOT a fruit?","answer":"Potato","options":["Apple","Banana","Potato","Mango"]},
{"question":"Which number is even?","answer":"6","options":["3","5","6","9"]},
{"question":"Which object gives us light at night?","answer":"Moon","options":["Sun","Star","Moon","Cloud"]},
{"question":"What color is coal?","answer":"Black","options":["White","Black","Red","Blue"]},
{"question":"Which of these can you eat?","answer":"Bread","options":["Bread","Ball","Pen","Tire"]},
{"question":"What is 4 × 2?","answer":"8","options":["6","8","10","12"]},
{"question":"What do you use to see things far away?","answer":"Binoculars","options":["Glasses","Binoculars","Compass","Phone"]},
{"question":"What goes tick-tock?","answer":"Clock","options":["Fan","Clock","Chair","Dog"]},
{"question":"How many days in February (non-leap year)?","answer":"28","options":["28","29","30","31"]},
{"question":"Which animal is known as man's best friend?","answer":"Dog","options":["Cat","Dog","Fish","Parrot"]},
{"question":"What shape has 3 sides?","answer":"Triangle","options":["Square","Triangle","Circle","Pentagon"]},
{"question":"Which animal goes 'quack'?","answer":"Duck","options":["Goose","Frog","Duck","Horse"]},
{"question":"Which of these is frozen water?","answer":"Ice","options":["Steam","Rain","Ice","Snow"]},
{"question":"How many hours are in a day?","answer":"24","options":["12","18","24","30"]},
{"question":"Which of these is hot?","answer":"Fire","options":["Fire","Snow","Ice","Rain"]}
]
}
# --- Setup Logging ---
logging.basicConfig(
level=logging.INFO,
format=CONFIG["LOG_FORMAT"],
handlers=[
logging.StreamHandler(),
logging.FileHandler(os.path.join(os.path.dirname(__file__), "app.log"))
]
)
logger = logging.getLogger(__name__)
# --- Load Environment Variables ---
load_dotenv()
class Config:
DISCORD_WEBHOOK = os.getenv("DISCORD_WEBHOOK")
DISCORD_ALERT_WEBHOOK = os.getenv("DISCORD_ALERT_WEBHOOK")
DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
MONGO_URI = os.getenv("MONGO_URI")
DISCORD_GUILD_ID = os.getenv("DISCORD_GUILD_ID")
@classmethod
def validate(cls):
required = ["DISCORD_BOT_TOKEN", "MONGO_URI", "DISCORD_ALERT_WEBHOOK", "DISCORD_GUILD_ID"]
missing = [var for var in required if not getattr(cls, var)]
if missing:
raise EnvironmentError(f"Missing required environment variables: {', '.join(missing)}")
Config.validate()
# --- Ultra-Strong Anti-DDoS System + Human Verification Triggers ---
class DDoSProtection:
def is_blocked(self, ip):
now = time.time()
# Check global block
if self.global_blocked and now < self.global_block_until:
self.log_event(ip, "global_block", "Global DDoS block active")
return True
# Check DB for blocked IP
block = self.db.get_blocked_ip(ip)
if block and block["unblock_time"] > now:
self.log_event(ip, "ip_blocked", "IP is blocked")
return True
elif block:
self.db.unblock_ip(ip)
return False
def block_ip(self, ip, duration, reason):
unblock_time = time.time() + duration
self.db.block_ip(ip, unblock_time, reason)
self.log_event(ip, "block_ip", reason, {"duration": duration})
def check(self, ip, user_agent):
now = time.time()
# Blocked IP
if self.is_blocked(ip):
self.log_event(ip, "blocked_request", "Blocked IP tried to access")
return False, "Blocked IP"
# IP blacklist (manual)
if ip in CONFIG.get("IP_BLACKLIST", []):
self.log_event(ip, "ip_blacklist", "Blacklisted IP")
if self.global_blocked:
self.block_ip(ip, CONFIG["BLOCK_DURATION"] * 3, "Blacklisted IP")
self.flag_human_verification(ip, "Blacklisted IP")
return False, "Blacklisted IP"
return False, "Blacklisted IP"
# --- Database Setup ---
# --- Database Setup ---
class DatabaseManager:
def __init__(self):
try:
self.client = MongoClient(Config.MONGO_URI, serverSelectionTimeoutMS=5000)
# Force a connection check
self.client.server_info() # This will trigger an exception if connection fails
self.db = self.client[CONFIG["MONGO_DB"]]
self.visitors = self.db[CONFIG["MONGO_COLLECTION"]]
self.archived_stats = self.db[CONFIG["ARCHIVE_COLLECTION"]]
self.month_tracker = self.db[CONFIG["MONTH_TRACKER_COLLECTION"]]
self.blocked_ips = self.db["blocked_ips"]
self.suspicious_logs = self.db["suspicious_logs"]
self._setup_indexes()
logger.info("Successfully connected to MongoDB")
except Exception as e:
logger.error(f"Failed to connect to MongoDB: {e}")
raise RuntimeError(f"Could not connect to MongoDB: {e}")
def _setup_indexes(self):
try:
# Visitors collection indexes
self.visitors.create_index([("ip", 1), ("month", 1)], unique=True)
self.visitors.create_index("timestamp")
self.visitors.create_index("date")
self.visitors.create_index("month")
self.visitors.create_index("year")
# Blocked IPs collection indexes
self.blocked_ips.create_index("ip", unique=True)
self.blocked_ips.create_index("unblock_time")
# Suspicious logs collection indexes
self.suspicious_logs.create_index("timestamp", -1) # For sorting by most recent
self.suspicious_logs.create_index("ip") # For filtering by IP
self.suspicious_logs.create_index([("timestamp", -1), ("ip", 1)]) # Compound index for common queries
logger.info("Database indexes created successfully")
except Exception as e:
logger.error(f"Failed to create database indexes: {e}")
def get_unique_ip_count(self) -> int:
"""Get count of unique IP addresses"""
try:
return len(self.visitors.distinct("ip"))
except Exception as e:
logger.error(f"Failed to get unique IP count: {e}")
return 0
def log_visitor(self, visitor_data: Dict[str, Any]) -> bool:
"""
Log visitor data only if unique IP for the current month.
Never store duplicate IPs for the same month.
Only store important fields: ip, timestamp, date, month, year.
"""
# self._check_monthly_reset() # <-- REMOVE call to monthly reset
try:
# Only keep the important fields, ignore extra keys
important_fields = ["ip", "timestamp", "date", "month", "year"]
data = {k: visitor_data[k] for k in important_fields if k in visitor_data}
# Check for duplicate (ip, month)
existing = self.visitors.find_one({
"ip": data["ip"],
"month": data["month"]
})
if existing:
logger.debug(f"Duplicate IP detected for current month: {data.get('ip')}")
return False
result = self.visitors.insert_one(data)
logger.info(f"Logged NEW unique visitor with IP: {data.get('ip')} (ID: {result.inserted_id})")
return True
except Exception as e:
logger.error(f"Failed to log visitor: {e}")
return False
def get_visitor_count(self, filter_query: Dict[str, Any] = None) -> int:
try:
return self.visitors.count_documents(filter_query or {})
except Exception as e:
logger.error(f"Failed to get visitor count: {e}")
return 0
def get_period_stats(self) -> Tuple[Dict[str, int], Dict[str, int], Dict[str, int]]:
"""Returns tuple of (day_counts, month_counts, year_counts)"""
try:
pipeline = [
{"$group": {
"_id": "$date",
"count": {"$sum": 1}
}},
{"$sort": {"count": -1}}
]
day_counts = {str(doc["_id"]): doc["count"] for doc in self.visitors.aggregate(pipeline)}
pipeline[0]["$group"]["_id"] = "$month"
month_counts = {str(doc["_id"]): doc["count"] for doc in self.visitors.aggregate(pipeline)}
pipeline[0]["$group"]["_id"] = "$year"
year_counts = {str(doc["_id"]): doc["count"] for doc in self.visitors.aggregate(pipeline)}
return day_counts, month_counts, year_counts
except Exception as e:
logger.error(f"Failed to get period stats: {e}")
return {}, {}, {}
def get_historical_stats(self):
"""Get combined stats from current data and archives"""
try:
current_stats = {
"total": self.get_visitor_count(),
"unique_ips": self.get_unique_ip_count(),
"day_counts": {},
"month_counts": {},
"year_counts": {}
}
day_counts, month_counts, year_counts = self.get_period_stats()
current_stats.update({
"day_counts": day_counts,
"month_counts": month_counts,
"year_counts": year_counts
})
archived_stats = list(self.archived_stats.find().sort("timestamp", -1))
return {
"current": current_stats,
"archived": archived_stats
}
except Exception as e:
logger.error(f"Failed to get historical stats: {e}")
return None
# --- DDoS Blocked IPs in MongoDB ---
def block_ip(self, ip, unblock_time, reason, admin_id=None):
doc = {
"ip": ip,
"unblock_time": unblock_time,
"reason": reason,
"blocked_at": time.time()
}
if admin_id:
doc["admin_id"] = admin_id
self.blocked_ips.update_one(
{"ip": ip},
{"$set": doc},
upsert=True
)
def get_blocked_ip(self, ip):
return self.blocked_ips.find_one({"ip": ip})
def unblock_ip(self, ip):
self.blocked_ips.delete_one({"ip": ip})
def get_blocked_ips(self, limit=100):
"""Return a list of blocked IPs, most recent first."""
try:
return list(self.blocked_ips.find().sort("blocked_at", -1).limit(limit))
except Exception as e:
logger.error(f"Failed to fetch blocked IPs: {e}")
return []
# --- Initialize global DatabaseManager instance ---
try:
db = DatabaseManager()
except Exception as e:
logger.error(f"Failed to initialize DatabaseManager: {e}")
raise
# --- IPUtils Utility ---
class IPUtils:
@staticmethod
def get_client_ip():
"""Get client IP address from request headers (supports proxies)"""
if request.headers.get('X-Forwarded-For'):
# X-Forwarded-For may contain multiple IPs, take the first
ip = request.headers.get('X-Forwarded-For').split(',')[0].strip()
else:
ip = request.remote_addr or "unknown"
return ip
# --- Discord API User Info Fetch ---
class DiscordAPI:
@staticmethod
def get_user_info(user_id: str, access_token: str = None) -> Dict[str, Any]:
"""
Fetch Discord user information.
If access_token is provided, use it for /users/@me, otherwise use bot token for user_id.
"""
if user_id == "me" and access_token:
url = "https://discord.com/api/v10/users/@me"
headers = {"Authorization": f"Bearer {access_token}"}
else:
url = f"https://discord.com/api/v10/users/{user_id}"
headers = {"Authorization": f"Bot {Config.DISCORD_BOT_TOKEN}"}
try:
response = requests.get(url, headers=headers, timeout=CONFIG["REQUEST_TIMEOUT"])
if response.status_code == 200:
data = response.json()
avatar_hash = data.get("avatar")
username = data.get("username", "Unknown")
discriminator = data.get("discriminator", "0")
if discriminator == "0":
full_username = username
else:
full_username = f"{username}#{discriminator}"
avatar_url = None
if avatar_hash:
if avatar_hash.startswith("a_"):
avatar_url = f"https://cdn.discordapp.com/avatars/{data['id']}/{avatar_hash}.gif?size=128"
else:
avatar_url = f"https://cdn.discordapp.com/avatars/{data['id']}/{avatar_hash}.png?size=128"
return {
"id": data.get("id", user_id),
"username": full_username,
"avatar_url": avatar_url or CONFIG["DEFAULT_AVATAR_URL"].format(int(data.get("id", "0")) % 5),
"avatar_gif": avatar_url if avatar_url and avatar_url.endswith(".gif?size=128") else None
}
else:
logger.error(f"Discord API error {response.status_code}: {response.text}")
except Exception as e:
logger.error(f"Failed to fetch Discord user {user_id}: {e}")
# fallback
fallback_id = user_id if user_id != "me" else "0"
return {
"id": fallback_id,
"username": "Unavailable",
"avatar_url": CONFIG["DEFAULT_AVATAR_URL"].format(int(fallback_id) % 5 if fallback_id.isdigit() else 0),
"avatar_gif": None
}
# --- StatsManager Utility ---
class StatsManager:
@staticmethod
def get_current_stats():
try:
now = datetime.now(timezone.utc)
today_str = now.strftime('%Y-%m-%d')
month_str = now.strftime('%Y-%m')
year_str = now.strftime('%Y')
today_count = db.get_visitor_count({"date": today_str})
month_count = db.get_visitor_count({"month": month_str})
year_count = db.get_visitor_count({"year": year_str})
total_count = db.get_visitor_count()
unique_ips = db.get_unique_ip_count()
day_counts, month_counts, year_counts = db.get_period_stats()
top_day = max(day_counts.items(), key=lambda x: x[1]) if day_counts else ("N/A", 0)
top_month = max(month_counts.items(), key=lambda x: x[1]) if month_counts else ("N/A", 0)
top_year = max(year_counts.items(), key=lambda x: x[1]) if year_counts else ("N/A", 0)
blocked_ips_count = db.blocked_ips.count_documents({})
# --- Aggregate archived stats for totals ---
archived = list(db.archived_stats.find())
archived_total = sum(a.get("total_visitors", 0) for a in archived)
archived_unique = sum(a.get("unique_ips", 0) for a in archived)
archived_top_day = ("N/A", 0)
archived_top_month = ("N/A", 0)
archived_top_year = ("N/A", 0)
# Find best top_day/month/year from archives
all_days = []
all_months = []
all_years = []
for a in archived:
all_days.extend(a.get("top_days", []))
all_months.extend(a.get("top_months", []))
all_years.extend(a.get("top_years", []))
if all_days:
archived_top_day = max(all_days, key=lambda x: x[1])
if all_months:
archived_top_month = max(all_months, key=lambda x: x[1])
if all_years:
archived_top_year = max(all_years, key=lambda x: x[1])
# Combine current and archived stats
combined_total = total_count + archived_total
combined_unique = unique_ips + archived_unique
combined_top_day = top_day if top_day[1] >= archived_top_day[1] else archived_top_day
combined_top_month = top_month if top_month[1] >= archived_top_month[1] else archived_top_month
combined_top_year = top_year if top_year[1] >= archived_top_year[1] else archived_top_year
# Calculate averages
day_counts, month_counts, year_counts = db.get_period_stats()
arv_day = round(combined_total / max(1, len(day_counts)), 2) if day_counts else 0
arv_month = round(combined_total / max(1, len(month_counts)), 2) if month_counts else 0
arv_year = round(combined_total / max(1, len(year_counts)), 2) if year_counts else 0
return {
"today": today_count,
"month": month_count,
"year": year_count,
"total": combined_total,
"unique_ips": combined_unique,
"top_day": combined_top_day,
"top_month": combined_top_month,
"top_year": combined_top_year,
"blocked_ips": blocked_ips_count,
"arv_day": arv_day,
"arv_month": arv_month,
"arv_year": arv_year,
"day_count": len(day_counts),
"month_count": len(month_counts),
"year_count": len(year_counts)
}
except Exception as e:
logging.error(f"Failed to fetch stats: {e}")
# Return empty stats to avoid template errors
return {
"today": 0,
"month": 0,
"year": 0,
"total": 0,
"unique_ips": 0,
"top_day": ("N/A", 0),
"top_month": ("N/A", 0),
"top_year": ("N/A", 0),
"blocked_ips": 0,
"arv_day": 0,
"arv_month": 0,
"arv_year": 0,
"day_count": 0,
"month_count": 0,
"year_count": 0
}
# --- Admin Auth Decorator ---
def admin_required(f):
@wraps(f)
def decorated(*args, **kwargs):
admin_pw = os.getenv("ADMIN_DASHBOARD_PASSWORD", "changeme")
if not admin_pw:
return "Admin password not set.", 403
auth = request.authorization
if not auth or auth.username != "admin" or auth.password != admin_pw:
return (
"Authentication required",
401,
{"WWW-Authenticate": 'Basic realm="Admin Dashboard"'},
)
return f(*args, **kwargs)
return decorated
# --- Discord OAuth2 Config ---
DISCORD_CLIENT_ID = os.getenv("DISCORD_CLIENT_ID")
DISCORD_CLIENT_SECRET = os.getenv("DISCORD_CLIENT_SECRET")
DISCORD_REDIRECT_URI = os.getenv("DISCORD_REDIRECT_URI", "http://localhost:5000/callback/discord")
DISCORD_OAUTH_SCOPE = "identify"
DISCORD_API_BASE = "https://discord.com/api"
# List of allowed Discord user IDs (website owners)
DASHBOARD_ALLOWED_USERS = {"1132413940693995541"} # Add more IDs as needed
# --- Dashboard User Permissions Database ---
def get_user_permissions(discord_id):
user = db.db["dashboard_users"].find_one({"discord_id": discord_id})
# --- Role-based permissions ---
role_perms = {
"owner": {
"can_view_stats": True,
"can_view_events": True,
"can_view_blocked": True,
"can_view_database": True,
"can_manage_database": True, # Full access to all database operations
"can_edit_documents": True, # Edit existing documents
"can_delete_documents": True, # Delete individual documents
"can_clear_collections": True, # Clear entire collections
"can_view_settings": True,
"can_manage_users": True,
"can_export": True,
"can_clear_data": True,
"can_edit_settings": True,
},
"admin": {
"can_view_stats": True,
"can_view_events": True,
"can_view_blocked": True,
"can_view_database": True,
"can_manage_database": True, # Full access to all database operations
"can_edit_documents": True, # Edit existing documents
"can_delete_documents": True, # Delete individual documents
"can_clear_collections": True, # Clear entire collections
"can_view_settings": True,
"can_manage_users": True,
"can_export": True,
"can_clear_data": True,
"can_edit_settings": True,
},
"moderator": {
"can_view_stats": True,
"can_view_events": True,
"can_view_blocked": True,
"can_view_database": False,
"can_view_settings": False,
"can_manage_users": False,
"can_export": False,
"can_clear_data": False,
"can_edit_settings": False
},
"analyst": {
"can_view_stats": True,
"can_view_events": True,
"can_view_database": True,
"can_export": True,
"can_view_blocked": False,
"can_view_settings": False,
"can_manage_users": False,
"can_clear_data": False,
"can_edit_settings": False
},
"user": {
"can_view_stats": True,
"can_view_events": False,
"can_view_blocked": False,
"can_view_database": False,
"can_view_settings": False,
"can_manage_users": False,
"can_export": False,
"can_clear_data": False,
"can_edit_settings": False
},
"custom": {}
}
if not user:
return {"role": "user", "permissions": role_perms["user"]}
role = user.get("role", "user")
if role not in role_perms:
role = "user"
perms = dict(role_perms[role])
if role == "custom":
perms.update(user.get("permissions", {}))
# Always include can_manage_users in the top-level for easier JS checks
top_level = dict(perms)
top_level["role"] = role
return {"role": role, "permissions": perms, **top_level}
def dashboard_login_required(f):
from functools import wraps
@wraps(f)
def decorated(*args, **kwargs):
user = session.get("discord_user")
if not user:
return redirect(url_for("login_discord"))
return f(*args, **kwargs)
return decorated
def require_manage_users(f):
from functools import wraps
@wraps(f)
def decorated(*args, **kwargs):
user = session.get("discord_user")
if not user:
return redirect(url_for("login_discord"))
perms = get_user_permissions(user["id"])
def require_database_permission(permission):
"""Decorator to check for specific database permissions.
Args:
permission (str): The permission to check (e.g., 'can_edit_documents', 'can_delete_documents')
"""
def decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
user = session.get("discord_user")
if not user or not user.get("id"):
return jsonify({"success": False, "error": "Unauthorized"}), 401
perms = get_user_permissions(user["id"])
if not perms.get("permissions", {}).get(permission, False):
return jsonify({"success": False, "error": "Insufficient permissions"}), 403
return f(*args, **kwargs)
return decorated_function
return decorator
# --- Delete user API for permissions tab modal ---
@app.route("/dashboard/api/users/delete", methods=["POST"])
@dashboard_login_required
@require_database_permission("can_manage_users")
def dashboard_api_users_delete():
data = request.get_json()
discord_id = data.get("discord_id")
if not discord_id:
return jsonify({"success": False, "error": "Missing Discord ID"}), 400
user = db.db["dashboard_users"].find_one({"discord_id": discord_id})
if user and user.get("role") == "owner":
return jsonify({"success": False, "error": "Cannot delete owner"}), 403
db.db["dashboard_users"].delete_one({"discord_id": discord_id})
return jsonify({"success": True})
# --- Dashboard Users API for Permissions Tab ---
@app.route('/dashboard/api/users', methods=['GET'])
@dashboard_login_required
def dashboard_api_users():
user = session.get('discord_user')
perms = get_user_permissions(user["id"]) if user and "id" in user else {"permissions": {}}
if not perms["permissions"].get("can_manage_users"):
return jsonify([]), 403
users = list(db.db["dashboard_users"].find({}, {"_id": 0}))
discord_info_cache = {}
formatted = []
for u in users:
discord_id = u.get("discord_id", "")
# Fetch Discord info only once per user
if discord_id not in discord_info_cache:
discord_info_cache[discord_id] = DiscordAPI.get_user_info(discord_id)
info = discord_info_cache[discord_id]
formatted.append({
"avatar_url": info.get("avatar_url") or f"https://cdn.discordapp.com/embed/avatars/{int(discord_id or '0') % 5}.png",
"avatar_gif": info.get("avatar_gif"),
"username": info.get("username", "Unknown User"),
"id": discord_id,
"role": u.get("role", "user"),
"permissions": u.get("permissions", {})
})
return jsonify(formatted)
# Add this to your app.py
@app.route('/dashboard/api/users/<user_id>', methods=['GET'])
@dashboard_login_required
def get_user_details(user_id):
"""Get details for a specific user"""
try:
user = db.db["dashboard_users"].find_one({"discord_id": user_id})
if not user:
return jsonify({"error": "User not found"}), 404
# Get Discord info with avatar
discord_info = DiscordAPI.get_user_info(user_id)
# Check if user is owner (prevent editing owners)
is_owner = user.get("role") == "owner"
return jsonify({
"id": user_id,
"username": discord_info.get("username", "Unknown User"),
"avatar_url": discord_info.get("avatar_url", ""),
"role": user.get("role", "user"),
"permissions": user.get("permissions", {}),
"is_owner": is_owner
})
except Exception as e:
logger.error(f"Error getting user details: {e}")
return jsonify({"error": "Internal server error"}), 500
# --- Dashboard Route: Pass user info for header and permissions for tabs ---
@app.route("/dashboard")
@dashboard_login_required
def dashboard():
# Get user info from session
user = session.get("discord_user")
user_id = user["id"] if user and "id" in user else None
# Always provide a permissions dict for template safety
user_permissions = get_user_permissions(user_id) if user_id else {"role": "user", "permissions": {}}
# Provide Discord info for header
user_discord_info = user if user else None
# List collections for DB viewer
db_collections = db.db.list_collection_names() if hasattr(db, "db") else []
return render_template(
"dashboard.html",
user_permissions=user_permissions,
user_discord_info=user_discord_info,
db_collections=db_collections
)
# --- Role mapping: Discord Role ID to Display Name ---
DISCORD_ROLE_MAP = {
# Replace these with your actual Discord role IDs
"1371407916581523468": "TACT Owner",
"1371922931172507678": "Tech House Owner",
"1371416000054038661": "TikTok Manager",
"1369443343515389972": "Discord Admin",
"1369442372408180757": "Discord Moderator",
"1369442992796074075": "Discord Support",
}
GUILD_ID = os.getenv("DISCORD_GUILD_ID") # Set this in your .env
# Fetch and group users by role
import collections
def get_guild_members_by_role():
if not GUILD_ID or not Config.DISCORD_BOT_TOKEN:
logger.error("Missing GUILD_ID or DISCORD_BOT_TOKEN")
return {
"Bot Team": [],
"Tiktok Team": [],
"Discord Team": [],
"TACT Owner": []
}
url = f"https://discord.com/api/v10/guilds/{GUILD_ID}/members?limit=1000"
headers = {"Authorization": f"Bot {Config.DISCORD_BOT_TOKEN}"}
try:
resp = requests.get(url, headers=headers, timeout=CONFIG["REQUEST_TIMEOUT"])
logger.info(f"Discord API /members status: {resp.status_code}")
if resp.status_code != 200:
logger.error(f"Failed to fetch guild members: {resp.status_code} {resp.text}")
return {
"Bot Team": [],
"Tiktok Team": [],
"Discord Team": [],
"TACT Owner": []
}
members = resp.json()
logger.info(f"Fetched {len(members)} members from Discord API.")
if not members:
logger.error(f"Discord API returned no members. Raw response: {resp.text}")
# Role priority: higher index = higher priority
role_priority = [
"TACT Owner",
"Tech House Owner",
"TikTok Manager",
"Discord Admin",
"Discord Moderator",
"Discord Support"
]
role_section_map = {
"TACT Owner": "Bot Team",
"Tech House Owner": "Tiktok Team",
"TikTok Manager": "Tiktok Team",
"Discord Admin": "Discord Team",
"Discord Moderator": "Discord Team",
"Discord Support": "Discord Team"
}
user_best_role = {}
user_best_role_name = {}
tact_owners = []
for member in members:
user = member["user"]
roles = member.get("roles", [])
avatar = user.get('avatar')
# Find the highest role for this user
best_role = None
best_priority = len(role_priority)
for role_id in roles:
role_name = DISCORD_ROLE_MAP.get(role_id)
if role_name and role_name in role_priority:
pri = role_priority.index(role_name)
if pri < best_priority:
best_priority = pri
best_role = role_name
if best_role:
user_best_role[user["id"]] = best_role
user_info = {
"id": user["id"],
"username": user.get("username", "Unknown"),
"avatar_url": f"https://cdn.discordapp.com/avatars/{user['id']}/{avatar}.png?size=128" if avatar else CONFIG["DEFAULT_AVATAR_URL"].format(int(user["id"]) % 5),
"avatar_gif": f"https://cdn.discordapp.com/avatars/{user['id']}/{avatar}.gif?size=128" if avatar and isinstance(avatar, str) and avatar.startswith('a_') else None,
"role": best_role
}
user_best_role_name[user["id"]] = user_info
if best_role == "TACT Owner":
tact_owners.append(user_info)
# Assign users to sections based on their highest role
sections = {
"Bot Team": [],
"Tiktok Team": [],
"Discord Team": [],
"TACT Owner": tact_owners
}
for user_id, info in user_best_role_name.items():
section = role_section_map.get(info["role"])
if section:
sections[section].append(info)
# Sort each section by role priority and username for a clean UI
def sort_key(u):
return (role_priority.index(u["role"]), u["username"].lower())
for key in sections:
sections[key] = sorted(sections[key], key=sort_key)
return sections
except Exception as e:
logger.error(f"Error fetching guild members: {e}")
return {
"Bot Team": [],
"Tiktok Team": [],
"Discord Team": [],
"TACT Owner": []
}
@app.route('/dashboard/api/update_document', methods=['POST'])
@dashboard_login_required
@require_database_permission("can_edit_documents")
def api_update_document():
collection_name = request.args.get('col')
doc_id = request.args.get('id')
if not collection_name or not doc_id:
return jsonify({'success': False, 'error': 'Missing collection or document ID'}), 400
try:
changes = request.get_json()
if not changes:
return jsonify({'success': False, 'error': 'No changes provided'}), 400
# Convert string IDs to ObjectId
if '_id' in changes and isinstance(changes['_id'], str):
changes['_id'] = ObjectId(changes['_id'])
collection = db.db[collection_name]
result = collection.update_one(
{'_id': ObjectId(doc_id)},
{'$set': changes}
)
if result.modified_count > 0 or result.matched_count > 0:
return jsonify({'success': True})
else:
return jsonify({'success': False, 'error': 'No changes made'})
except Exception as e:
app.logger.error(f"Error updating document: {str(e)}")
return jsonify({'success': False, 'error': str(e)}), 500
@app.route('/dashboard/api/delete_document', methods=['DELETE', 'POST'])
@dashboard_login_required
@require_database_permission("can_delete_documents")
def api_delete_document():
# Try to get parameters from both query string and JSON body for backward compatibility
collection_name = request.args.get('col') or (request.json.get('collection') if request.is_json else None)
doc_id = request.args.get('id') or (request.json.get('docId') or request.json.get('id') if request.is_json else None)
if not collection_name or not doc_id:
return jsonify({'success': False, 'error': 'Missing collection or document ID'}), 400
try:
collection = db.db[collection_name]
result = collection.delete_one({'_id': ObjectId(doc_id)})
if result.deleted_count > 0:
return jsonify({'success': True})
else:
return jsonify({'success': False, 'error': 'Document not found'}), 404
except Exception as e:
app.logger.error(f"Error deleting document: {str(e)}")
return jsonify({'success': False, 'error': str(e)}), 500
@app.route('/dashboard/api/delete_field', methods=['POST'])
@dashboard_login_required
def api_delete_field():
if not request.json:
return jsonify({'success': False, 'error': 'Invalid request'}), 400
collection_name = request.json.get('collection')
doc_id = request.json.get('docId')
field = request.json.get('field')
if not collection_name or not doc_id or not field:
return jsonify({'success': False, 'error': 'Missing parameters'}), 400
try:
collection = db.db[collection_name]
result = collection.update_one(
{'_id': ObjectId(doc_id)},
{'$unset': {field: ""}}
)
if result.modified_count > 0:
return jsonify({'success': True})
else:
return jsonify({'success': False, 'error': 'No changes made'})
except Exception as e:
return jsonify({'success': False, 'error': str(e)}), 500