bace1920 / v2ray-grpc-sdk-php

V2ray的gRPC接口sdk,由protoc生成。
MIT License
6 stars 3 forks source link

请问能给一个php的调用例子吗 #1

Open iddddg opened 5 years ago

iddddg commented 5 years ago

如题,谢谢

bace1920 commented 5 years ago

以下代码仅供参考

<?php
namespace App\Service;

use Bluehead\V2ray\Core\App\Stats\Command\GetStatsRequest;
use Bluehead\V2ray\Core\App\Stats\Command\GetStatsResponse;
use Bluehead\V2ray\Core\App\Stats\Command\StatsServiceClient;
use Grpc\ChannelCredentials;

class V2rayService

//    rpc call example
    public function getStats()
    {
        $client = new StatsServiceClient('127.0.0.1:10085', ['credentials' => ChannelCredentials::createInsecure()]);
        $resquest = (new GetStatsRequest())->setName("inbound>>>localsocksinbound>>>traffic>>>downlink")->setReset(false);
        $call = $client->GetStats($arg);
        /** @var GetStatsResponse $resp */
        list($resp, $status) = $call->wait();
        $stat = $resp->getStat();
   }

}
iddddg commented 5 years ago

以下代码仅供参考

<?php
namespace App\Service;

use Bluehead\V2ray\Core\App\Stats\Command\GetStatsRequest;
use Bluehead\V2ray\Core\App\Stats\Command\GetStatsResponse;
use Bluehead\V2ray\Core\App\Stats\Command\StatsServiceClient;
use Grpc\ChannelCredentials;

class V2rayService

//    rpc call example
    public function getStats()
    {
        $client = new StatsServiceClient('127.0.0.1:10085', ['credentials' => ChannelCredentials::createInsecure()]);
        $resquest = (new GetStatsRequest())->setName("inbound>>>localsocksinbound>>>traffic>>>downlink")->setReset(false);
        $call = $client->GetStats($arg);
        /** @var GetStatsResponse $resp */
        list($resp, $status) = $call->wait();
        $stat = $resp->getStat();
   }

}

刚刚自己就这么写的,折腾半天不成功,后来发现安全组没开端口,晕 再次感谢

nickfox-taterli commented 4 years ago

Update:终于给我搞明白了,下面是可用代码.

功能:添加一个新端口,指向一个新的输入.

$client = new \Bluehead\V2ray\Core\App\Proxyman\Command\HandlerServiceClient('127.0.0.1:54431', ['credentials' => \Grpc\ChannelCredentials::createInsecure()]);
$account = (new \Bluehead\V2ray\Core\Proxy\Vmess\Account())->setId('66ad4540-b58c-4ad2-9926-ea63445a9b57')->setAlterId(16);
$account = (new \Bluehead\V2ray\Core\Common\Serial\TypedMessage())->setValue($account->serializeToString())->setType('v2ray.core.proxy.vmess.Account');
$port = (new \Bluehead\V2ray\Core\Common\Net\PortRange())->setTo(10240)->setFrom(10240);
$receiver = (new \Bluehead\V2ray\Core\App\Proxyman\ReceiverConfig())->setPortRange($port);
$receiver = (new \Bluehead\V2ray\Core\Common\Serial\TypedMessage())->setValue($receiver->serializeToString())->setType('v2ray.core.app.proxyman.ReceiverConfig');
$user = (new \Bluehead\V2ray\Core\Common\Protocol\User())->setAccount($account);
$proxy = (new \Bluehead\V2ray\Core\Proxy\Vmess\Inbound\Config())->setUser([$user]);
$proxy = (new \Bluehead\V2ray\Core\Common\Serial\TypedMessage())->setValue($proxy->serializeToString())->setType('v2ray.core.proxy.vmess.inbound.Config');
$config = (new \Bluehead\V2ray\Core\InboundHandlerConfig)->setTag('hello')->setReceiverSettings($receiver)->setProxySettings($proxy);
$resquest = (new \Bluehead\V2ray\Core\App\Proxyman\Command\AddInboundRequest())->setInbound($config);
$call = $client->AddInbound($resquest);
list($resp, $status) = $call->wait();
var_dump($status);
var_dump($resp);