Compare commits
4
Commits
22962e0df8
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6143633a45
|
||
|
|
df0858c6f2
|
||
|
|
5e21734fb6
|
||
|
|
9cb5468976
|
Generated
+20
-20
@@ -4,21 +4,21 @@ version = 4
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bytes"
|
name = "bytes"
|
||||||
version = "1.11.1"
|
version = "1.12.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33"
|
checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.186"
|
version = "0.2.191"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"
|
checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mio"
|
name = "mio"
|
||||||
version = "1.2.0"
|
version = "1.2.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "50b7e5b27aa02a74bac8c3f23f448f8d87ff11f92d3aac1a6ed369ee08cc56c1"
|
checksum = "1788edb87fdc09c7e26304471e2f5be8cdefb1b6930d6e3985fc02ff53bf86ee"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"wasi",
|
"wasi",
|
||||||
@@ -33,27 +33,27 @@ checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.106"
|
version = "1.0.107"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
|
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "quote"
|
name = "quote"
|
||||||
version = "1.0.45"
|
version = "1.0.47"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
|
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "socket2"
|
name = "socket2"
|
||||||
version = "0.6.3"
|
version = "0.6.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e"
|
checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"windows-sys",
|
"windows-sys",
|
||||||
@@ -61,9 +61,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.117"
|
version = "3.0.9"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
|
checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@@ -72,9 +72,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio"
|
name = "tokio"
|
||||||
version = "1.52.3"
|
version = "1.53.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8fc7f01b389ac15039e4dc9531aa973a135d7a4135281b12d7c1bc79fd57fffe"
|
checksum = "ce3335fa71841cda333a58d7615b03901380ecf09d59b3296d21f8bbac0dde4e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bytes",
|
"bytes",
|
||||||
"libc",
|
"libc",
|
||||||
@@ -87,9 +87,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio-macros"
|
name = "tokio-macros"
|
||||||
version = "2.7.0"
|
version = "2.7.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "385a6cb71ab9ab790c5fe8d67f1645e6c450a7ce006a33de03daa956cf70a496"
|
checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@@ -98,9 +98,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-ident"
|
name = "unicode-ident"
|
||||||
version = "1.0.24"
|
version = "1.0.27"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "urls-txt"
|
name = "urls-txt"
|
||||||
|
|||||||
+1
-1
@@ -4,4 +4,4 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tokio = { version = "1.52.3", features = ["io-util", "macros", "net", "rt-multi-thread"] }
|
tokio = { version = "1.52", features = ["io-util", "macros", "net", "rt-multi-thread"] }
|
||||||
|
|||||||
+15
-10
@@ -7,13 +7,16 @@
|
|||||||
################################################################################
|
################################################################################
|
||||||
# Create a stage for building the application.
|
# Create a stage for building the application.
|
||||||
|
|
||||||
ARG RUST_VERSION=1.89.0
|
ARG RUST_VERSION=1
|
||||||
FROM rust:${RUST_VERSION}-slim-bullseye AS build
|
FROM rust:${RUST_VERSION}-slim AS build
|
||||||
ARG TARGET=release
|
ARG TARGET=release
|
||||||
ARG APP_NAME=urls-txt
|
ARG APP_NAME=urls-txt
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
RUN rm /var/lib/dpkg/info/libc-bin.* && apt-get clean && apt-get update && apt install libc-bin
|
RUN rm /var/lib/dpkg/info/libc-bin.* \
|
||||||
|
&& apt-get clean \
|
||||||
|
&& apt-get update \
|
||||||
|
&& apt-get install -y libc-bin
|
||||||
|
|
||||||
# Build the application.
|
# Build the application.
|
||||||
# Leverage a cache mount to /usr/local/cargo/registry/
|
# Leverage a cache mount to /usr/local/cargo/registry/
|
||||||
@@ -46,9 +49,13 @@ EOF
|
|||||||
# most recent version of that tag when you build your Dockerfile. If
|
# most recent version of that tag when you build your Dockerfile. If
|
||||||
# reproducability is important, consider using a digest
|
# reproducability is important, consider using a digest
|
||||||
# (e.g., debian@sha256:ac707220fbd7b67fc19b112cee8170b41a9e97f703f588b2cdbbcdcecdd8af57).
|
# (e.g., debian@sha256:ac707220fbd7b67fc19b112cee8170b41a9e97f703f588b2cdbbcdcecdd8af57).
|
||||||
FROM debian:bullseye-slim AS final
|
FROM debian:trixie-slim AS final
|
||||||
RUN rm /var/lib/dpkg/info/libc-bin.* && apt-get clean && apt-get update && apt install libc-bin
|
|
||||||
ENV URLS_TXT_BIND_ADDRESS=0.0.0.0:3000
|
RUN rm /var/lib/dpkg/info/libc-bin.* \
|
||||||
|
&& apt-get clean \
|
||||||
|
&& apt-get update \
|
||||||
|
&& apt-get upgrade -y \
|
||||||
|
&& apt-get install -y libc-bin adduser
|
||||||
|
|
||||||
# Create a non-privileged user that the app will run under.
|
# Create a non-privileged user that the app will run under.
|
||||||
# See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/ #user
|
# See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/ #user
|
||||||
@@ -63,11 +70,9 @@ RUN adduser \
|
|||||||
appuser
|
appuser
|
||||||
USER appuser
|
USER appuser
|
||||||
|
|
||||||
# Copy the executable from the "build" stage.
|
|
||||||
COPY --from=build /bin/server /bin/
|
COPY --from=build /bin/server /bin/
|
||||||
|
|
||||||
# Expose the port that the application listens on.
|
EXPOSE 3000
|
||||||
EXPOSE 8000
|
|
||||||
|
|
||||||
# What the container should run when it is started.
|
ENV URLS_TXT_BIND_ADDRESS=0.0.0.0:3000
|
||||||
CMD ["/bin/server", "/urls.txt"]
|
CMD ["/bin/server", "/urls.txt"]
|
||||||
|
|||||||
@@ -5,5 +5,7 @@ services:
|
|||||||
target: final
|
target: final
|
||||||
ports:
|
ports:
|
||||||
- 3000:3000
|
- 3000:3000
|
||||||
|
environment:
|
||||||
|
URLS_TXT_IGNORE_QUERY_PARAMS: true # change to `false` to include query params
|
||||||
volumes:
|
volumes:
|
||||||
- "./urls.txt:/urls.txt"
|
- "./urls.txt:/urls.txt"
|
||||||
|
|||||||
@@ -63,9 +63,11 @@ for example, for the link <http://pls.cx/🤬> the following line is in my urls.
|
|||||||
|
|
||||||
trailing slashes are ignored.
|
trailing slashes are ignored.
|
||||||
|
|
||||||
### query parameters are ignored
|
### query parameters are ignored by default
|
||||||
|
|
||||||
anything after a question mark character (`?`) are ignored.
|
anything after a question mark character (`?`) are ignored by default.
|
||||||
|
|
||||||
|
to disable this, set the environment variable `URLS_TXT_IGNORE_QUERY_PARAMS` to `false`.
|
||||||
|
|
||||||
## why?
|
## why?
|
||||||
|
|
||||||
|
|||||||
+33
-12
@@ -13,14 +13,33 @@ enum ResponseCode {
|
|||||||
NotFound,
|
NotFound,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove query parameters and trailing slashes.
|
#[derive(Clone, Copy)]
|
||||||
/// Makes all paths lowercase.
|
struct Config {
|
||||||
fn normalise_path(path: &str) -> String {
|
ignore_query_params: bool,
|
||||||
let mut path = path
|
}
|
||||||
|
|
||||||
|
impl Default for Config {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
ignore_query_params: match env::var("URLS_TXT_IGNORE_QUERY_PARAMS") {
|
||||||
|
Ok(val) => val != "false",
|
||||||
|
_ => true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn normalise_path(path: &str, config: &Config) -> String {
|
||||||
|
let mut path = path;
|
||||||
|
|
||||||
|
if config.ignore_query_params {
|
||||||
|
path = path
|
||||||
.split('?')
|
.split('?')
|
||||||
.next()
|
.next()
|
||||||
.expect("split operation returns at least one substring");
|
.expect("split operation returns at least one substring");
|
||||||
if path.len() > 1
|
}
|
||||||
|
|
||||||
|
while path.len() > 1
|
||||||
&& let Some('/') = path.chars().next_back()
|
&& let Some('/') = path.chars().next_back()
|
||||||
{
|
{
|
||||||
path = &path[0..path.len() - 1];
|
path = &path[0..path.len() - 1];
|
||||||
@@ -49,7 +68,7 @@ async fn simple_response(mut socket: BufStream<TcpStream>, rc: ResponseCode) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn process_socket(mut socket: BufStream<TcpStream>, urlmap: Arc<HashMap<String, String>>) {
|
async fn process_socket(mut socket: BufStream<TcpStream>, urlmap: Arc<HashMap<String, String>>, config: &Config) {
|
||||||
let mut request_line = String::default();
|
let mut request_line = String::default();
|
||||||
if let Err(e) = socket.read_line(&mut request_line).await {
|
if let Err(e) = socket.read_line(&mut request_line).await {
|
||||||
println!("process_request: failed to read line: {e}");
|
println!("process_request: failed to read line: {e}");
|
||||||
@@ -63,7 +82,7 @@ async fn process_socket(mut socket: BufStream<TcpStream>, urlmap: Arc<HashMap<St
|
|||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
let path = normalise_path(path);
|
let path = normalise_path(path, config);
|
||||||
|
|
||||||
if http_method != "GET" {
|
if http_method != "GET" {
|
||||||
println!("process_request: forbidden method: http_method={http_method}");
|
println!("process_request: forbidden method: http_method={http_method}");
|
||||||
@@ -110,9 +129,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let config = spawn_blocking(move || std::fs::read_to_string(urls_filename)).await??;
|
let config = Config::default();
|
||||||
|
|
||||||
|
let urls_file = spawn_blocking(move || std::fs::read_to_string(urls_filename)).await??;
|
||||||
let mut urlmap = HashMap::new();
|
let mut urlmap = HashMap::new();
|
||||||
for url_pair in config.split('\n') {
|
for url_pair in urls_file.split('\n') {
|
||||||
if url_pair.is_empty() {
|
if url_pair.is_empty() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -132,18 +153,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
Err(format!("Unable to parse line: '{}'", url_pair))?
|
Err(format!("Unable to parse line: '{}'", url_pair))?
|
||||||
};
|
};
|
||||||
|
|
||||||
urlmap.insert(normalise_path(slug), redirect.to_string());
|
urlmap.insert(normalise_path(slug, &config), redirect.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
let urlmap = Arc::new(urlmap);
|
let urlmap = Arc::new(urlmap);
|
||||||
|
|
||||||
println!("Starting TCP Listener...");
|
println!("Starting TCP Listener on {bind_address}...");
|
||||||
let listener = TcpListener::bind(&bind_address).await?;
|
let listener = TcpListener::bind(&bind_address).await?;
|
||||||
loop {
|
loop {
|
||||||
let (socket, _addr) = listener.accept().await?;
|
let (socket, _addr) = listener.accept().await?;
|
||||||
let urlmap = urlmap.clone();
|
let urlmap = urlmap.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
process_socket(BufStream::new(socket), urlmap).await;
|
process_socket(BufStream::new(socket), urlmap, &config).await;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user