Compare commits
13 Commits
v0.1.8
...
feature/tl
| Author | SHA1 | Date | |
|---|---|---|---|
| 6bc6bb4d62 | |||
| 913e50ff1c | |||
| aecffa0d14 | |||
| 4c2711fc81 | |||
| 1a9ca771ac | |||
| b7ec67ed07 | |||
| aff46b6bfb | |||
| 922ea1f030 | |||
| 6300c43495 | |||
| c21ff86ee4 | |||
| 8d6387773a | |||
| 95149ffd9f | |||
| a140748647 |
@@ -1,5 +1,5 @@
|
||||
local executableName = 'l4p';
|
||||
local build_image = 'img.kie.rs/jjkiers/rust-cross:rust1.71.1-zig';
|
||||
local build_image = 'img.kie.rs/jjkiers/rust-crossbuild:rust1.79.0-zig0.11.0-zig';
|
||||
|
||||
local archs = [
|
||||
{ target: 'aarch64-unknown-linux-musl', short: 'arm64-musl' },
|
||||
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,4 @@
|
||||
/.idea
|
||||
/.vscode
|
||||
/target
|
||||
config.yaml
|
||||
|
||||
55
CHANGELOG.md
Normal file
55
CHANGELOG.md
Normal file
@@ -0,0 +1,55 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [0.1.10] - 2025-01-09
|
||||
|
||||
### Fixed
|
||||
|
||||
* The ClientHello TLS header is now read in full before it is parsed, solving
|
||||
an error where there was not enough data to fully read it. In those cases
|
||||
it was not possible to determine the upstream address and therefore the proxy
|
||||
would go the the default action instead.
|
||||
|
||||
### Changed
|
||||
|
||||
* Updated some dependencies to prevent the build from breaking.
|
||||
|
||||
## [0.1.9] - 2024-06-22
|
||||
|
||||
### Deprecated
|
||||
|
||||
The ability to run `l4p` without arguments is now deprecated. Please use
|
||||
`l4p serve` going forward.
|
||||
|
||||
### Added
|
||||
|
||||
* Added self update functionality. Just run `l4p update` to use it.
|
||||
* Now keeping a change log in the `CHANGELOG.md` file.
|
||||
|
||||
### Changed
|
||||
|
||||
* Updated build pipeline to generate much smaller binaries
|
||||
|
||||
-------
|
||||
|
||||
## Previous versions
|
||||
|
||||
[unreleased]: https://code.kiers.eu/jjkiers/layer4-proxy/compare/v0.1.9...HEAD
|
||||
[0.1.10]: https://code.kiers.eu/jjkiers/layer4-proxy/compare/v0.1.9...v0.1.10
|
||||
[0.1.9]: https://code.kiers.eu/jjkiers/layer4-proxy/compare/v0.1.8...v0.1.9
|
||||
|
||||
|
||||
Types of changes:
|
||||
|
||||
* `Added` for new features.
|
||||
* `Changed` for changes in existing functionality.
|
||||
* `Deprecated` for soon-to-be removed features.
|
||||
* `Removed` for now removed features.
|
||||
* `Fixed` for any bug fixes.
|
||||
* `Security` in case of vulnerabilities.
|
||||
1011
Cargo.lock
generated
1011
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
16
Cargo.toml
16
Cargo.toml
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "l4p"
|
||||
version = "0.1.8"
|
||||
version = "0.1.11"
|
||||
edition = "2021"
|
||||
authors = ["Jacob Kiers <code@kiers.eu>"]
|
||||
license = "Apache-2.0"
|
||||
@@ -20,15 +20,25 @@ name = "l4p"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.102"
|
||||
async-trait = "0.1.73"
|
||||
byte_string = "1"
|
||||
bytes = "1.1"
|
||||
futures = "0.3"
|
||||
log = "0.4"
|
||||
pico-args = "0.5.0"
|
||||
pretty_env_logger = "0.5"
|
||||
rustls = "0.23"
|
||||
rustls-pemfile = "2.2.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_yaml = "0.9.21"
|
||||
time = { version = "0.3.1", features = ["local-offset", "formatting"] }
|
||||
tls-parser = "0.11"
|
||||
time = { version = "0.3.37", features = ["local-offset", "formatting"] }
|
||||
tls-parser = "0.12.2"
|
||||
tokio = { version = "1.0", features = ["full"] }
|
||||
tokio-rustls = "0.26.4"
|
||||
url = "2.2.2"
|
||||
|
||||
[dependencies.self_update]
|
||||
version = "0.42.0"
|
||||
default-features = false
|
||||
features = ["rustls"]
|
||||
|
||||
30
PLAN.md
Normal file
30
PLAN.md
Normal file
@@ -0,0 +1,30 @@
|
||||
## Plan for TLS Termination and Dynamic Port Handling
|
||||
|
||||
### Task
|
||||
Modify the `l4p` (layer 4 proxy) to perform TLS termination and handle dynamic/random ports for backend services. The backend services are on a specific IPv6 address, and the user can dynamically determine the hostname of these services in the format "https://{port}.my-host". The proxy should listen on port 443 and use SNI for routing.
|
||||
|
||||
### Completed Actions
|
||||
|
||||
- Added `tokio-rustls`, `rustls-pemfile`, `anyhow` dependencies to `Cargo.toml`.
|
||||
- Modified `src/config/config_v1.rs` to include `TlsTerminationConfig`, `CertificateConfig`, `SniCertificateConfig` structs and updated `ServerConfig`.
|
||||
- Created `src/tls.rs` (multiple iterations due to compilation issues).
|
||||
- Integrated `anyhow::Result` into various functions and imported `Context` to `src/servers/mod.rs`.
|
||||
- Corrected imports in `src/main.rs`, `src/servers/mod.rs`, `src/servers/protocol/tcp.rs`.
|
||||
- Removed `mod tls;` from `src/servers/protocol/mod.rs`.
|
||||
- Attempted to fix various compilation errors related to `rustls` API changes, lifetime issues, and `tokio` task handling.
|
||||
- Changed `handle.await??;` to explicit match for debugging purposes.
|
||||
|
||||
### Current State (with persistent errors)
|
||||
|
||||
The code currently has compilation errors, primarily related to:
|
||||
|
||||
1. **`src/servers/mod.rs`**: Still showing an error for `map_err` not found for unit type `()`. This arises from the complex double `Result` handling (`Result<anyhow::Result<()>, JoinError>`) when awaiting spawned tasks.
|
||||
2. **`src/tls.rs`**: Facing issues with `rustls::pki_types::PrivateKeyDer` and `CertificateDer` conversions, specifically for ensuring `'static` lifetimes and incorrect method usages like `to_vec()` or `as_ref()`, or `into_owned()` methods not existing for certain types. The `borrowed data escapes outside of function` error indicates deeper lifetime mismatches.
|
||||
|
||||
### Next Steps (Requires Manual Intervention)
|
||||
|
||||
- **Refactor `src/servers/mod.rs` error handling**: The current `match handle.await` block needs to be carefully reviewed to ensure correct unwraping of the nested `Result` types and proper error propagation from `tokio::task::JoinError` to `anyhow::Error`.
|
||||
- **Re-evaluate `src/tls.rs` `rustls::pki_types` usage**: A deeper understanding of `rustls-pki-types` crate and its `CertificateDer` and `PrivateKeyDer` lifetimes and conversion methods is needed. The specific error message `no method named to_vec found for struct PrivatePkcs8KeyDer` is a key indicator of incorrect usage.
|
||||
- **Review `rustls` version and documentation**: It might be helpful to review the `rustls` and `tokio-rustls` documentation for version-specific changes and best practices regarding `pki_types` and asynchronous error handling.
|
||||
|
||||
This commit contains the work in progress as of the current session, including these unresolved errors, to allow for external review and debugging.
|
||||
@@ -57,7 +57,7 @@ For detailed configuration, check [this example](./config.yaml.example).
|
||||
|
||||
## Thanks
|
||||
|
||||
- [`l4p`](https://crates.io/crates/`l4p`), of which this is a heavily modified fork.
|
||||
- [`fourth`](https://crates.io/crates/fourth), of which this is a heavily modified fork.
|
||||
|
||||
## License
|
||||
|
||||
|
||||
@@ -35,6 +35,9 @@ pub struct ServerConfig {
|
||||
pub tls: Option<bool>,
|
||||
pub sni: Option<HashMap<String, String>>,
|
||||
pub default: Option<String>,
|
||||
pub termination_certs: Option<TlsTerminationConfig>,
|
||||
pub dynamic_backend_pattern: Option<String>,
|
||||
pub fixed_backend_ipv6: Option<String>,
|
||||
}
|
||||
impl TryInto<ProxyToUpstream> for &str {
|
||||
type Error = ConfigError;
|
||||
@@ -201,6 +204,25 @@ fn verify_config(config: ParsedConfigV1) -> Result<ParsedConfigV1, ConfigError>
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize, Clone)]
|
||||
pub struct TlsTerminationConfig {
|
||||
pub default_certificate: CertificateConfig,
|
||||
pub sni_certificates: Option<Vec<SniCertificateConfig>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize, Clone)]
|
||||
pub struct CertificateConfig {
|
||||
pub certificate_path: String,
|
||||
pub private_key_path: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize, Clone)]
|
||||
pub struct SniCertificateConfig {
|
||||
pub hostname: String,
|
||||
pub certificate_path: String,
|
||||
pub private_key_path: String,
|
||||
}
|
||||
|
||||
impl From<IOError> for ConfigError {
|
||||
fn from(err: IOError) -> ConfigError {
|
||||
ConfigError::IO(err)
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
mod config_v1;
|
||||
pub mod config_v1;
|
||||
pub(crate) use config_v1::ConfigV1;
|
||||
pub(crate) use config_v1::ParsedConfigV1;
|
||||
|
||||
41
src/main.rs
41
src/main.rs
@@ -1,14 +1,53 @@
|
||||
mod config;
|
||||
mod servers;
|
||||
mod update;
|
||||
mod upstreams;
|
||||
mod tls; // NEW: Declare the new TLS module
|
||||
|
||||
use crate::config::ConfigV1;
|
||||
use crate::servers::Server;
|
||||
use std::io::{stderr, stdout, Write};
|
||||
|
||||
use log::{debug, error, info};
|
||||
use log::{debug, error};
|
||||
use pico_args::Arguments;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
let mut args = Arguments::from_env();
|
||||
|
||||
match args.subcommand().expect("Unexpected error").as_deref() {
|
||||
Some("serve") => serve(),
|
||||
Some("update") => update::update(),
|
||||
Some("help") => {
|
||||
let _ = print_usage(&mut stdout().lock());
|
||||
}
|
||||
Some(cmd) => {
|
||||
eprintln!("Invalid command: {cmd}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
None => {
|
||||
eprintln!("Calling l4p without argument is deprecated now. Please use: l4p serve");
|
||||
let _ = print_usage(&mut stderr().lock());
|
||||
serve();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn print_usage(out: &mut dyn Write) -> std::io::Result<()> {
|
||||
writeln!(
|
||||
out,
|
||||
"{} v{}",
|
||||
env!("CARGO_PKG_NAME"),
|
||||
env!("CARGO_PKG_VERSION")
|
||||
)?;
|
||||
writeln!(out, "Usage:")?;
|
||||
writeln!(out, "\tupdate\tUpdate l4p to the latest version")?;
|
||||
writeln!(out, "\tserve\tServe the proxy")?;
|
||||
writeln!(out, "\thelp\tPrint this message")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serve() {
|
||||
let config_path = match find_config() {
|
||||
Ok(p) => p,
|
||||
Err(paths) => {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
use log::{error, info};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::net::SocketAddr;
|
||||
@@ -8,10 +9,15 @@ use tokio::task::JoinHandle;
|
||||
mod protocol;
|
||||
pub(crate) mod upstream_address;
|
||||
|
||||
use crate::config::ParsedConfigV1;
|
||||
use crate::config::{ParsedConfigV1, config_v1::TlsTerminationConfig};
|
||||
// use crate::tls;
|
||||
use crate::upstreams::Upstream;
|
||||
use protocol::tcp;
|
||||
|
||||
// A helper to convert Box<dyn Error> to anyhow::Error
|
||||
fn unhandled_error_for_box_error(e: Box<dyn std::error::Error>) -> anyhow::Error {
|
||||
anyhow!("{}", e)
|
||||
}
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Server {
|
||||
pub proxies: Vec<Arc<Proxy>>,
|
||||
@@ -26,6 +32,9 @@ pub(crate) struct Proxy {
|
||||
pub sni: Option<HashMap<String, String>>,
|
||||
pub default_action: String,
|
||||
pub upstream: HashMap<String, Upstream>,
|
||||
pub termination_certs: Option<TlsTerminationConfig>,
|
||||
pub dynamic_backend_pattern: Option<String>,
|
||||
pub fixed_backend_ipv6: Option<String>,
|
||||
}
|
||||
|
||||
impl Server {
|
||||
@@ -64,6 +73,9 @@ impl Server {
|
||||
sni: sni.clone(),
|
||||
default_action: default.clone(),
|
||||
upstream: upstream.clone(),
|
||||
termination_certs: proxy.termination_certs.clone(),
|
||||
dynamic_backend_pattern: proxy.dynamic_backend_pattern.clone(),
|
||||
fixed_backend_ipv6: proxy.fixed_backend_ipv6.clone(),
|
||||
};
|
||||
new_server.proxies.push(Arc::new(proxy));
|
||||
}
|
||||
@@ -73,7 +85,7 @@ impl Server {
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn run(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
pub async fn run(&mut self) -> Result<()> {
|
||||
let proxies = self.proxies.clone();
|
||||
let mut handles: Vec<JoinHandle<()>> = Vec::new();
|
||||
|
||||
@@ -83,15 +95,24 @@ impl Server {
|
||||
config.protocol, config.name, config.listen
|
||||
);
|
||||
let handle = tokio::spawn(async move {
|
||||
match config.protocol.as_ref() {
|
||||
"tcp" | "tcp4" | "tcp6" => {
|
||||
let res = tcp::proxy(config.clone()).await;
|
||||
if res.is_err() {
|
||||
error!("Failed to start {}: {}", config.name, res.err().unwrap());
|
||||
}
|
||||
if config.tls && config.termination_certs.is_some() {
|
||||
// New TLS termination handling
|
||||
let res = tcp::tls_proxy(config.clone()).await;
|
||||
if res.is_err() {
|
||||
error!("Failed to start TLS server {}: {}", config.name, res.err().unwrap());
|
||||
}
|
||||
_ => {
|
||||
error!("Invalid protocol: {}", config.protocol)
|
||||
} else {
|
||||
// Existing plain TCP handling
|
||||
match config.protocol.as_ref() {
|
||||
"tcp" | "tcp4" | "tcp6" => {
|
||||
let res = tcp::proxy(config.clone()).await;
|
||||
if res.is_err() {
|
||||
error!("Failed to start {}: {}", config.name, res.err().unwrap());
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
error!("Invalid protocol: {}", config.protocol)
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -99,8 +120,7 @@ impl Server {
|
||||
}
|
||||
|
||||
for handle in handles {
|
||||
handle.await?;
|
||||
}
|
||||
handle.await.map_err(anyhow::Error::from)?.map_err(anyhow::Error::from)?; }
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
pub mod tcp;
|
||||
pub mod tls;
|
||||
|
||||
@@ -1,11 +1,48 @@
|
||||
use crate::servers::protocol::tls::get_sni;
|
||||
use anyhow::Result;
|
||||
use crate::servers::Proxy;
|
||||
use log::{debug, error, info, warn};
|
||||
use std::error::Error;
|
||||
use std::sync::Arc;
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tokio_rustls::TlsAcceptor;
|
||||
use crate::tls;
|
||||
|
||||
pub(crate) async fn proxy(config: Arc<Proxy>) -> Result<(), Box<dyn Error>> {
|
||||
pub(crate) async fn tls_proxy(config: Arc<Proxy>) -> Result<()> {
|
||||
let listener = TcpListener::bind(config.listen).await?;
|
||||
let config = config.clone();
|
||||
|
||||
let acceptor = tls::build_tls_acceptor(
|
||||
config.termination_certs.as_ref().expect("TLS termination config missing"),
|
||||
)?;
|
||||
|
||||
loop {
|
||||
let _config = config.clone();
|
||||
let thread_acceptor = acceptor.clone();
|
||||
match listener.accept().await {
|
||||
Err(err) => {
|
||||
error!("Failed to accept TLS connection: {}", err);
|
||||
}
|
||||
Ok((stream, _)) => {
|
||||
tokio::spawn(async move {
|
||||
let res = match thread_acceptor.accept(stream).await {
|
||||
Ok(tls_stream) => {
|
||||
info!("TLS handshake successful with {:?}", tls_stream.into_inner().0.peer_addr().ok().map(|s| s.to_string()).unwrap_or_else(|| "unknown".to_string()));
|
||||
Ok(()) // Return Ok(()) for now
|
||||
}
|
||||
Err(err) => {
|
||||
error!("TLS handshake failed: {}", err);
|
||||
Err(anyhow::anyhow!("{}", err))
|
||||
}
|
||||
};
|
||||
if res.is_err() {
|
||||
error!("TLS handling error: {}", res.unwrap_err());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn proxy(config: Arc<Proxy>) -> Result<()> {
|
||||
let listener = TcpListener::bind(config.listen).await?;
|
||||
let config = config.clone();
|
||||
|
||||
@@ -14,7 +51,7 @@ pub(crate) async fn proxy(config: Arc<Proxy>) -> Result<(), Box<dyn Error>> {
|
||||
match listener.accept().await {
|
||||
Err(err) => {
|
||||
error!("Failed to accept connection: {}", err);
|
||||
return Err(Box::new(err));
|
||||
return Err(anyhow::Error::new(err)); // Convert to anyhow::Error
|
||||
}
|
||||
Ok((stream, _)) => {
|
||||
tokio::spawn(async move {
|
||||
@@ -30,35 +67,10 @@ pub(crate) async fn proxy(config: Arc<Proxy>) -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
}
|
||||
|
||||
async fn accept(inbound: TcpStream, proxy: Arc<Proxy>) -> Result<(), Box<dyn Error>> {
|
||||
async fn accept(inbound: TcpStream, proxy: Arc<Proxy>) -> Result<()> {
|
||||
info!("New connection from {:?}", inbound.peer_addr()?);
|
||||
|
||||
let upstream_name = match proxy.tls {
|
||||
false => proxy.default_action.clone(),
|
||||
true => {
|
||||
let mut hello_buf = [0u8; 1024];
|
||||
inbound.peek(&mut hello_buf).await?;
|
||||
let snis = get_sni(&hello_buf);
|
||||
if snis.is_empty() {
|
||||
proxy.default_action.clone()
|
||||
} else {
|
||||
match proxy.sni.clone() {
|
||||
Some(sni_map) => {
|
||||
let mut upstream = proxy.default_action.clone();
|
||||
for sni in snis {
|
||||
let m = sni_map.get(&sni);
|
||||
if m.is_some() {
|
||||
upstream = m.unwrap().clone();
|
||||
break;
|
||||
}
|
||||
}
|
||||
upstream
|
||||
}
|
||||
None => proxy.default_action.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
let upstream_name = proxy.default_action.clone();
|
||||
|
||||
debug!("Upstream: {}", upstream_name);
|
||||
|
||||
@@ -69,9 +81,9 @@ async fn accept(inbound: TcpStream, proxy: Arc<Proxy>) -> Result<(), Box<dyn Err
|
||||
"No upstream named {:?} on server {:?}",
|
||||
proxy.default_action, proxy.name
|
||||
);
|
||||
proxy.upstream.get(&proxy.default_action).unwrap()
|
||||
proxy.upstream.get(&proxy.default_action).expect("Default upstream must exist")
|
||||
}
|
||||
};
|
||||
|
||||
upstream.process(inbound).await
|
||||
upstream.process(inbound).await.map_err(|e| anyhow::anyhow!("{}", e))
|
||||
}
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
use log::{debug, warn};
|
||||
use crate::servers::Proxy;
|
||||
use log::{debug, error, trace, warn};
|
||||
use std::error::Error;
|
||||
use std::io; // Import io for ErrorKind
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration; // For potential delays
|
||||
use tls_parser::{
|
||||
parse_tls_extensions, parse_tls_raw_record, parse_tls_record_with_header, TlsMessage,
|
||||
TlsMessageHandshake,
|
||||
};
|
||||
use tokio::net::TcpStream;
|
||||
use tokio::time::timeout; // Use timeout for peek operations
|
||||
|
||||
pub fn get_sni(buf: &[u8]) -> Vec<String> {
|
||||
fn get_sni(buf: &[u8]) -> Vec<String> {
|
||||
let mut snis: Vec<String> = Vec::new();
|
||||
match parse_tls_raw_record(buf) {
|
||||
Ok((_, ref r)) => match parse_tls_record_with_header(r.data, &r.hdr) {
|
||||
@@ -53,10 +60,257 @@ pub fn get_sni(buf: &[u8]) -> Vec<String> {
|
||||
snis
|
||||
}
|
||||
|
||||
// Timeout duration for waiting for TLS Hello data
|
||||
const TLS_PEEK_TIMEOUT: Duration = Duration::from_secs(5); // Adjust as needed
|
||||
|
||||
pub(crate) async fn determine_upstream_name(
|
||||
inbound: &TcpStream,
|
||||
proxy: &Arc<Proxy>,
|
||||
) -> Result<String, Box<dyn Error>> {
|
||||
let default_upstream = proxy.default_action.clone();
|
||||
|
||||
let mut header = [0u8; 9];
|
||||
|
||||
// --- Step 1: Peek the initial header (9 bytes) with timeout ---
|
||||
match timeout(TLS_PEEK_TIMEOUT, async {
|
||||
loop {
|
||||
match inbound.peek(&mut header).await {
|
||||
Ok(n) if n >= header.len() => return Ok::<usize, io::Error>(n), // Got enough bytes
|
||||
Ok(0) => {
|
||||
// Connection closed cleanly before sending enough data
|
||||
trace!("Connection closed while peeking for TLS header");
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::UnexpectedEof,
|
||||
"Connection closed while peeking for TLS header",
|
||||
)
|
||||
.into()); // Convert to Box<dyn Error>
|
||||
}
|
||||
Ok(_) => {
|
||||
// Not enough bytes yet, yield and loop again
|
||||
tokio::task::yield_now().await;
|
||||
}
|
||||
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
|
||||
// Should not happen with await, but yield defensively
|
||||
tokio::task::yield_now().await;
|
||||
}
|
||||
Err(e) => {
|
||||
// Other I/O error
|
||||
warn!("Error peeking for TLS header: {}", e);
|
||||
return Err(e.into()); // Convert to Box<dyn Error>
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(Ok(_)) => { /* Header peeked successfully */ }
|
||||
Ok(Err(e)) => {
|
||||
// Inner loop returned an error (e.g., EOF, IO error)
|
||||
trace!("Failed to peek header (inner error): {}", e);
|
||||
return Ok(default_upstream); // Fallback on error/EOF
|
||||
}
|
||||
Err(_) => {
|
||||
// Timeout occurred
|
||||
error!("Timeout waiting for TLS header");
|
||||
return Ok(default_upstream); // Fallback on timeout
|
||||
}
|
||||
}
|
||||
|
||||
// --- Step 2: Calculate required size ---
|
||||
let required_bytes = match client_hello_buffer_size(&header) {
|
||||
Ok(size) => size,
|
||||
Err(e) => {
|
||||
// Header was invalid or not a ClientHello
|
||||
trace!("Could not determine required buffer size: {}", e);
|
||||
return Ok(default_upstream);
|
||||
}
|
||||
};
|
||||
|
||||
// Basic sanity check on size
|
||||
if required_bytes > 16384 + 9 {
|
||||
// TLS max record size + header approx
|
||||
error!(
|
||||
"Calculated required TLS buffer size is too large: {}",
|
||||
required_bytes
|
||||
);
|
||||
return Ok(default_upstream);
|
||||
}
|
||||
|
||||
// --- Step 3: Peek the full ClientHello with timeout ---
|
||||
let mut hello_buf = vec![0; required_bytes];
|
||||
match timeout(TLS_PEEK_TIMEOUT, async {
|
||||
let mut total_peeked = 0;
|
||||
loop {
|
||||
// Peek into the portion of the buffer that hasn't been filled yet.
|
||||
match inbound.peek(&mut hello_buf[total_peeked..]).await {
|
||||
Ok(0) => {
|
||||
// Connection closed cleanly before sending full ClientHello
|
||||
trace!(
|
||||
"Connection closed while peeking for full ClientHello (peeked {}/{} bytes)",
|
||||
total_peeked,
|
||||
required_bytes
|
||||
);
|
||||
return Err::<usize, io::Error>(
|
||||
io::Error::new(
|
||||
io::ErrorKind::UnexpectedEof,
|
||||
"Connection closed while peeking for full ClientHello",
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
Ok(n) => {
|
||||
total_peeked += n;
|
||||
if total_peeked >= required_bytes {
|
||||
trace!("Successfully peeked {} bytes for ClientHello", total_peeked);
|
||||
return Ok(total_peeked); // Got enough
|
||||
} else {
|
||||
// Not enough bytes yet, yield and loop again
|
||||
trace!(
|
||||
"Peeked {}/{} bytes for ClientHello, waiting for more...",
|
||||
total_peeked,
|
||||
required_bytes
|
||||
);
|
||||
tokio::task::yield_now().await;
|
||||
}
|
||||
}
|
||||
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
|
||||
tokio::task::yield_now().await;
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Error peeking for full ClientHello: {}", e);
|
||||
return Err(e.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(Ok(_)) => { /* Full hello peeked successfully */ }
|
||||
Ok(Err(e)) => {
|
||||
error!("Could not peek full ClientHello (inner error): {}", e);
|
||||
return Ok(default_upstream); // Fallback on error/EOF
|
||||
}
|
||||
Err(_) => {
|
||||
error!(
|
||||
"Timeout waiting for full ClientHello (needed {} bytes)",
|
||||
required_bytes
|
||||
);
|
||||
return Ok(default_upstream); // Fallback on timeout
|
||||
}
|
||||
}
|
||||
|
||||
// --- Step 4: Parse SNI ---
|
||||
let snis = get_sni(&hello_buf);
|
||||
|
||||
// --- Step 5: Determine upstream based on SNI ---
|
||||
if snis.is_empty() {
|
||||
debug!("No SNI found in ClientHello, using default upstream.");
|
||||
return Ok(default_upstream);
|
||||
} else {
|
||||
match proxy.sni.clone() {
|
||||
Some(sni_map) => {
|
||||
let mut upstream = default_upstream.clone(); // Clone here for default case
|
||||
let mut found_match = false;
|
||||
for sni in snis {
|
||||
// snis is already Vec<String>
|
||||
if let Some(target_upstream) = sni_map.get(&sni) {
|
||||
debug!(
|
||||
"Found matching SNI '{}', routing to upstream: {}",
|
||||
sni, target_upstream
|
||||
);
|
||||
upstream = target_upstream.clone();
|
||||
found_match = true;
|
||||
break;
|
||||
} else {
|
||||
trace!("SNI '{}' not found in map.", sni);
|
||||
}
|
||||
}
|
||||
if !found_match {
|
||||
debug!("SNI(s) found but none matched configuration, using default upstream.");
|
||||
}
|
||||
Ok(upstream)
|
||||
}
|
||||
None => {
|
||||
debug!("SNI found but no SNI map configured, using default upstream.");
|
||||
Ok(default_upstream)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn client_hello_buffer_size(data: &[u8]) -> Result<usize, String> {
|
||||
// TLS record header
|
||||
// -----------------
|
||||
// byte 0: rec type (should be 0x16 == Handshake)
|
||||
// byte 1-2: version (should be 0x3000 < v < 0x3003)
|
||||
// byte 3-4: rec len
|
||||
|
||||
if data.len() < 9 {
|
||||
trace!("Not enough bytes to even check the TLS header.");
|
||||
return Err("Not enough bytes to even check the TLS header.".into());
|
||||
}
|
||||
|
||||
if data[0] != 0x16 {
|
||||
trace!("Not a TLS handshake.");
|
||||
return Err("Not a TLS handshake.".into());
|
||||
}
|
||||
|
||||
// Check the record length
|
||||
let record_length = ((data[3] as u16) << 8) | (data[4] as u16);
|
||||
if record_length == 0 || record_length > 16384 {
|
||||
trace!("Client send invalid header: way too long record header.");
|
||||
return Err("Client send invalid header: way too long record header.".into());
|
||||
}
|
||||
|
||||
// Handshake record header
|
||||
// -----------------------
|
||||
// byte 5: hs msg type (should be 0x01 == client_hello)
|
||||
// byte 6-8: hs msg len
|
||||
|
||||
if data[5] != 0x01 {
|
||||
trace!("Not a ClientHello message");
|
||||
return Err("Not a ClientHello message".into());
|
||||
}
|
||||
|
||||
// Check the handshake message length
|
||||
let handshake_length =
|
||||
((data[6] as usize) << 16) | ((data[7] as usize) << 8) | (data[8] as usize);
|
||||
|
||||
if handshake_length <= 0 || handshake_length > (record_length - 4).into() {
|
||||
warn!("Invalid client hello length (fragmentation not implemented)");
|
||||
return Err("Invalid client hello length (fragmentation not implemented)".into());
|
||||
}
|
||||
|
||||
// Calculate the handshake length and return it
|
||||
Ok(handshake_length + 9)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_too_little_data_end() {
|
||||
let length = client_hello_buffer_size(&TOO_LITTLE_DATA_END);
|
||||
|
||||
if length.is_ok() {
|
||||
assert!(false);
|
||||
}
|
||||
|
||||
let msg = length.unwrap_err();
|
||||
dbg!(msg);
|
||||
|
||||
assert!(true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_too_little_data_start() {
|
||||
let length = client_hello_buffer_size(&TOO_LITTLE_DATA_START);
|
||||
|
||||
assert!(length.is_ok());
|
||||
assert_eq!(1712, length.unwrap())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sni_extract() {
|
||||
const BUF: [u8; 517] = [
|
||||
@@ -99,6 +353,454 @@ mod tests {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
];
|
||||
let sni = get_sni(&BUF);
|
||||
assert!(sni[0] == *"www.lirui.tech");
|
||||
assert_eq!(sni[0], *"www.lirui.tech");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sni_extract_tiny() {
|
||||
const BUF: [u8; 1712] = [
|
||||
0x16, 0x03, 0x01, 0x06, 0xab, 0x01, 0x00, 0x06, // |........|
|
||||
0xa7, 0x03, 0x03, 0x84, 0x53, 0xb2, 0xd7, 0x37, // |....S..7|
|
||||
0xcd, 0x27, 0xda, 0xf4, 0x70, 0xd8, 0x78, 0x26, // |.'..p.x&|
|
||||
0x34, 0x7f, 0xe3, 0xa7, 0x5d, 0xfe, 0x97, 0x29, // |4...]..)|
|
||||
0x89, 0x29, 0xa2, 0xd8, 0x62, 0x05, 0x7b, 0x13, // |.)..b.{.|
|
||||
0xcf, 0x4b, 0x13, 0x20, 0x5b, 0x74, 0x4e, 0x23, // |.K. [tN#|
|
||||
0x90, 0x08, 0x5a, 0x43, 0xbf, 0xe0, 0x0d, 0xeb, // |..ZC....|
|
||||
0x8a, 0xc8, 0x4d, 0x14, 0x1e, 0x35, 0x43, 0x04, // |..M..5C.|
|
||||
0x36, 0x32, 0xdc, 0x71, 0xff, 0xcc, 0xb3, 0x5b, // |62.q...[|
|
||||
0x63, 0x4b, 0x2b, 0xee, 0x00, 0x20, 0xba, 0xba, // |cK+.. ..|
|
||||
0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2b, // |.......+|
|
||||
0xc0, 0x2f, 0xc0, 0x2c, 0xc0, 0x30, 0xcc, 0xa9, // |./.,.0..|
|
||||
0xcc, 0xa8, 0xc0, 0x13, 0xc0, 0x14, 0x00, 0x9c, // |........|
|
||||
0x00, 0x9d, 0x00, 0x2f, 0x00, 0x35, 0x01, 0x00, // |.../.5..|
|
||||
0x06, 0x3e, 0x7a, 0x7a, 0x00, 0x00, 0x00, 0x0a, // |.>zz....|
|
||||
0x00, 0x0c, 0x00, 0x0a, 0xda, 0xda, 0x11, 0xec, // |........|
|
||||
0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, 0xff, 0x01, // |........|
|
||||
0x00, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, // |........|
|
||||
0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, // |........|
|
||||
0x68, 0x61, 0x2e, 0x68, 0x6f, 0x6d, 0x65, 0x2e, // |ha.home.|
|
||||
0x6b, 0x69, 0x65, 0x2e, 0x72, 0x73, 0xfe, 0x0d, // |kie.rs..|
|
||||
0x00, 0xba, 0x00, 0x00, 0x01, 0x00, 0x01, 0x11, // |........|
|
||||
0x00, 0x20, 0xf7, 0xf4, 0x20, 0xc8, 0xb7, 0xeb, // |. .. ...|
|
||||
0xf1, 0x2d, 0x8b, 0x30, 0x2c, 0xc8, 0x5e, 0xd3, // |.-.0,.^.|
|
||||
0xa3, 0x02, 0x38, 0xf2, 0x41, 0xf7, 0x3f, 0x2d, // |..8.A.?-|
|
||||
0xb4, 0xf0, 0xd7, 0x3b, 0xe5, 0x19, 0x3f, 0xc3, // |...;..?.|
|
||||
0xae, 0x1f, 0x00, 0x90, 0x27, 0x8d, 0x4c, 0xc9, // |....'.L.|
|
||||
0xb3, 0xd1, 0x63, 0x20, 0xe4, 0x33, 0x18, 0x56, // |..c .3.V|
|
||||
0xd5, 0x9b, 0xd5, 0xf9, 0xf2, 0x94, 0x1d, 0xe4, // |........|
|
||||
0xa6, 0x88, 0x47, 0xd2, 0x85, 0x4f, 0xf4, 0x30, // |..G..O.0|
|
||||
0x22, 0xff, 0x67, 0x80, 0x60, 0x33, 0x17, 0xa0, // |".g.`3..|
|
||||
0x4f, 0xdb, 0x98, 0x53, 0x00, 0xa4, 0xc8, 0x89, // |O..S....|
|
||||
0xb8, 0x1b, 0x3f, 0xbd, 0xdf, 0xeb, 0x48, 0x1a, // |..?...H.|
|
||||
0xa1, 0x33, 0xd7, 0xc1, 0x8d, 0x76, 0xf2, 0xcf, // |.3...v..|
|
||||
0xbe, 0x30, 0x1d, 0xcd, 0x3a, 0xfe, 0xf1, 0xb0, // |.0..:...|
|
||||
0x86, 0xbc, 0x28, 0x74, 0x78, 0xa1, 0x9a, 0x60, // |..(tx..`|
|
||||
0x14, 0xfe, 0x12, 0x92, 0x4d, 0xb5, 0x9e, 0x85, // |....M...|
|
||||
0x79, 0x62, 0x9c, 0x68, 0x73, 0xc6, 0x0e, 0xe5, // |yb.hs...|
|
||||
0xad, 0x5b, 0xe2, 0x69, 0x00, 0xc0, 0x26, 0x24, // |.[.i..&$|
|
||||
0x88, 0xfa, 0x22, 0x29, 0x36, 0x7b, 0x16, 0x59, // |..")6{.Y|
|
||||
0x48, 0xbe, 0xf9, 0x1c, 0x86, 0x55, 0xcb, 0x67, // |H....U.g|
|
||||
0xae, 0xb6, 0x7b, 0x69, 0x3e, 0xd0, 0x48, 0x31, // |..{i>.H1|
|
||||
0x58, 0x8a, 0xd8, 0xba, 0x06, 0x21, 0xf0, 0xd4, // |X....!..|
|
||||
0x4e, 0xef, 0xcf, 0x67, 0xc5, 0x63, 0x97, 0x59, // |N..g.c.Y|
|
||||
0x95, 0x12, 0x47, 0x90, 0x00, 0x2d, 0x00, 0x02, // |..G..-..|
|
||||
0x01, 0x01, 0x00, 0x10, 0x00, 0x0b, 0x00, 0x09, // |........|
|
||||
0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, // |.http/1.|
|
||||
0x31, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, // |1.......|
|
||||
0x03, 0x08, 0x04, 0x04, 0x01, 0x05, 0x03, 0x08, // |........|
|
||||
0x05, 0x05, 0x01, 0x08, 0x06, 0x06, 0x01, 0x00, // |........|
|
||||
0x2b, 0x00, 0x07, 0x06, 0x0a, 0x0a, 0x03, 0x04, // |+.......|
|
||||
0x03, 0x03, 0x00, 0x23, 0x00, 0x00, 0x00, 0x12, // |...#....|
|
||||
0x00, 0x00, 0x00, 0x1b, 0x00, 0x03, 0x02, 0x00, // |........|
|
||||
0x02, 0x00, 0x33, 0x04, 0xef, 0x04, 0xed, 0xda, // |..3.....|
|
||||
0xda, 0x00, 0x01, 0x00, 0x11, 0xec, 0x04, 0xc0, // |........|
|
||||
0xc6, 0x12, 0x85, 0x0b, 0xba, 0x73, 0x9d, 0x00, // |.....s..|
|
||||
0x29, 0x08, 0x40, 0x3a, 0xb8, 0xfc, 0x9e, 0x99, // |).@:....|
|
||||
0x25, 0xbd, 0x60, 0xb6, 0x8a, 0x56, 0x51, 0xac, // |%.`..VQ.|
|
||||
0x38, 0xa3, 0x15, 0x37, 0x21, 0x80, 0x86, 0x02, // |8..7!...|
|
||||
0xb2, 0x10, 0x4b, 0x29, 0xeb, 0x37, 0x04, 0x47, // |..K).7.G|
|
||||
0x16, 0x12, 0x0e, 0x63, 0x2d, 0x32, 0xf6, 0x2a, // |...c-2.*|
|
||||
0x86, 0x09, 0x7b, 0x41, 0x28, 0x8c, 0xcf, 0xfa, // |..{A(...|
|
||||
0x08, 0x2e, 0x0c, 0xb2, 0x55, 0xb4, 0xb4, 0xd2, // |....U...|
|
||||
0x76, 0x38, 0x47, 0x44, 0x78, 0xf0, 0x01, 0xb6, // |v8GDx...|
|
||||
0xee, 0xf0, 0x1f, 0x4b, 0xc5, 0x6b, 0xb3, 0x93, // |...K.k..|
|
||||
0x4d, 0xa5, 0x25, 0x29, 0xda, 0x33, 0x1e, 0xc5, // |M.%).3..|
|
||||
0x15, 0x98, 0xf5, 0x41, 0x3e, 0xd2, 0xf7, 0x82, // |...A>...|
|
||||
0xd7, 0xbb, 0x56, 0xf0, 0x86, 0x29, 0xa3, 0x56, // |..V..).V|
|
||||
0x25, 0xdc, 0xaa, 0x03, 0xaa, 0x28, 0xa7, 0x2b, // |%....(.+|
|
||||
0xc0, 0x41, 0xca, 0x66, 0x3e, 0xcc, 0x21, 0x40, // |.A.f>.!@|
|
||||
0x60, 0x34, 0x5f, 0x9f, 0x69, 0x37, 0xac, 0x30, // |`4_.i7.0|
|
||||
0x06, 0x7a, 0xf9, 0x26, 0xfe, 0x3c, 0x13, 0x05, // |.z.&.<..|
|
||||
0xf6, 0xbe, 0x5f, 0x0c, 0x9a, 0x43, 0x18, 0xa2, // |.._..C..|
|
||||
0xd9, 0xc5, 0xa0, 0x06, 0x0b, 0x0a, 0x21, 0xf1, // |......!.|
|
||||
0x6b, 0x12, 0x4a, 0x5d, 0xec, 0xf6, 0x01, 0x30, // |k.J]...0|
|
||||
0xb6, 0x3b, 0x34, 0x62, 0xcd, 0x5a, 0x6a, 0x26, // |.;4b.Zj&|
|
||||
0x08, 0x98, 0xc9, 0xd0, 0x8a, 0x49, 0x94, 0x07, // |.....I..|
|
||||
0x48, 0x45, 0x78, 0x45, 0xae, 0x24, 0x2b, 0x83, // |HExE.$+.|
|
||||
0xb6, 0x69, 0x6c, 0x20, 0x33, 0xa9, 0xc4, 0x8e, // |.il 3...|
|
||||
0xe7, 0x1a, 0x90, 0x28, 0xc6, 0x3f, 0x16, 0xf2, // |...(.?..|
|
||||
0xae, 0x3e, 0x22, 0x17, 0x26, 0x9c, 0x38, 0xf5, // |.>".&.8.|
|
||||
0x88, 0x60, 0x79, 0x16, 0x28, 0xce, 0x05, 0x72, // |.`y.(..r|
|
||||
0x2f, 0x64, 0x99, 0xdd, 0x8c, 0x5b, 0xa6, 0xe2, // |/d...[..|
|
||||
0x65, 0x8a, 0xe2, 0x8d, 0xb6, 0x24, 0x9e, 0x6d, // |e....$.m|
|
||||
0x5a, 0x70, 0xfd, 0xea, 0xca, 0xec, 0x77, 0x46, // |Zp....wF|
|
||||
0x20, 0xa8, 0x1f, 0x78, 0xf6, 0x34, 0x52, 0x13, // | ..x.4R.|
|
||||
0x97, 0xef, 0x60, 0xb9, 0xe5, 0xc6, 0x85, 0xf2, // |..`.....|
|
||||
0x84, 0x64, 0xdc, 0x08, 0x07, 0xe2, 0x63, 0xa6, // |.d....c.|
|
||||
0x23, 0x64, 0x54, 0xb8, 0x72, 0xac, 0x23, 0xda, // |#dT.r.#.|
|
||||
0x8f, 0x73, 0xe4, 0x9b, 0x80, 0x77, 0x66, 0x3f, // |.s...wf?|
|
||||
0x69, 0x34, 0xc4, 0xfb, 0x45, 0x3d, 0x1c, 0xa7, // |i4..E=..|
|
||||
0x86, 0x98, 0x2e, 0xb4, 0xe0, 0x84, 0xb6, 0x47, // |.......G|
|
||||
0x78, 0xeb, 0x2b, 0x10, 0x17, 0x45, 0x8a, 0xcf, // |x.+..E..|
|
||||
0xea, 0xb5, 0x58, 0x42, 0x93, 0xbe, 0x4b, 0xad, // |..XB..K.|
|
||||
0xfb, 0x28, 0x11, 0x12, 0xe0, 0x7c, 0x3d, 0x34, // |.(...|=4|
|
||||
0x8c, 0x82, 0x07, 0x84, 0xda, 0x8b, 0x35, 0x86, // |......5.|
|
||||
0x37, 0x35, 0x1d, 0x1a, 0xa2, 0xbf, 0x0a, 0xb4, // |75......|
|
||||
0x8e, 0xf0, 0x91, 0xc4, 0xa8, 0x3f, 0x38, 0x03, // |.....?8.|
|
||||
0x37, 0xc1, 0x9a, 0x94, 0x43, 0x09, 0x57, 0xee, // |7...C.W.|
|
||||
0xaa, 0xcb, 0x3d, 0x13, 0xa2, 0x33, 0xd1, 0x04, // |..=..3..|
|
||||
0x2c, 0x6c, 0xb4, 0x1c, 0x86, 0x07, 0x0c, 0x3c, // |,l.....<|
|
||||
0x5c, 0xc9, 0x8c, 0xc8, 0x1a, 0x85, 0xa6, 0xdd, // |\.......|
|
||||
0xd3, 0xc5, 0xae, 0x84, 0x4d, 0xfe, 0xa2, 0x99, // |....M...|
|
||||
0xd3, 0x0b, 0x1f, 0x43, 0x01, 0xa6, 0x7b, 0xb2, // |...C..{.|
|
||||
0x5b, 0xd5, 0xa0, 0x3e, 0xd4, 0x6c, 0x65, 0x75, // |[..>.leu|
|
||||
0x55, 0x28, 0x4d, 0x1c, 0x28, 0x86, 0xda, 0x94, // |U(M.(...|
|
||||
0xbe, 0x0a, 0x99, 0x61, 0xa4, 0x88, 0xd9, 0x6a, // |...a...j|
|
||||
0x20, 0x1d, 0x78, 0x45, 0x5f, 0x66, 0xcc, 0x8c, // | .xE_f..|
|
||||
0xe1, 0xba, 0x4c, 0x51, 0x99, 0x54, 0x27, 0x77, // |..LQ.T'w|
|
||||
0xb4, 0x84, 0x61, 0x4e, 0xf9, 0x90, 0x6f, 0x19, // |..aN..o.|
|
||||
0x44, 0x93, 0x27, 0x1d, 0x95, 0x82, 0x74, 0x7f, // |D.'...t.|
|
||||
0x35, 0xaf, 0x04, 0xe4, 0x58, 0x41, 0x3a, 0x51, // |5...XA:Q|
|
||||
0x0b, 0x22, 0x45, 0xaf, 0x44, 0x2a, 0xe9, 0xa3, // |."E.D*..|
|
||||
0x71, 0x65, 0x15, 0x22, 0xea, 0x40, 0x10, 0xaf, // |qe.".@..|
|
||||
0x5b, 0x27, 0xfc, 0x02, 0x00, 0x23, 0xa3, 0x70, // |['...#.p|
|
||||
0xa9, 0x6c, 0xa7, 0xf7, 0x29, 0x5c, 0x75, 0x9b, // |.l..)\u.|
|
||||
0x4c, 0x23, 0x14, 0x51, 0x12, 0x62, 0x71, 0xbb, // |L#.Q.bq.|
|
||||
0x75, 0x64, 0x65, 0xb3, 0xaa, 0x1e, 0x10, 0x14, // |ude.....|
|
||||
0xbf, 0xd0, 0x8b, 0xe0, 0xe4, 0x51, 0x6e, 0xa8, // |.....Qn.|
|
||||
0x1a, 0x95, 0x21, 0xa9, 0x9f, 0xf7, 0x2a, 0xac, // |..!...*.|
|
||||
0x5c, 0x1c, 0x12, 0xac, 0x9d, 0xac, 0x57, 0x14, // |\.....W.|
|
||||
0x27, 0xaa, 0xa7, 0xee, 0xc3, 0x9d, 0x63, 0x48, // |'.....cH|
|
||||
0x0e, 0xd7, 0xf8, 0x92, 0x9f, 0x28, 0xb9, 0x82, // |.....(..|
|
||||
0x71, 0x99, 0xa1, 0xcb, 0x69, 0x0c, 0x29, 0x7d, // |q...i.)}|
|
||||
0x67, 0x73, 0xae, 0x9d, 0xd7, 0xc7, 0x51, 0x7a, // |gs....Qz|
|
||||
0x2c, 0x3a, 0x74, 0x89, 0x7d, 0x76, 0x35, 0xb5, // |,:t.}v5.|
|
||||
0x97, 0x73, 0x4a, 0xfc, 0x29, 0x9a, 0x1a, 0x06, // |.sJ.)...|
|
||||
0x2f, 0xd0, 0x89, 0x32, 0xfc, 0x3b, 0x17, 0xec, // |/..2.;..|
|
||||
0x7a, 0xb5, 0x3c, 0x66, 0x0f, 0x43, 0x55, 0x41, // |z.<f.CUA|
|
||||
0x49, 0x3f, 0xbf, 0xa1, 0x6f, 0x8a, 0x05, 0x76, // |I?..o..v|
|
||||
0xd4, 0x02, 0x33, 0x52, 0x78, 0xc8, 0x08, 0xe9, // |..3Rx...|
|
||||
0x49, 0xb8, 0x42, 0x05, 0xed, 0x34, 0x0a, 0xb1, // |I.B..4..|
|
||||
0xa8, 0x32, 0x00, 0x6b, 0x00, 0x42, 0x56, 0x8a, // |.2.k.BV.|
|
||||
0xe9, 0x04, 0x7a, 0xac, 0xc8, 0x72, 0x7f, 0x40, // |..z..r.@|
|
||||
0x4c, 0xd6, 0xa9, 0x34, 0x0b, 0xc3, 0x63, 0x39, // |L..4..c9|
|
||||
0x21, 0xbf, 0x04, 0xb0, 0x2b, 0x81, 0xf9, 0x07, // |!...+...|
|
||||
0xe6, 0x15, 0x92, 0x89, 0x9b, 0x1e, 0xe6, 0x4b, // |.......K|
|
||||
0x5b, 0x0b, 0x33, 0x5f, 0x89, 0x96, 0xa2, 0x74, // |[.3_...t|
|
||||
0x41, 0x6b, 0x15, 0xe8, 0x8a, 0x62, 0xf5, 0x1c, // |Ak...b..|
|
||||
0x37, 0x38, 0x62, 0x77, 0xd4, 0x57, 0x7b, 0x43, // |78bw.W{C|
|
||||
0x42, 0x4f, 0x01, 0x9c, 0xf2, 0xe0, 0x68, 0xb7, // |BO....h.|
|
||||
0xf1, 0x66, 0x93, 0xd8, 0x8e, 0x78, 0x80, 0x24, // |.f...x.$|
|
||||
0x4c, 0x61, 0x11, 0xbb, 0xf2, 0x79, 0xf7, 0x96, // |La...y..|
|
||||
0x02, 0x80, 0xaa, 0xc7, 0xcd, 0xbb, 0x55, 0x03, // |......U.|
|
||||
0x22, 0x5e, 0xda, 0xa2, 0x44, 0x7d, 0x82, 0x41, // |"^..D}.A|
|
||||
0x86, 0x9b, 0x92, 0x0a, 0xd5, 0x7e, 0xf2, 0x78, // |.....~.x|
|
||||
0x84, 0x50, 0x00, 0x2d, 0x0b, 0xab, 0x92, 0x7a, // |.P.-...z|
|
||||
0x96, 0x15, 0xcf, 0x5a, 0x34, 0x45, 0x35, 0xa7, // |...Z4E5.|
|
||||
0x18, 0x61, 0x2b, 0x88, 0x45, 0xaa, 0xd3, 0xe2, // |.a+.E...|
|
||||
0x54, 0xf9, 0xc7, 0xbb, 0xe7, 0x00, 0x86, 0xbd, // |T.......|
|
||||
0x8b, 0xbb, 0x6d, 0x3b, 0x0f, 0x8d, 0xfb, 0x4d, // |..m;...M|
|
||||
0x5d, 0x8b, 0x50, 0x2e, 0x68, 0x74, 0x5d, 0x03, // |].P.ht].|
|
||||
0x16, 0x2a, 0x49, 0x24, 0x54, 0x5b, 0xa9, 0x34, // |.*I$T[.4|
|
||||
0x25, 0x17, 0x79, 0xe3, 0xc3, 0x3a, 0x2a, 0x12, // |%.y..:*.|
|
||||
0x75, 0x64, 0x16, 0xa4, 0xb7, 0x7e, 0x39, 0x5a, // |ud...~9Z|
|
||||
0x4e, 0x3e, 0x53, 0x2b, 0x49, 0x1b, 0x26, 0xdf, // |N>S+I.&.|
|
||||
0xfc, 0x29, 0x99, 0xcb, 0xad, 0x29, 0x2c, 0x72, // |.)...),r|
|
||||
0x3f, 0xa7, 0xcb, 0x45, 0x4c, 0x14, 0xee, 0x46, // |?..EL..F|
|
||||
0x74, 0x64, 0xdb, 0x4b, 0x4b, 0xa4, 0x35, 0x3c, // |td.KK.5<|
|
||||
0x91, 0xc4, 0x9b, 0xb0, 0x66, 0xc6, 0x70, 0xb6, // |....f.p.|
|
||||
0xf2, 0x07, 0x3b, 0xbf, 0x74, 0x72, 0xb4, 0x24, // |..;.tr.$|
|
||||
0x7e, 0x87, 0xd4, 0x0a, 0x37, 0xd9, 0x49, 0x04, // |~...7.I.|
|
||||
0x09, 0x36, 0xd1, 0x63, 0x88, 0xe1, 0xe8, 0x08, // |.6.c....|
|
||||
0xbf, 0x17, 0xc4, 0xcd, 0xcb, 0x3c, 0xef, 0x88, // |.....<..|
|
||||
0x2c, 0xf6, 0xa3, 0x6d, 0x89, 0x39, 0xc9, 0xfe, // |,..m.9..|
|
||||
0x97, 0x25, 0xb3, 0x9a, 0x02, 0x40, 0xd4, 0x90, // |.%...@..|
|
||||
0x28, 0x6a, 0x79, 0xbd, 0x4b, 0x8e, 0x10, 0x18, // |(jy.K...|
|
||||
0xc9, 0xaf, 0xe9, 0xc0, 0x6e, 0xd5, 0xb1, 0xcf, // |....n...|
|
||||
0xe8, 0xa4, 0xdc, 0x94, 0x12, 0x82, 0xfb, 0x08, // |........|
|
||||
0x42, 0xd4, 0x1a, 0x76, 0xa2, 0x4b, 0x3f, 0xc3, // |B..v.K?.|
|
||||
0xb4, 0x0b, 0xa3, 0x0c, 0xec, 0x19, 0x7c, 0x5f, // |......|_|
|
||||
0xd5, 0x98, 0x99, 0xf4, 0x1a, 0xca, 0x83, 0xaa, // |........|
|
||||
0xbd, 0x26, 0x31, 0x95, 0x77, 0x90, 0x43, 0x7a, // |.&1.w.Cz|
|
||||
0x75, 0x15, 0xcb, 0x68, 0xae, 0x24, 0xc5, 0x1b, // |u..h.$..|
|
||||
// Cutoff here.
|
||||
0x8c, 0x49, 0xbe, 0xfc, 0x61, 0x54, 0xd7, 0x18, // |.I..aT..|
|
||||
0x9d, 0x21, 0x10, 0x14, 0xe2, 0x6d, 0x5b, 0x4b, // |.!...m[K|
|
||||
0xb0, 0x94, 0xaa, 0x6e, 0xd5, 0x7b, 0xba, 0x6e, // |...n.{.n|
|
||||
0xe0, 0x03, 0xac, 0x9a, 0xbb, 0xe1, 0x17, 0x9b, // |........|
|
||||
0x18, 0x0c, 0x33, 0xcc, 0x05, 0x91, 0x1c, 0x43, // |..3....C|
|
||||
0x37, 0xd2, 0x10, 0xb7, 0xc6, 0xc7, 0x6b, 0xda, // |7.....k.|
|
||||
0x87, 0x9c, 0xaf, 0x93, 0x52, 0x2f, 0x4c, 0x6e, // |....R/Ln|
|
||||
0x14, 0xdb, 0x49, 0xbc, 0xeb, 0x96, 0xda, 0xb6, // |..I.....|
|
||||
0x3b, 0xf8, 0xc0, 0x33, 0xba, 0x15, 0x37, 0x39, // |;..3..79|
|
||||
0xe7, 0xae, 0xb6, 0x48, 0x3e, 0xd8, 0x57, 0x67, // |...H>.Wg|
|
||||
0x9c, 0xb6, 0x9c, 0xc0, 0x18, 0x0e, 0x74, 0x67, // |......tg|
|
||||
0xae, 0x8e, 0xc6, 0x80, 0x7f, 0x81, 0x25, 0xc4, // |......%.|
|
||||
0xe9, 0x04, 0xe8, 0xd9, 0x98, 0xb6, 0x99, 0x93, // |........|
|
||||
0xa1, 0xa4, 0x5e, 0x57, 0x74, 0x89, 0x30, 0x38, // |..^Wt.08|
|
||||
0xa9, 0xbb, 0x99, 0x4a, 0x7e, 0x42, 0x3c, 0xd2, // |...J~B<.|
|
||||
0x59, 0xb6, 0x49, 0xb0, 0xc7, 0x11, 0x57, 0x03, // |Y.I...W.|
|
||||
0x6d, 0x23, 0x1b, 0x72, 0xe7, 0x24, 0xdb, 0x75, // |m#.r.$.u|
|
||||
0x78, 0xd1, 0x38, 0x01, 0x46, 0xb6, 0x8c, 0x1b, // |x.8.F...|
|
||||
0x41, 0xb4, 0xbd, 0xc1, 0xa2, 0x00, 0x63, 0xa5, // |A.....c.|
|
||||
0x97, 0x30, 0x5d, 0xbe, 0xd1, 0x37, 0x31, 0xf1, // |.0]..71.|
|
||||
0xbb, 0xc6, 0xf8, 0x81, 0x35, 0x86, 0x32, 0xa6, // |....5.2.|
|
||||
0xc3, 0x35, 0x54, 0x45, 0x50, 0xdf, 0x61, 0x46, // |.5TEP.aF|
|
||||
0x5b, 0x83, 0x6b, 0xac, 0x5c, 0x2d, 0xa2, 0xc3, // |[.k.\-..|
|
||||
0x2e, 0x71, 0x32, 0x18, 0x41, 0x29, 0x99, 0x66, // |.q2.A).f|
|
||||
0x8c, 0x50, 0x28, 0x92, 0x45, 0xae, 0x96, 0x38, // |.P(.E..8|
|
||||
0xa4, 0x83, 0x94, 0x4a, 0x2f, 0x0e, 0x62, 0x13, // |...J/.b.|
|
||||
0x07, 0x13, 0xc2, 0x0b, 0x84, 0xfd, 0x27, 0xab, // |......'.|
|
||||
0x6c, 0xb4, 0x69, 0x0d, 0xd2, 0xdb, 0xfb, 0x8e, // |l.i.....|
|
||||
0xa7, 0x09, 0x65, 0x76, 0x7e, 0x09, 0xa4, 0x7a, // |..ev~..z|
|
||||
0xe9, 0xfe, 0xec, 0x52, 0x89, 0x7d, 0x07, 0x6f, // |...R.}.o|
|
||||
0xff, 0xa0, 0xde, 0x8a, 0x42, 0x2d, 0xc3, 0x75, // |....B-.u|
|
||||
0x05, 0x6d, 0x60, 0x76, 0xce, 0xe1, 0x6c, 0xfd, // |.m`v..l.|
|
||||
0xae, 0x1f, 0x5e, 0x02, 0x94, 0x39, 0x2a, 0x55, // |..^..9*U|
|
||||
0x00, 0x1d, 0x00, 0x20, 0x8d, 0x89, 0x9a, 0x19, // |... ....|
|
||||
0x1d, 0x53, 0x52, 0xd5, 0xc1, 0x3e, 0x3a, 0x1d, // |.SR..>:.|
|
||||
0x12, 0x15, 0xae, 0x33, 0x2e, 0x54, 0xd1, 0x6f, // |...3.T.o|
|
||||
0xd6, 0xb1, 0x73, 0xd9, 0x56, 0x98, 0x6f, 0x8f, // |..s.V.o.|
|
||||
0x7e, 0xf5, 0xd9, 0x75, 0x00, 0x0b, 0x00, 0x02, // |~..u....|
|
||||
0x01, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, // |........|
|
||||
0x00, 0x00, 0x00, 0x9a, 0x9a, 0x00, 0x01, 0x00, // |........|
|
||||
];
|
||||
let sni = get_sni(&BUF);
|
||||
assert_eq!(sni[0], *"ha.home.kie.rs");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_too_little_data() {
|
||||
let sni = get_sni(&TOO_LITTLE_DATA_END);
|
||||
assert_eq!(0, sni.len());
|
||||
}
|
||||
|
||||
const TOO_LITTLE_DATA_START: [u8; 1392] = [
|
||||
0x16, 0x03, 0x01, 0x06, 0xab, 0x01, 0x00, 0x06, // |........|
|
||||
0xa7, 0x03, 0x03, 0x84, 0x53, 0xb2, 0xd7, 0x37, // |....S..7|
|
||||
0xcd, 0x27, 0xda, 0xf4, 0x70, 0xd8, 0x78, 0x26, // |.'..p.x&|
|
||||
0x34, 0x7f, 0xe3, 0xa7, 0x5d, 0xfe, 0x97, 0x29, // |4...]..)|
|
||||
0x89, 0x29, 0xa2, 0xd8, 0x62, 0x05, 0x7b, 0x13, // |.)..b.{.|
|
||||
0xcf, 0x4b, 0x13, 0x20, 0x5b, 0x74, 0x4e, 0x23, // |.K. [tN#|
|
||||
0x90, 0x08, 0x5a, 0x43, 0xbf, 0xe0, 0x0d, 0xeb, // |..ZC....|
|
||||
0x8a, 0xc8, 0x4d, 0x14, 0x1e, 0x35, 0x43, 0x04, // |..M..5C.|
|
||||
0x36, 0x32, 0xdc, 0x71, 0xff, 0xcc, 0xb3, 0x5b, // |62.q...[|
|
||||
0x63, 0x4b, 0x2b, 0xee, 0x00, 0x20, 0xba, 0xba, // |cK+.. ..|
|
||||
0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2b, // |.......+|
|
||||
0xc0, 0x2f, 0xc0, 0x2c, 0xc0, 0x30, 0xcc, 0xa9, // |./.,.0..|
|
||||
0xcc, 0xa8, 0xc0, 0x13, 0xc0, 0x14, 0x00, 0x9c, // |........|
|
||||
0x00, 0x9d, 0x00, 0x2f, 0x00, 0x35, 0x01, 0x00, // |.../.5..|
|
||||
0x06, 0x3e, 0x7a, 0x7a, 0x00, 0x00, 0x00, 0x0a, // |.>zz....|
|
||||
0x00, 0x0c, 0x00, 0x0a, 0xda, 0xda, 0x11, 0xec, // |........|
|
||||
0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, 0xff, 0x01, // |........|
|
||||
0x00, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, // |........|
|
||||
0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, // |........|
|
||||
0x68, 0x61, 0x2e, 0x68, 0x6f, 0x6d, 0x65, 0x2e, // |ha.home.|
|
||||
0x6b, 0x69, 0x65, 0x2e, 0x72, 0x73, 0xfe, 0x0d, // |kie.rs..|
|
||||
0x00, 0xba, 0x00, 0x00, 0x01, 0x00, 0x01, 0x11, // |........|
|
||||
0x00, 0x20, 0xf7, 0xf4, 0x20, 0xc8, 0xb7, 0xeb, // |. .. ...|
|
||||
0xf1, 0x2d, 0x8b, 0x30, 0x2c, 0xc8, 0x5e, 0xd3, // |.-.0,.^.|
|
||||
0xa3, 0x02, 0x38, 0xf2, 0x41, 0xf7, 0x3f, 0x2d, // |..8.A.?-|
|
||||
0xb4, 0xf0, 0xd7, 0x3b, 0xe5, 0x19, 0x3f, 0xc3, // |...;..?.|
|
||||
0xae, 0x1f, 0x00, 0x90, 0x27, 0x8d, 0x4c, 0xc9, // |....'.L.|
|
||||
0xb3, 0xd1, 0x63, 0x20, 0xe4, 0x33, 0x18, 0x56, // |..c .3.V|
|
||||
0xd5, 0x9b, 0xd5, 0xf9, 0xf2, 0x94, 0x1d, 0xe4, // |........|
|
||||
0xa6, 0x88, 0x47, 0xd2, 0x85, 0x4f, 0xf4, 0x30, // |..G..O.0|
|
||||
0x22, 0xff, 0x67, 0x80, 0x60, 0x33, 0x17, 0xa0, // |".g.`3..|
|
||||
0x4f, 0xdb, 0x98, 0x53, 0x00, 0xa4, 0xc8, 0x89, // |O..S....|
|
||||
0xb8, 0x1b, 0x3f, 0xbd, 0xdf, 0xeb, 0x48, 0x1a, // |..?...H.|
|
||||
0xa1, 0x33, 0xd7, 0xc1, 0x8d, 0x76, 0xf2, 0xcf, // |.3...v..|
|
||||
0xbe, 0x30, 0x1d, 0xcd, 0x3a, 0xfe, 0xf1, 0xb0, // |.0..:...|
|
||||
0x86, 0xbc, 0x28, 0x74, 0x78, 0xa1, 0x9a, 0x60, // |..(tx..`|
|
||||
0x14, 0xfe, 0x12, 0x92, 0x4d, 0xb5, 0x9e, 0x85, // |....M...|
|
||||
0x79, 0x62, 0x9c, 0x68, 0x73, 0xc6, 0x0e, 0xe5, // |yb.hs...|
|
||||
0xad, 0x5b, 0xe2, 0x69, 0x00, 0xc0, 0x26, 0x24, // |.[.i..&$|
|
||||
0x88, 0xfa, 0x22, 0x29, 0x36, 0x7b, 0x16, 0x59, // |..")6{.Y|
|
||||
0x48, 0xbe, 0xf9, 0x1c, 0x86, 0x55, 0xcb, 0x67, // |H....U.g|
|
||||
0xae, 0xb6, 0x7b, 0x69, 0x3e, 0xd0, 0x48, 0x31, // |..{i>.H1|
|
||||
0x58, 0x8a, 0xd8, 0xba, 0x06, 0x21, 0xf0, 0xd4, // |X....!..|
|
||||
0x4e, 0xef, 0xcf, 0x67, 0xc5, 0x63, 0x97, 0x59, // |N..g.c.Y|
|
||||
0x95, 0x12, 0x47, 0x90, 0x00, 0x2d, 0x00, 0x02, // |..G..-..|
|
||||
0x01, 0x01, 0x00, 0x10, 0x00, 0x0b, 0x00, 0x09, // |........|
|
||||
0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, // |.http/1.|
|
||||
0x31, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, // |1.......|
|
||||
0x03, 0x08, 0x04, 0x04, 0x01, 0x05, 0x03, 0x08, // |........|
|
||||
0x05, 0x05, 0x01, 0x08, 0x06, 0x06, 0x01, 0x00, // |........|
|
||||
0x2b, 0x00, 0x07, 0x06, 0x0a, 0x0a, 0x03, 0x04, // |+.......|
|
||||
0x03, 0x03, 0x00, 0x23, 0x00, 0x00, 0x00, 0x12, // |...#....|
|
||||
0x00, 0x00, 0x00, 0x1b, 0x00, 0x03, 0x02, 0x00, // |........|
|
||||
0x02, 0x00, 0x33, 0x04, 0xef, 0x04, 0xed, 0xda, // |..3.....|
|
||||
0xda, 0x00, 0x01, 0x00, 0x11, 0xec, 0x04, 0xc0, // |........|
|
||||
0xc6, 0x12, 0x85, 0x0b, 0xba, 0x73, 0x9d, 0x00, // |.....s..|
|
||||
0x29, 0x08, 0x40, 0x3a, 0xb8, 0xfc, 0x9e, 0x99, // |).@:....|
|
||||
0x25, 0xbd, 0x60, 0xb6, 0x8a, 0x56, 0x51, 0xac, // |%.`..VQ.|
|
||||
0x38, 0xa3, 0x15, 0x37, 0x21, 0x80, 0x86, 0x02, // |8..7!...|
|
||||
0xb2, 0x10, 0x4b, 0x29, 0xeb, 0x37, 0x04, 0x47, // |..K).7.G|
|
||||
0x16, 0x12, 0x0e, 0x63, 0x2d, 0x32, 0xf6, 0x2a, // |...c-2.*|
|
||||
0x86, 0x09, 0x7b, 0x41, 0x28, 0x8c, 0xcf, 0xfa, // |..{A(...|
|
||||
0x08, 0x2e, 0x0c, 0xb2, 0x55, 0xb4, 0xb4, 0xd2, // |....U...|
|
||||
0x76, 0x38, 0x47, 0x44, 0x78, 0xf0, 0x01, 0xb6, // |v8GDx...|
|
||||
0xee, 0xf0, 0x1f, 0x4b, 0xc5, 0x6b, 0xb3, 0x93, // |...K.k..|
|
||||
0x4d, 0xa5, 0x25, 0x29, 0xda, 0x33, 0x1e, 0xc5, // |M.%).3..|
|
||||
0x15, 0x98, 0xf5, 0x41, 0x3e, 0xd2, 0xf7, 0x82, // |...A>...|
|
||||
0xd7, 0xbb, 0x56, 0xf0, 0x86, 0x29, 0xa3, 0x56, // |..V..).V|
|
||||
0x25, 0xdc, 0xaa, 0x03, 0xaa, 0x28, 0xa7, 0x2b, // |%....(.+|
|
||||
0xc0, 0x41, 0xca, 0x66, 0x3e, 0xcc, 0x21, 0x40, // |.A.f>.!@|
|
||||
0x60, 0x34, 0x5f, 0x9f, 0x69, 0x37, 0xac, 0x30, // |`4_.i7.0|
|
||||
0x06, 0x7a, 0xf9, 0x26, 0xfe, 0x3c, 0x13, 0x05, // |.z.&.<..|
|
||||
0xf6, 0xbe, 0x5f, 0x0c, 0x9a, 0x43, 0x18, 0xa2, // |.._..C..|
|
||||
0xd9, 0xc5, 0xa0, 0x06, 0x0b, 0x0a, 0x21, 0xf1, // |......!.|
|
||||
0x6b, 0x12, 0x4a, 0x5d, 0xec, 0xf6, 0x01, 0x30, // |k.J]...0|
|
||||
0xb6, 0x3b, 0x34, 0x62, 0xcd, 0x5a, 0x6a, 0x26, // |.;4b.Zj&|
|
||||
0x08, 0x98, 0xc9, 0xd0, 0x8a, 0x49, 0x94, 0x07, // |.....I..|
|
||||
0x48, 0x45, 0x78, 0x45, 0xae, 0x24, 0x2b, 0x83, // |HExE.$+.|
|
||||
0xb6, 0x69, 0x6c, 0x20, 0x33, 0xa9, 0xc4, 0x8e, // |.il 3...|
|
||||
0xe7, 0x1a, 0x90, 0x28, 0xc6, 0x3f, 0x16, 0xf2, // |...(.?..|
|
||||
0xae, 0x3e, 0x22, 0x17, 0x26, 0x9c, 0x38, 0xf5, // |.>".&.8.|
|
||||
0x88, 0x60, 0x79, 0x16, 0x28, 0xce, 0x05, 0x72, // |.`y.(..r|
|
||||
0x2f, 0x64, 0x99, 0xdd, 0x8c, 0x5b, 0xa6, 0xe2, // |/d...[..|
|
||||
0x65, 0x8a, 0xe2, 0x8d, 0xb6, 0x24, 0x9e, 0x6d, // |e....$.m|
|
||||
0x5a, 0x70, 0xfd, 0xea, 0xca, 0xec, 0x77, 0x46, // |Zp....wF|
|
||||
0x20, 0xa8, 0x1f, 0x78, 0xf6, 0x34, 0x52, 0x13, // | ..x.4R.|
|
||||
0x97, 0xef, 0x60, 0xb9, 0xe5, 0xc6, 0x85, 0xf2, // |..`.....|
|
||||
0x84, 0x64, 0xdc, 0x08, 0x07, 0xe2, 0x63, 0xa6, // |.d....c.|
|
||||
0x23, 0x64, 0x54, 0xb8, 0x72, 0xac, 0x23, 0xda, // |#dT.r.#.|
|
||||
0x8f, 0x73, 0xe4, 0x9b, 0x80, 0x77, 0x66, 0x3f, // |.s...wf?|
|
||||
0x69, 0x34, 0xc4, 0xfb, 0x45, 0x3d, 0x1c, 0xa7, // |i4..E=..|
|
||||
0x86, 0x98, 0x2e, 0xb4, 0xe0, 0x84, 0xb6, 0x47, // |.......G|
|
||||
0x78, 0xeb, 0x2b, 0x10, 0x17, 0x45, 0x8a, 0xcf, // |x.+..E..|
|
||||
0xea, 0xb5, 0x58, 0x42, 0x93, 0xbe, 0x4b, 0xad, // |..XB..K.|
|
||||
0xfb, 0x28, 0x11, 0x12, 0xe0, 0x7c, 0x3d, 0x34, // |.(...|=4|
|
||||
0x8c, 0x82, 0x07, 0x84, 0xda, 0x8b, 0x35, 0x86, // |......5.|
|
||||
0x37, 0x35, 0x1d, 0x1a, 0xa2, 0xbf, 0x0a, 0xb4, // |75......|
|
||||
0x8e, 0xf0, 0x91, 0xc4, 0xa8, 0x3f, 0x38, 0x03, // |.....?8.|
|
||||
0x37, 0xc1, 0x9a, 0x94, 0x43, 0x09, 0x57, 0xee, // |7...C.W.|
|
||||
0xaa, 0xcb, 0x3d, 0x13, 0xa2, 0x33, 0xd1, 0x04, // |..=..3..|
|
||||
0x2c, 0x6c, 0xb4, 0x1c, 0x86, 0x07, 0x0c, 0x3c, // |,l.....<|
|
||||
0x5c, 0xc9, 0x8c, 0xc8, 0x1a, 0x85, 0xa6, 0xdd, // |\.......|
|
||||
0xd3, 0xc5, 0xae, 0x84, 0x4d, 0xfe, 0xa2, 0x99, // |....M...|
|
||||
0xd3, 0x0b, 0x1f, 0x43, 0x01, 0xa6, 0x7b, 0xb2, // |...C..{.|
|
||||
0x5b, 0xd5, 0xa0, 0x3e, 0xd4, 0x6c, 0x65, 0x75, // |[..>.leu|
|
||||
0x55, 0x28, 0x4d, 0x1c, 0x28, 0x86, 0xda, 0x94, // |U(M.(...|
|
||||
0xbe, 0x0a, 0x99, 0x61, 0xa4, 0x88, 0xd9, 0x6a, // |...a...j|
|
||||
0x20, 0x1d, 0x78, 0x45, 0x5f, 0x66, 0xcc, 0x8c, // | .xE_f..|
|
||||
0xe1, 0xba, 0x4c, 0x51, 0x99, 0x54, 0x27, 0x77, // |..LQ.T'w|
|
||||
0xb4, 0x84, 0x61, 0x4e, 0xf9, 0x90, 0x6f, 0x19, // |..aN..o.|
|
||||
0x44, 0x93, 0x27, 0x1d, 0x95, 0x82, 0x74, 0x7f, // |D.'...t.|
|
||||
0x35, 0xaf, 0x04, 0xe4, 0x58, 0x41, 0x3a, 0x51, // |5...XA:Q|
|
||||
0x0b, 0x22, 0x45, 0xaf, 0x44, 0x2a, 0xe9, 0xa3, // |."E.D*..|
|
||||
0x71, 0x65, 0x15, 0x22, 0xea, 0x40, 0x10, 0xaf, // |qe.".@..|
|
||||
0x5b, 0x27, 0xfc, 0x02, 0x00, 0x23, 0xa3, 0x70, // |['...#.p|
|
||||
0xa9, 0x6c, 0xa7, 0xf7, 0x29, 0x5c, 0x75, 0x9b, // |.l..)\u.|
|
||||
0x4c, 0x23, 0x14, 0x51, 0x12, 0x62, 0x71, 0xbb, // |L#.Q.bq.|
|
||||
0x75, 0x64, 0x65, 0xb3, 0xaa, 0x1e, 0x10, 0x14, // |ude.....|
|
||||
0xbf, 0xd0, 0x8b, 0xe0, 0xe4, 0x51, 0x6e, 0xa8, // |.....Qn.|
|
||||
0x1a, 0x95, 0x21, 0xa9, 0x9f, 0xf7, 0x2a, 0xac, // |..!...*.|
|
||||
0x5c, 0x1c, 0x12, 0xac, 0x9d, 0xac, 0x57, 0x14, // |\.....W.|
|
||||
0x27, 0xaa, 0xa7, 0xee, 0xc3, 0x9d, 0x63, 0x48, // |'.....cH|
|
||||
0x0e, 0xd7, 0xf8, 0x92, 0x9f, 0x28, 0xb9, 0x82, // |.....(..|
|
||||
0x71, 0x99, 0xa1, 0xcb, 0x69, 0x0c, 0x29, 0x7d, // |q...i.)}|
|
||||
0x67, 0x73, 0xae, 0x9d, 0xd7, 0xc7, 0x51, 0x7a, // |gs....Qz|
|
||||
0x2c, 0x3a, 0x74, 0x89, 0x7d, 0x76, 0x35, 0xb5, // |,:t.}v5.|
|
||||
0x97, 0x73, 0x4a, 0xfc, 0x29, 0x9a, 0x1a, 0x06, // |.sJ.)...|
|
||||
0x2f, 0xd0, 0x89, 0x32, 0xfc, 0x3b, 0x17, 0xec, // |/..2.;..|
|
||||
0x7a, 0xb5, 0x3c, 0x66, 0x0f, 0x43, 0x55, 0x41, // |z.<f.CUA|
|
||||
0x49, 0x3f, 0xbf, 0xa1, 0x6f, 0x8a, 0x05, 0x76, // |I?..o..v|
|
||||
0xd4, 0x02, 0x33, 0x52, 0x78, 0xc8, 0x08, 0xe9, // |..3Rx...|
|
||||
0x49, 0xb8, 0x42, 0x05, 0xed, 0x34, 0x0a, 0xb1, // |I.B..4..|
|
||||
0xa8, 0x32, 0x00, 0x6b, 0x00, 0x42, 0x56, 0x8a, // |.2.k.BV.|
|
||||
0xe9, 0x04, 0x7a, 0xac, 0xc8, 0x72, 0x7f, 0x40, // |..z..r.@|
|
||||
0x4c, 0xd6, 0xa9, 0x34, 0x0b, 0xc3, 0x63, 0x39, // |L..4..c9|
|
||||
0x21, 0xbf, 0x04, 0xb0, 0x2b, 0x81, 0xf9, 0x07, // |!...+...|
|
||||
0xe6, 0x15, 0x92, 0x89, 0x9b, 0x1e, 0xe6, 0x4b, // |.......K|
|
||||
0x5b, 0x0b, 0x33, 0x5f, 0x89, 0x96, 0xa2, 0x74, // |[.3_...t|
|
||||
0x41, 0x6b, 0x15, 0xe8, 0x8a, 0x62, 0xf5, 0x1c, // |Ak...b..|
|
||||
0x37, 0x38, 0x62, 0x77, 0xd4, 0x57, 0x7b, 0x43, // |78bw.W{C|
|
||||
0x42, 0x4f, 0x01, 0x9c, 0xf2, 0xe0, 0x68, 0xb7, // |BO....h.|
|
||||
0xf1, 0x66, 0x93, 0xd8, 0x8e, 0x78, 0x80, 0x24, // |.f...x.$|
|
||||
0x4c, 0x61, 0x11, 0xbb, 0xf2, 0x79, 0xf7, 0x96, // |La...y..|
|
||||
0x02, 0x80, 0xaa, 0xc7, 0xcd, 0xbb, 0x55, 0x03, // |......U.|
|
||||
0x22, 0x5e, 0xda, 0xa2, 0x44, 0x7d, 0x82, 0x41, // |"^..D}.A|
|
||||
0x86, 0x9b, 0x92, 0x0a, 0xd5, 0x7e, 0xf2, 0x78, // |.....~.x|
|
||||
0x84, 0x50, 0x00, 0x2d, 0x0b, 0xab, 0x92, 0x7a, // |.P.-...z|
|
||||
0x96, 0x15, 0xcf, 0x5a, 0x34, 0x45, 0x35, 0xa7, // |...Z4E5.|
|
||||
0x18, 0x61, 0x2b, 0x88, 0x45, 0xaa, 0xd3, 0xe2, // |.a+.E...|
|
||||
0x54, 0xf9, 0xc7, 0xbb, 0xe7, 0x00, 0x86, 0xbd, // |T.......|
|
||||
0x8b, 0xbb, 0x6d, 0x3b, 0x0f, 0x8d, 0xfb, 0x4d, // |..m;...M|
|
||||
0x5d, 0x8b, 0x50, 0x2e, 0x68, 0x74, 0x5d, 0x03, // |].P.ht].|
|
||||
0x16, 0x2a, 0x49, 0x24, 0x54, 0x5b, 0xa9, 0x34, // |.*I$T[.4|
|
||||
0x25, 0x17, 0x79, 0xe3, 0xc3, 0x3a, 0x2a, 0x12, // |%.y..:*.|
|
||||
0x75, 0x64, 0x16, 0xa4, 0xb7, 0x7e, 0x39, 0x5a, // |ud...~9Z|
|
||||
0x4e, 0x3e, 0x53, 0x2b, 0x49, 0x1b, 0x26, 0xdf, // |N>S+I.&.|
|
||||
0xfc, 0x29, 0x99, 0xcb, 0xad, 0x29, 0x2c, 0x72, // |.)...),r|
|
||||
0x3f, 0xa7, 0xcb, 0x45, 0x4c, 0x14, 0xee, 0x46, // |?..EL..F|
|
||||
0x74, 0x64, 0xdb, 0x4b, 0x4b, 0xa4, 0x35, 0x3c, // |td.KK.5<|
|
||||
0x91, 0xc4, 0x9b, 0xb0, 0x66, 0xc6, 0x70, 0xb6, // |....f.p.|
|
||||
0xf2, 0x07, 0x3b, 0xbf, 0x74, 0x72, 0xb4, 0x24, // |..;.tr.$|
|
||||
0x7e, 0x87, 0xd4, 0x0a, 0x37, 0xd9, 0x49, 0x04, // |~...7.I.|
|
||||
0x09, 0x36, 0xd1, 0x63, 0x88, 0xe1, 0xe8, 0x08, // |.6.c....|
|
||||
0xbf, 0x17, 0xc4, 0xcd, 0xcb, 0x3c, 0xef, 0x88, // |.....<..|
|
||||
0x2c, 0xf6, 0xa3, 0x6d, 0x89, 0x39, 0xc9, 0xfe, // |,..m.9..|
|
||||
0x97, 0x25, 0xb3, 0x9a, 0x02, 0x40, 0xd4, 0x90, // |.%...@..|
|
||||
0x28, 0x6a, 0x79, 0xbd, 0x4b, 0x8e, 0x10, 0x18, // |(jy.K...|
|
||||
0xc9, 0xaf, 0xe9, 0xc0, 0x6e, 0xd5, 0xb1, 0xcf, // |....n...|
|
||||
0xe8, 0xa4, 0xdc, 0x94, 0x12, 0x82, 0xfb, 0x08, // |........|
|
||||
0x42, 0xd4, 0x1a, 0x76, 0xa2, 0x4b, 0x3f, 0xc3, // |B..v.K?.|
|
||||
0xb4, 0x0b, 0xa3, 0x0c, 0xec, 0x19, 0x7c, 0x5f, // |......|_|
|
||||
0xd5, 0x98, 0x99, 0xf4, 0x1a, 0xca, 0x83, 0xaa, // |........|
|
||||
0xbd, 0x26, 0x31, 0x95, 0x77, 0x90, 0x43, 0x7a, // |.&1.w.Cz|
|
||||
0x75, 0x15, 0xcb, 0x68, 0xae, 0x24, 0xc5, 0x1b, // |u..h.$..|
|
||||
];
|
||||
|
||||
const TOO_LITTLE_DATA_END: [u8; 312] = [
|
||||
0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, // |........|
|
||||
0x00, 0x01, 0x00, 0x06, 0x14, 0x49, 0xbc, 0x07, // |.....I..|
|
||||
0xd1, 0x60, 0x00, 0x00, 0x45, 0x00, 0x01, 0x24, // |.`..E..$|
|
||||
0x46, 0x04, 0x40, 0x00, 0x7f, 0x06, 0x11, 0x27, // |F.@....'|
|
||||
0x4d, 0xaf, 0x56, 0xec, 0xc0, 0xa8, 0x3e, 0x65, // |M.V...>e|
|
||||
0xbf, 0x7f, 0x01, 0xbb, 0xaf, 0x09, 0x43, 0xb4, // |......C.|
|
||||
0xde, 0x93, 0xcc, 0x66, 0x50, 0x18, 0x02, 0x01, // |...fP...|
|
||||
0xd3, 0x21, 0x00, 0x00, 0xba, 0x15, 0x37, 0x39, // |.!....79|
|
||||
0xe7, 0xae, 0xb6, 0x48, 0x3e, 0xd8, 0x57, 0x67, // |...H>.Wg|
|
||||
0x9c, 0xb6, 0x9c, 0xc0, 0x18, 0x0e, 0x74, 0x67, // |......tg|
|
||||
0xae, 0x8e, 0xc6, 0x80, 0x7f, 0x81, 0x25, 0xc4, // |......%.|
|
||||
0xe9, 0x04, 0xe8, 0xd9, 0x98, 0xb6, 0x99, 0x93, // |........|
|
||||
0xa1, 0xa4, 0x5e, 0x57, 0x74, 0x89, 0x30, 0x38, // |..^Wt.08|
|
||||
0xa9, 0xbb, 0x99, 0x4a, 0x7e, 0x42, 0x3c, 0xd2, // |...J~B<.|
|
||||
0x59, 0xb6, 0x49, 0xb0, 0xc7, 0x11, 0x57, 0x03, // |Y.I...W.|
|
||||
0x6d, 0x23, 0x1b, 0x72, 0xe7, 0x24, 0xdb, 0x75, // |m#.r.$.u|
|
||||
0x78, 0xd1, 0x38, 0x01, 0x46, 0xb6, 0x8c, 0x1b, // |x.8.F...|
|
||||
0x41, 0xb4, 0xbd, 0xc1, 0xa2, 0x00, 0x63, 0xa5, // |A.....c.|
|
||||
0x97, 0x30, 0x5d, 0xbe, 0xd1, 0x37, 0x31, 0xf1, // |.0]..71.|
|
||||
0xbb, 0xc6, 0xf8, 0x81, 0x35, 0x86, 0x32, 0xa6, // |....5.2.|
|
||||
0xc3, 0x35, 0x54, 0x45, 0x50, 0xdf, 0x61, 0x46, // |.5TEP.aF|
|
||||
0x5b, 0x83, 0x6b, 0xac, 0x5c, 0x2d, 0xa2, 0xc3, // |[.k.\-..|
|
||||
0x2e, 0x71, 0x32, 0x18, 0x41, 0x29, 0x99, 0x66, // |.q2.A).f|
|
||||
0x8c, 0x50, 0x28, 0x92, 0x45, 0xae, 0x96, 0x38, // |.P(.E..8|
|
||||
0xa4, 0x83, 0x94, 0x4a, 0x2f, 0x0e, 0x62, 0x13, // |...J/.b.|
|
||||
0x07, 0x13, 0xc2, 0x0b, 0x84, 0xfd, 0x27, 0xab, // |......'.|
|
||||
0x6c, 0xb4, 0x69, 0x0d, 0xd2, 0xdb, 0xfb, 0x8e, // |l.i.....|
|
||||
0xa7, 0x09, 0x65, 0x76, 0x7e, 0x09, 0xa4, 0x7a, // |..ev~..z|
|
||||
0xe9, 0xfe, 0xec, 0x52, 0x89, 0x7d, 0x07, 0x6f, // |...R.}.o|
|
||||
0xff, 0xa0, 0xde, 0x8a, 0x42, 0x2d, 0xc3, 0x75, // |....B-.u|
|
||||
0x05, 0x6d, 0x60, 0x76, 0xce, 0xe1, 0x6c, 0xfd, // |.m`v..l.|
|
||||
0xae, 0x1f, 0x5e, 0x02, 0x94, 0x39, 0x2a, 0x55, // |..^..9*U|
|
||||
0x00, 0x1d, 0x00, 0x20, 0x8d, 0x89, 0x9a, 0x19, // |... ....|
|
||||
0x1d, 0x53, 0x52, 0xd5, 0xc1, 0x3e, 0x3a, 0x1d, // |.SR..>:.|
|
||||
0x12, 0x15, 0xae, 0x33, 0x2e, 0x54, 0xd1, 0x6f, // |...3.T.o|
|
||||
0xd6, 0xb1, 0x73, 0xd9, 0x56, 0x98, 0x6f, 0x8f, // |..s.V.o.|
|
||||
0x7e, 0xf5, 0xd9, 0x75, 0x00, 0x0b, 0x00, 0x02, // |~..u....|
|
||||
0x01, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, // |........|
|
||||
0x00, 0x00, 0x00, 0x9a, 0x9a, 0x00, 0x01, 0x00, // |........|
|
||||
];
|
||||
}
|
||||
|
||||
@@ -4,7 +4,8 @@ use std::io::Result;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
use std::sync::RwLock;
|
||||
use time::{Duration, Instant, OffsetDateTime};
|
||||
use std::time::Instant;
|
||||
use time::{Duration, OffsetDateTime};
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub(crate) struct UpstreamAddress {
|
||||
|
||||
37
src/tls.rs
Normal file
37
src/tls.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
use rustls::pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer};
|
||||
use rustls::ServerConfig;
|
||||
use rustls_pemfile::{certs, pkcs8_private_keys};
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub fn load_certs(path: &str) -> Result<Vec<CertificateDer>> {
|
||||
let mut reader = BufReader::new(File::open(path)?);
|
||||
certs(&mut reader)
|
||||
.collect::<Result<Vec<CertificateDer>, std::io::Error>>()
|
||||
.map_err(|e| anyhow!("failed to load certificates from {}: {}", path, e))
|
||||
}
|
||||
|
||||
pub fn load_private_key(path: &str) -> Result<PrivateKeyDer> {
|
||||
let mut reader = BufReader::new(File::open(path)?);
|
||||
let mut keys = pkcs8_private_keys(&mut reader)
|
||||
.collect::<Result<Vec<PrivatePkcs8KeyDer>, std::io::Error>>()
|
||||
.map_err(|e| anyhow!("failed to load private keys from {}: {}", path, e))?;
|
||||
keys.pop()
|
||||
.map(|k| PrivateKeyDer::Pkcs8(k.to_vec().into()))
|
||||
.ok_or_else(|| anyhow!("no private keys found for {}", path))
|
||||
}
|
||||
|
||||
pub fn build_tls_acceptor(
|
||||
config: &crate::config::config_v1::TlsTerminationConfig,
|
||||
) -> Result<tokio_rustls::TlsAcceptor> {
|
||||
let certs = load_certs(&config.default_certificate.certificate_path)?;
|
||||
let key = load_private_key(&config.default_certificate.private_key_path)?;
|
||||
|
||||
let tls_config = ServerConfig::builder()
|
||||
.with_no_client_auth()
|
||||
.with_single_cert(certs, key)?;
|
||||
|
||||
Ok(tokio_rustls::TlsAcceptor::from(Arc::new(tls_config)))
|
||||
}
|
||||
22
src/update.rs
Normal file
22
src/update.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use self_update::cargo_crate_version;
|
||||
|
||||
pub(crate) fn update() {
|
||||
println!("Updating to the latest version...");
|
||||
|
||||
let backend = self_update::backends::gitea::Update::configure()
|
||||
.with_host("https://code.kiers.eu")
|
||||
.repo_owner("jjkiers")
|
||||
.repo_name("layer4-proxy")
|
||||
.bin_name("l4p")
|
||||
.show_download_progress(true)
|
||||
.current_version(cargo_crate_version!())
|
||||
.build()
|
||||
.expect("Should initialize correctly.");
|
||||
|
||||
let status = backend.update_extended();
|
||||
|
||||
match status {
|
||||
Err(e) => eprintln!("Error updating: {e}"),
|
||||
Ok(_) => (),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user