简体   繁体   English

棘轮套筒 ws 到 wss

[英]Ratchet socket ws to wss

I have two Docker containers.我有两个 Docker 容器。 One is app running on https, other is a web socket one running on Ratchet library.一个是在 https 上运行的应用程序,另一个是在 Ratchet 库上运行的 web 套接字。

When I'm on a Mac I have no issues connecting from https to ws , and I presume it is because Docker is running on 127.0.0.1 so I suppose there are some special rules which allow it.当我在 Mac 上时,从https连接到ws没有问题,我认为这是因为 Docker 在127.0.0.1上运行,所以我想有一些特殊规则允许它。

Switching to Win machine the problem arises because Docker is on 192.168.99.100 and now connecting https to ws no longer works because browser is expecting https to wss connection.切换到 Win 机器时出现问题,因为wss位于192.168.99.100上,现在将https连接到ws不再有效,因为浏览器期待https连接。 Just switching endpoint to wss:// instead of ws:// obviously doesn't work out of the box as the connection handshake naturally times out.只是将端点切换到wss://而不是ws://显然不能开箱即用,因为连接握手自然会超时。

My socket container is not running nginx nor apache, it is a simple Ratchet server which ultimately just exposes the port I told it on outside.我的套接字容器没有运行 nginx 和 apache,它是一个简单的 Ratchet 服务器,最终只是暴露了我在外面告诉它的端口。

Are there some good resources on how to switch to wss because I could find none.是否有一些关于如何切换到wss的好资源,因为我找不到。

This is what I've tried:这是我尝试过的:

$server = IoServer::factory(new HttpServer($wsServer), $config['port'], '0.0.0.0', [
    'local_cert'        => __DIR__ . '/../config/cert.pem',
    'allow_self_signed' => true,
    'verify_peer'       => false,
    'ciphers'           => 'TLSv1.2'
]);

This code doesn't error out, but nor does it enable wss endpoint because the same thing is happening.此代码不会出错,但也不会启用wss端点,因为同样的事情正在发生。 How can I proceed and make this work?我该如何继续并完成这项工作? Also I've read somewhere that wss self signed certificates are not allowed.另外,我在某处读到不允许使用 wss 自签名证书。 Can I use a signed cert on socket container and have app container on self signed certificate or they both need to use the same one?我可以在套接字容器上使用签名证书并在自签名证书上使用应用程序容器,还是它们都需要使用同一个?

EDIT:编辑:

since Ratchet removed SSL a while ago, I added the code to the IoServer::factory() :由于 Ratchet 不久前删除了 SSL,我将代码添加到IoServer::factory()

    public static function factory(MessageComponentInterface $component, $port = 80, $address = '0.0.0.0', array $sslContext = null) {
        $loop   = LoopFactory::create();
        $socket = new Reactor($address . ':' . $port, $loop);

        if (is_array($sslContext)) {
            $socket = new SecureReactor($socket, $loop, $sslContext);
        }

        return new static($component, $socket, $loop);
    }

But even without it, I tried it the other way also:但即使没有它,我也尝试了另一种方式:

$server = new IoServer(new HttpServer($wsServer), new SecureServer($webSock, $loop, array(
        'local_cert'        => __DIR__ . '/../config/cert.pem',
        'allow_self_signed' => true,
        'verify_peer'       => false,
    )
));

I ended up starting Ratchet as-is (normal ws ) and doing a nginx proxy pass on app side:我最终按原样启动 Ratchet (正常ws )并在应用程序端执行 nginx 代理传递:

   location /ws {
        resolver 127.0.0.11 valid=30s;
        set $backend "http://container_name:port";
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
        proxy_pass $backend;
     }

The resolver part ensures that nginx doesn't error out when you kill the container.解析器部分确保 nginx 在您杀死容器时不会出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM