2025-06-25 09:14:45 +08:00
|
|
|
<?php
|
|
|
|
|
namespace app\process;
|
|
|
|
|
|
2025-06-25 09:48:22 +08:00
|
|
|
use support\Log;
|
2025-06-25 09:14:45 +08:00
|
|
|
use Workerman\Connection\AsyncTcpConnection;
|
|
|
|
|
use Workerman\Connection\TcpConnection;
|
|
|
|
|
use Workerman\Protocols\Http\Request;
|
|
|
|
|
|
|
|
|
|
class Proxy
|
|
|
|
|
{
|
|
|
|
|
public function onMessage(TcpConnection $connection, Request $request)
|
|
|
|
|
{
|
|
|
|
|
$replace = [
|
|
|
|
|
// 'api.example.com' => 'api.openai.com',
|
2025-06-25 10:10:27 +08:00
|
|
|
'ntrans.xample.cn' => 'ntrans.xfyun.cn',
|
2025-06-25 09:14:45 +08:00
|
|
|
];
|
|
|
|
|
$host = $request->host(true);
|
2025-06-25 09:48:22 +08:00
|
|
|
log::info('$host:'.$host);
|
2025-06-25 09:14:45 +08:00
|
|
|
if (!isset($replace[$host])) {
|
|
|
|
|
return $connection->send(response('404 not found', 404));
|
|
|
|
|
}
|
|
|
|
|
$host = $replace[$host];
|
|
|
|
|
$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();
|
|
|
|
|
}
|
|
|
|
|
}
|