From eabb52a6c0028bc4e4bf9cb1cf1add28ab3df185 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Tue, 10 May 2022 11:29:17 +0200 Subject: [PATCH] Doc + example + fix domain/host bug --- README.md | 23 +++++++++++++++++ examples/.gitignore | 1 + examples/node/client.mjs | 11 +++++++++ examples/node/package-lock.json | 44 +++++++++++++++++++++++++++++++++ examples/node/package.json | 14 +++++++++++ examples/node/server.mjs | 9 +++++++ src/https.rs | 10 ++++++-- 7 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 examples/.gitignore create mode 100644 examples/node/client.mjs create mode 100644 examples/node/package-lock.json create mode 100644 examples/node/package.json create mode 100644 examples/node/server.mjs diff --git a/README.md b/README.md index 503b686..243c9c9 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,29 @@ Logs are the privileged place to get information about what Tricot is doing. You - `RUST_LOG=tricot=debug`: Tricot will show for each request the backend to which it is routed. It will also show all of its interactions with Consul - `RUST_LOG=tricot=trace`: Tricot will show details such as the request's headers for all request at all stages of proxying. +## For developers + +Build Tricot: + +```bash +git clone https://git.deuxfleurs.fr/Deuxfleurs/tricot +cd tricot +cargo build +``` + +Start Tricot: + +```bash +consul agent -dev +cargo run -- --letsencrypt-email you@example.com --http-bind-addr [::1]:8080 --https-bind-addr [::1]:4443 +``` + +Register services: + +``` +consul services register -name=localhost -tag="tricot localhost" -address [::1] -port 3000 +``` + ## License diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/examples/node/client.mjs b/examples/node/client.mjs new file mode 100644 index 0000000..2a10d1e --- /dev/null +++ b/examples/node/client.mjs @@ -0,0 +1,11 @@ +import WebSocket from 'ws'; + +const u = 'wss://localhost:4443'; +//const u = 'ws://localhost:3000'; + +const ws = new WebSocket(u, { + rejectUnauthorized: false, +}); + +ws.on('open', () => ws.send('something')) +ws.on('message', msg => console.log('received: %s', msg)) diff --git a/examples/node/package-lock.json b/examples/node/package-lock.json new file mode 100644 index 0000000..8e0f855 --- /dev/null +++ b/examples/node/package-lock.json @@ -0,0 +1,44 @@ +{ + "name": "nodeserver", + "version": "1.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "nodeserver", + "version": "1.0.0", + "license": "AGPL-3.0-or-later", + "dependencies": { + "ws": "^8.6.0" + } + }, + "node_modules/ws": { + "version": "8.6.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.6.0.tgz", + "integrity": "sha512-AzmM3aH3gk0aX7/rZLYvjdvZooofDu3fFOzGqcSnQ1tOcTWwhM/o+q++E8mAyVVIyUdajrkzWUGftaVSDLn1bw==", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": "^5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + } + }, + "dependencies": { + "ws": { + "version": "8.6.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.6.0.tgz", + "integrity": "sha512-AzmM3aH3gk0aX7/rZLYvjdvZooofDu3fFOzGqcSnQ1tOcTWwhM/o+q++E8mAyVVIyUdajrkzWUGftaVSDLn1bw==", + "requires": {} + } + } +} diff --git a/examples/node/package.json b/examples/node/package.json new file mode 100644 index 0000000..83f5cb7 --- /dev/null +++ b/examples/node/package.json @@ -0,0 +1,14 @@ +{ + "name": "nodeserver", + "version": "1.0.0", + "description": "", + "main": "server.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Quentin Dufour ", + "license": "AGPL-3.0-or-later", + "dependencies": { + "ws": "^8.6.0" + } +} diff --git a/examples/node/server.mjs b/examples/node/server.mjs new file mode 100644 index 0000000..474ebe0 --- /dev/null +++ b/examples/node/server.mjs @@ -0,0 +1,9 @@ +import { WebSocketServer } from 'ws'; + +const wss = new WebSocketServer({ port: 3000 }); + +wss.on('connection', ws => + ws.on('message', msg => { + console.log('received: %s', msg) + ws.send(msg) +})) diff --git a/src/https.rs b/src/https.rs index 7dcf051..c3c4550 100644 --- a/src/https.rs +++ b/src/https.rs @@ -153,14 +153,20 @@ async fn handle( .ok_or_else(|| anyhow!("Missing host header"))? .to_str()? }; + let domain = match host.split_once(':') { + Some((domain, _port)) => domain, + _ => host, + }; + debug!("Matching on domain {}", domain); + let path = req.uri().path(); let accept_encoding = accept_encoding_fork::encodings(req.headers()).unwrap_or_else(|_| vec![]); let best_match = proxy_config .entries .iter() - .filter(|ent| { - ent.host.matches(host) + .filter(|ent| { + ent.host.matches(domain) && ent .path_prefix .as_ref()