amphp/quic
Composer 安装命令:
composer require amphp/quic
包简介
README 文档
README
AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind. amphp/quic is a library for establishing quic connections. It provides a socket abstraction for clients and servers. It abstracts the really low levels of the cloudflare/quiche library.
Installation
This package can be installed as a Composer dependency.
composer require amphp/quic
Requirements
amphp/quic implements the basic Server and Client interfaces of amphp/socket. It requires the FFI extension to be installed.
TLS
Given that QUIC requires TLS, it is required to have a valid certificate on the server side. Also, one or more application layer protocol names (ALPN) MUST be specified.
Note: Fingerprinting is not supported and ignored. To use a self-signed certificate, disabling peer verification is required, or having a proper custom root CA.
Connecting to a Server
The simplest way to connect is just a target and an ALPN protocol.
$quicConnection = \Amp\Quic\connect("example.com:1234", ["alpn protocol"]);
Handling connections
Every QUIC connection is multiplexing streams. QuicConnection allows creating and accepting new streams.
Use accept() to await a new stream (QuicSocket), similarly to Amp\Socket\ServerSocket. Create new streams with openStream(). Also, datagrams can be sent and received on the connection with sendDatagram() and receiveDatagram().
QUIC streams implement Amp\Socket\Socket for sending and receiving data.
Shutting down a connection via close() (or losing all references to it) immediately terminates all streams.
Server
amphp/quic allows listening for incoming QUIC connections over UDP. As with the client, it requires an ALPN protocol. Also a valid TLS certificate is required.
$tls = (new ServerTlsContext) ->withDefaultCertificate(new Certificate(__DIR__ . "/cert.pem", __DIR__ . "/key.pem")) ->withApplicationLayerProtocols(["test"]); $quicServerSocket = \Amp\Quic\bind(["0.0.0.0:1234", "[::]:1234"], $tls);
Note: SNI based certificate distinction is not supported. There must be exactly one certificate provided via ServerTlsContext::withDefaultCertificate().
Accepting connections
It's possible to accept() on a QuicServerSocket, but this will directly return a QuicSocket. This mode of operation is recommended, when connections do not have a specific meaning and streams are all what you are intersted in.
To actually get a QuicConnection, use acceptConnection():
while ($quicConnection = $quicServerSocket->acceptConnection()) { async(function() use ($quicConnection) { // handle the connection, open and accept new streams, send and receive datagrams }) }
Configuration
While most configuration of Amp\Socket\ConnectContext and Amp\Socket\BindContext is understood and applied, there is also QUIC specific configuration exposed by QuicConfig and the respective child classes QuicClientConfig and QuicServerConfig which are both accepted by the connect() and bind() functions respectively.
Note that the QUIC interface generally accepts all sorts of communication, i.e. unidirectional streams, bidirectional streams and datagrams, as well as having an idle timeout for timely connection timeouts, with a ping being sent every so often to prevent timeouts on live connections. In particular QUIC servers may desire to tweak these settings.
Security
If you discover any security related issues, please email me@kelunik.com instead of using the issue tracker.
License
The MIT License (MIT). Please see LICENSE for more information.
amphp/quic 适用场景与选型建议
amphp/quic 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 97 次下载、GitHub Stars 达 6, 最近一次更新时间为 2024 年 01 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 amphp/quic 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 amphp/quic 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 97
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-01-05