-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunpay_useragent_rotator.py
More file actions
1521 lines (1311 loc) · 58 KB
/
Copy pathfunpay_useragent_rotator.py
File metadata and controls
1521 lines (1311 loc) · 58 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
from __future__ import annotations
import html
import json
import logging
import os
import random
import re
import threading
import time
import uuid as uuid_lib
from html.parser import HTMLParser
from pathlib import Path
from typing import TYPE_CHECKING, Any
import requests
import telebot
from telebot.types import InlineKeyboardButton as B
from telebot.types import InlineKeyboardMarkup as K
from tg_bot import CBT
if TYPE_CHECKING:
from cardinal import Cardinal
NAME = "FunPay User-Agent Rotator"
VERSION = "0.1.0"
DESCRIPTION = "Кнопочный ротатор User-Agent для FunPayCardinal с генерацией, проверкой и массовым импортом."
CREDITS = "@takouq, @llzzvvww"
UUID = "f25a286a-51f6-4d2c-b0f3-7b85966d5f55"
SETTINGS_PAGE = True
BIND_TO_DELETE = []
logger = logging.getLogger("FPC.funpay_useragent_rotator")
S_DIR = Path(f"storage/plugins/{UUID}")
SETTINGS_FILE = S_DIR / "settings.json"
STATE_FILE = S_DIR / "state.json"
CATALOG_URL_PLATFORMS = "https://whatmyuseragent.com/platforms"
CATALOG_URL_BROWSERS = "https://whatmyuseragent.com/browser"
VERIFY_URL = "https://whatmyuseragent.com/api"
DEFAULT_SELECT_PLATFORMS = 100
DEFAULT_SELECT_BROWSERS = 300
CATALOG_CACHE_TTL_SEC = 6 * 60 * 60
VERIFY_MIN_GAP_SEC = 3
SELECTOR_PAGE_SIZE = 8
RECENT_MAX = 60
CBT_OPEN = f"{CBT.PLUGIN_SETTINGS}:{UUID}"
def _cb(action: str) -> str:
return f"uarot:{action}"
CBT_BACK = _cb("back")
CBT_ROTATE = _cb("rotate")
CBT_TOGGLE_AUTO = _cb("auto")
CBT_TOGGLE_NOTIFY = _cb("notify")
CBT_TOGGLE_VERIFY = _cb("verify")
CBT_VERIFY_NOW = _cb("verify_now")
CBT_CYCLE_MODE = _cb("mode")
CBT_REFRESH = _cb("refresh")
CBT_SET_INTERVAL = _cb("set_interval")
CBT_IMPORT = _cb("import")
CBT_CLEAR_CUSTOM = _cb("clear_custom")
CBT_PLAT_MENU = _cb("plats")
CBT_BROWSER_MENU = _cb("browsers")
INPUT_INTERVAL = "interval"
INPUT_IMPORT = "import"
DEFAULT_SETTINGS: dict[str, Any] = {
"enabled": False,
"notify_enabled": True,
"verify_enabled": True,
"rotation_interval_sec": 60,
"verify_gap_sec": VERIFY_MIN_GAP_SEC,
"source_mode": "generated",
"notify_chat_id": 0,
"defaults_seeded": False,
"catalog_defaults_applied": False,
"scope_funpay_api": False,
"scope_cardinal_process": True,
"scope_server_env": False,
"selected_platforms": [],
"selected_browsers": [],
"custom_user_agents": [],
}
DEFAULT_STATE: dict[str, Any] = {
"current_user_agent": "",
"current_source": "",
"current_platform_name": "",
"current_browser_name": "",
"last_rotate_ts": 0.0,
"next_rotate_ts": 0.0,
"last_error": "",
"last_verify_ts": 0.0,
"last_verify_browser": "",
"last_verify_os": "",
"last_verify_device": "",
"last_verify_brand": "",
"last_verify_model": "",
"last_verify_raw": {},
"recent_user_agents": [],
"catalogs": {
"platforms": [],
"browsers": [],
"fetched_ts": 0.0,
},
}
FALLBACK_PLATFORMS = [
"Windows",
"Windows 11",
"Windows 10",
"Windows 8.1",
"Windows 7",
"Windows Phone",
"macOS",
"Mac OS X",
"Android",
"Android TV",
"iOS",
"iPadOS",
"Linux",
"Ubuntu",
"Debian",
"Fedora",
"Arch Linux",
"Kali Linux",
"Chrome OS",
"Raspberry Pi OS",
"FreeBSD",
"OpenBSD",
"PlayStation",
"Xbox",
"Nintendo",
"Tizen",
"webOS",
"HarmonyOS",
]
FALLBACK_BROWSERS = [
"Chrome",
"Chrome Mobile",
"Firefox",
"Firefox Mobile",
"Safari",
"Safari Mobile",
"Edge",
"Edge Mobile",
"Opera",
"Opera GX",
"Opera Mini",
"Brave",
"Vivaldi",
"Yandex Browser",
"Samsung Internet",
"DuckDuckGo",
"UC Browser",
"Chromium",
"Tor Browser",
"Waterfox",
"Pale Moon",
"SeaMonkey",
"QQ Browser",
"Coc Coc",
"Maxthon",
"Lynx",
]
IGNORED_LINK_TEXTS = {
"WhatMyUserAgent.com",
"Browser",
"Platforms",
"Brand",
"Bots",
"Application",
"API",
}
class RT:
def __init__(self) -> None:
self.lock = threading.RLock()
self.settings: dict[str, Any] = {}
self.state: dict[str, Any] = {}
self.pending_inputs: dict[tuple[int, int], dict[str, Any]] = {}
self.loop_started = False
self.tg_registered = False
self.catalog_refresh_started = False
self.requests_patched = False
self.original_request = None
R = RT()
class _AnchorTextParser(HTMLParser):
def __init__(self) -> None:
super().__init__()
self._in_anchor = False
self._buf: list[str] = []
self.items: list[str] = []
def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
if tag.lower() == "a":
self._in_anchor = True
self._buf = []
def handle_data(self, data: str) -> None:
if self._in_anchor:
self._buf.append(data)
def handle_endtag(self, tag: str) -> None:
if tag.lower() != "a":
return
self._in_anchor = False
text = "".join(self._buf).strip()
self._buf = []
if text:
self.items.append(text)
def _ensure_paths() -> None:
S_DIR.mkdir(parents=True, exist_ok=True)
def _json_copy(value: Any) -> Any:
return json.loads(json.dumps(value))
def _settings() -> dict[str, Any]:
return R.settings
def _state() -> dict[str, Any]:
return R.state
def _as_bool(value: Any, default: bool = False) -> bool:
if isinstance(value, bool):
return value
if isinstance(value, (int, float)):
return bool(int(value))
text = str(value or "").strip().lower()
if text in {"1", "true", "yes", "on", "y", "да"}:
return True
if text in {"0", "false", "no", "off", "n", "нет"}:
return False
return default
def _as_int(value: Any, default: int = 0) -> int:
try:
return int(float(str(value).strip()))
except Exception:
return default
def _human_seconds(sec: int) -> str:
sec = max(0, int(sec))
if sec >= 3600 and sec % 3600 == 0:
return f"{sec // 3600}ч"
if sec >= 60 and sec % 60 == 0:
return f"{sec // 60}м"
return f"{sec}с"
def _short_ua(value: str, limit: int = 120) -> str:
value = str(value or "").strip()
if len(value) <= limit:
return value
return value[: limit - 1] + "…"
def _storage_key(chat_id: int, user_id: int) -> tuple[int, int]:
return int(chat_id), int(user_id)
def _set_pending(chat_id: int, user_id: int, mode: str, **extra: Any) -> None:
with R.lock:
R.pending_inputs[_storage_key(chat_id, user_id)] = {"mode": mode, **extra}
def _get_pending(chat_id: int, user_id: int) -> dict[str, Any] | None:
with R.lock:
return R.pending_inputs.get(_storage_key(chat_id, user_id))
def _pop_pending(chat_id: int, user_id: int) -> dict[str, Any] | None:
with R.lock:
return R.pending_inputs.pop(_storage_key(chat_id, user_id), None)
def _clear_pending(chat_id: int, user_id: int) -> None:
with R.lock:
R.pending_inputs.pop(_storage_key(chat_id, user_id), None)
def _load_settings(force: bool = False) -> None:
with R.lock:
if R.settings and not force:
return
_ensure_paths()
data = _json_copy(DEFAULT_SETTINGS)
if SETTINGS_FILE.exists():
try:
raw = json.loads(SETTINGS_FILE.read_text(encoding="utf-8"))
if isinstance(raw, dict):
data.update(raw)
except Exception:
logger.exception("useragent_rotator: failed to load settings")
if not isinstance(data.get("selected_platforms"), list):
data["selected_platforms"] = []
if not isinstance(data.get("selected_browsers"), list):
data["selected_browsers"] = []
if not isinstance(data.get("custom_user_agents"), list):
data["custom_user_agents"] = []
R.settings = data
_save_settings()
def _save_settings() -> None:
with R.lock:
_ensure_paths()
SETTINGS_FILE.write_text(json.dumps(_settings(), ensure_ascii=False, indent=2), encoding="utf-8")
def _load_state(force: bool = False) -> None:
with R.lock:
if R.state and not force:
return
_ensure_paths()
data = _json_copy(DEFAULT_STATE)
if STATE_FILE.exists():
try:
raw = json.loads(STATE_FILE.read_text(encoding="utf-8"))
if isinstance(raw, dict):
data.update(raw)
except Exception:
logger.exception("useragent_rotator: failed to load state")
catalogs = data.get("catalogs", {})
if not isinstance(catalogs, dict):
catalogs = {}
catalogs.setdefault("platforms", [])
catalogs.setdefault("browsers", [])
catalogs.setdefault("fetched_ts", 0.0)
if not isinstance(catalogs.get("platforms"), list):
catalogs["platforms"] = []
if not isinstance(catalogs.get("browsers"), list):
catalogs["browsers"] = []
recent = data.get("recent_user_agents", [])
data["recent_user_agents"] = recent if isinstance(recent, list) else []
data["catalogs"] = catalogs
R.state = data
_save_state()
def _save_state() -> None:
with R.lock:
_ensure_paths()
STATE_FILE.write_text(json.dumps(_state(), ensure_ascii=False, indent=2), encoding="utf-8")
def _catalogs() -> dict[str, Any]:
value = _state().setdefault("catalogs", {})
if not isinstance(value, dict):
value = {}
_state()["catalogs"] = value
value.setdefault("platforms", [])
value.setdefault("browsers", [])
value.setdefault("fetched_ts", 0.0)
return value
def _normalize_scopes(*, save: bool = False) -> None:
funpay = _as_bool(_settings().get("scope_funpay_api"), False)
cardinal = _as_bool(_settings().get("scope_cardinal_process"), True)
server = _as_bool(_settings().get("scope_server_env"), False)
if server:
funpay = False
cardinal = False
elif not funpay and not cardinal:
cardinal = True
_settings()["scope_funpay_api"] = funpay
_settings()["scope_cardinal_process"] = cardinal
_settings()["scope_server_env"] = server
if save:
_save_settings()
def _scope_funpay_enabled() -> bool:
_normalize_scopes()
return _as_bool(_settings().get("scope_funpay_api"), False)
def _scope_cardinal_enabled() -> bool:
_normalize_scopes()
return _as_bool(_settings().get("scope_cardinal_process"), True)
def _scope_server_enabled() -> bool:
_normalize_scopes()
return _as_bool(_settings().get("scope_server_env"), False)
def _toggle_scope(scope_name: str) -> None:
_normalize_scopes()
if scope_name == "server":
enabled = not _scope_server_enabled()
_settings()["scope_server_env"] = enabled
if enabled:
_settings()["scope_funpay_api"] = False
_settings()["scope_cardinal_process"] = False
elif scope_name == "funpay":
_settings()["scope_funpay_api"] = not _scope_funpay_enabled()
if _settings()["scope_funpay_api"]:
_settings()["scope_server_env"] = False
elif scope_name == "cardinal":
_settings()["scope_cardinal_process"] = not _scope_cardinal_enabled()
if _settings()["scope_cardinal_process"]:
_settings()["scope_server_env"] = False
_normalize_scopes(save=True)
def _scope_summary() -> str:
parts: list[str] = []
if _scope_server_enabled():
parts.append("Ubuntu env/profile")
else:
if _scope_funpay_enabled():
parts.append("FunPay API")
if _scope_cardinal_enabled():
parts.append("Cardinal process")
return " + ".join(parts) if parts else "не выбран"
def _unique_strings(values: list[str]) -> list[str]:
seen: set[str] = set()
result: list[str] = []
for item in values:
value = re.sub(r"\s+", " ", str(item or "").strip())
if not value:
continue
key = value.casefold()
if key in seen:
continue
seen.add(key)
result.append(value)
return result
def _fetch_catalog(url: str, kind: str) -> list[str]:
response = requests.get(url, timeout=25)
response.raise_for_status()
parser = _AnchorTextParser()
parser.feed(response.text)
items: list[str] = []
for raw in parser.items:
item = re.sub(r"\s+", " ", raw).strip()
if not item or item in IGNORED_LINK_TEXTS:
continue
if item.startswith("©"):
continue
if len(item) > 80:
continue
if kind == "platforms" and item.lower() == "browser":
continue
items.append(item)
return _unique_strings(items)
def _catalog_list(kind: str) -> list[str]:
cached = _catalogs().get(kind, [])
if isinstance(cached, list) and cached:
return [str(x) for x in cached if str(x).strip()]
return FALLBACK_PLATFORMS[:] if kind == "platforms" else FALLBACK_BROWSERS[:]
def _selected_platforms() -> list[str]:
current = [str(x) for x in _settings().get("selected_platforms", []) if str(x).strip()]
catalog = _catalog_list("platforms")
return [x for x in current if x in catalog]
def _selected_browsers() -> list[str]:
current = [str(x) for x in _settings().get("selected_browsers", []) if str(x).strip()]
catalog = _catalog_list("browsers")
return [x for x in current if x in catalog]
def _seed_defaults_if_needed() -> None:
if _as_bool(_settings().get("defaults_seeded"), False):
return
changed = False
if not [x for x in _settings().get("selected_platforms", []) if str(x).strip()]:
_settings()["selected_platforms"] = _catalog_list("platforms")[:DEFAULT_SELECT_PLATFORMS]
changed = True
if not [x for x in _settings().get("selected_browsers", []) if str(x).strip()]:
_settings()["selected_browsers"] = _catalog_list("browsers")[:DEFAULT_SELECT_BROWSERS]
changed = True
_settings()["defaults_seeded"] = True
if changed:
_save_settings()
def _refresh_catalogs(force: bool = False) -> tuple[bool, str]:
catalogs = _catalogs()
fetched_ts = float(catalogs.get("fetched_ts") or 0.0)
if not force and fetched_ts > 0 and time.time() - fetched_ts < CATALOG_CACHE_TTL_SEC:
return True, "catalog cache is fresh"
try:
platforms = _fetch_catalog(CATALOG_URL_PLATFORMS, "platforms")
browsers = _fetch_catalog(CATALOG_URL_BROWSERS, "browsers")
except Exception as exc:
logger.warning("useragent_rotator: catalog refresh failed: %s", exc)
if not catalogs.get("platforms"):
catalogs["platforms"] = FALLBACK_PLATFORMS[:]
if not catalogs.get("browsers"):
catalogs["browsers"] = FALLBACK_BROWSERS[:]
_save_state()
return False, str(exc)
if platforms:
catalogs["platforms"] = platforms
if browsers:
catalogs["browsers"] = browsers
catalogs["fetched_ts"] = time.time()
current_platforms = [x for x in _selected_platforms() if x in catalogs["platforms"]]
current_browsers = [x for x in _selected_browsers() if x in catalogs["browsers"]]
if not _as_bool(_settings().get("catalog_defaults_applied"), False):
for item in catalogs["platforms"]:
if item not in current_platforms:
current_platforms.append(item)
if len(current_platforms) >= min(DEFAULT_SELECT_PLATFORMS, len(catalogs["platforms"])):
break
for item in catalogs["browsers"]:
if item not in current_browsers:
current_browsers.append(item)
if len(current_browsers) >= min(DEFAULT_SELECT_BROWSERS, len(catalogs["browsers"])):
break
_settings()["catalog_defaults_applied"] = True
_settings()["selected_platforms"] = current_platforms
_settings()["selected_browsers"] = current_browsers
_seed_defaults_if_needed()
_save_settings()
_save_state()
return True, f"platforms={len(catalogs['platforms'])}, browsers={len(catalogs['browsers'])}"
def _start_catalog_refresh(cardinal: Any | None = None, *, force: bool = False, chat_id: int | None = None) -> None:
with R.lock:
if R.catalog_refresh_started and not force:
return
R.catalog_refresh_started = True
def worker() -> None:
try:
ok, msg = _refresh_catalogs(force=force)
if chat_id:
text = (
f"Каталоги обновлены.\nПлатформ: <code>{len(_catalog_list('platforms'))}</code>\n"
f"Браузеров: <code>{len(_catalog_list('browsers'))}</code>"
) if ok else f"Не удалось обновить каталоги: <code>{html.escape(msg[:220])}</code>"
_tg_send(cardinal, text, chat_id=chat_id)
finally:
with R.lock:
R.catalog_refresh_started = False
threading.Thread(target=worker, daemon=True).start()
def _current_user_agent() -> str:
return str(_state().get("current_user_agent") or "").strip()
def _remember_recent_user_agent(user_agent: str) -> None:
recent = _state().get("recent_user_agents", [])
if not isinstance(recent, list):
recent = []
values = [str(x) for x in recent if str(x).strip()]
values.append(user_agent)
_state()["recent_user_agents"] = values[-RECENT_MAX:]
def _platform_profile(platform_name: str) -> tuple[str, str]:
name = str(platform_name or "").strip()
lower = name.lower()
if "windows phone" in lower:
return "Windows Phone 10.0; Android 6.0.1; Microsoft; Lumia 950", "mobile"
if "windows" in lower:
if "11" in lower or "10" in lower:
return "Windows NT 10.0; Win64; x64", "desktop"
if "8.1" in lower:
return "Windows NT 6.3; Win64; x64", "desktop"
if "8" in lower:
return "Windows NT 6.2; Win64; x64", "desktop"
if "7" in lower:
return "Windows NT 6.1; Win64; x64", "desktop"
return "Windows NT 10.0; Win64; x64", "desktop"
if "iphone" in lower or "ios" in lower:
return "iPhone; CPU iPhone OS 17_4 like Mac OS X", "iphone"
if "ipad" in lower or "ipados" in lower:
return "iPad; CPU OS 17_4 like Mac OS X", "ipad"
if "mac" in lower:
return "Macintosh; Intel Mac OS X 14_4", "mac"
if "chrome os" in lower or "chromebook" in lower or "fydeos" in lower:
return "CrOS x86_64 16181.47.0", "desktop"
if any(x in lower for x in ("android tv", "google tv", "smart tv", "smarttv", "webos", "tizen", "vidaa", "coolita", "whale os", "roku")):
return "Linux; Android 12; Smart TV", "tv"
if "tablet" in lower:
return "Linux; Android 14; Tablet", "tablet"
if any(x in lower for x in ("wear os", "watch", "watchos")):
return "Linux; Android 13; Wear", "wearable"
if any(x in lower for x in ("android", "lineage", "cyanogen", "fire os", "harmony", "yunos", "kaios", "plasma mobile", "sailfish", "maemo", "meego", "mocordroid", "smartisan", "blackberry", "symbian")):
return "Linux; Android 14; Mobile", "mobile"
if "playstation" in lower or lower == "ps5" or lower == "ps4":
return "PlayStation 5 1.0", "console"
if "xbox" in lower:
return "Xbox; Xbox Series X", "console"
if "nintendo" in lower or "switch" in lower:
return "Nintendo Switch; WifiWebAuthApplet", "console"
if "bsd" in lower:
return f"X11; {name}; amd64", "desktop"
if any(x in lower for x in ("solaris", "os/2", "haiku", "amiga", "aix", "irix", "hp-ux", "serenity", "morphos", "risc os")):
return name, "desktop"
if any(x in lower for x in ("ubuntu", "debian", "fedora", "arch", "kali", "suse", "gentoo", "mint", "raspbian", "raspberry", "linux")):
return f"X11; {name}; Linux x86_64", "desktop"
return f"X11; {name}; Linux x86_64", "desktop"
def _rand_chrome_version() -> tuple[int, int, int, int]:
return random.randint(122, 135), 0, random.randint(6100, 6999), random.randint(40, 180)
def _rand_firefox_version() -> int:
return random.randint(120, 137)
def _rand_webkit_version() -> tuple[int, int]:
return random.randint(605, 617), random.randint(1, 50)
def _browser_family(browser_name: str) -> str:
name = str(browser_name or "").lower()
if "internet explorer" in name or name == "ie":
return "ie"
if any(x in name for x in ("lynx", "w3m", "links")):
return "text"
if any(x in name for x in ("firefox", "waterfox", "palemoon", "pale moon", "basilisk", "seamonkey", "librewolf", "tor browser")):
return "firefox"
if "safari" in name and "chrome" not in name and "edge" not in name and "opera" not in name:
return "safari"
if any(x in name for x in ("edge", "edg")):
return "edge"
if any(x in name for x in ("opera", "opr", "gx")):
return "opera"
if "samsung" in name:
return "samsung"
if "uc browser" in name or name.startswith("uc "):
return "uc"
if "duckduckgo" in name:
return "duck"
if "brave" in name:
return "brave"
if "vivaldi" in name:
return "vivaldi"
if "yandex" in name:
return "yandex"
return "chromium"
def _build_generated_user_agent(browser_name: str, platform_name: str) -> str:
profile, device = _platform_profile(platform_name)
family = _browser_family(browser_name)
chrome_major, chrome_minor, chrome_build, chrome_patch = _rand_chrome_version()
chrome_version = f"{chrome_major}.{chrome_minor}.{chrome_build}.{chrome_patch}"
firefox_major = _rand_firefox_version()
wk_major, wk_minor = _rand_webkit_version()
webkit = f"{wk_major}.{wk_minor}.{random.randint(1, 15)}"
mobile_safari = "Mobile/15E148" if device in {"mobile", "iphone", "ipad", "tablet", "wearable"} else ""
safari_tail = "Mobile Safari/537.36" if device in {"mobile", "iphone", "ipad", "tablet", "wearable"} else "Safari/537.36"
if family == "ie":
return f"Mozilla/5.0 ({profile}; Trident/7.0; rv:11.0) like Gecko"
if family == "text":
return f"Lynx/2.{random.randint(8, 9)}.{random.randint(0, 9)} libwww-FM/2.14 SSL-MM/1.4.1"
if family == "firefox":
extra = " Mobile" if device in {"mobile", "iphone", "ipad", "tablet"} else ""
return f"Mozilla/5.0 ({profile}; rv:{firefox_major}.0) Gecko/20100101 Firefox/{firefox_major}.0{extra}"
if family == "safari":
version_major = random.randint(16, 18)
if device in {"iphone", "ipad", "tablet"}:
return (
f"Mozilla/5.0 ({profile}) AppleWebKit/{webkit} (KHTML, like Gecko) "
f"Version/{version_major}.0 {mobile_safari} Safari/{webkit}"
)
return (
f"Mozilla/5.0 ({profile}) AppleWebKit/{webkit} (KHTML, like Gecko) "
f"Version/{version_major}.0 Safari/{webkit}"
)
base = f"Mozilla/5.0 ({profile}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{chrome_version} {safari_tail}"
if family == "edge":
token = "EdgA" if device in {"mobile", "iphone", "ipad", "tablet"} else "Edg"
return f"{base} {token}/{random.randint(122, 135)}.0.{random.randint(2100, 2600)}.{random.randint(40, 120)}"
if family == "opera":
return f"{base} OPR/{random.randint(100, 118)}.0.{random.randint(4700, 5200)}.{random.randint(40, 120)}"
if family == "samsung":
mobile = profile if device in {"mobile", "tablet", "iphone", "ipad"} else "Linux; Android 14; SAMSUNG SM-S928B"
return (
f"Mozilla/5.0 ({mobile}) AppleWebKit/537.36 (KHTML, like Gecko) "
f"SamsungBrowser/{random.randint(24, 28)}.0 Chrome/{chrome_version} Mobile Safari/537.36"
)
if family == "uc":
mobile = profile if device in {"mobile", "tablet", "iphone", "ipad"} else "Linux; Android 13; Mobile"
return (
f"Mozilla/5.0 ({mobile}) AppleWebKit/537.36 (KHTML, like Gecko) "
f"Chrome/{chrome_version} Mobile Safari/537.36 UCBrowser/{random.randint(13, 15)}.0.{random.randint(1000, 2500)}.{random.randint(10, 99)}"
)
if family == "duck":
return f"{base} DuckDuckGo/{random.randint(5, 8)}"
if family == "brave":
return f"{base} Brave/{chrome_major}.1.{random.randint(40, 99)}.{random.randint(10, 180)}"
if family == "vivaldi":
return f"{base} Vivaldi/{random.randint(6, 7)}.{random.randint(0, 9)}.{random.randint(2000, 3200)}.{random.randint(10, 80)}"
if family == "yandex":
return f"{base} YaBrowser/{random.randint(24, 26)}.{random.randint(1, 9)}.{random.randint(1000, 3000)}.{random.randint(10, 99)}"
return base
def _generate_from_pool() -> dict[str, str]:
platforms = _selected_platforms()
browsers = _selected_browsers()
if not platforms:
raise RuntimeError("platform pool is empty")
if not browsers:
raise RuntimeError("browser pool is empty")
recent = set(str(x) for x in _state().get("recent_user_agents", []) if str(x).strip())
for _ in range(25):
platform_name = random.choice(platforms)
browser_name = random.choice(browsers)
user_agent = _build_generated_user_agent(browser_name, platform_name)
if user_agent not in recent:
return {
"user_agent": user_agent,
"source": "generated",
"browser_name": browser_name,
"platform_name": platform_name,
}
platform_name = random.choice(platforms)
browser_name = random.choice(browsers)
return {
"user_agent": _build_generated_user_agent(browser_name, platform_name),
"source": "generated",
"browser_name": browser_name,
"platform_name": platform_name,
}
def _pick_user_agent_entry() -> dict[str, str]:
mode = str(_settings().get("source_mode") or "generated").strip().lower()
custom = [str(x).strip() for x in _settings().get("custom_user_agents", []) if str(x).strip()]
if mode == "custom":
choices = ["custom"] if custom else ["generated"]
elif mode == "mixed":
choices = ["generated"] + (["custom"] if custom else [])
else:
choices = ["generated"]
picked = random.choice(choices)
if picked == "custom":
return {
"user_agent": random.choice(custom),
"source": "custom",
"browser_name": "custom",
"platform_name": "custom",
}
return _generate_from_pool()
def _verify_user_agent(user_agent: str, *, force: bool = False) -> tuple[bool, str]:
if not user_agent:
return False, "empty user-agent"
gap = max(VERIFY_MIN_GAP_SEC, _as_int(_settings().get("verify_gap_sec"), VERIFY_MIN_GAP_SEC))
now_ts = time.time()
last_verify_ts = float(_state().get("last_verify_ts") or 0.0)
if not force and last_verify_ts > 0 and now_ts - last_verify_ts < gap:
return False, f"verify rate limit: wait {gap - int(now_ts - last_verify_ts)}s"
try:
response = requests.get(VERIFY_URL, params={"ua": user_agent, "key": "NOTREQUIED"}, timeout=20)
response.raise_for_status()
data = response.json()
except Exception as exc:
_state()["last_error"] = str(exc)
_save_state()
return False, str(exc)
browser = data.get("browser", {}) if isinstance(data, dict) else {}
os_data = data.get("os", {}) if isinstance(data, dict) else {}
device = data.get("device", {}) if isinstance(data, dict) else {}
_state()["last_verify_ts"] = time.time()
_state()["last_verify_browser"] = str(browser.get("name") or browser.get("family") or "").strip()
_state()["last_verify_os"] = str(os_data.get("name") or os_data.get("family") or "").strip()
_state()["last_verify_device"] = str(device.get("deviceType") or "").strip()
_state()["last_verify_brand"] = str(device.get("brand") or "").strip()
_state()["last_verify_model"] = str(device.get("model") or "").strip()
_state()["last_verify_raw"] = data if isinstance(data, dict) else {}
_save_state()
return True, "ok"
def _server_profile_path() -> Path | None:
if os.name != "posix":
return None
return Path.home() / ".config" / "fpc-user-agent.env"
def _write_server_profile(user_agent: str) -> None:
os.environ["USER_AGENT"] = user_agent
os.environ["HTTP_USER_AGENT"] = user_agent
profile_path = _server_profile_path()
if profile_path is None:
return
try:
profile_path.parent.mkdir(parents=True, exist_ok=True)
escaped = user_agent.replace("\\", "\\\\").replace('"', '\\"')
profile_path.write_text(
f'export USER_AGENT="{escaped}"\nexport HTTP_USER_AGENT="{escaped}"\n',
encoding="utf-8",
)
except Exception:
logger.debug("useragent_rotator: failed to write server profile", exc_info=True)
def _install_requests_patch() -> None:
with R.lock:
if R.requests_patched:
return
R.original_request = requests.sessions.Session.request
def wrapped(session: requests.Session, method: str, url: str, **kwargs: Any) -> Any:
headers = dict(kwargs.get("headers") or {})
current_ua = _current_user_agent()
if current_ua and (_scope_cardinal_enabled() or _scope_server_enabled()):
headers["User-Agent"] = current_ua
kwargs["headers"] = headers
try:
session.headers["User-Agent"] = current_ua
except Exception:
pass
return R.original_request(session, method, url, **kwargs)
requests.sessions.Session.request = wrapped
R.requests_patched = True
def _apply_user_agent(cardinal: Any, user_agent: str) -> None:
if _scope_server_enabled():
_write_server_profile(user_agent)
if _scope_cardinal_enabled() or _scope_server_enabled():
try:
setattr(cardinal, "user_agent", user_agent)
except Exception:
pass
for owner in (cardinal, getattr(cardinal, "account", None)):
if owner is None:
continue
for attr in ("session", "requests_session", "http_session"):
obj = getattr(owner, attr, None)
if isinstance(obj, requests.Session):
try:
obj.headers["User-Agent"] = user_agent
except Exception:
pass
for attr in ("headers", "default_headers"):
obj = getattr(owner, attr, None)
if isinstance(obj, dict):
obj["User-Agent"] = user_agent
if _scope_funpay_enabled():
acc = getattr(cardinal, "account", None)
if acc is not None:
for attr in ("session", "requests_session", "http_session"):
obj = getattr(acc, attr, None)
if isinstance(obj, requests.Session):
try:
obj.headers["User-Agent"] = user_agent
except Exception:
pass
for attr in ("headers", "default_headers"):
obj = getattr(acc, attr, None)
if isinstance(obj, dict):
obj["User-Agent"] = user_agent
def _notify_chat_id(cardinal: Any) -> int:
chat_id = _as_int(_settings().get("notify_chat_id"), 0)
if chat_id > 0:
return chat_id
tg = getattr(cardinal, "telegram", None)
auth = getattr(tg, "authorized_users", []) if tg is not None else []
if isinstance(auth, (list, tuple, set)):
for item in auth:
val = _as_int(item, 0)
if val > 0:
return val
return 0
def _tg_send(cardinal: Any, text: str, *, chat_id: int | None = None, markup: Any | None = None) -> None:
tg = getattr(cardinal, "telegram", None)
if tg is None:
return
cid = int(chat_id or _notify_chat_id(cardinal) or 0)
if cid <= 0:
return
tg.bot.send_message(cid, text, reply_markup=markup, parse_mode="HTML", disable_web_page_preview=True)
def _tg_edit(cardinal: Any, call: Any, text: str, markup: Any | None = None) -> None:
try:
cardinal.telegram.bot.edit_message_text(
text,
call.message.chat.id,
call.message.id,
reply_markup=markup,
parse_mode="HTML",
disable_web_page_preview=True,
)
except Exception:
_tg_send(cardinal, text, chat_id=call.message.chat.id, markup=markup)
def _mode_label() -> str:
mode = str(_settings().get("source_mode") or "generated").strip().lower()
return {
"generated": "генерация",
"custom": "свои UA",
"mixed": "микс",
}.get(mode, mode)
def _render_panel() -> str:
_load_settings(False)
_load_state(False)
current_ua = _current_user_agent()
next_ts = float(_state().get("next_rotate_ts") or 0.0)
next_in = max(0, int(next_ts - time.time())) if next_ts > 0 else 0
platforms_total = len(_catalog_list("platforms"))
browsers_total = len(_catalog_list("browsers"))
lines = [
"<b>User-Agent Rotator</b>",
"",
f"Разработчики: <code>{html.escape(CREDITS)}</code>",
f"Авто-ротация: <b>{'включена' if _as_bool(_settings().get('enabled'), False) else 'выключена'}</b>",
f"Интервал: <code>{_human_seconds(_as_int(_settings().get('rotation_interval_sec'), 60))}</code>",
f"Охват: <code>{html.escape(_scope_summary())}</code>",
f"Уведомления: <b>{'ON' if _as_bool(_settings().get('notify_enabled'), True) else 'OFF'}</b>",
f"Проверка API: <b>{'ON' if _as_bool(_settings().get('verify_enabled'), True) else 'OFF'}</b>",
f"Источник: <code>{_mode_label()}</code>",
f"Платформ выбрано: <code>{len(_selected_platforms())}</code> / <code>{platforms_total}</code>",
f"Браузеров выбрано: <code>{len(_selected_browsers())}</code> / <code>{browsers_total}</code>",
f"Своих UA: <code>{len([x for x in _settings().get('custom_user_agents', []) if str(x).strip()])}</code>",
f"Следующая смена: <code>{_human_seconds(next_in) if next_in else '-'}</code>",
]
if _state().get("current_source"):
lines.append(f"Текущий источник: <code>{html.escape(str(_state().get('current_source')))}</code>")
if _state().get("current_platform_name"):
lines.append(f"Текущая платформа: <code>{html.escape(str(_state().get('current_platform_name')))}</code>")
if _state().get("current_browser_name"):
lines.append(f"Текущий браузер: <code>{html.escape(str(_state().get('current_browser_name')))}</code>")
if current_ua:
lines.extend(["", "<b>Текущий User-Agent</b>", f"<code>{html.escape(_short_ua(current_ua, 240))}</code>"])
verify_browser = str(_state().get("last_verify_browser") or "").strip()
verify_os = str(_state().get("last_verify_os") or "").strip()
verify_device = str(_state().get("last_verify_device") or "").strip()
verify_brand = str(_state().get("last_verify_brand") or "").strip()
if verify_browser or verify_os or verify_device or verify_brand:
bits = [x for x in [verify_browser, verify_os, verify_device, verify_brand] if x]
lines.extend(["", "<b>Последняя проверка API</b>", f"<code>{html.escape(' / '.join(bits))}</code>"])