kypt0nn/bpn

一个允许您创建去中心化网络的库

1.2.0 2021-08-28 12:30 UTC

This package is auto-updated.

Last update: 2024-09-28 18:48:03 UTC


README

🚀 BPN 是一个庞大的库,允许您在 PHP 8+ 上创建自己的去中心化网络
并提供各种通信工具

安装

composer require krypt0nn/bpn

使用示例

通过未加密的 UDP 数据包通过事件进行通信

client.php

<?php

require 'vendor/autoload.php';

use BPN\BPN;
use BPN\Networking\Endpoint;
use BPN\Encoding\ECC;
use BPN\Data\Packet;

$keypair = ECC::generateKeyPair();

BPN::configure ([
    'public_key'  => $keypair['public'],
    'private_key' => $keypair['private']
]);

BPN::get()->send (
    Endpoint::format('server address'),
    Packet::performEvent('hello-world', 'Hello, World!')
);

server.php

<?php

require 'vendor/autoload.php';

use BPN\BPN;
use BPN\Networking\Endpoint;
use BPN\Encoding\ECC;

$keypair = ECC::generateKeyPair();

BPN::configure ([
    'public_key'  => $keypair['public'],
    'private_key' => $keypair['private']
]);

BPN::on('hello-world', fn (Packet $packet) => echo $packet->data['data'] . PHP_EOL);

while (true)
    BPN::get()->update();

通过受保护的 TCP 隧道进行直接连接

client.php

<?php

require 'vendor/autoload.php';

use BPN\BPN;
use BPN\Networking\Endpoint;
use BPN\Networking\Tunneling\Tunnel;
use BPN\Encoding\ECC;

$keypair = ECC::generateKeyPair();

BPN::configure ([
    'public_key'  => $keypair['public'],
    'private_key' => $keypair['private']
]);

$tunnel = Tunnel::create(Endpoint::format('server address'));

if ($tunnel === null)
    die ('Tunnel creation error');

else while (true)
{
    $message = readline ('> ');

    $tunnel->send ($message);
}

server.php

<?php

require 'vendor/autoload.php';

use BPN\BPN;
use BPN\Networking\Endpoint;
use BPN\Networking\Tunneling\Tunnel;

$keypair = ECC::generateKeyPair();

BPN::configure ([
    'public_key'  => $keypair['public'],
    'private_key' => $keypair['private']
]);

while (!($tunnel = Tunnel::listen(Endpoint::local())));

while (true)
{
    $tunnel->update (function ($data)
    {
        echo $data . PHP_EOL;
    });
}

通过 BPN 网络通过其 UUID 搜索客户端

<?php

require 'vendor/autoload.php';

use BPN\BPN;
use BPN\Networking\DNS;
use BPN\Networking\DNS\Record;
use BPN\Data\Packet;

$keypair = ECC::generateKeyPair();

BPN::configure ([
    'public_key'  => $keypair['public'],
    'private_key' => $keypair['private']
]);

DNS::searchRecords ('client uuid', function (Record $record, Packet $packet)
{
    echo 'Client with endpoint '. $packet->author_endpoint->toString() .
         ' found client we wanted to find'.
         ' and his endpoint is '. $record->endpoint()->toString() . PHP_EOL;

    // if you want to not to receive another records
    // you can return false from this callback
    // return false;
});

广播数据

<?php

use BPN\BPN;

$keypair = ECC::generateKeyPair();

BPN::configure ([
    'public_key'  => $keypair['public'],
    'private_key' => $keypair['private']
]);

# Add broadcast handler for incoming data
BPN::onBroadcast(function ($data)
{
    echo '['. $data['author'] .'] '. $data['message'] . PHP_EOL;
});

# Broadcast data
BPN::broadcast ([
    'author'  => 'aboba',
    'message' => 'Hello, World!'
]);

文档

文档将在未来添加


作者: Nikita Podvirnyy