AgentSkillsCN

rust-network-module

为 nat464-sidecar 项目搭建新的 Rust 异步网络模块。适用于添加 TCP/UDP 监听器、协议处理器、代理或转换模块时使用。触发器包括:“搭建模块”、“添加新模块”、“创建监听器”、“创建代理”、“添加协议处理器”、“新建网络模块”、“搭建 UDP 模块”、“添加 ICMP 转换”。

SKILL.md
--- frontmatter
name: rust-network-module
description: "Scaffold new Rust async networking modules for the nat464-sidecar project. Use when adding TCP/UDP listeners, protocol handlers, proxies, or translation modules. Triggers: 'scaffold module', 'add new module', 'create listener', 'create proxy', 'add protocol handler', 'new networking module', 'scaffold UDP', 'add ICMP translation'."

Rust Network Module Scaffolding

Scaffold new async networking modules following nat464-sidecar's established patterns.

Workflow

1. Determine Module Type

TypePatternExample
ListenerAccept connections, spawn handler tasksproxy/inbound.rs
Protocol handlerParse/serialize protocol messagessocks5/handshake.rs
Relay/proxyBidirectional byte forwarding between streamsproxy/copy.rs, socks5/relay.rs
Resolver/racerDNS resolution, connection racinghappy_eyeballs/resolver.rs, racer.rs
HTTP serverHyper-based HTTP endpointhealth.rs

2. Create Module Files

Every module is a directory under src/ with mod.rs + implementation files.

code
src/<module_name>/
├── mod.rs           # Re-exports, shared types/constants
├── <primary>.rs     # Main logic
└── (optional).rs    # Additional files as needed

Register in parent: add pub mod <module_name>; to src/main.rs or parent mod.rs.

3. Apply Project Conventions

See references/patterns.md for the full pattern catalog with code templates.

Key conventions:

  • pub async fn run_<name>(port: u16, ...) -> anyhow::Result<()> for server entry points
  • tokio::spawn per connection, errors logged inside spawn (never crash the server)
  • tracing structured fields: debug! per-connection, info! lifecycle, error! failures
  • anyhow::Result everywhere, anyhow::bail! for protocol errors
  • In-module #[cfg(test)] mod tests with test_pair() helper for TCP stream pairs
  • Tests bind to port 0 for random allocation

4. Add to Cargo.toml If Needed

Only add dependencies if the new module requires crates not already in Cargo.toml. Current deps: tokio, clap, tracing, tracing-subscriber, hyper, hyper-util, http-body-util, tokio-util, anyhow, thiserror.

5. Wire Into main.rs

Add the new server to tokio::try_join! in main.rs if it runs as a long-lived server. Add CLI flags to config.rs if the module needs configuration.

6. Verify

bash
cargo build          # Compiles
cargo test           # All tests pass
cargo clippy -- -W clippy::all  # No new warnings

Module Templates

Use the asset templates as starting points:

TemplateUse For
assets/listener.rs.tmplTCP/UDP listener with spawn-per-connection
assets/protocol.rs.tmplProtocol parser with handshake + types
assets/mod.rs.tmplModule root with constants and re-exports
assets/tests.rs.tmplTest block with test_pair() helper