workerman 有没有个http 的gateway的代理服务

bigman

比如:把所有所有的 xxx/test/user/test 全部代理到 0.0.0.0:8888 的http服务中
把所有所有的 xxx/test1/user/test 全部代理到 0.0.0.0:8887 的http服务中
根据不同的规则转发 比如上面的 test 和 test1 作为模块代理到不同的服务去

1115 2 2
2个回答

chen
admin
<?php

namespace app\cdn\process;

use Workerman\Connection\AsyncTcpConnection;
use Workerman\Connection\TcpConnection;

class CdnTcp
{
    public function onConnect(TcpConnection $connection)
    {
    }

    public function onWebSocketConnect(TcpConnection $connection, $http_buffer)
    {
        echo "onWebSocketConnect\n";
    }

    public function onMessage(TcpConnection $connection, $data)
    {
        // Parse http header.
        list($method, $addr, $http_version) = explode(' ', $data);
        $_headers=explode("\r\n",$data);
        $headers=[];
        foreach ($_headers as $_header){
            $_point=strpos($_header,':');
            if ($_point===false) continue;
            $headers[substr($_header,0,$_point)]=trim(trim(substr($_header,$_point),':'));
        }

        //源站IP地址
        $source_ip='baidu.com';
        $source_host='xx.xx.xx.xx';
        print_r('TCP转发管道建立:用户IP:'.$connection->getRemoteAddress().'| 访问域名:'.$headers['Host'] .' | 源站:'.gethostbyname($source_ip).'| 回源域名'.$source_host);

        // Async TCP connection.
        $remote_connection = new AsyncTcpConnection('tcp://'.$source_ip);
        // CONNECT.

        if ($method !== 'CONNECT') {
            //替换request 中的回源host
            $remote_connection->send(CdnTcp::replaceHost($data,$source_host));
            // POST GET PUT DELETE etc.
        } else {
            $connection->send("HTTP/1.1 200 Connection Established\r\n\r\n");
        }

        $remote_connection->onMessage=function (TcpConnection $SourceConn, $SourceData)use ($connection){
            $connection->send($SourceData);
        };

        $remote_connection->onClose = function ($source) use ($connection) {
            $connection->close();
        };

        $remote_connection->onBufferFull = function ($SourceConn) use ($connection) {
            $connection->pauseRecv();
        };

        $remote_connection->onBufferDrain = function ($SourceConn) use ($connection) {
            $connection->resumeRecv();
        };

        $connection->onMessage=function (TcpConnection $ClientConn, $ClientData)use ($remote_connection,$source_host){
            $remote_connection->send(CdnTcp::replaceHost($ClientData,$source_host));
        };

        $connection->onClose = function (TcpConnection $ClientConn) use ($remote_connection) {
            $remote_connection->close();
        };

        $connection->onBufferFull = function (TcpConnection $ClientConn) use ($remote_connection) {
            $remote_connection->pauseRecv();
        };

        $connection->onBufferDrain = function (TcpConnection $ClientConn) use ($remote_connection) {
            $remote_connection->resumeRecv();
        };

        $remote_connection->connect();
    }

    public static function replaceHost(string $data,$host):string
    {
        return preg_replace('/Host: (.*)/i','Host: '.$host,$data);
    }

    public function onClose(TcpConnection $connection)
    {
        echo "onClose\n";
    }

}
  • bigman 2022-03-28

    谢谢! 我去试试

  • bigman 2022-03-28

    可以留个qq吗?联系一下!

  • admin 2022-03-28

    你好,进群@我就行

  • bigman 2022-03-28

    我只有webman交流群1,可以把群号发下吗?

  • admin 2022-03-28

    就在webman1群里,大家都可以帮助你

  • bigman 2022-03-28

    你id是什么 我私信下你

年代过于久远,无法发表回答
×
🔝