2019-11-01 00:02:38 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-11-01 00:02:38 +03:00
|
|
|
/**
|
|
|
|
* Checks if the website for a given bridge is reachable.
|
|
|
|
*
|
|
|
|
* **Remarks**
|
|
|
|
* - This action is only available in debug mode.
|
|
|
|
* - Returns the bridge status as Json-formatted string.
|
|
|
|
* - Returns an error if the bridge is not whitelisted.
|
|
|
|
* - Returns a responsive web page that automatically checks all whitelisted
|
|
|
|
* bridges (using JavaScript) if no bridge is specified.
|
|
|
|
*/
|
2022-06-22 19:30:37 +03:00
|
|
|
class ConnectivityAction implements ActionInterface
|
|
|
|
{
|
2022-07-08 13:54:23 +03:00
|
|
|
private BridgeFactory $bridgeFactory;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
2022-08-06 23:46:28 +03:00
|
|
|
$this->bridgeFactory = new BridgeFactory();
|
2022-07-08 13:54:23 +03:00
|
|
|
}
|
|
|
|
|
2024-08-07 01:21:06 +03:00
|
|
|
public function __invoke(Request $request): Response
|
2019-11-01 00:02:38 +03:00
|
|
|
{
|
|
|
|
if (!Debug::isEnabled()) {
|
2023-09-23 19:54:14 +03:00
|
|
|
return new Response('This action is only available in debug mode!', 403);
|
2019-11-01 00:02:38 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-01-25 18:06:24 +03:00
|
|
|
$bridgeName = $request->get('bridge');
|
2023-08-01 20:35:15 +03:00
|
|
|
if (!$bridgeName) {
|
2024-08-07 01:21:06 +03:00
|
|
|
return new Response(render_template('connectivity.html.php'));
|
2019-11-01 00:02:38 +03:00
|
|
|
}
|
2023-08-01 20:35:15 +03:00
|
|
|
$bridgeClassName = $this->bridgeFactory->createBridgeClassName($bridgeName);
|
|
|
|
if (!$bridgeClassName) {
|
2023-09-10 22:50:15 +03:00
|
|
|
return new Response('Bridge not found', 404);
|
2023-08-01 20:35:15 +03:00
|
|
|
}
|
2022-11-07 20:22:54 +03:00
|
|
|
return $this->reportBridgeConnectivity($bridgeClassName);
|
2019-11-01 00:02:38 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-07-08 13:54:23 +03:00
|
|
|
private function reportBridgeConnectivity($bridgeClassName)
|
2019-11-01 00:02:38 +03:00
|
|
|
{
|
2023-06-11 04:16:03 +03:00
|
|
|
if (!$this->bridgeFactory->isEnabled($bridgeClassName)) {
|
2022-08-06 23:46:28 +03:00
|
|
|
throw new \Exception('Bridge is not whitelisted!');
|
2019-11-01 00:02:38 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-07-08 13:54:23 +03:00
|
|
|
$bridge = $this->bridgeFactory->create($bridgeClassName);
|
2019-11-01 00:02:38 +03:00
|
|
|
$curl_opts = [
|
2023-09-10 22:50:15 +03:00
|
|
|
CURLOPT_CONNECTTIMEOUT => 5,
|
|
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
|
|
];
|
|
|
|
$result = [
|
|
|
|
'bridge' => $bridgeClassName,
|
|
|
|
'successful' => false,
|
|
|
|
'http_code' => null,
|
2019-11-01 00:02:38 +03:00
|
|
|
];
|
|
|
|
try {
|
2023-09-10 22:50:15 +03:00
|
|
|
$response = getContents($bridge::URI, [], $curl_opts, true);
|
2024-07-31 18:30:06 +03:00
|
|
|
$result['http_code'] = $response->getCode();
|
|
|
|
if (in_array($result['http_code'], [200])) {
|
2023-09-10 22:50:15 +03:00
|
|
|
$result['successful'] = true;
|
2019-11-01 00:02:38 +03:00
|
|
|
}
|
2022-08-06 23:46:28 +03:00
|
|
|
} catch (\Exception $e) {
|
2019-11-01 00:02:38 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
return new Response(Json::encode($result), 200, ['content-type' => 'text/json']);
|
2019-11-01 00:02:38 +03:00
|
|
|
}
|
|
|
|
}
|