From fa132fe25056faf5e3941a6813fedac89bdb9299 Mon Sep 17 00:00:00 2001 From: HardAndHeavy Date: Wed, 13 Nov 2024 00:41:31 +0300 Subject: [PATCH] Add proxy for downloading registry --- Cargo.toml | 99 ++++++++++++++++++++-------------------- Dockerfile | 22 ++++++--- docs/src/introduction.md | 2 +- integration/main.rs | 1 + src/config.rs | 6 +++ src/lib.rs | 12 ++++- 6 files changed, 84 insertions(+), 58 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bd347f2c..8490fbaf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,60 +7,61 @@ about = "Terraform mirroring proxy" [[test]] name = "integration" -path = "integration/main.rs" + path = "integration/main.rs" -[[bin]] -name = "bootstrap" -path = "src/lambda.rs" + [[bin]] + name = "bootstrap" + path = "src/lambda.rs" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[dependencies] -anyhow = "^1.0.82" -aws-sdk-s3 = { version = "^1.25.0" } -axum = { version = "^0.7.5", features = ["http2", "json", "tracing"] } -clap = { version = "^4.5.4", features = [ - "derive", - "env", - "unicode", - "wrap_help", -] } -http = "^1.1.0" -hyper = { version = "^1.3.1", features = ["full"] } -lazy_static = "1.4.0" -reqwest = { version = "0.12.4", default-features = false, features = [ - "rustls-tls-manual-roots", - "gzip", - "deflate", - "brotli", - "stream", -] } -serde = { version = "^1.0.200", features = ["serde_derive"] } -serde_json = "^1.0.116" -thiserror = "^1.0.59" -tower = "^0.4.13" -tower-http = { version = "^0.5.2", features = [ - "tracing", - "trace", - "metrics", - "util", -] } -tracing = "^0.1.40" -tracing-subscriber = { version = "^0.3.18", features = [ - "env-filter", - "json", - "tracing-log", -] } -url = { version = "^2.5.0", features = ["serde"] } -tokio = { version = "^1.37.0", features = ["full"] } -sqlx = { version = "^0.8.0", features = [ - "runtime-tokio", - "tls-rustls", - "postgres", -] } -tokio-stream = "^0.1.15" + [dependencies] + anyhow = "^1.0.82" + aws-sdk-s3 = { version = "^1.25.0" } + axum = { version = "^0.7.5", features = ["http2", "json", "tracing"] } + clap = { version = "^4.5.4", features = [ + "derive", + "env", + "unicode", + "wrap_help", + ] } + http = "^1.1.0" + hyper = { version = "^1.3.1", features = ["full"] } + lazy_static = "1.4.0" + reqwest = { version = "0.12.4", default-features = false, features = [ + "rustls-tls-manual-roots", + "gzip", + "deflate", + "brotli", + "stream", + "socks", + ] } + serde = { version = "^1.0.200", features = ["serde_derive"] } + serde_json = "^1.0.116" + thiserror = "^1.0.59" + tower = "^0.4.13" + tower-http = { version = "^0.5.2", features = [ + "tracing", + "trace", + "metrics", + "util", + ] } + tracing = "^0.1.40" + tracing-subscriber = { version = "^0.3.18", features = [ + "env-filter", + "json", + "tracing-log", + ] } + url = { version = "^2.5.0", features = ["serde"] } + tokio = { version = "^1.37.0", features = ["full"] } + sqlx = { version = "^0.8.0", features = [ + "runtime-tokio", + "tls-rustls", + "postgres", + ] } + tokio-stream = "^0.1.15" aws-config = { version = "^1.3.0" } futures = "0.3.30" humantime = "2.1.0" diff --git a/Dockerfile b/Dockerfile index 9089b087..4df7161a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,16 @@ -FROM docker.io/library/rust:1.82@sha256:81584ce20ac0fc77ac45384c28f356cb76489e8c71998962fed0008dbe496987 -WORKDIR /usr/src/app -COPY Cargo.toml Cargo.lock . -RUN mkdir src/ && touch src/main.rs +FROM rust:1.82.0 + +WORKDIR /app + +RUN apt-get update && apt-get install -y musl-tools +RUN rustup target add x86_64-unknown-linux-musl + +RUN cargo install sqlx-cli + +COPY Cargo* . RUN cargo fetch -COPY src tests resources migrations build.rs . -RUN cargo install --path . -CMD ["terrashine"] \ No newline at end of file +COPY . . +RUN SQLX_OFFLINE=1 cargo build --release +RUN mv ./target/x86_64-unknown-linux-musl/release/terrashine /usr/bin/terrashine +ENV RUST_LOG=info +CMD ["terrashine", "server"] diff --git a/docs/src/introduction.md b/docs/src/introduction.md index 9e5889bb..97124acf 100644 --- a/docs/src/introduction.md +++ b/docs/src/introduction.md @@ -26,7 +26,7 @@ rustup target add x86_64-unknown-linux-musl SQLX_OFFLINE=1 cargo build --release ``` -Once built, the binary can be found at `./target/release/terrashine` +Once built, the binary can be found at `./target/x86_64-unknown-linux-musl/release/terrashine` ## Install ```sh diff --git a/integration/main.rs b/integration/main.rs index 42ac1d78..3f78a83c 100644 --- a/integration/main.rs +++ b/integration/main.rs @@ -32,6 +32,7 @@ fn test_server_startup(_: PoolOptions, db_options: PgConnectOptions) { http_listen: SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 0), refresh_interval: Duration::from_secs(10), upstream_registry_port: 443, + http_proxy: "", }; let cancellation_token = tokio_util::sync::CancellationToken::new(); let (tx, rx) = tokio::sync::oneshot::channel(); diff --git a/src/config.rs b/src/config.rs index f3bd2f87..5e0dffa7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -104,6 +104,12 @@ pub struct ServerArgs { hide = true )] pub upstream_registry_port: u16, + + /// Proxy for downloading registry + /// + /// The address to the proxy server. For example "socks5://127.0.0.1:9150" + #[arg(long, default_value = "", env = "TERRASHINE_HTTP_PROXY")] + pub http_proxy: String, } #[derive(clap::Args, Debug, Clone)] diff --git a/src/lib.rs b/src/lib.rs index 941d8e8d..f4fad635 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ use hyper_util::{ server, }; use migrate::run_migrate; -use reqwest::{Certificate, Client}; +use reqwest::{Certificate, Client, Proxy}; use sqlx::{postgres::PgPoolOptions, PgPool}; use std::{net::SocketAddr, time::Duration}; use tokio::{ @@ -155,6 +155,16 @@ pub async fn setup_server( http_builder = http_builder .add_root_certificate(Certificate::from_der(cert.as_ref()).expect("Not a certificate")); } + if !&config.http_proxy.is_empty() { + let proxy = match Proxy::all(&config.http_proxy) { + Ok(proxy) => proxy, + Err(error) => { + error!(reason = %error, "Could not initialize proxy, exiting."); + return Err(()); + } + }; + http_builder = http_builder.proxy(proxy); + }; let http = match http_builder.build() { Ok(client) => client, Err(error) => {