aws-samples/sample-x402-content-monetization-with-cloudfront-and-waf
GitHub: aws-samples/sample-x402-content-monetization-with-cloudfront-and-waf
基于 AWS CloudFront 边缘节点和 x402 支付协议,实现面向 AI 代理和机器人的按请求付费内容变现方案。
Stars: 12 | Forks: 2
# sample-x402-content-monetization-with-cloudfront-and-waf
通过一键部署实现内容变现。此解决方案使用 [x402 支付协议](https://x402.org) 向访问您内容的 AI 代理和机器人收费 — 在 Base 区块链上以 USDC 稳定币进行支付,并在 AWS 边缘节点强制执行。
部署单个 SAM 堆栈即可获得:带有示例内容的 Amazon CloudFront 分发、带有 Bot Control v5(650 多种机器人)的 AWS WAF、用于支付验证和结算的 AWS Lambda@Edge、可视化路由配置编辑器以及收入仪表板。配置存储在 AWS Systems Manager (SSM) Parameter Store 中,凭证存储在 AWS Secrets Manager 中,日志存储在 Amazon CloudWatch 中,内容存储在 Amazon S3 中。无需管理服务器,无需编写代码。

## 工作原理
发布者通过基于条件的访问策略,按 URL 路径配置定价。经过验证的机器人、未经验证的机器人和人类可以分别设定不同的价格 — 或者直接被完全阻止。配置存储在 SSM Parameter Store 中,可以在不重新部署的情况下进行更新。
## 架构
```
graph LR
Agent["AI Agent"] --> WAF["WAF +
Bot Control"] WAF -->|"Price header"| LReq WAF --->|"Block -> 403"| Agent subgraph CloudFront direction TB LReq["Origin Request
Lambda@Edge
(verify)"] LRes["Origin Response
Lambda@Edge
(settle)"] end LReq -->|Forward| Origin["Origin"] Origin -->|Response| LRes LRes --->|Content| Agent subgraph "External Services" direction LR Facilitator["x402
Facilitator"] Config["SSM /
Secrets Manager"] end LReq -->|Verify| Facilitator LRes -->|Settle| Facilitator LReq -.->|Config| Config LRes -.->|Config| Config classDef waf fill:#dd344c,stroke:#232f3e,color:white classDef lambda fill:#f90,stroke:#232f3e,color:#232f3e classDef storage fill:#3b48cc,stroke:#232f3e,color:white classDef external fill:#2ea44f,stroke:#232f3e,color:white class WAF waf class LReq,LRes lambda class Config,Origin storage class Facilitator,Agent external ``` ### 请求序列 ``` sequenceDiagram participant Agent as AI Agent participant WAF as AWS WAF participant CF as CloudFront participant LReq as Origin Request Lambda@Edge participant Origin as Origin Server participant LRes as Origin Response Lambda@Edge participant Facilitator as Facilitator Agent->>WAF: HTTP Request WAF->>WAF: Bot Control labels + Route evaluation alt WAF block rule matches WAF-->>Agent: 403 Forbidden (WAF Block) else WAF injects price header WAF->>CF: Request + x-x402-route-action header CF->>LReq: Origin Request event alt Price is "0" or header absent LReq->>Origin: Pass through (no payment) else Has price, no payment header LReq-->>Agent: 402 Payment Required else Has price + payment header LReq->>Facilitator: Verify payment alt Payment invalid LReq-->>Agent: 402 with error else Payment valid LReq->>Origin: Forward + x-x402-pending-settlement Origin->>LRes: Origin Response event alt Status < 400 LRes->>Facilitator: Settle payment LRes-->>Agent: Content response else Status >= 400 LRes-->>Agent: Error response (no settlement) end end end end ``` ## 路由配置 路由使用 glob 模式和基于条件的访问策略。配置存储在 SSM Parameter Store 中,可以在不重新部署的情况下进行更新 — 使用可视化编辑器或 CLI。  默认部署包含带有按路由定价的示例内容: | 路由 | 已验证的机器人 | 未验证的机器人 | 人类 | |---|---|---|---| | `/api/sports.json` | $0.003/req | $0.03/req | 已阻止 | | `/api/politics.json` | $0.005/req | $0.05/req | 已阻止 | | `/articles/politics.html` | $0.002/req | $0.02/req | 免费 | | `/api/**` (全匹配) | $0.003/req | $0.03/req | 已阻止 | | `/articles/**` (全匹配) | $0.001/req | $0.01/req | 免费 | | `/**` (全匹配) | 免费 | $0.001/req | 免费 | 路由从上到下进行评估 — 第一个匹配的模式胜出。在路由内部,策略从上到下进行评估 — 第一个匹配的条件决定操作。条件与 [AWS WAF Bot Control 标签](https://docs.aws.amazon.com/waf/latest/developerguide/aws-managed-rule-groups-bot.html) 进行匹配。 ### 配置格式 - **`pattern`** — URL 路径 glob 模式:`*` 匹配单个片段,`**` 匹配多个片段,精确路径按字面值匹配 - **`condition`** — WAF 标签字符串,`"default"`(后备)或用于组合条件的布尔表达式(`and`、`or`、`not`) - **`action`** — 以 USD 计的价格(例如 `"0.001"`),`"0"` 表示免费,或 `"block"` 表示拒绝访问 ### 更新定价(无需重新部署) ``` aws ssm put-parameter \ --name "/x402-edge//config/routes" \
--value '' \
--type String \
--overwrite
```
更改会通过 EventBridge 在几秒钟内传播到 WAF。系统会运行一个每 5 分钟执行一次的计划同步作为追赶机制。您也可以使用位于 `/editor/index.html` 的可视化编辑器。
## AI 活动仪表板
AWS WAF 包含 [AI Activity Dashboard](https://aws.amazon.com/about-aws/whats-new/2026/02/aws-waf-ai-activity-dashboard/)。它提供了对 AI 机器人流量趋势的可见性,展示了哪些 AI 机器人正在访问您的内容、随时间变化的请求量以及类别细分 — 帮助您做出明智的定价决策。
## Facilitator 选择
| FacilitatorType | 服务 | 需要身份验证 | 网络 |
|---|---|---|---|
| `x402.org` | `https://x402.org/facilitator` | 否 | 仅限测试网 (Base Sepolia, Solana Devnet) |
| `cdp` | CDP Facilitator | 是 (CDP API 密钥) | 测试网 + 主网 (Base, Base Sepolia, Solana, Solana Devnet) |
facilitator 负责处理支付验证和链上结算。`x402.org` facilitator 仅限测试网使用 — 对于主网部署,请使用 `cdp`。有关详细信息,请参阅 [x402 网络支持](https://www.x402.org/) 文档。
## 开始使用
### 前置条件
- 具有创建 CloudFront、WAF、Lambda、SSM、Secrets Manager 和 S3 资源权限的 AWS 账户
- [AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html)
- Node.js 24+
- 一个 Ethereum 钱包地址(用于接收 USDC 支付)
### 部署
```
sam build
sam deploy --guided --region us-east-1 --capabilities CAPABILITY_NAMED_IAM
```
SAM 将提示输入以下参数:
| 参数 | 描述 | 默认值 |
|---|---|---|
| `PayToAddress` | 您的 Ethereum 钱包地址(接收 USDC) | (必填) |
| `Network` | `eip155:84532` (Base Sepolia 测试网) 或 `eip155:8453` (Base 主网) | `eip155:84532` |
| `FacilitatorType` | `x402.org` (免费,无需身份验证,仅限测试网) 或 `cdp` (需要 CDP API 密钥,测试网 + 主网) | `x402.org` |
| `RouteConfigJson` | 定价配置 JSON (见上文) | 默认配置 |
| `OriginDomainName` | 自定义源域 (留空 = 示例 S3 源) | `""` |
| `CdpApiKeyName` | CDP API 密钥名称 (仅在 FacilitatorType 为 `cdp` 时) | `""` |
| `CdpApiKeyPrivateKey` | CDP API 密钥私钥 (仅在 FacilitatorType 为 `cdp` 时) | `""` |
堆栈输出包括:
- **CloudFront URL** — 您启用付费墙的内容
- **编辑器 URL** — 位于 `/editor/index.html` 的可视化路由配置编辑器
- **仪表板 URL** — CloudWatch 收入仪表板
### 流量生成器
包含一个用于测试和演示的流量生成器。它发送带有实际链上 x402 支付的真实 HTTP 流量。有关设置和使用,请参阅 [`scripts/README.md`](scripts/README.md)。
```
npx tsx scripts/traffic-gen.ts # one-shot playlist (18 requests)
npx tsx scripts/traffic-gen.ts --duration 15 # continuous mode (15 min, sinusoidal trends)
```

### 开发
```
npm install
npm test # all tests
npm run test:unit # unit tests
npm run test:property # property-based tests (fast-check)
npm run test:integration # integration tests with mocked AWS SDK
```
## 安全
有关更多信息,请参阅 [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications)。
## 许可证
本库基于 MIT-0 许可证授权。请参阅 LICENSE 文件。
Bot Control"] WAF -->|"Price header"| LReq WAF --->|"Block -> 403"| Agent subgraph CloudFront direction TB LReq["Origin Request
Lambda@Edge
(verify)"] LRes["Origin Response
Lambda@Edge
(settle)"] end LReq -->|Forward| Origin["Origin"] Origin -->|Response| LRes LRes --->|Content| Agent subgraph "External Services" direction LR Facilitator["x402
Facilitator"] Config["SSM /
Secrets Manager"] end LReq -->|Verify| Facilitator LRes -->|Settle| Facilitator LReq -.->|Config| Config LRes -.->|Config| Config classDef waf fill:#dd344c,stroke:#232f3e,color:white classDef lambda fill:#f90,stroke:#232f3e,color:#232f3e classDef storage fill:#3b48cc,stroke:#232f3e,color:white classDef external fill:#2ea44f,stroke:#232f3e,color:white class WAF waf class LReq,LRes lambda class Config,Origin storage class Facilitator,Agent external ``` ### 请求序列 ``` sequenceDiagram participant Agent as AI Agent participant WAF as AWS WAF participant CF as CloudFront participant LReq as Origin Request Lambda@Edge participant Origin as Origin Server participant LRes as Origin Response Lambda@Edge participant Facilitator as Facilitator Agent->>WAF: HTTP Request WAF->>WAF: Bot Control labels + Route evaluation alt WAF block rule matches WAF-->>Agent: 403 Forbidden (WAF Block) else WAF injects price header WAF->>CF: Request + x-x402-route-action header CF->>LReq: Origin Request event alt Price is "0" or header absent LReq->>Origin: Pass through (no payment) else Has price, no payment header LReq-->>Agent: 402 Payment Required else Has price + payment header LReq->>Facilitator: Verify payment alt Payment invalid LReq-->>Agent: 402 with error else Payment valid LReq->>Origin: Forward + x-x402-pending-settlement Origin->>LRes: Origin Response event alt Status < 400 LRes->>Facilitator: Settle payment LRes-->>Agent: Content response else Status >= 400 LRes-->>Agent: Error response (no settlement) end end end end ``` ## 路由配置 路由使用 glob 模式和基于条件的访问策略。配置存储在 SSM Parameter Store 中,可以在不重新部署的情况下进行更新 — 使用可视化编辑器或 CLI。  默认部署包含带有按路由定价的示例内容: | 路由 | 已验证的机器人 | 未验证的机器人 | 人类 | |---|---|---|---| | `/api/sports.json` | $0.003/req | $0.03/req | 已阻止 | | `/api/politics.json` | $0.005/req | $0.05/req | 已阻止 | | `/articles/politics.html` | $0.002/req | $0.02/req | 免费 | | `/api/**` (全匹配) | $0.003/req | $0.03/req | 已阻止 | | `/articles/**` (全匹配) | $0.001/req | $0.01/req | 免费 | | `/**` (全匹配) | 免费 | $0.001/req | 免费 | 路由从上到下进行评估 — 第一个匹配的模式胜出。在路由内部,策略从上到下进行评估 — 第一个匹配的条件决定操作。条件与 [AWS WAF Bot Control 标签](https://docs.aws.amazon.com/waf/latest/developerguide/aws-managed-rule-groups-bot.html) 进行匹配。 ### 配置格式 - **`pattern`** — URL 路径 glob 模式:`*` 匹配单个片段,`**` 匹配多个片段,精确路径按字面值匹配 - **`condition`** — WAF 标签字符串,`"default"`(后备)或用于组合条件的布尔表达式(`and`、`or`、`not`) - **`action`** — 以 USD 计的价格(例如 `"0.001"`),`"0"` 表示免费,或 `"block"` 表示拒绝访问 ### 更新定价(无需重新部署) ``` aws ssm put-parameter \ --name "/x402-edge/
标签:AI代理支付, AWS CloudFront, AWS SAM, AWS Secrets Manager, AWS WAF, AWS无服务器架构, Base区块链, Bot Control, CISA项目, Lambda@Edge, MITM代理, SSM Parameter Store, Streamlit, USDC, Web3, x402协议, 亚马逊CloudWatch, 亚马逊S3, 亚马逊云科技, 内容变现, 加密货币, 区块链支付, 可视化配置, 微支付, 收入仪表盘, 无服务器, 机器人管理, 漏洞探索, 稳定币, 自动化攻击, 访问控制, 边缘计算, 边缘验证, 防爬虫