webman作为http代理服务器

walkor

1 新建 process/Proxy.php

<?php
namespace process;

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',
            'discord.example.com' => 'discord.com',
            'cdn.example.com' => 'cdn.discordapp.com',
            'gateway.example.com' => 'gateway.discord.gg',
        ];
        $host = $request->host(true);
        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();
    }
}

2 修改 config/process.php

<?php
return [
    // ... 这里省略了其它配置 ...

    // http代理配置
    'proxy' => [
        'handler' => \process\Proxy::class,
        'listen' => 'http://0.0.0.0:8989',
        'count' => cpu_count(),
        'reloadable' => false,
    ]
];

3 执行 php start.php restart 重启webman

4 设置nginx代理,用于开启https。(以api.example.com为例)

server {
  server_name api.example.com;
  listen 80;
  root /home/www/webman/public;
  # 这个配置很重要
  proxy_buffering off;

  # https证书 
  listen 443 ssl;
  ssl_certificate ssl/xxx.pem;
  ssl_certificate_key ssl/xxx.key;
  ssl_session_cache shared:le_nginx_SSL:1m;
  ssl_session_timeout 1440m;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers "ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS";

  location ^~ / {
      proxy_set_header Host $host;
      proxy_http_version 1.1;
      proxy_set_header Connection "";
      if (!-f $request_filename){
          proxy_pass http://127.0.0.1:8989;
      }
  }
}

5 设置nginx代理,用于开启wss。(以gateway.example.com为例)

server {
  server_name gateway.example.com;
  listen 80;
  root /home/www/webman/public;
  # 这个配置很重要
  proxy_buffering off;

  # https证书 
  listen 443 ssl;
  ssl_certificate ssl/xxx.pem;
  ssl_certificate_key ssl/xxx.key;
  ssl_session_cache shared:le_nginx_SSL:1m;
  ssl_session_timeout 1440m;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers "ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS";

  location ^~ / {
      proxy_set_header Host $host;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "Upgrade";
      if (!-f $request_filename){
          proxy_pass http://127.0.0.1:8989;
      }
  }
}

6 重启nginx

这样访问 https://api.example.com 实际上就是访问 https://api.openai.com
实现了代理api.example.com代理api.openai.com

7305 17 65
17个评论

古树

赞~

  • 暂无评论
宋可敬

赞!已测试,可以使用。

  • 暂无评论
zh7314

之前用的是curl的代理ip访问的,这个方案也不错

  • 暂无评论
aria

这个功能是用于什么场景?

ak47f16200

直接用nginx代理实现的

lidazui

这个代理文件建在哪里?直接在app/controller吗?另有这个代理需要部署在外面的服务器吗?

  • 暂无评论
ocink

这样能够实现cdn的效果嗎

  • 暂无评论
liziyu

  • 暂无评论
zhaihongli2023

老大 这个是要安装ai的那一套进行绑定域名吗

weijer

Midjourney代理里面这个怎么配置转发

-e mj.ng-discord.wss=wss://ws.imgin.top \

wocall

留个脚印,要是能收藏就好了

  • 2024-09-17

    文章最后一行,有一个“星号”,点击即可收藏了

roczyl

能不能实现对远程服务器返回的数据进行修改,比如嵌入一段自己的代码?

那么这段代理服务器的代码是不是需要放到,一个能访问到api.openai.com的服务器上才行

z

这种方法可以用来访问google吗

  • liangdodo 2024-09-05

    workerman是完全可以轻松实现的,亲测成功,性能很高。但是比较麻烦,你需要开发服务端和客户端,并且配置系统或浏览器的代理,还会遇到一些小坑,如果你只是为了学习研究下倒也没啥,如果有另外的想法就真的大可不必了。

tulei

配置了http代理,有时候走了代理 大多数http 请求没走代理,直接请求了,不知是否和没有配置https 有关

  • 暂无评论

再温习一次

  • 暂无评论
ck5283

怎么联系你呢

  • 暂无评论

walkor

153421
积分
0
获赞数
0
粉丝数
2014-05-04 加入
×
🔝