xray-grpc-php
PHP clients for the Xray gRPC API and protobuf message classes. Requires ext-grpc, grpc/grpc and google/protobuf as declared in composer.json.
Installation
Section titled “Installation”composer require localzet/xray-grpccomposer check-platform-reqsService clients
Section titled “Service clients”| Class | Purpose |
|---|---|
| Xray\App\Proxyman\Command\HandlerServiceClient | Inbound/outbound handlers |
| Xray\App\Log\Command\LoggerServiceClient | Logging control |
| Xray\App\Router\Command\RoutingServiceClient | Routes and balancers |
| Xray\App\Stats\Command\StatsServiceClient | Traffic counters and system statistics |
| Xray\Core\App\Observatory\Command\ObservatoryServiceClient | Observed outbound status |
Construct clients with an address and channel options. Unary RPC methods return a call object; wait() returns response/status. Check status.code before reading response fields. The timeout call option is measured in microseconds.
Statistics
Section titled “Statistics”GetStats reads one named counter; QueryStats selects counters by pattern; GetSysStats returns process statistics. reset controls whether reading resets the counter. The example sets reset=false and a one-second deadline. Replace inbound tag example with your configured tag and enable the corresponding counters/API in Xray configuration.
The unencrypted example channel targets loopback. Protect remote transport and restrict access: this is an administrative Xray interface.
Client methods and message fields.
Example
Section titled “Example”<?php
declare(strict_types=1);
use Grpc\ChannelCredentials;use Xray\App\Stats\Command\GetStatsRequest;use Xray\App\Stats\Command\StatsServiceClient;
require __DIR__ . '/vendor/autoload.php';
$client = new StatsServiceClient('127.0.0.1:10085', [ 'credentials' => ChannelCredentials::createInsecure(),]);$request = new GetStatsRequest();$request->setName('inbound>>>example>>>traffic>>>uplink');$request->setReset(false);[$response, $status] = $client->GetStats($request, [], ['timeout' => 1_000_000])->wait();if ($status->code !== Grpc\STATUS_OK) { throw new RuntimeException($status->details);}$stat = $response->getStat();if ($stat === null) { throw new RuntimeException('No statistic returned');}echo $stat->getValue(), PHP_EOL;Source · AGPL-3.0