Skip to content

Commit 63f1758

Browse files
authored
Update elephant.io (#3)
* Remove polling fallback * Update elephantio * Update phpstan * Replace deprecated method for elephant.io client * Replace deprecated method * Update elephant.io client * Update elephantio * Fix update * Update elephantio to latest version and use 'wait' instead of 'drain' * Update elephantio to latest version
1 parent ab8261b commit 63f1758

File tree

4 files changed

+23
-221
lines changed

4 files changed

+23
-221
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"ext-json": "*",
1313
"guzzlehttp/guzzle": "^7.8",
1414
"symfony/console": "^6.4|^7.0",
15-
"elephantio/elephant.io": "4.3.*"
15+
"elephantio/elephant.io": "^4.8"
1616
},
1717
"require-dev": {
1818
"phpstan/phpstan": "^1.10"

composer.lock

Lines changed: 16 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Service/ScannerService.php

Lines changed: 5 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -221,17 +221,6 @@ private function scanPad(ScannerServiceCallbackInterface $callback): void
221221
$this->doSocketWebsocket($socketIoVersion, $cookieString, $callback, $token);
222222
} catch (Exception $e) {
223223
$callback->onScanPadException($e);
224-
225-
try {
226-
if ($socketIoVersion === ElephantClient::CLIENT_4X) {
227-
$this->doSocketPolling4($cookies, $token, $callback);
228-
return;
229-
}
230-
231-
$this->doSocketPolling($socketIoVersion, $cookies, $token, $callback);
232-
} catch (Exception $e) {
233-
$callback->onScanPadException($e);
234-
}
235224
}
236225
}
237226

@@ -356,191 +345,6 @@ private function scanHealth(ScannerServiceCallbackInterface $callback): void
356345
}
357346
}
358347

359-
private function doSocketPolling(
360-
int $socketIoVersion,
361-
CookieJar $cookies,
362-
string $token,
363-
ScannerServiceCallbackInterface $callback
364-
): void {
365-
$engine = ElephantClient::engine($socketIoVersion, '');
366-
367-
$queryParameters = [
368-
'padId' => $this->padId,
369-
'EIO' => $engine->getOptions()['version'],
370-
'transport' => 'polling',
371-
't' => Yeast::yeast(),
372-
'b64' => 1,
373-
];
374-
375-
$response = $this->client->get($this->baseUrl . 'socket.io/', [
376-
'query' => $queryParameters,
377-
'cookies' => $cookies,
378-
]);
379-
$body = (string)$response->getBody();
380-
if ($body === 'Welcome to socket.io.') {
381-
$this->packageVersion = '1.4.0';
382-
throw new Exception('Socket.io 1 not supported');
383-
}
384-
$curlyBracketPos = strpos($body, '{');
385-
if ($curlyBracketPos === false) {
386-
throw new Exception('No JSON response: ' . $body);
387-
}
388-
$body = substr($body, $curlyBracketPos);
389-
$data = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
390-
$sid = $data['sid'];
391-
392-
$queryParameters['sid'] = $sid;
393-
$queryParameters['t'] = Yeast::yeast();
394-
395-
$response = $this->client->get($this->baseUrl . 'socket.io/', [
396-
'query' => $queryParameters,
397-
'cookies' => $cookies,
398-
]);
399-
$body = (string)$response->getBody();
400-
if ($body !== '2:40') {
401-
throw new Exception('Invalid response: ' . $body);
402-
}
403-
404-
$postData = json_encode([
405-
'message',
406-
[
407-
'component' => 'pad',
408-
'type' => 'CLIENT_READY',
409-
'padId' => $this->padId,
410-
'sessionID' => 'null',
411-
'token' => $token,
412-
'password' => null,
413-
'protocolVersion' => 2,
414-
]
415-
]);
416-
417-
$queryParameters['t'] = Yeast::yeast();
418-
$response = $this->client->post($this->baseUrl . 'socket.io/', [
419-
'query' => $queryParameters,
420-
'body' => (mb_strlen($postData) + 2) . ':42' . $postData,
421-
'cookies' => $cookies,
422-
]);
423-
$body = (string)$response->getBody();
424-
if ($body !== 'ok') {
425-
throw new Exception('Invalid response: ' . $body);
426-
}
427-
428-
$queryParameters['t'] = Yeast::yeast();
429-
$response = $this->client->get($this->baseUrl . 'socket.io/', [
430-
'query' => $queryParameters,
431-
'cookies' => $cookies,
432-
]);
433-
$this->handleClientVarsResponse($response, $callback);
434-
}
435-
436-
private function doSocketPolling4(
437-
CookieJar $cookies,
438-
string $token,
439-
ScannerServiceCallbackInterface $callback
440-
): void {
441-
$queryParameters = [
442-
'padId' => $this->padId,
443-
'EIO' => 4,
444-
'transport' => 'polling',
445-
't' => Yeast::yeast(),
446-
'b64' => 1,
447-
];
448-
449-
$response = $this->client->get($this->baseUrl . 'socket.io/', [
450-
'query' => $queryParameters,
451-
'cookies' => $cookies,
452-
]);
453-
$body = (string)$response->getBody();
454-
$curlyBracketPos = strpos($body, '{');
455-
if ($curlyBracketPos === false) {
456-
throw new Exception('No JSON response: ' . $body);
457-
}
458-
$body = substr($body, $curlyBracketPos);
459-
$data = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
460-
$sid = $data['sid'];
461-
462-
$queryParameters['sid'] = $sid;
463-
$queryParameters['t'] = Yeast::yeast();
464-
465-
$response = $this->client->post($this->baseUrl . 'socket.io/', [
466-
'query' => $queryParameters,
467-
'cookies' => $cookies,
468-
'body' => '40',
469-
]);
470-
$body = (string)$response->getBody();
471-
if ($body !== 'ok') {
472-
throw new Exception('Invalid response: ' . $body);
473-
}
474-
475-
$queryParameters['t'] = Yeast::yeast();
476-
477-
$response = $this->client->get($this->baseUrl . 'socket.io/', [
478-
'query' => $queryParameters,
479-
'cookies' => $cookies,
480-
]);
481-
$body = (string)$response->getBody();
482-
483-
if (str_starts_with($body, '40') === false) {
484-
throw new Exception('Invalid response: ' . $body);
485-
}
486-
487-
$postData = json_encode([
488-
'message',
489-
[
490-
'component' => 'pad',
491-
'type' => 'CLIENT_READY',
492-
'padId' => $this->padId,
493-
'sessionID' => 'null',
494-
'token' => $token,
495-
'password' => null,
496-
'protocolVersion' => 2,
497-
]
498-
]);
499-
500-
$queryParameters['t'] = Yeast::yeast();
501-
$response = $this->client->post($this->baseUrl . 'socket.io/', [
502-
'query' => $queryParameters,
503-
'body' => '42' . $postData,
504-
'cookies' => $cookies,
505-
]);
506-
$body = (string)$response->getBody();
507-
if ($body !== 'ok') {
508-
throw new Exception('Invalid response: ' . $body);
509-
}
510-
511-
$queryParameters['t'] = Yeast::yeast();
512-
$response = $this->client->get($this->baseUrl . 'socket.io/', [
513-
'query' => $queryParameters,
514-
'cookies' => $cookies,
515-
]);
516-
$this->handleClientVarsResponse($response, $callback);
517-
}
518-
519-
private function handleClientVarsResponse(
520-
ResponseInterface $response,
521-
ScannerServiceCallbackInterface $callback,
522-
): void
523-
{
524-
$body = (string)$response->getBody();
525-
$body = substr($body, strpos($body, '['));
526-
$data = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
527-
$data = $data[1];
528-
$accessStatus = $data['accessStatus'] ?? null;
529-
if ($accessStatus === 'deny') {
530-
$callback->onScanPadException(new EtherpadServiceNotPublicException('Pads are not publicly accessible'));
531-
return;
532-
}
533-
534-
$version = $data['data']['plugins']['plugins']['ep_etherpad-lite']['package']['version'];
535-
$onlyPlugins = $data['data']['plugins']['plugins'];
536-
unset($onlyPlugins['ep_etherpad-lite']);
537-
538-
$this->packageVersion = $version;
539-
$callback->onClientVars($version, $data);
540-
$callback->onScanPluginsList($onlyPlugins);
541-
$callback->onScanPadSuccess();
542-
}
543-
544348
private function doSocketWebsocket(
545349
int $socketIoVersion,
546350
string $cookieString,
@@ -560,7 +364,7 @@ private function doSocketWebsocket(
560364
]
561365
]), $callback->getConsoleLogger());
562366

563-
$socketIoClient->initialize();
367+
$socketIoClient->connect();
564368
$socketIoClient->of('/');
565369
$socketIoClient->emit('message', [
566370
'component' => 'pad',
@@ -572,12 +376,8 @@ private function doSocketWebsocket(
572376
'protocolVersion' => 2,
573377
]);
574378

575-
$expirationTime = microtime(true) + 2;
576-
577-
while (microtime(true) < $expirationTime) {
578-
usleep(10000);
579-
$result = $socketIoClient->drain();
580-
if ($result !== null && is_array($result->data)) {
379+
while ($result = $socketIoClient->wait('message', 2)) {
380+
if (is_array($result->data)) {
581381
$accessStatus = $result->data['accessStatus'] ?? null;
582382
if ($accessStatus === 'deny') {
583383
$callback->onScanPadException(new EtherpadServiceNotPublicException('Pads are not publicly accessible'));
@@ -600,11 +400,11 @@ private function doSocketWebsocket(
600400
}
601401
}
602402

603-
$socketIoClient->close();
403+
$socketIoClient->disconnect();
604404
}
605405

606406
public function getBaseUrl(): string
607407
{
608408
return $this->baseUrl;
609409
}
610-
}
410+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
[debug] Receive: HTTP/1.1 400 Bad Request
1+
[debug] Stream receive: HTTP/1.1 400 Bad Request
22
[INFO] Package version: 1.8.17

0 commit comments

Comments
 (0)