Skip to content

jarviswrap/lan

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

secure-comm

为聊天系统提供安全的文本/文件传输子系统。支持两种传输方式:TLS 与 Noise(编译期特性切换,默认 Noise)。统一采用长度前缀帧与 bincode 序列化;Noise 模式支持断点续传与完整性校验(BLAKE3)。

项目结构

  • Cargo.toml:依赖、特性与二进制目标(默认 noise 特性)
  • src/lib.rs:帧定义、加密帧读写(Noise)、TLS 配置加载(TLS)
  • src/bin/server.rs:服务端(按特性选择 Noise/TLS 的入口与处理)
  • src/bin/client.rs:客户端(按特性选择 Noise/TLS 的入口与处理)
  • scripts/gen-keys.sh:生成 Noise 所需 X25519 密钥并打印公钥指纹

帧与常量

  • Frame 变体(src/lib.rs
    • Text { id, content }
    • FileChunk { file_id, seq, data, eof, total_size, digest }
    • ResumeQuery { file_id }(Noise 续传)
    • ResumeStatus { file_id, next_seq, written }(Noise 续传)
  • 常量
    • MAX_FRAME_SIZE = 8MB
    • CHUNK_SIZE = 64KB

编译期特性

  • default = ["noise"]
  • noise:启用 snow,使用 Noise IK (Noise_IK_25519_ChaChaPoly_BLAKE2s)
  • tls:启用 rustlstokio-rustlsrustls-pemfile,使用 TCP+TLS
  • 互斥保护:同时启用会报错(features 'noise' and 'tls' are mutually exclusive

运行方式

  • Noise(默认特性)
    • 生成密钥:chmod +x scripts/gen-keys.sh && ./scripts/gen-keys.sh ./keys
    • 服务端:cargo run --bin server -- 0.0.0.0:8443 ./keys/server_priv.key ./storage
    • 客户端文本:cargo run --bin client -- 127.0.0.1:8443 ./keys/client_priv.key ./keys/server_pub.key text 你好
    • 客户端文件:cargo run --bin client -- 127.0.0.1:8443 ./keys/client_priv.key ./keys/server_pub.key file ./big.bin
  • TLS(需关闭默认特性)
    • 生成证书:openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/CN=localhost"
    • 服务端:cargo run --no-default-features --features tls --bin server -- 0.0.0.0:8443 cert.pem key.pem ./storage
    • 客户端文本:cargo run --no-default-features --features tls --bin client -- 127.0.0.1:8443 cert.pem text 你好
    • 客户端文件:cargo run --no-default-features --features tls --bin client -- 127.0.0.1:8443 cert.pem file ./big.bin

行为与验证

  • 文本消息(Noise/TLS)
    • 服务端打印 text <uuid> <内容>
  • 文件传输(Noise)
    • 续传:客户端先发 ResumeQuery,服务端回 ResumeStatus,客户端按 written 偏移继续;分块大小 CHUNK_SIZE
    • 完整性:EOF 尾帧携带全文件 BLAKE3 摘要与总长度,服务端落盘后整体校验,成功打印 saved
  • 文件传输(TLS)
    • 不含续传;同样在 EOF 校验长度与摘要并打印结果

常见问题

  • 切换到 TLS 时必须使用 --no-default-features --features tls
  • Noise 握手需匹配的密钥对:客户端用 client_priv.key 与服务端 server_pub.key
  • 续传文件的 file_id 使用 UUID v5(path+size),源文件路径或大小变化会被视为新文件

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors