PHP 中有 STUN/TURN/ICE 客户端库吗?

发布于 2024-10-03 02:55:53 字数 140 浏览 3 评论 0原文

我正在尝试在部署在不同网络(均位于 NAT 后面)的计算机上的两个 PHP 守护进程之间建立 P2P。我在 Google 上搜索了使用 PHP 的 NAT 遍历,似乎 PHP 中没有现有的解决方案。

有谁知道使用 PHP 解决这个问题的解决方案/库?

I am trying to establish P2P between two PHP daemon deployed on machines in different network (both behind NAT). I searched around for NAT traversal using PHP on Google and seems like their is no existing solution for this in PHP.

Does anyone know about a solution/library to work this around with PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兮子 2024-10-10 02:55:53

如果其他人正在寻找答案,这里有一个简单的类可以完成这项工作:

<?php

class STUNClient
{
    private $socket;

    public function __construct()
    {
        //stun.l.google.com
        $this->setServerAddr("66.102.1.127", 19302);
        $this->createSocket();
    }

    public function setServerAddr($host, $port = 3478)
    {
        $this->serverIP   = $host;
        $this->serverPort = $port;
    }

    public function createSocket()
    {
        $this->socket = socket_create(AF_INET, SOCK_DGRAM, getprotobyname("udp"));
        socket_set_nonblock($this->socket);
    }

    public function getPublicIp()
    {
        $msg = "\x00\x01\x00\x08\xc0\x0c\xee\x42\x7c\x20\x25\xa3\x3f\x0f\xa1\x7f\xfd\x7f\x00\x00\x00\x03\x00\x04\x00\x00\x00\x00";

        $numberOfBytesSent = socket_sendto($this->socket, $msg, strlen($msg), 0, $this->serverIP, $this->serverPort);

        $st = time();
        while (time() - $st < 1) {

            socket_recvfrom($this->socket, $data, 32, 0, $remoteIP, $remotePort);

            if (strlen($data) < 32) {
                continue;
            }
            break;
        }

        try {

            $info = unpack("nport/C4s", substr($data, 26, 6));
            $ip   = sprintf("%u.%u.%u.%u", $info["s1"], $info["s2"], $info["s3"], $info["s4"]);
            $port = $info['port'];
            return [
                'ip'   => $ip,
                'port' => $port
            ];
        } catch (Exception $e) {

            return [
                'ip'   => "0.0.0.0",
                'port' => "0"
            ];
        }

    }

}

它的使用方式如下:

$sc  = new STUNClient;
print_r( $sc->getPublicIp() ); //prnints out the public ip and port

If anybody else is looking for an answer, here is a simple class that does the job:

<?php

class STUNClient
{
    private $socket;

    public function __construct()
    {
        //stun.l.google.com
        $this->setServerAddr("66.102.1.127", 19302);
        $this->createSocket();
    }

    public function setServerAddr($host, $port = 3478)
    {
        $this->serverIP   = $host;
        $this->serverPort = $port;
    }

    public function createSocket()
    {
        $this->socket = socket_create(AF_INET, SOCK_DGRAM, getprotobyname("udp"));
        socket_set_nonblock($this->socket);
    }

    public function getPublicIp()
    {
        $msg = "\x00\x01\x00\x08\xc0\x0c\xee\x42\x7c\x20\x25\xa3\x3f\x0f\xa1\x7f\xfd\x7f\x00\x00\x00\x03\x00\x04\x00\x00\x00\x00";

        $numberOfBytesSent = socket_sendto($this->socket, $msg, strlen($msg), 0, $this->serverIP, $this->serverPort);

        $st = time();
        while (time() - $st < 1) {

            socket_recvfrom($this->socket, $data, 32, 0, $remoteIP, $remotePort);

            if (strlen($data) < 32) {
                continue;
            }
            break;
        }

        try {

            $info = unpack("nport/C4s", substr($data, 26, 6));
            $ip   = sprintf("%u.%u.%u.%u", $info["s1"], $info["s2"], $info["s3"], $info["s4"]);
            $port = $info['port'];
            return [
                'ip'   => $ip,
                'port' => $port
            ];
        } catch (Exception $e) {

            return [
                'ip'   => "0.0.0.0",
                'port' => "0"
            ];
        }

    }

}

It's used like this:

$sc  = new STUNClient;
print_r( $sc->getPublicIp() ); //prnints out the public ip and port
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文