国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

WebMam AI 代理故障自動(dòng)轉(zhuǎn)移的方案

mliev

先說(shuō)明新建的三個(gè)文件都是干嘛的
GlobalData.php 使用了 GlobalData變量共享組件
HealthCheck.php 按照設(shè)定的時(shí)間做健康檢查
Proxy.php 代理設(shè)置,需要將后臺(tái)的代理改成這里的設(shè)置

  1. 使用Composer添加一些依賴(lài)

    composer require workerman/globaldata
    composer require guzzlehttp/guzzle
  2. process 目錄新建 GlobalData.php 并添加代碼

<?php

namespace process;

class GlobalData extends \GlobalData\Server
{

}
  1. process 目錄新建 HealthCheck.php 并添加代碼
<?php

namespace process;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Promise\Utils;
use Psr\Http\Message\ResponseInterface;
use Workerman\Worker;

class HealthCheck
{

    //沒(méi)五分鐘做一次健康檢查
    protected bool $quitSignal = false;

    //這里填你需要監(jiān)控的代理列表
    protected array $list = [
        ['protocol' => 'https', 'domain' => 'api.xxx.ink'],
        ['protocol' => 'https', 'domain' => 'api.xxxx.fun'],
    ];

    protected \GlobalData\Client $GlobalData;

    protected string $GlobalKey = 'serviceList';

    public function onWorkerStart(Worker $worker)
    {
        $this->GlobalData = new \GlobalData\Client('127.0.0.1:2207');
        $this->quitSignal = false;

        $this->check();
    }

    public function onWorkerStop(Worker $worker)
    {
        $this->quitSignal = true;
    }

    public function check(): void
    {
        $client = new Client([
            \GuzzleHttp\RequestOptions::CONNECT_TIMEOUT => 30,
            \GuzzleHttp\RequestOptions::TIMEOUT => 30,
            \GuzzleHttp\RequestOptions::VERIFY => false,
            \GuzzleHttp\RequestOptions::HTTP_ERRORS => false,
        ]);
        $promises  = [];

        foreach ($this->list as $item) {
            $now = intval(microtime(true) * 1000);
            $protocol = $item['protocol'];
            $domain   = $item['domain'];
            try {
                $client->get("{$protocol}://$domain");
                $newTime = intval(microtime(true) * 1000) - $now;
                //echo "請(qǐng)求域名:{$domain} 響應(yīng):{$newTime}ms\n";
                $this->setKeyGlobalData($domain, $newTime);
            } catch (GuzzleException $e) {
                //echo "請(qǐng)求{$domain}失?。簕$e->getMessage()}\n";
                if (!empty($this->GlobalData->serviceList) && isset($this->GlobalData->serviceList[$domain])) {
                    //echo "域名下線:{$domain}\n";
                    $this->delKeyGlobalData($domain);
                }
            }
        }

        if (!$this->quitSignal){
            \Workerman\Timer::add(300, [$this, 'check'], [], false);
        }
    }

    protected function setKeyGlobalData($key, $value): void
    {
        $serviceList = $this->GlobalData->serviceList;
        if (empty($serviceList)) $serviceList = [];
        $serviceList[$key] = $value;
        $this->GlobalData->serviceList = $serviceList;
    }

    protected function delKeyGlobalData($key): void
    {
        $serviceList = $this->GlobalData->serviceList;
        if (empty($serviceList)) $serviceList = [];
        if (isset($serviceList[$key])){
            unset($serviceList[$key]);
        }
        $this->GlobalData->serviceList = $serviceList;
    }
}
  1. process 目錄新建 Proxy.php 并添加代碼

<?php

namespace process;

use Workerman\Connection\AsyncTcpConnection;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
use Workerman\Worker;

class Proxy
{
    //這里填和HealthCheck.php配置一樣的代理列表
    protected array $defaultHostList = [
        'api.xxx.ink',
        'api.xxx.fun'
    ];

    protected $selectChannel = [];

    //這里填非官方的代理地址和key,我用了一個(gè)第三方的GTP4的代理(密鑰=》代理地址)
    protected array $tokenHostList = [
        'sk-xxxxxxxxxxxxx' => ['api.xxx.ltd'],
    ];

    protected \GlobalData\Client $GlobalData;

    public function onWorkerStart(Worker $worker): void
    {
        $this->GlobalData = new \GlobalData\Client('127.0.0.1:2207');
    }

    public function onMessage(TcpConnection $connection, Request $request): void
    {
        $this->cluster($connection, $request);
    }

    /**
     * 集群調(diào)用
     * @param TcpConnection $connection
     * @param Request $request
     * @return void
     */
    public function cluster(TcpConnection $connection, Request $request): void
    {
        $authorization = $request->header('Authorization','');
        list(, $token) = explode(' ', $authorization);

        $hostList = $this->defaultHostList;

        if (isset($this->tokenHostList[$token])){
            $hostList = $this->tokenHostList[$token];
        }

        $hostList = $this->getHostList($hostList);

        foreach ($hostList as $host) {
            try {
                $this->proxy($connection, $request, $host);
                $this->selectChannel[$host] = true;
                return;
            } catch (\Exception $e) {
                continue;
            }
        }

        //如果全部都失敗則直接返回超時(shí)提示
        $connection->send('{"error":{"code":503,"message":"Proxy Service Timeout.","param":null,"type":"cf_service_unavailable"}}');

    }

    /**
     * @param $hostList
     * @return string[]
     */
    public function getHostList($hostList): array
    {
        $serviceList = $this->GlobalData->serviceList;

        $list_a = [];
        $list_b = [];

        foreach ($hostList as $domain) {
            if (isset($serviceList[$domain])){
                if (isset($this->selectChannel[$domain])){
                    $serviceList[$domain] = $serviceList[$domain] - 120;
                }
                $list_a[$domain] = $serviceList[$domain];
            }else{
                $list_b[] = $domain;
            }
        }
        $list_a = array_flip($list_a);
        ksort($list_a);

        $retList = [];

        foreach ($list_a as $domain) {
            $retList[] = $domain;
        }

        foreach ($list_b as $domain) {
            $retList[] = $domain;
        }

        return $retList;
    }

    /**
     * @throws \Exception
     */
    public function proxy(TcpConnection $connection, Request $request, string $host): void
    {
        $buffer = (string)$request;
        $con = new AsyncTcpConnection("tcp://$host:443", ['ssl' =>['verify_peer' => false]]);
        $buffer = preg_replace("/Host: ?(.*?)\r\n/", "Host: $host\r\n", $buffer);
        $con->transport = 'ssl';
        $connection->protocol = null;
        $con->send($buffer);
        $con->pipe($connection);
        $connection->pipe($con);
        $con->connect();
    }
}
  1. 設(shè)置配置文件

config/process.php里面增加下面的配置

'Proxy' => [
        'handler' => process\Proxy::class,
        'listen' => 'http://0.0.0.0:8988',//本地代理地址,可以自己更換
        'count' => cpu_count(),
        'reloadable' => false,
    ],
    'GlobalData' => [
        'handler' => process\GlobalData::class,
        'listen' => 'frame://127.0.0.1:2207',
        'count' => 1,
        'constructor' => [
            'ip' => '127.0.0.1',
            'port' => 2207
        ],
    ],
    'HealthCheck' => [
        'handler' => process\HealthCheck::class,
        'count' => 1,
    ],
891 1 2
1個(gè)評(píng)論

walkor

  • 暫無(wú)評(píng)論

mliev

376
積分
0
獲贊數(shù)
0
粉絲數(shù)
2018-12-21 加入
??