Skip to content

Commit

Permalink
Merge pull request #1 from sunsided/feature/tls
Browse files Browse the repository at this point in the history
TLS
  • Loading branch information
sunsided authored Nov 3, 2023
2 parents c971862 + 4acf4cc commit cbd2640
Show file tree
Hide file tree
Showing 10 changed files with 413 additions and 12 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Rust

on:
push:
branches:
- main
- feature/*
pull_request:
branches:
- main

env:
CARGO_TERM_COLOR: always

jobs:
checks:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- windows-latest

toolchain:
- 1.73.0
- stable
- nightly

steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.toolchain }}
profile: minimal
override: true
components: rustfmt, clippy
- name: Install Protoc
uses: arduino/setup-protoc@v1
- uses: Swatinem/rust-cache@v2
with:
key: ${{ runner.os }}-${{ hashFiles('Cargo.lock') }}-${{ matrix.toolchain }}
- name: Build
run: cargo build
- name: Run tests
run: cargo test
- name: clippy
run: cargo clippy -- --deny "warnings"
- name: fmt
run: cargo fmt -- --check
115 changes: 115 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ axum = { version = "0.6.20", features = ["http2"] }
ctrlc = "3.4.1"
dotenvy = "0.15.7"
exitcode = "1.1.2"
futures-util = "0.3.29"
hyper = { version = "0.14.27", features = ["http1", "http2"] }
log = "0.4.20"
pin-project = "1.1.3"
prost = "0.12.1"
rustls = "0.21.8"
tokio = { version = "1.33.0", features = ["rt-multi-thread", "macros"] }
tonic = "0.10.2"
tonic-reflection = "0.10.2"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.17", features = ["env-filter", "json"] }
rustls-pemfile = "1.0.3"
tokio-rustls = "0.24.1"
tls-listener = { version = "0.8.0", features = ["rustls", "hyper-h1", "hyper-h2"] }
futures-util = "0.3.29"

[build-dependencies]
tonic-build = "0.10.2"
61 changes: 58 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,72 @@
# gRPC and HTTP/1.1 co-hosting
# Tonic gRPC and Axum HTTP/1.1+2 co-hosting in Rust

An attempt at hosting HTTP/1.1, HTTP/2 and gRPC on the same port(s).

Multiple Hyper servers are spawned on different endpoints to showcase the use of binding to different IP addresses
and ports while reusing the same server components. A Hyper service is used to switch the incoming traffic based on the
`content-type` header and if `application/grpc` is detected, traffic is forwarded to the Tonic server; all other
cases forward to Axum. This allows for transparent use of HTTP/1.1 and HTTP/2 (prior knowledge), as well
as ALPN on the TLS-enabled ports.

This project uses:

- [Hyper] as the server for HTTP/1 and HTTP/2 support.
- [tls-listener] with [tokio-rustls] is used to provide TLS with `h2` and `http/1.1` ALPN support.
- [Axum] as the HTTP server.
- [Tonic] as the gRPC server.

[Hyper]: https://github.com/hyperium/hyper
[Axum]: https://github.com/tokio-rs/axum
[Tonic]: https://github.com/hyperium/tonic
[tls-listener]: https://github.com/tmccombs/tls-listener
[tokio-rustls]: https://github.com/rustls/tokio-rustls

## Curl

### non-TLS

```shell
curl -v http://127.0.0.1:36849/
curl -v http://127.1.0.1:36849/
curl --http2-prior-knowledge --insecure -vv https://127.0.0.1:36849/
```

### TLS with ALPN

```shell
curl --insecure -v https://127.0.0.1:36850/
curl --http1.1 --insecure -vv https://127.0.0.1:36850/
curl --http2 --insecure -vv https://127.0.0.1:36850/
```

## nghttp (HTTP/2)

```shell
nghttp -v http://127.0.0.1:36849
nghttp -y -v https://127.0.0.1:36850
```

## gRPC testing

Use gRPC reflection to introspect the service:

```shell
grpcurl --plaintext --use-reflection 127.0.0.1:50052 list
grpcurl --plaintext --use-reflection 127.0.0.1:36849 list
grpcurl --insecure --use-reflection 127.0.0.1:36850 list
```

Send a test request:

```shell
grpcurl --plaintext --use-reflection -d '{ "message": "World" }' 127.0.0.1:50052 example.YourService/YourMethod
grpcurl --plaintext --use-reflection -d '{ "message": "World" }' 127.0.0.1:36849 example.YourService/YourMethod
grpcurl --insecure --use-reflection -d '{ "message": "World" }' 127.0.0.1:36850 example.YourService/YourMethod
```

## Recommended reads

The _Combining Axum, Hyper, Tonic, and Tower for hybrid web/gRPC apps_ series:

- [Part 1: Overview of Tower](https://www.fpcomplete.com/blog/axum-hyper-tonic-tower-part1/)
- [Part 2: Understanding Hyper, and first experiences with Axum](https://www.fpcomplete.com/blog/axum-hyper-tonic-tower-part2/)
- [Part 3: Demonstration of Tonic for a gRPC client/server](https://www.fpcomplete.com/blog/axum-hyper-tonic-tower-part3/)
- [Part 4: How to combine Axum and Tonic services into a single service](https://www.fpcomplete.com/blog/axum-hyper-tonic-tower-part4/)
33 changes: 33 additions & 0 deletions certs/server.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-----BEGIN CERTIFICATE-----
MIIFwTCCA6mgAwIBAgIUPnWB8oaSufyY/RTe3j6sSP1rMi4wDQYJKoZIhvcNAQEL
BQAwbzELMAkGA1UEBhMCREUxDzANBgNVBAgMBkJlcmxpbjEPMA0GA1UEBwwGQmVy
bGluMRQwEgYDVQQKDAt3aWRlbWVhZG93czEUMBIGA1UECwwLd2lkZW1lYWRvd3Mx
EjAQBgNVBAMMCWxvY2FsaG9zdDAgFw0yMzExMDMyMTEyMDNaGA8yMTIzMTAxMDIx
MTIwM1owbzELMAkGA1UEBhMCREUxDzANBgNVBAgMBkJlcmxpbjEPMA0GA1UEBwwG
QmVybGluMRQwEgYDVQQKDAt3aWRlbWVhZG93czEUMBIGA1UECwwLd2lkZW1lYWRv
d3MxEjAQBgNVBAMMCWxvY2FsaG9zdDCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCC
AgoCggIBAM8TJ3266Hxoy7qH+/fgdvkF+G6MlPVljzyAKMxkXGenwSWqL7yhUe65
KOc4zhkGra1Hu2gkwyNyMbZ3Jo+McG57Mel5H58u9mBSaO90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