vzool/dove.php

鸽信通知文件系统 (DNFS)

1.0.52 2023-02-23 11:21 UTC

This package is auto-updated.

Last update: 2024-09-25 14:38:12 UTC


README

ar Build Status Total Downloads Latest Stable Version License

鸽信是一个基于文件存储系统的通知系统,用于传递消息,它作为一个从服务器到客户端的单向数据流工作。因此,服务器只放置消息,稍后客户端将根据自己的时间表检查任何更新。

默认情况下,DNFS 将其所有数据存储在与 dove.php 相同目录中的 .dove 目录中,这可以通过构造函数中的 $path 参数进行更改。

# constructor
$dove = new Dove(
    string $client, # client address used to reference.
    int $expiration_in_days = 0, # disabled by default, store forever without removing any.
    int $integrity = Dove::INTEGRITY_ALL, # level of integrity of the messages.
    string $cmd = '*', # all commands allowed, otherwise start with a limited constructor without `Push` cmd.
    string $path = __DIR__ . '/'
);

鸽信不会以任何方式处理发送或接收的数据,如加密或编码,因此它只充当从服务器到客户端传输数据的桥梁。如果您对二进制数据有顾虑,可以使用 base64 进行编码,并使用加密来维护隐私,所有这些都留给开发者自行决定。

消息过期”>在创建 Dove 对象的新实例时暂时存储在请求内存中,默认值为零,表示禁用,否则它将以天为单位。如果禁用过期,DNFS 将存储所有消息而不删除任何消息。因此,过期后的删除操作是在特定客户端执行拉取调用时计算的,并且旧消息被删除。

DNFS 是一个懒汉式演员,它不需要任何计划任务活跃即可执行其工作,它只是在等待客户端的动作来更新消息的状态。

完整性”>是 Dove 希望提供的零信任的一部分,并且它有许多选项

class Dove{
    const INTEGRITY_DISABLED = 0b0000;
    const INTEGRITY_GENERATE_HASH = 0b0001;
    const INTEGRITY_VERIFY_HASH = 0b0010;
    const INTEGRITY_GENERATE_SIGNATURE = 0b0100;
    const INTEGRITY_VERIFY_SIGNATURE = 0b1000;
    const INTEGRITY_ALL = 0b1111; # generate and verify for hash and signature
    # ...
}

通常,在通过 Push 命令推送新消息时生成哈希和签名,当调用 Read 命令时进行验证。最快的选项是 Dove::INTEGRITY_DISABLED,然后是 (Dove::INTEGRITY_GENERATE_HASHDove::INTEGRITY_VERIFY_HASH),然后是 (Dove::INTEGRITY_GENERATE_SIGNATUREDove::INTEGRITY_VERIFY_SIGNATURE),而最慢的选项是 Dove::INTEGRITY_ALL,这取决于消息大小。因此,您必须进行基准测试以选择最佳选项。

要启动服务器,其行为类似于 Dove 构造函数

Dove::Serve(
    true, # Block the execution.
    int $expiration_in_days = 0, # disabled by default, store forever without removing any.
    int $integrity = Dove::INTEGRITY_ALL, # level of integrity of the messages.
    string $path = __DIR__ . '/'
);

对于完整性参数是一个位变量,您可以同时组合多个选项,如下所示

$dove = new Dove(
    # ..
    Dove::INTEGRITY_GENERATE_HASH | Dove::INTEGRITY_GENERATE_SIGNATURE, # generate all without verifying
    # ..
);

# AND

Dove::Serve(
    # ..
    Dove::INTEGRITY_GENERATE_HASH | Dove::INTEGRITY_GENERATE_SIGNATURE, # generate all without verifying
    # ..
);

⛔ 密码学矩阵

密码学仅限于在传输过程中作为消息完整性的选项,Dove 传递消息而不对消息内容进行任何编码或加密。

✨ 动机

主要想法来自 Passky-Server 项目,在 discord 服务器 上讨论 LastPass 数据泄露事件,该事件影响了个人信息 (PII),并允许恶意行为者使用该信息进行钓鱼攻击。有许多想法被分享,其中一个是 Zica Zajc,他是伟大的人,也是 Passky 项目的 CEO,他建议服务器可以存储消息,而客户端稍后会检查它们。因此,我认为将这个想法整合成一个可用的库将更好。

👀 解剖结构

鸽信是一个非常小的库,小于 180 LOC(代码行数),核心实现仅占大约 59%,10% 用于 HTTP 处理,其余用于测试。

是的,所有内容都在一个单独的文件中,dove.php 文件包含了实现、HTTP 路由和测试,这不是很棒吗?😋✌️

事实上,Dove 是一个特殊的库,您可以使用单个文件 dove.php,也可以通过 composer 安装,无需任何命名空间。这两种方法都将让您使用库的全部功能。

Dove 存储数据结构 (DSDS)

dove-storage-system

.dove 目录可以位于公共路径中,如果 Web 服务器支持目录列表功能,但它也可以放在私有位置,DNFS 将成为这些数据的唯一访问点。

storage-data-file-content storage-hash-file-content storage-signature-file-content

REST API 提交请求

rest-api-pull-request

这是一份按时间降序排列的时间列表,从最新到最旧。因此,通过选择一个时间,您可以请求该时间之后的最新的消息,或者阅读消息内容。

某些时间之后的 REST API 提交请求

rest-api-pull-request

最后时间的 REST API 提交请求

rest-api-pull-request

REST API 读取请求

rest-api-pull-request

DNFS 通过在内部和外部之间建立明确的边界来尝试实现零信任,因此它总是自动编码时间,并且用于编码/解码的密钥会根据 dove.php 内容、文件状态和位置自动更改。

如果 dove.php 身份发生变化,那么 .dove 整个文件夹将自动删除。

因此,如果客户端获得了某些时间引用,那么 dove.php 内容以某种方式更新了,或者文件位置发生了变化,那么旧引用将不会工作,除非客户端请求新的引用,然后客户端可以使用更新的引用获取剩余的消息。

带有无效签名的 REST API 读取请求

rest-api-pull-request-invalid-signature

这里的签名是无效的,而哈希是有效的,因为哈希只涉及消息内容,而签名涉及到 dove.php 本身的源代码作为真相的来源,它已经改变并生成了所有新的密钥对,因此默认情况下所有过去的签名都是无效的。在正常情况下,并且在消息传输过程中,dove.php 文件不应该更改,除非有紧急更新,这样开发者以后可以决定这是否可以接受,或者取消所有消息并在需要时创建新的消息。

缺失消息的 REST API 读取请求

rest-api-pull-request-missing

🏢 要求

  • PHP 7.3+

⚓ 安装与使用

Dove 项目将尽最大努力与其所有发布版本兼容,因此在未来开发版本中,不会有破坏性更改。

🔧 单文件库(服务器端)

整个库只有一个名为 dove.php 的文件,因此您可以将它复制并粘贴到您需要的地方。

当您想要处理消息时,请使用以下命令

<?php

define('DOVE', 1);
require_once 'dove.php';

$dove = new Dove('abdelaziz');
$result = $dove->Push('Salam, World!');
$message = $dove->Read($result['time']); # time just act like id

$times = $dove->Pull($result['time']); # all times of messages after `$time`
$times = $dove->Pull(); # all times of messages

$dove->Delete($result['time']); # delete one message
$dove->Delete(); # delete all messages
?>

然后,要处理客户端请求,运行以下命令

php -S localhost:8080 dove.php

🎵 PHP(服务器端)Composer 依赖管理器 Composer

composer require vzool/dove.php

当您想要处理消息时,请使用以下命令

<?php

define('DOVE', 1);
require_once 'vendor/vzool/dove.php/dove.php';

$dove = new Dove('abdelaziz');
$result = $dove->Push('Salam, World!');
$message = $dove->Read($result['time']); # time act like id
# ...
?>

然后,仅将以下内容包含在 $_REQUEST 的路径中,它将自动处理请求。

<?php require_once 'vendor/vzool/dove.php/dove.php'; ?>

# OR

<?php
    define('DOVE', 1);
    require_once 'vendor/vzool/dove.php/dove.php';
    Dove::Serve(true);
?>

🌍 HTTP REST API(客户端)[GET/POST/ANY]

  • 拉取所有消息时间

    • https://:8080/dove.php?client=abdelaziz&cmd=pull
    • https://:8080/dove.php?client=abdelaziz
  • 拉取时间(369)之后的消息的最新时间

    • https://:8080/dove.php?client=abdelaziz&cmd=pull&time=369
    • https://:8080/dove.php?&client=abdelaziz&time=369
  • 在时间 https://:8080/dove.php?client=abdelaziz&cmd=read&time=369 中读取消息

🏁 基准测试

  • CPU:3.7 GHz 6 核英特尔酷睿 i5
  • RAM:72 GB 2667 MHz DDR4
  • 操作系统:macOS Ventura 13.1

stats

INTEGRITY_TYPE = Dove::INTEGRITY_DISABLED

==========================================================================
Dove Benchmarking started at: 2023-02-20 14:04:55
==========================================================================
Write messages for (00:00:30) ...
Write finished on: 2023-02-20 14:05:26
--------------------------------------------------------------------------
Read all written messages...
Read finished on: 2023-02-20 14:05:38
--------------------------------------------------------------------------
Delete all written messages...
Delete finished on: 2023-02-20 14:06:09
==========================================================================
Write Count 126,061 (msg) in (00:00:30)
Write Speed 4,202 (msg/sec).
--------------------------------------------------------------------------
Read Count 126,061 (msg) in (00:00:12)
Read Speed 10,505 (msg/sec).
--------------------------------------------------------------------------
Delete Count 126,061 (msg) in (00:00:31)
Delete Speed 4,066 (msg/sec).
--------------------------------------------------------------------------
Average Count 126,061 (msg).
Average Speed 6,258 (msg/sec).
==========================================================================
Dove Benchmarking done at: (2023-02-20 14:06:09) and took (00:01:13)
==========================================================================


INTEGRITY_TYPE = Dove::INTEGRITY_GENERATE_HASH

==========================================================================
Dove Benchmarking started at: 2023-02-20 14:06:09
==========================================================================
Write messages for (00:00:30) ...
Write finished on: 2023-02-20 14:06:40
--------------------------------------------------------------------------
Read all written messages...
Read finished on: 2023-02-20 14:07:03
--------------------------------------------------------------------------
Delete all written messages...
Delete finished on: 2023-02-20 14:07:26
==========================================================================
Write Count 93,540 (msg) in (00:00:30)
Write Speed 3,118 (msg/sec).
--------------------------------------------------------------------------
Read Count 93,540 (msg) in (00:00:23)
Read Speed 4,067 (msg/sec).
--------------------------------------------------------------------------
Delete Count 93,540 (msg) in (00:00:23)
Delete Speed 4,067 (msg/sec).
--------------------------------------------------------------------------
Average Count 93,540 (msg).
Average Speed 3,751 (msg/sec).
==========================================================================
Dove Benchmarking done at: (2023-02-20 14:07:26) and took (00:01:16)
==========================================================================


INTEGRITY_TYPE = Dove::INTEGRITY_VERIFY_HASH

==========================================================================
Dove Benchmarking started at: 2023-02-20 14:07:26
==========================================================================
Write messages for (00:00:30) ...
Write finished on: 2023-02-20 14:07:57
--------------------------------------------------------------------------
Read all written messages...
Read finished on: 2023-02-20 14:08:05
--------------------------------------------------------------------------
Delete all written messages...
Delete finished on: 2023-02-20 14:08:33
==========================================================================
Write Count 119,980 (msg) in (00:00:30)
Write Speed 3,999 (msg/sec).
--------------------------------------------------------------------------
Read Count 119,980 (msg) in (00:00:08)
Read Speed 14,998 (msg/sec).
--------------------------------------------------------------------------
Delete Count 119,980 (msg) in (00:00:28)
Delete Speed 4,285 (msg/sec).
--------------------------------------------------------------------------
Average Count 119,980 (msg).
Average Speed 7,761 (msg/sec).
==========================================================================
Dove Benchmarking done at: (2023-02-20 14:08:33) and took (00:01:06)
==========================================================================


INTEGRITY_TYPE = Dove::INTEGRITY_GENERATE_HASH | Dove::INTEGRITY_VERIFY_HASH

==========================================================================
Dove Benchmarking started at: 2023-02-20 14:08:33
==========================================================================
Write messages for (00:00:30) ...
Write finished on: 2023-02-20 14:09:04
--------------------------------------------------------------------------
Read all written messages...
Read finished on: 2023-02-20 14:09:11
--------------------------------------------------------------------------
Delete all written messages...
Delete finished on: 2023-02-20 14:09:40
==========================================================================
Write Count 77,086 (msg) in (00:00:30)
Write Speed 2,570 (msg/sec).
--------------------------------------------------------------------------
Read Count 77,086 (msg) in (00:00:07)
Read Speed 11,012 (msg/sec).
--------------------------------------------------------------------------
Delete Count 77,086 (msg) in (00:00:29)
Delete Speed 2,658 (msg/sec).
--------------------------------------------------------------------------
Average Count 77,086 (msg).
Average Speed 5,413 (msg/sec).
==========================================================================
Dove Benchmarking done at: (2023-02-20 14:09:40) and took (00:01:06)
==========================================================================


INTEGRITY_TYPE = Dove::INTEGRITY_GENERATE_SIGNATURE

==========================================================================
Dove Benchmarking started at: 2023-02-20 14:09:40
==========================================================================
Write messages for (00:00:30) ...
Write finished on: 2023-02-20 14:10:11
--------------------------------------------------------------------------
Read all written messages...
Read finished on: 2023-02-20 14:10:16
--------------------------------------------------------------------------
Delete all written messages...
Delete finished on: 2023-02-20 14:10:41
==========================================================================
Write Count 67,758 (msg) in (00:00:30)
Write Speed 2,259 (msg/sec).
--------------------------------------------------------------------------
Read Count 67,758 (msg) in (00:00:05)
Read Speed 13,552 (msg/sec).
--------------------------------------------------------------------------
Delete Count 67,758 (msg) in (00:00:25)
Delete Speed 2,710 (msg/sec).
--------------------------------------------------------------------------
Average Count 67,758 (msg).
Average Speed 6,174 (msg/sec).
==========================================================================
Dove Benchmarking done at: (2023-02-20 14:10:41) and took (00:01:00)
==========================================================================


INTEGRITY_TYPE = Dove::INTEGRITY_VERIFY_SIGNATURE

==========================================================================
Dove Benchmarking started at: 2023-02-20 14:10:41
==========================================================================
Write messages for (00:00:30) ...
Write finished on: 2023-02-20 14:11:12
--------------------------------------------------------------------------
Read all written messages...
Read finished on: 2023-02-20 14:11:19
--------------------------------------------------------------------------
Delete all written messages...
Delete finished on: 2023-02-20 14:11:46
==========================================================================
Write Count 99,785 (msg) in (00:00:30)
Write Speed 3,326 (msg/sec).
--------------------------------------------------------------------------
Read Count 99,785 (msg) in (00:00:07)
Read Speed 14,255 (msg/sec).
--------------------------------------------------------------------------
Delete Count 99,785 (msg) in (00:00:27)
Delete Speed 3,696 (msg/sec).
--------------------------------------------------------------------------
Average Count 99,785 (msg).
Average Speed 7,092 (msg/sec).
==========================================================================
Dove Benchmarking done at: (2023-02-20 14:11:46) and took (00:01:04)
==========================================================================


INTEGRITY_TYPE = Dove::INTEGRITY_GENERATE_SIGNATURE | Dove::INTEGRITY_VERIFY_SIGNATURE

==========================================================================
Dove Benchmarking started at: 2023-02-20 14:11:46
==========================================================================
Write messages for (00:00:30) ...
Write finished on: 2023-02-20 14:12:17
--------------------------------------------------------------------------
Read all written messages...
Read finished on: 2023-02-20 14:12:26
--------------------------------------------------------------------------
Delete all written messages...
Delete finished on: 2023-02-20 14:12:45
==========================================================================
Write Count 64,287 (msg) in (00:00:30)
Write Speed 2,143 (msg/sec).
--------------------------------------------------------------------------
Read Count 64,287 (msg) in (00:00:09)
Read Speed 7,143 (msg/sec).
--------------------------------------------------------------------------
Delete Count 64,287 (msg) in (00:00:19)
Delete Speed 3,384 (msg/sec).
--------------------------------------------------------------------------
Average Count 64,287 (msg).
Average Speed 4,223 (msg/sec).
==========================================================================
Dove Benchmarking done at: (2023-02-20 14:12:45) and took (00:00:58)
==========================================================================


INTEGRITY_TYPE = Dove::INTEGRITY_ALL

==========================================================================
Dove Benchmarking started at: 2023-02-20 14:12:45
==========================================================================
Write messages for (00:00:30) ...
Write finished on: 2023-02-20 14:13:16
--------------------------------------------------------------------------
Read all written messages...
Read finished on: 2023-02-20 14:13:24
--------------------------------------------------------------------------
Delete all written messages...
Delete finished on: 2023-02-20 14:13:44
==========================================================================
Write Count 52,978 (msg) in (00:00:30)
Write Speed 1,766 (msg/sec).
--------------------------------------------------------------------------
Read Count 52,978 (msg) in (00:00:08)
Read Speed 6,622 (msg/sec).
--------------------------------------------------------------------------
Delete Count 52,978 (msg) in (00:00:20)
Delete Speed 2,649 (msg/sec).
--------------------------------------------------------------------------
Average Count 52,978 (msg).
Average Speed 3,679 (msg/sec).
==========================================================================
Dove Benchmarking done at: (2023-02-20 14:13:44) and took (00:00:58)
==========================================================================
==========================================================================
Dove Benchmarking done at: (2023-02-20 14:13:44) and all took (00:08:41)
==========================================================================

您可以使用以下命令在您的 PC 上运行自己的基准测试 php benchmark.php

🔬 测试

它应该没有任何问题,否则将抛出异常。

php dove.php
# OR
composer test

🔮 未来发展

DNFS 库可以成为您自己的云生态系统的一部分,其中安装了客户端设备的各种服务应用程序,这些应用程序客户端定期从云端拉取状态和通知,就像谷歌有“Google Play 服务”或华为有他们的应用 HMS(华为移动服务)一样。当然,谷歌和华为都是大公司,他们一直在构建自己的技术基础设施,但是,Dove 可以为您提供美味、简单和可靠的东西。

别忘了谷歌公司是从车库开始的,所以开始构建您的车库吧。 😂✌️