Skip to content

Commit 1909935

Browse files
committed
epic: first committed version
1 parent 1be4d9b commit 1909935

21 files changed

Lines changed: 2538 additions & 0 deletions

.github/workflows/ci.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
RUST_BACKTRACE: 1
12+
13+
jobs:
14+
check:
15+
name: Check ${{ matrix.arch }}
16+
runs-on: ${{ matrix.runs-on }}
17+
strategy:
18+
matrix:
19+
include:
20+
- arch: x86_64
21+
runs-on: ubuntu-24.04
22+
- arch: aarch64
23+
runs-on: ubuntu-24.04-arm
24+
steps:
25+
- uses: actions/checkout@v4
26+
- uses: dtolnay/rust-toolchain@nightly
27+
with:
28+
components: rustfmt
29+
- uses: dtolnay/rust-toolchain@stable
30+
with:
31+
components: clippy
32+
- name: Check kTLS environment
33+
run: bash scripts/check-ktls.sh
34+
- name: Run all checks
35+
run: bash scripts/check.sh

.github/workflows/release.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*'
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
publish:
13+
name: Publish to crates.io
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
- uses: dtolnay/rust-toolchain@stable
18+
- name: Publish
19+
run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}
20+
21+
github-release:
22+
name: Create GitHub Release
23+
runs-on: ubuntu-latest
24+
steps:
25+
- uses: actions/checkout@v4
26+
- name: Extract release notes
27+
id: extract-release-notes
28+
run: |
29+
VERSION=${GITHUB_REF#refs/tags/v}
30+
echo "version=$VERSION" >> $GITHUB_OUTPUT
31+
- name: Create Release
32+
uses: softprops/action-gh-release@v1
33+
with:
34+
generate_release_notes: true

CONTRIBUTING.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Contributing to compio-ktls
2+
3+
Thank you for your interest in contributing to compio-ktls!
4+
5+
## Getting Started
6+
7+
1. Fork the repository
8+
2. Clone your fork: `git clone https://github.com/YOUR_USERNAME/compio-ktls.git`
9+
3. Create a new branch: `git checkout -b my-feature`
10+
11+
## Development Setup
12+
13+
### Prerequisites
14+
15+
- Linux kernel 6.6 LTS or newer (for running tests)
16+
- kTLS kernel module loaded: `sudo modprobe tls`
17+
18+
Run the environment check script:
19+
20+
```bash
21+
./scripts/check-ktls.sh
22+
```
23+
24+
### Running Tests
25+
26+
```bash
27+
# Run all checks (fmt, clippy, tests, doc)
28+
./scripts/check.sh
29+
30+
# Or run individually
31+
cargo +nightly fmt --all -- --check
32+
cargo clippy --all-targets --all-features -- -D warnings
33+
cargo test --all-features
34+
cargo doc --all-features --no-deps
35+
```
36+
37+
## Making Changes
38+
39+
1. **Code Style**: Follow the project's code style. Run `cargo fmt` before committing.
40+
2. **Tests**: Add tests for new functionality. Ensure all tests pass.
41+
3. **Documentation**: Update documentation for public APIs.
42+
4. **Commit Messages**: Write clear, concise commit messages.
43+
44+
## Pull Request Process
45+
46+
1. Ensure all tests pass locally
47+
2. Update documentation if needed
48+
3. Create a pull request with a clear description of changes
49+
4. Address any feedback from reviewers
50+
51+
## Code of Conduct
52+
53+
- Be respectful and inclusive
54+
- Focus on constructive feedback
55+
- Help create a welcoming environment for all contributors
56+
57+
## License
58+
59+
By contributing, you agree that your contributions will be licensed under the same licenses as the project (Apache-2.0 OR MulanPSL-2.0).
60+

Cargo.toml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,46 @@ edition = "2024"
55
description = "Kernel TLS (kTLS) support for Compio"
66
repository = "https://github.com/fantix/compio-ktls"
77
license = "Apache-2.0 OR MulanPSL-2.0"
8+
authors = ["Fantix King <fantix.king@gmail.com>"]
89
keywords = ["ktls", "tls", "compio", "async", "kernel"]
910
categories = ["asynchronous", "network-programming", "cryptography"]
1011
readme = "README.md"
1112

1213
[dependencies]
14+
rustls = { version = "0.23", default-features = false, optional = true }
15+
16+
[dependencies.compio-io]
17+
version = "0.9"
18+
default-features = false
19+
features = ["ancillary", "bytemuck"]
20+
21+
[dependencies.compio-buf]
22+
version = "0.8"
23+
default-features = false
24+
25+
[target."cfg(target_os = \"linux\")".dependencies]
26+
libc = "0.2"
27+
ktls-core = "0.0.5"
28+
29+
[features]
30+
default = ["rustls"]
31+
rustls = ["dep:rustls", "ktls-core/shim-rustls"]
32+
ring = ["rustls", "rustls/ring"]
33+
app-write-with-empty-ancillary = []
34+
35+
[dev-dependencies]
36+
compio = { version = "0.18", features = ["net", "rustls", "macros"] }
37+
rustls-native-certs = "0.8"
38+
rustls-pemfile = "2.2"
39+
40+
[patch.crates-io.compio]
41+
git = "https://github.com/fantix/compio.git"
42+
rev = "a40dbc4685898f61f8a6702da9aa38872e5f7629"
43+
44+
[patch.crates-io.compio-io]
45+
git = "https://github.com/fantix/compio.git"
46+
rev = "a40dbc4685898f61f8a6702da9aa38872e5f7629"
47+
48+
[patch.crates-io.compio-buf]
49+
git = "https://github.com/fantix/compio.git"
50+
rev = "a40dbc4685898f61f8a6702da9aa38872e5f7629"

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,82 @@
22

33
Kernel TLS (kTLS) support for [Compio](https://github.com/compio-rs/compio).
44

5+
## Overview
6+
7+
- Built on top of [ktls-core](https://github.com/hanyu-dev/ktls)
8+
- Not tied to any specific Compio runtime implementation
9+
- Pluggable TLS implementations (currently supports Rustls)
10+
- Currently supports TLS 1.3 only
11+
- Supports NewSessionTicket, KeyUpdate, and Alert message handling
12+
13+
## Features
14+
15+
- `rustls` (default): Enable Rustls integration
16+
- `ring`: Use ring as the crypto backend
17+
- `app-write-with-empty-ancillary`: Use `write_with_ancillary()` with empty ancillary data
18+
for application data writes. Enable this when using `compio-net`'s zero-copy send API
19+
(introduced in compio#756), as it bypasses io-uring's `MSG_ZEROCOPY` flag which is
20+
incompatible with kTLS.
21+
22+
## Usage
23+
24+
```rust
25+
use compio_ktls::{KtlsConnector, KtlsAcceptor};
26+
27+
// Client side
28+
let connector = KtlsConnector::from(client_config);
29+
match connector.connect("example.com", tcp_stream).await? {
30+
Ok(stream) => {
31+
// kTLS enabled successfully
32+
}
33+
Err(stream) => {
34+
// kTLS unavailable, fallback to original stream
35+
}
36+
}
37+
38+
// Server side
39+
let acceptor = KtlsAcceptor::from(server_config);
40+
match acceptor.accept(tcp_stream).await? {
41+
Ok(stream) => {
42+
// kTLS enabled successfully
43+
}
44+
Err(stream) => {
45+
// kTLS unavailable, fallback to original stream
46+
}
47+
}
48+
```
49+
50+
## Requirements
51+
52+
Requires Linux kernel with kTLS support, version 6.6 LTS or newer is recommended.
53+
54+
Check if the kTLS module is loaded:
55+
56+
```bash
57+
lsmod | grep tls
58+
```
59+
60+
If not loaded, you can manually load it:
61+
62+
```bash
63+
sudo modprobe tls
64+
```
65+
66+
Also requires Rustls with `enable_secret_extraction` enabled:
67+
68+
```rust
69+
use std::sync::Arc;
70+
use rustls::ClientConfig;
71+
72+
let mut config = ClientConfig::builder()
73+
.dangerous()
74+
.with_custom_certificate_verifier(/* ... */)
75+
.with_no_client_auth();
76+
77+
config.enable_secret_extraction = true;
78+
79+
let config = Arc::new(config);
80+
```
581

682
## License
783

README.zh.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,81 @@
22

33
[Compio](https://github.com/compio-rs/compio) 提供内核 TLS (kTLS) 支持。
44

5+
## 概述
6+
7+
- 基于 [ktls-core](https://github.com/hanyu-dev/ktls) 实现
8+
- 不锁定特定的 Compio 运行时实现
9+
- 可插拔的 TLS 实现(目前支持 Rustls)
10+
- 目前仅支持 TLS 1.3
11+
- 支持 NewSessionTicket、KeyUpdate 和 Alert 消息处理
12+
13+
## 可选 features
14+
15+
- `rustls`(默认):启用 Rustls 集成
16+
- `ring`:使用 ring 作为加密后端
17+
- `app-write-with-empty-ancillary`:在写入应用数据时使用 `write_with_ancillary()`
18+
并传入空的辅助数据。当使用 `compio-net` 的零拷贝发送 API(compio#756 引入)时需要启用此
19+
feature,因为它绕过了 io-uring 的 `MSG_ZEROCOPY` 标志,该标志与 kTLS 不兼容。
20+
21+
## 使用方法
22+
23+
```rust
24+
use compio_ktls::{KtlsConnector, KtlsAcceptor};
25+
26+
// 客户端
27+
let connector = KtlsConnector::from(client_config);
28+
match connector.connect("example.com", tcp_stream).await? {
29+
Ok(stream) => {
30+
// 成功启用 kTLS
31+
}
32+
Err(stream) => {
33+
// kTLS 不可用,回退到原始 stream
34+
}
35+
}
36+
37+
// 服务端
38+
let acceptor = KtlsAcceptor::from(server_config);
39+
match acceptor.accept(tcp_stream).await? {
40+
Ok(stream) => {
41+
// 成功启用 kTLS
42+
}
43+
Err(stream) => {
44+
// kTLS 不可用,回退到原始 stream
45+
}
46+
}
47+
```
48+
49+
## 环境要求
50+
51+
需要 Linux 内核支持 kTLS,建议使用 6.6 或更新版本的 LTS 内核。
52+
53+
检查内核是否已加载 kTLS 模块:
54+
55+
```bash
56+
lsmod | grep tls
57+
```
58+
59+
如果没有加载,可以手动加载:
60+
61+
```bash
62+
sudo modprobe tls
63+
```
64+
65+
另需 Rustls 启用 `enable_secret_extraction`
66+
67+
```rust
68+
use std::sync::Arc;
69+
use rustls::ClientConfig;
70+
71+
let mut config = ClientConfig::builder()
72+
.dangerous()
73+
.with_custom_certificate_verifier(/* ... */)
74+
.with_no_client_auth();
75+
76+
config.enable_secret_extraction = true;
77+
78+
let config = Arc::new(config);
79+
```
580

681
## 许可证
782

rustfmt.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
unstable_features = true
2+
3+
style_edition = "2024"
4+
5+
group_imports = "StdExternalCrate"
6+
imports_granularity = "Crate"
7+
reorder_imports = true
8+
9+
wrap_comments = true
10+
normalize_comments = true
11+
12+
reorder_impl_items = true
13+
condense_wildcard_suffixes = true
14+
enum_discrim_align_threshold = 20
15+
use_field_init_shorthand = true
16+
17+
format_strings = true
18+
format_code_in_doc_comments = true
19+
format_macro_matchers = true

0 commit comments

Comments
 (0)