-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFemiwikiCrawlingBlockerHooks.php
More file actions
285 lines (256 loc) · 7.47 KB
/
FemiwikiCrawlingBlockerHooks.php
File metadata and controls
285 lines (256 loc) · 7.47 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
<?php
/**
* FemiwikiCrawlingBlocker
* @license MIT
*
* A MediaWiki extension to block bots using reCAPTCHA.
* Reference: https://github.com/mywikis/CrawlerProtection/
*/
namespace FemiwikiCrawlingBlocker;
use Config;
use MWException;
use RequestContext;
use User;
use WebRequest;
class FemiwikiCrawlingBlockerHooks implements
\MediaWiki\Hook\MediaWikiPerformActionHook,
\MediaWiki\SpecialPage\Hook\SpecialPageBeforeExecuteHook
{
private const COOKIE_NAME = "FemiwikiCrawlingBlockerPassed";
/** 1일 (1 day) */
private const COOKIE_EXPIRY = 86400;
/** @var Config */
private $config;
/**
* @param Config $config
*/
public function __construct( Config $config ) {
$this->config = $config;
}
/**
* MediaWiki 액션 처리 시 CAPTCHA 검증을 수행합니다.
*
* Perform CAPTCHA verification during MediaWiki actions.
*
* @inheritDoc
*/
public function onMediaWikiPerformAction(
$output,
$article,
$title,
$user,
$request,
$ActionEntryPoint
) {
if ( !$this->config->get( 'FemiwikiCrawlingBlockerEnabled' ) ) {
return true;
}
$type = $request->getVal( "type" );
$action = $request->getVal( "action" );
$diffId = (int)$request->getVal( "diff" );
$oldId = (int)$request->getVal( "oldid" );
if (
!$user->isRegistered() &&
( $type === "revision" ||
$action === "history" ||
$diffId > 0 ||
$oldId > 0 )
) {
return self::captchaExec( $request, $user );
}
return true;
}
/**
* SpecialPage 실행 전 CAPTCHA 검증을 수행합니다.
*
* Perform CAPTCHA verification before executing a SpecialPage.
*
* @inheritDoc
*/
public function onSpecialPageBeforeExecute(
$specialPage,
$subPage
) {
if ( !$this->config->get( 'FemiwikiCrawlingBlockerEnabled' ) ) {
return true;
}
$user = $specialPage->getContext()->getUser();
$request = RequestContext::getMain()->getRequest();
return self::captchaExec( $request, $user );
}
/**
* CAPTCHA 검증 로직을 실행합니다.
*
* Execute the CAPTCHA verification logic.
*
* @param WebRequest $request
* @param User $user
* @return bool
*/
private static function captchaExec( WebRequest $request, User $user ): bool {
try {
$requestUrl = $request->getRequestURL();
} catch ( MWException $e ) {
return true;
}
$verifyCode = self::generateVerifyCode();
if (
isset( $_COOKIE[self::COOKIE_NAME] ) &&
hash_equals( $_COOKIE[self::COOKIE_NAME], $verifyCode )
) {
return true;
}
if ( $request->wasPosted() ) {
$token = $request->getVal( "g-recaptcha-response" );
if ( self::verifyRecaptcha( $token ) ) {
setcookie( self::COOKIE_NAME, $verifyCode, [
"expires" => time() + self::COOKIE_EXPIRY,
"path" => "/",
"httponly" => true,
"secure" => true,
"samesite" => "Lax",
] );
header( "Location: " . $requestUrl );
exit();
}
}
http_response_code( 429 );
return self::outputRecaptchaPage( $requestUrl );
}
/**
* CAPTCHA 검증용 해시를 생성합니다.
*
* Generate the verification hash for CAPTCHA.
*
* @return string
*/
private static function generateVerifyCode(): string {
global $wgReCaptchaSiteKey, $wgReCaptchaSecretKey;
$keys = [
// 날짜가 바뀌면 다시 시작
"DATE" => date( "Y-m-d" ),
"HOSTNAME" => $_SERVER["HTTP_HOST"] ?? "unknown",
"REMOTE_ADDR" => $_SERVER["REMOTE_ADDR"] ?? "unknown",
"REMOTE_HOST" => $_SERVER["REMOTE_HOST"] ?? "unknown",
"SERVER_SOFTWARE" => $_SERVER["SERVER_SOFTWARE"] ?? "unknown",
"HTTP_HOST" => $_SERVER["HTTP_HOST"] ?? "HTTP_HOST unknown",
"HTTP_X_FORWARDED_HOST" =>
$_SERVER["HTTP_X_FORWARDED_HOST"] ??
"HTTP_X_FORWARDED_HOST unknown",
"DOCUMENT_ROOT" =>
$_SERVER["DOCUMENT_ROOT"] ?? "DOCUMENT_ROOT unknown",
"HTTP_ACCEPT_LANGUAGE" =>
$_SERVER["HTTP_ACCEPT_LANGUAGE"] ??
"HTTP_ACCEPT_LANGUAGE unknown",
"PHP_CPPFLAGS" =>
$_SERVER["PHP_CPPFLAGS"] ?? "PHP_CPPFLAGS unknown",
"WG_DB_USER" => $_SERVER["WG_DB_USER"] ?? "WG_DB_USER not set",
"MEDIAWIKI_SERVER" =>
$_SERVER["MEDIAWIKI_SERVER"] ?? "MEDIAWIKI_SERVER not set",
"SITE_KEY" => $wgReCaptchaSiteKey,
"SECRET_KEY" => $wgReCaptchaSecretKey,
"GPG_KEYS" => $_SERVER["GPG_KEYS"] ?? "GPG_KEYS not set",
"mtime" =>
filemtime( realpath( __DIR__ . "/../../LocalSettings.php" ) ) ??
"LocalSettings.php not found",
];
$seed = sha1( implode( "salt-[RecaptchaBlocker]-salt", $keys ) );
foreach ( $keys as $k => $v ) {
$suffix = empty( $v ) ? "empty" : $v;
$seed .= sha1(
$seed . "salt-[RecaptchaBlocker]-salt-{$k}-{$suffix}"
);
}
return sha1( "RecaptchaBlocker-Check-Key" . $seed );
}
/**
* Google reCAPTCHA API를 호출하여 응답을 검증합니다.
*
* Verify the response by calling the Google reCAPTCHA API.
*
* @param string $token
* @return bool
*/
private static function verifyRecaptcha( string $token ): bool {
global $wgReCaptchaSecretKey;
$reCaptchaPayload = http_build_query( [
"secret" => $wgReCaptchaSecretKey,
"response" => $token,
] );
$ch = curl_init( "https://www.google.com/recaptcha/api/siteverify" );
curl_setopt_array( $ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $reCaptchaPayload,
CURLOPT_TIMEOUT => 5,
] );
$response = curl_exec( $ch );
curl_close( $ch );
if ( !$response ) {
return false;
}
$result = json_decode( $response, true );
return $result["success"] ?? false;
}
/**
* CAPTCHA 페이지를 출력하여 봇 검증을 수행합니다.
*
* Output the CAPTCHA verification page.
*
* @param string $currentUrl
* @return bool
*/
private static function outputRecaptchaPage( string $currentUrl ): bool {
global $wgReCaptchaSiteKey;
header( "Content-Type: text/html; charset=utf-8" );
$currentUrl = htmlspecialchars( $currentUrl );
$siteKey = $wgReCaptchaSiteKey;
$translate = "wfMessage";
echo <<<HTML
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8">
<title>{$translate("recaptcha-blocker-title")->text()}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://www.google.com/recaptcha/api.js?render={$siteKey}"></script>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { background: #263038; height: 100%; }
.loader { width: 48px; height: 48px; border: 5px solid #fff; border-bottom-color: transparent;
border-radius: 50%; animation: spin 1s linear infinite; margin: 20px auto; }
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
#container { display: flex; align-items: center; justify-content: center; height: 100%; }
#box { background: rgba(0,0,0,0.3); border-radius: 10px; padding: 70px 20px; text-align: center; width: 300px; }
#box .message { color: #fff; font-size: 20px; margin-bottom: 40px; }
</style>
</head>
<body>
<div id="container">
<div id="box">
<div class="message">
{$translate("recaptcha-blocker-please-wait")->text()}
<br>
{$translate("recaptcha-blocker-please-wait-description")->text()}
</div>
<div class="loader"></div>
<form method="post">
<input type="hidden" name="original-url" value="{$currentUrl}">
<input type="hidden" name="g-recaptcha-response" id="g-recaptcha-response">
</form>
</div>
</div>
<script>
grecaptcha.ready(function() {
grecaptcha.execute('{$siteKey}', { action: 'homepage' }).then(function(token) {
document.getElementById('g-recaptcha-response').value = token;
document.forms[0].submit();
});
});
</script>
</body>
</html>
HTML;
exit();
}
}