@@ -9,18 +9,28 @@ class AccessControl
99{
1010 /** @var User[] */
1111 private array $ users = [];
12+ /** @var User[] */
13+ private array $ mediaUsers = [];
1214
1315 /** @var int Interval to remove old clients: 60 seconds */
1416 private const CLEANUP_INTERVAL_MS = 60 *1000 ;
1517 private int $ rpmLimit ;
1618 private int $ errorsLimit ;
19+
20+ private int $ mediaRpmLimit ;
21+ private int $ mediaErrorsLimit ;
22+
1723 /** @var int[] */
1824 private array $ clientsSettings ;
1925
2026 public function __construct ()
2127 {
22- $ this ->rpmLimit = (int ) Config::getInstance ()->get ('access.default_rpm ' );
23- $ this ->errorsLimit = (int ) Config::getInstance ()->get ('access.default_errors_limit ' );
28+ $ this ->rpmLimit = (int ) Config::getInstance ()->get ('access.rpm ' );
29+ $ this ->errorsLimit = (int ) Config::getInstance ()->get ('access.errors_limit ' );
30+
31+ $ this ->mediaRpmLimit = (int ) Config::getInstance ()->get ('access.media_rpm ' );
32+ $ this ->mediaErrorsLimit = (int ) Config::getInstance ()->get ('access.media_errors_limit ' );
33+
2434 $ this ->clientsSettings = (array ) Config::getInstance ()->get ('access.clients_settings ' );
2535
2636 Timer::tick (static ::CLEANUP_INTERVAL_MS , function () {
@@ -33,26 +43,38 @@ private function removeOldUsers(): void
3343 $ now = time ();
3444 foreach ($ this ->users as $ ip => $ user ) {
3545 if ($ user ->isOld ($ now )) {
36- $ this ->removeUser ($ ip );
46+ unset($ this ->users [$ ip ]);
47+ }
48+ }
49+ foreach ($ this ->mediaUsers as $ ip => $ user ) {
50+ if ($ user ->isOld ($ now )) {
51+ unset($ this ->mediaUsers [$ ip ]);
3752 }
3853 }
3954 }
4055
41- private function removeUser (string $ ip ): void
56+ public function getOrCreateUser (string $ ip, string $ type = ' default ' ): User
4257 {
43- unset($ this ->users [$ ip ]);
44- }
58+ if ($ type === 'media ' ) {
59+ if (!isset ($ this ->mediaUsers [$ ip ])) {
60+ $ this ->mediaUsers [$ ip ] = new User (
61+ $ this ->clientsSettings [$ ip ]['media_rpm ' ] ?? $ this ->clientsSettings [$ ip ]['rpm ' ] ?? $ this ->mediaRpmLimit ,
62+ $ this ->clientsSettings [$ ip ]['media_errors_limit ' ] ?? $ this ->clientsSettings [$ ip ]['errors_limit ' ] ?? $ this ->mediaErrorsLimit
63+ );
64+ }
4565
46- public function getOrCreateUser ($ ip )
47- {
48- if (!isset ($ this ->users [$ ip ])) {
49- $ user = $ this ->users [$ ip ] = new User (
50- $ this ->clientsSettings [$ ip ]['rpm ' ] ?? $ this ->rpmLimit ,
51- $ this ->clientsSettings [$ ip ]['errorsLimit ' ] ?? $ this ->errorsLimit
52- );
66+ return $ this ->mediaUsers [$ ip ];
67+ } else {
68+ if (!isset ($ this ->users [$ ip ])) {
69+ $ this ->users [$ ip ] = new User (
70+ $ this ->clientsSettings [$ ip ]['rpm ' ] ?? $ this ->rpmLimit ,
71+ $ this ->clientsSettings [$ ip ]['errors_limit ' ] ?? $ this ->errorsLimit
72+ );
73+ }
74+
75+ return $ this ->users [$ ip ];
5376 }
5477
55- return $ this ->users [$ ip ];
5678 }
5779
5880}
0 commit comments