library that allows you to create decentralized networks

1.2.0 2021-08-28 12:30 UTC

This package is auto-updated.

Last update: 2024-10-28 18:55:38 UTC


README

🚀 BPN is a huge library which allows you to create your own decentralized network on PHP 8+
and provides various communication tools

Installation

composer require krypt0nn/bpn

Usage example

Communication through events with unprotected UDP packets

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();

Direct connection through protected TCP tunnel

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;
    });
}

Search client through BPN network by his 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;
});

Broadcast data

<?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!'
]);

Documentation

Documentation will be added in future


Author: Nikita Podvirnyy