安装并运行 Aperture(Lightning Labs 的 L402 闪电反向代理),用于创建 L402 付费墙、配置付费 API 端点、为其他代理托管付费内容或测试 L402 认证流程。
光圈 - L402 闪电反转代理. 光圈是一个反向代用, 执行 L402 协议, 启用是一项面向实际任务的技能,主要用于付费加给的 API 访问 通过闪电网络;它坐落在您的 : 背面。
从功能定位来看,该技能强调把分散的操作要求整理成清晰、可复用的处理流程,使用户能够围绕既定目标快速准备输入、选择执行方式并获得结构化结果。实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;
若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。
Aperture 是一个实现 L402 协议的反向代理,可通过 Lightning Network(闪电网络)实现按支付访问 API 的能力。它部署在后端服务之前,在授予客户端访问权限前要求其完成闪电网络微支付。
源码地址:github.com/lightninglabs/aperture
# 1. 安装 aperture
skills/aperture/scripts/install.sh
# 2. 生成配置文件(自动连接本地 lnd)
skills/aperture/scripts/setup.sh
# 3. 确保 invoice.macaroon 文件存在(L402 发票创建所必需)
# 若不存在,可使用 macaroon-bakery 技能生成:
skills/macaroon-bakery/scripts/bake.sh --role invoice-only
--save-to ~/.lnd/data/chain/bitcoin/mainnet/invoice.macaroon
# 4. 启动 aperture
skills/aperture/scripts/start.sh
# 5. 使用 lnget 测试
lnget -k --no-pay https://localhost:8081/api/test
WWW-Authenticate: L402 响应头中携带一个 macaroon 和一张闪电网络发票Authorization: L402 : 头skills/aperture/scripts/install.sh
该脚本将执行以下操作:
go install github.com/lightninglabs/aperture/cmd/aperture@latestaperture 是否已加入 $PATH如需手动安装:
go install github.com/lightninglabs/aperture/cmd/aperture@latest
或从源码构建:
git clone https://github.com/lightninglabs/aperture.git cd aperture make install
skills/aperture/scripts/setup.sh
该脚本基于配置模板生成 ~/.aperture/aperture.yaml,其中包含合理默认值,并自动探测本地 lnd 节点路径。
可选参数:
# 自定义网络
setup.sh --network testnet
# 自定义 lnd 路径
setup.sh --lnd-host localhost:10009
--lnd-tls ~/.lnd/tls.cert
--lnd-macdir ~/.lnd/data/chain/bitcoin/mainnet
# 自定义监听端口
setup.sh --port 8081
# 禁用 TLS(仅限开发环境)
setup.sh --insecure
# 禁用鉴权(不强制支付)
setup.sh --no-auth
skills/aperture/scripts/start.sh
以后台进程方式启动 aperture,并读取 ~/.aperture/aperture.yaml 配置文件。
可选参数:
start.sh --foreground # 前台运行 start.sh --config /path/to # 指定自定义配置路径
skills/aperture/scripts/stop.sh
配置文件路径:~/.aperture/aperture.yaml
Aperture 需要位于配置项 macdir 下的 invoice.macaroon 文件,用于为 L402 挑战生成闪电网络发票。该文件不同于admin.macaroon。若缺失此文件,Aperture 将无法启动,或在客户端请求付费资源时返回错误。
使用 macaroon-bakery 技能生成 invoice macaroon:
skills/macaroon-bakery/scripts/bake.sh --role invoice-only
--save-to ~/.lnd/data/chain/bitcoin/mainnet/invoice.macaroon
setup.sh 脚本会在预期路径未找到 invoice.macaroon 时发出警告。
以下是最简配置,适用于在本地运行 lnd 节点并托管付费接口的 Agent:
listenaddr: "localhost:8081"
insecure: true
debuglevel: "info"
dbbackend: "sqlite"
sqlite:
dbfile: "~/.aperture/aperture.db"
authenticator:
network: "mainnet"
lndhost: "localhost:10009"
tlspath: "~/.lnd/tls.cert"
macdir: "~/.lnd/data/chain/bitcoin/mainnet"
services:
- name: "my-api"
hostregexp: ".*"
pathregexp: "^/api/.*$"
address: "127.0.0.1:8080"
protocol: http
price: 100
每个 services 条目定义一个待保护的后端服务:
services:
- name: "service-name"
# 通过 host 正则表达式匹配请求。
hostregexp: "^api.example.com$"
# 通过 path 正则表达式匹配请求。
pathregexp: "^/paid/.*$"
# 代理目标后端地址。
address: "127.0.0.1:8080"
# 协议类型:http 或 https。
protocol: http
# 固定价格(单位:satoshi)。
price: 100
# 基础层级授予的 macaroon 权限。
capabilities: "read,write"
# 令牌过期时间(单位:秒;31557600 = 1 年)。
timeout: 31557600
# 免费访问路径白名单(正则表达式)。
authwhitelistpaths:
- "^/health$"
- "^/public/.*$"
# 按路径粒度设置速率限制(令牌桶算法)。
ratelimits:
- pathregexp: "^/api/query.*$"
requests: 10
per: 1s
burst: 20
authenticator: network: "mainnet" lndhost: "localhost:10009" tlspath: "~/.lnd/tls.cert" macdir: "~/.lnd/data/chain/bitcoin/mainnet"
authenticator: network: "mainnet" passphrase: "your-pairing-phrase" mailboxaddress: "mailbox.terminal.lightning.today:443"
authenticator: disable: true
SQLite(推荐用于 Agent 场景):
dbbackend: "sqlite" sqlite: dbfile: "~/.aperture/aperture.db"
PostgreSQL:
dbbackend: "postgres" postgres: host: "localhost" port: 5432 user: "aperture" password: "secret" dbname: "aperture"
# 启用自动 Let's Encrypt 证书。 autocert: true servername: "api.example.com" # 或禁用 TLS(仅限开发/本地环境)。 insecure: true
若上述两项均未设置,Aperture 将在 ~/.aperture/ 目录下自动生成自签名证书。
可对接 gRPC 定价服务,替代静态定价:
services:
- name: "my-api"
dynamicprice:
enabled: true
grpcaddress: "127.0.0.1:10010"
insecure: false
tlscertpath: "/path/to/pricer/tls.cert"
一种常见模式是托管其他 Agent 需付费才能访问的信息:
# 1. 启动一个简单的 HTTP 后端提供内容
mkdir -p /tmp/paid-content
echo '{"data": "valuable information"}' > /tmp/paid-content/info.json
cd /tmp/paid-content && python3 -m http.server 8080 &
# 2. 配置 aperture 对其进行保护
skills/aperture/scripts/setup.sh --insecure --port 8081
# 3. 启动 aperture
skills/aperture/scripts/start.sh
# 4. 其他 Agent 可发起支付并获取内容
lnget --max-cost 100 https://localhost:8081/api/info.json
当三个组件均正常运行时:
# 验证 lnd 是否正在运行 skills/lnd/scripts/lncli.sh getinfo # 启动 aperture(复用同一 lnd 实例生成发票) skills/aperture/scripts/start.sh # 获取付费资源 lnget --max-cost 1000 https://localhost:8081/api/data # 查看已存储的令牌 lnget tokens list
| 路径 | 用途 |
|---|---|
~/.aperture/aperture.yaml |
配置文件 |
~/.aperture/aperture.db |
SQLite 数据库 |
~/.aperture/tls.cert |
TLS 证书 |
~/.aperture/tls.key |
TLS 私钥 |
~/.aperture/aperture.log |
日志文件 |
修改配置中的 listenaddr 为其他端口,或使用 setup.sh --port 参数指定新端口。
确认 lnd 正在运行且钱包已解锁。检查配置中 lndhost、tlspath 和 macdir 是否指向正确的 lnd 实例。
确认请求路径匹配某项服务的 pathregexp,且未落入 authwhitelistpaths 白名单中。同时确保 authenticator.disable 未设为 true。
客户端必须精确提交挑战响应中提供的 macaroon,并附带正确的 preimage。请验证该 preimage 是否与付款哈希一致。
相关专题
热门下载
相关下载
精品课程
共6课时 | 54.7万人学习
共89课时 | 133.7万人学习
共49课时 | 82.4万人学习