@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PhabricatorClientRateLimit
4 extends PhabricatorClientLimit {
5
6 protected function getBucketDuration() {
7 return 60;
8 }
9
10 protected function getBucketCount() {
11 return 5;
12 }
13
14 protected function shouldRejectConnection($score) {
15 $limit = $this->getLimit();
16
17 // Reject connections if the average score across all buckets exceeds the
18 // limit.
19 $average_score = $score / $this->getBucketCount();
20
21 return ($average_score > $limit);
22 }
23
24 protected function getConnectScore() {
25 return 0;
26 }
27
28 protected function getPenaltyScore() {
29 return 1;
30 }
31
32 protected function getDisconnectScore(array $request_state) {
33 $score = 1;
34
35 // If the user was logged in, let them make more requests.
36 if (isset($request_state['viewer'])) {
37 $viewer = $request_state['viewer'];
38 if ($viewer->isOmnipotent()) {
39 // If the viewer was omnipotent, this was an intracluster request or
40 // some other kind of special request, so don't give it any points
41 // toward rate limiting.
42 $score = 0;
43 } else if ($viewer->isLoggedIn()) {
44 // If the viewer was logged in, give them fewer points than if they
45 // were logged out, since this traffic is much more likely to be
46 // legitimate.
47 $score = 0.25;
48 }
49 }
50
51 return $score;
52 }
53
54 protected function getRateLimitReason($score) {
55 $client_key = $this->getClientKey();
56
57 // NOTE: This happens before we load libraries, so we can not use pht()
58 // here.
59
60 return
61 "TOO MANY REQUESTS\n".
62 "You (\"{$client_key}\") are issuing too many requests ".
63 "too quickly.\n";
64 }
65
66}