mirror of
https://github.com/sadoyan/aralez.git
synced 2026-05-30 03:44:06 +08:00
Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
15d356f087 | ||
|
|
b17962e6d5 | ||
|
|
ea779182f2 | ||
|
|
9339a142a6 | ||
|
|
cfc9bd319d | ||
|
|
a5ced59e5c | ||
|
|
c3df7bc131 | ||
|
|
0248d73836 | ||
|
|
96a5aef7d0 | ||
|
|
5c3b72b7a3 | ||
|
|
3cf0fc493f | ||
|
|
207ee481fb | ||
|
|
1c2f7327aa | ||
|
|
310a554a25 | ||
|
|
69a5167346 | ||
|
|
4734ccab2f | ||
|
|
61d65f6e4e | ||
|
|
20ac39067d | ||
|
|
7afa76de8f | ||
|
|
e29161965f | ||
|
|
9216710dda |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -7,6 +7,8 @@
|
||||
*.sh
|
||||
/docs/
|
||||
/docs
|
||||
etc/
|
||||
etc
|
||||
/target/
|
||||
*.iml
|
||||
.idea/
|
||||
|
||||
92
Cargo.lock
generated
92
Cargo.lock
generated
@@ -142,7 +142,6 @@ dependencies = [
|
||||
"jsonwebtoken",
|
||||
"log",
|
||||
"log4rs",
|
||||
"mimalloc",
|
||||
"moka",
|
||||
"notify",
|
||||
"pingora",
|
||||
@@ -161,6 +160,8 @@ dependencies = [
|
||||
"serde_yml",
|
||||
"sha2 0.11.0",
|
||||
"subtle",
|
||||
"tikv-jemalloc-ctl",
|
||||
"tikv-jemallocator",
|
||||
"tokio",
|
||||
"tonic",
|
||||
"tower-http",
|
||||
@@ -359,6 +360,15 @@ version = "1.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06"
|
||||
|
||||
[[package]]
|
||||
name = "bit-vec"
|
||||
version = "0.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b71798fca2c1fe1086445a7258a4bc81e6e49dcd24c8d0dd9a1e57395b603f51"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
@@ -1938,15 +1948,6 @@ version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981"
|
||||
|
||||
[[package]]
|
||||
name = "libmimalloc-sys"
|
||||
version = "0.1.47"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2d1eacfa31c33ec25e873c136ba5669f00f9866d0688bea7be4d3f7e43067df6"
|
||||
dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libyml"
|
||||
version = "0.0.5"
|
||||
@@ -1995,9 +1996,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.29"
|
||||
version = "0.4.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897"
|
||||
checksum = "616ec5685824bcc94416c6d4a7a446eea774a31efd7062c8480ba6fd06d7a6e5"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
]
|
||||
@@ -2073,15 +2074,6 @@ dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mimalloc"
|
||||
version = "0.1.50"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b3627c4272df786b9260cabaa46aec1d59c93ede723d4c3ef646c503816b0640"
|
||||
dependencies = [
|
||||
"libmimalloc-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mime"
|
||||
version = "0.3.17"
|
||||
@@ -2494,6 +2486,12 @@ dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "paste"
|
||||
version = "1.0.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
||||
|
||||
[[package]]
|
||||
name = "pem"
|
||||
version = "3.0.6"
|
||||
@@ -3147,9 +3145,9 @@ checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69"
|
||||
|
||||
[[package]]
|
||||
name = "rcgen"
|
||||
version = "0.14.7"
|
||||
version = "0.14.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "10b99e0098aa4082912d4c649628623db6aba77335e4f4569ff5083a6448b32e"
|
||||
checksum = "57f6d249aad744e274e682777a50283a225a32705394ee6d5fcc01efa25e4055"
|
||||
dependencies = [
|
||||
"aws-lc-rs",
|
||||
"pem",
|
||||
@@ -3200,9 +3198,9 @@ checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a"
|
||||
|
||||
[[package]]
|
||||
name = "reqwest"
|
||||
version = "0.13.3"
|
||||
version = "0.13.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "62e0021ea2c22aed41653bc7e1419abb2c97e038ff2c33d0e1309e49a97deec0"
|
||||
checksum = "219c5811de6525e5416c7d5d53bb656d3afdbc6c5af816e0802bcfa42dbdc1c3"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"bytes",
|
||||
@@ -3550,9 +3548,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.149"
|
||||
version = "1.0.150"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86"
|
||||
checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"memchr",
|
||||
@@ -3921,6 +3919,37 @@ dependencies = [
|
||||
"trackable",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tikv-jemalloc-ctl"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3a184c43b8ab2f41df2733b55556e3f5f632f4aeaa205b1bb018f574b7f5f142"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"paste",
|
||||
"tikv-jemalloc-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tikv-jemalloc-sys"
|
||||
version = "0.7.1+5.3.1-0-g81034ce1f1373e37dc865038e1bc8eeecf559ce8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1a2825c78386b4ae0314074867860ba9577875de945f05992c38815cbec327f0"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tikv-jemallocator"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "249f09e49ab1609436f34c776e84231bead18d6a955f119f939bdc1d847561bd"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"tikv-jemalloc-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.3.47"
|
||||
@@ -4111,9 +4140,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tower-http"
|
||||
version = "0.6.10"
|
||||
version = "0.6.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "68d6fdd9f81c2819c9a8b0e0cd91660e7746a8e6ea2ba7c6b2b057985f6bcb51"
|
||||
checksum = "4cfcf7e2740e6fc6d4d688b4ef00650406bb94adf4731e43c096c3a19fe40840"
|
||||
dependencies = [
|
||||
"bitflags 2.11.1",
|
||||
"bytes",
|
||||
@@ -4891,10 +4920,11 @@ checksum = "fdd20c5420375476fbd4394763288da7eb0cc0b8c11deed431a91562af7335d3"
|
||||
|
||||
[[package]]
|
||||
name = "yasna"
|
||||
version = "0.5.2"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e17bb3549cc1321ae1296b9cdc2698e2b6cb1992adfa19a8c72e5b7a738f44cd"
|
||||
checksum = "b5f6765e852b9b4dc8e2a76843e4d64d1cea8e79bcde0b6901aea8e7c7f08282"
|
||||
dependencies = [
|
||||
"bit-vec",
|
||||
"time",
|
||||
]
|
||||
|
||||
|
||||
14
Cargo.toml
14
Cargo.toml
@@ -20,11 +20,11 @@ pingora-proxy = "0.8.0"
|
||||
pingora-http = "0.8.0"
|
||||
pingora-limits = "0.8.0"
|
||||
async-trait = "0.1.89"
|
||||
log = "0.4.29"
|
||||
log = "0.4.30"
|
||||
futures = "0.3.32"
|
||||
notify = "9.0.0-rc.4"
|
||||
axum = { version = "0.8.9" }
|
||||
reqwest = { version = "0.13.3", features = ["json", "stream", "blocking"] }
|
||||
reqwest = { version = "0.13.4", features = ["json", "stream", "blocking"] }
|
||||
serde_yml = "0.0.12"
|
||||
rand = "0.10.1"
|
||||
base64 = "0.22.1"
|
||||
@@ -34,17 +34,19 @@ sha2 = { version = "0.11.0-rc.5", default-features = false }
|
||||
base16ct = { version = "1.0.0", features = ["alloc"] }
|
||||
urlencoding = "2.1.3"
|
||||
arc-swap = "1.9.1"
|
||||
mimalloc = { version = "0.1.50", default-features = false }
|
||||
prometheus = "0.14.0"
|
||||
x509-parser = "0.18.1"
|
||||
rustls-pemfile = "2.2.0"
|
||||
tower-http = { version = "0.6.10", features = ["fs"] }
|
||||
tower-http = { version = "0.6.11", features = ["fs"] }
|
||||
privdrop = "0.5.6"
|
||||
ctrlc = "3.5.2"
|
||||
serde_json = "1.0.149"
|
||||
serde_json = "1.0.150"
|
||||
subtle = "2.6.1"
|
||||
moka = { version = "0.12.15", features = ["sync"] }
|
||||
ahash = "0.8.12"
|
||||
instant-acme = "0.8.5"
|
||||
rcgen = "0.14.7"
|
||||
rcgen = "0.14.8"
|
||||
log4rs = "1.4.0"
|
||||
#mimalloc = { version = "0.1.52", default-features = false }
|
||||
tikv-jemallocator = "0.7.0"
|
||||
tikv-jemalloc-ctl = { version = "0.7.0", features = ["stats"] }
|
||||
|
||||
13
Dockerfile
13
Dockerfile
@@ -1,13 +0,0 @@
|
||||
FROM debian:trixie-slim
|
||||
|
||||
RUN apt-get update && apt-get install -y ca-certificates curl net-tools iputils-ping
|
||||
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY aralez /usr/local/bin/aralez
|
||||
|
||||
RUN chmod +x /usr/local/bin/aralez
|
||||
RUN mkdir -p /etc/aralez/certs/upstreams
|
||||
|
||||
WORKDIR /etc/aralez
|
||||
|
||||
ENTRYPOINT ["/usr/local/bin/aralez", "-c", "/etc/aralez/main.yaml"]
|
||||
22
README.md
22
README.md
@@ -113,6 +113,24 @@ For getting the best performance on newer hardware use `aralez-x86_64-*.gz`.
|
||||
docker run -d -v /path/to/config:/etc/aralez:rw -p 80:80 -p 443:443 sadoyan/aralez
|
||||
```
|
||||
|
||||
**Dockerfile :**
|
||||
|
||||
```dockerfile
|
||||
FROM debian:trixie-slim
|
||||
|
||||
RUN apt-get update && apt-get install -y ca-certificates curl net-tools iputils-ping
|
||||
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY aralez /usr/local/bin/aralez
|
||||
|
||||
RUN chmod +x /usr/local/bin/aralez
|
||||
RUN mkdir -p /etc/aralez/certs/upstreams
|
||||
|
||||
WORKDIR /etc/aralez
|
||||
|
||||
ENTRYPOINT ["/usr/local/bin/aralez", "-c", "/etc/aralez/main.yaml"]
|
||||
```
|
||||
|
||||
## Running the Proxy
|
||||
|
||||
```bash
|
||||
@@ -121,7 +139,7 @@ docker run -d -v /path/to/config:/etc/aralez:rw -p 80:80 -p 443:443 sadoyan/aral
|
||||
|
||||
## Systemd integration
|
||||
|
||||
Assuming Arales in installed in `/opt/aralez` folder
|
||||
Assuming Aralez in installed in `/opt/aralez` folder
|
||||
|
||||
```bash
|
||||
cat > /etc/systemd/system/aralez.service <<EOF
|
||||
@@ -209,7 +227,7 @@ DEFAULT:
|
||||
|
||||
**This means:**
|
||||
|
||||
- Sticky sessions are enabled globally. This setting applies to all upstreams. If enabled the value withh be set for `Max-Age=` cookie.
|
||||
- Sticky sessions are enabled globally. This setting applies to all upstreams. If enabled the value will be set for `Max-Age=` cookie.
|
||||
- HTTP to HTTPS redirect disabled globally, but can be overridden by `to_https` setting per upstream.
|
||||
- All upstreams will receive custom headers : `X-Forwarded-Proto:https` and `X-Forwarded-Port:443`
|
||||
- Additionally, myhost.mydomain.com with path `/` will receive custom headers : `X-Another-Header:Hohohohoho` and `X-Something-Else:Foobar`
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# Main configuration file, applied on startup
|
||||
threads: 12 # Number of daemon threads default setting
|
||||
runuser: aralez # Username for running aralez after dropping root privileges, requires program to start as root
|
||||
rungroup: aralez # Group for running aralez after dropping root privileges, requires program to start as root
|
||||
#runuser: pastor # Username for running aralez after dropping root privileges, requires program to start as root
|
||||
#rungroup: pastor # Group for running aralez after dropping root privileges, requires program to start as root
|
||||
daemon: false # Run in background
|
||||
upstream_keepalive_pool_size: 500 # Pool size for upstream keepalive connections
|
||||
pid_file: /tmp/aralez.pid # Path to PID file
|
||||
@@ -11,12 +11,13 @@ config_api_enabled: true # Boolean to enable/disable remote config push capabili
|
||||
config_address: 0.0.0.0:3000 # HTTP API address for pushing upstreams.yaml from remote location
|
||||
proxy_address_http: 0.0.0.0:6193 # Proxy HTTP bind address
|
||||
proxy_address_tls: 0.0.0.0:6194 # Optional, Proxy TLS bind address
|
||||
proxy_configs: /opt/aralez/etc # Mandatory if proxy_address_tls set, should contain a certificate and key files strictly in a format {NAME}.crt, {NAME}.key.
|
||||
proxy_tls_grade: a+ # Grade of TLS suite for proxy (a+, a, b, c, unsafe), matching grades of Qualys SSL Labs
|
||||
upstreams_conf: /opt/aralez/etc/upstreams.yaml # the location of upstreams file
|
||||
file_server_folder: /opt/storage # Optional, local folder to serve
|
||||
file_server_address: 127.0.0.1:3002 # Optional, Local address for file server. Can set as upstream for public access.
|
||||
proxy_configs: /opt/Rust/Projects/asyncweb/etc # Mandatory if proxy_address_tls set, should contain a certificate and key files strictly in a format {NAME}.crt, {NAME}.key.
|
||||
proxy_tls_grade: high # Grade of TLS suite for proxy (high, medium, unsafe), matching grades of Qualys SSL Labs
|
||||
upstreams_conf: /opt/Rust/Projects/asyncweb/etc/upstreams.yaml # the location of upstreams file
|
||||
#file_server_folder: /opt/storage # Optional, local folder to serve
|
||||
#file_server_address: 127.0.0.1:3002 # Optional, Local address for file server. Can set as upstream for public access.
|
||||
log_level: info # info, warn, error, debug, trace, off
|
||||
log_file: /tmp/aralez.log # Optional, the location of log file. If this entry does not exist logs will be emitted to stdout.
|
||||
hc_method: HEAD # Healthcheck method (HEAD, GET, POST are supported) UPPERCASE
|
||||
hc_interval: 2 #Interval for health checks in seconds
|
||||
master_key: 910517d9-f9a1-48de-8826-dbadacbd84af-cb6f830e-ab16-47ec-9d8f-0090de732774 # Mater key for working with API server and JWT Secret
|
||||
#master_key: 910517d9-f9a1-48de-8826-dbadacbd84af-cb6f830e-ab16-47ec-9d8f-0090de732774 # Mater key for working with API server and JWT Secret
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
# The file under watch and hot reload, changes are applied immediately, no need to restart or reload.
|
||||
provider: "file" # "file" "consul" "kubernetes"
|
||||
sticky_sessions: false
|
||||
sticky_sessions: 8600
|
||||
to_https: false
|
||||
rate_limit: 100
|
||||
rate_limit: 300
|
||||
x4xx_limit: 200
|
||||
server_headers:
|
||||
- "X-Forwarded-Proto:https"
|
||||
- "X-Forwarded-Port:443"
|
||||
@@ -62,6 +63,7 @@ upstreams:
|
||||
paths:
|
||||
"/":
|
||||
rate_limit: 200
|
||||
x4xx_limit: 100
|
||||
to_https: false
|
||||
client_headers:
|
||||
- "X-Proxy-From:Aralez"
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
use tikv_jemallocator::Jemalloc;
|
||||
|
||||
mod tls;
|
||||
mod utils;
|
||||
mod web;
|
||||
|
||||
#[global_allocator]
|
||||
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
||||
static ALLOC: Jemalloc = Jemalloc;
|
||||
// static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
||||
// pub static A: CountingAllocator = CountingAllocator;
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -53,15 +53,14 @@ async fn build_upstreams(fullist: &UpstreamsDashMap, method: &str, client: &Clie
|
||||
let mut innervec = Vec::new();
|
||||
|
||||
for upstream in path_entry.value().0.iter() {
|
||||
let tls = detect_tls(upstream.address.as_ref(), &upstream.port, client).await;
|
||||
let is_h2 = matches!(tls.1, Some(Version::HTTP_2));
|
||||
|
||||
let link = if tls.0 {
|
||||
format!("https://{}:{}{}", upstream.address, upstream.port, path)
|
||||
let tls = if upstream.healthcheck.unwrap_or(true) {
|
||||
detect_tls(upstream.address.as_ref(), &upstream.port, client).await
|
||||
} else {
|
||||
format!("http://{}:{}{}", upstream.address, upstream.port, path)
|
||||
(false, None)
|
||||
};
|
||||
|
||||
let is_h2 = matches!(tls.1, Some(Version::HTTP_2));
|
||||
|
||||
let mut scheme = InnerMap {
|
||||
address: upstream.address.clone(),
|
||||
port: upstream.port,
|
||||
@@ -76,6 +75,12 @@ async fn build_upstreams(fullist: &UpstreamsDashMap, method: &str, client: &Clie
|
||||
};
|
||||
|
||||
if scheme.healthcheck.unwrap_or(true) {
|
||||
let link = if tls.0 {
|
||||
format!("https://{}:{}{}", upstream.address, upstream.port, path)
|
||||
} else {
|
||||
format!("http://{}:{}{}", upstream.address, upstream.port, path)
|
||||
};
|
||||
|
||||
let resp = http_request(&link, method, "", client).await;
|
||||
if resp.0 {
|
||||
if resp.1 {
|
||||
|
||||
@@ -222,7 +222,7 @@ async fn clone_compare(upstreams: &UpstreamsDashMap, prev_upstreams: &UpstreamsD
|
||||
};
|
||||
clone_dashmap_into(upstreams, prev_upstreams);
|
||||
clone_dashmap_into(upstreams, &tosend.upstreams);
|
||||
print_upstreams(&tosend.upstreams);
|
||||
print_upstreams(&tosend.upstreams, &tosend.extraparams);
|
||||
return Some(tosend);
|
||||
};
|
||||
None
|
||||
|
||||
@@ -5,6 +5,7 @@ use prometheus::{register_histogram, register_int_counter, register_int_counter_
|
||||
use std::sync::Arc;
|
||||
use std::sync::LazyLock;
|
||||
use std::time::Duration;
|
||||
use tikv_jemalloc_ctl::{epoch, stats};
|
||||
|
||||
pub struct MetricTypes {
|
||||
pub method: Method,
|
||||
@@ -14,18 +15,28 @@ pub struct MetricTypes {
|
||||
pub version: Version,
|
||||
}
|
||||
|
||||
pub static OPEN_FILES: LazyLock<IntGauge> = LazyLock::new(|| register_int_gauge!("aralez_open_files", "Number of open file descriptors").unwrap());
|
||||
pub static MEMORY_USAGE: LazyLock<IntGauge> = LazyLock::new(|| register_int_gauge!("aralez_memory_bytes", "Total memory allocated in bytes").unwrap());
|
||||
pub static ACTIVE_SESSIONS: LazyLock<IntGauge> = LazyLock::new(|| register_int_gauge!("aralez_active_sessions", "Current number of active sessions").unwrap());
|
||||
|
||||
pub static REQUEST_COUNT: LazyLock<IntCounter> = LazyLock::new(|| register_int_counter!("aralez_requests_total", "Total number of requests handled by Aralez").unwrap());
|
||||
|
||||
pub static RESPONSE_CODES: LazyLock<IntCounterVec> =
|
||||
LazyLock::new(|| register_int_counter_vec!("aralez_responses_total", "Responses grouped by status code", &["status"]).unwrap());
|
||||
|
||||
// pub static RESPONSE_LATENCY: LazyLock<Histogram> = LazyLock::new(|| {
|
||||
// register_histogram!(
|
||||
// "aralez_response_latency_seconds",
|
||||
// "Response latency in seconds",
|
||||
// vec![0.01, 0.05, 0.1, 0.25, 0.5, 1.0, 2.0, 5.0]
|
||||
// )
|
||||
// .unwrap()
|
||||
// });
|
||||
|
||||
pub static RESPONSE_LATENCY: LazyLock<Histogram> = LazyLock::new(|| {
|
||||
register_histogram!(
|
||||
"aralez_response_latency_seconds",
|
||||
"Response latency in seconds",
|
||||
vec![0.01, 0.05, 0.1, 0.25, 0.5, 1.0, 2.0, 5.0]
|
||||
vec![0.001, 0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 1.0, 2.0, 5.0]
|
||||
)
|
||||
.unwrap()
|
||||
});
|
||||
@@ -54,3 +65,12 @@ pub fn calc_metrics(metric_types: &MetricTypes) {
|
||||
REQUESTS_BY_UPSTREAM.with_label_values(&[metric_types.upstream.as_ref()]).inc();
|
||||
RESPONSE_LATENCY.observe(metric_types.latency.as_secs_f64());
|
||||
}
|
||||
|
||||
pub fn get_memory_usage() -> usize {
|
||||
epoch::mib().unwrap().advance().unwrap(); // refresh stats
|
||||
stats::allocated::mib().unwrap().read().unwrap() // bytes allocated
|
||||
}
|
||||
|
||||
pub fn get_open_files() -> usize {
|
||||
std::fs::read_dir("/proc/self/fd").map(|dir| dir.count()).unwrap_or(0)
|
||||
}
|
||||
|
||||
@@ -21,57 +21,82 @@ pub static DOMAINS: LazyLock<DashMap<String, bool>> = LazyLock::new(DashMap::new
|
||||
pub async fn load_configuration(d: &str, kind: &str) -> (Option<Configuration>, String) {
|
||||
let mut conf_files = Vec::new();
|
||||
let yaml_data = match kind {
|
||||
"filepath" => match fs::read_to_string(d) {
|
||||
Ok(data) => {
|
||||
let mut confdir = Path::new(d).parent().unwrap().to_path_buf();
|
||||
let mut autocfg = Path::new(d).parent().unwrap().to_path_buf();
|
||||
|
||||
autocfg.push("autoconfigs");
|
||||
if fs::metadata(autocfg.clone()).is_err() {
|
||||
fs::create_dir_all(autocfg.clone()).ok();
|
||||
}
|
||||
autocfg.push("domains.json");
|
||||
if autocfg.exists() {
|
||||
let json: Option<Vec<String>> = fs::read_to_string(autocfg).ok().and_then(|s| serde_json::from_str(&s).ok());
|
||||
if let Some(domains) = json {
|
||||
for domain in domains {
|
||||
DOMAINS.insert(domain, true);
|
||||
"filepath" => {
|
||||
let mut data = String::new();
|
||||
let mut last_error = None;
|
||||
for _ in 0..5 {
|
||||
match fs::read_to_string(d) {
|
||||
Ok(content) => {
|
||||
if !content.trim().is_empty() {
|
||||
data = content;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
confdir.push("conf.d");
|
||||
|
||||
if let Ok(entries) = fs::read_dir(&confdir) {
|
||||
let mut paths: Vec<_> = entries
|
||||
.flatten()
|
||||
.map(|e| e.path())
|
||||
.filter(|p| p.extension().and_then(|e| e.to_str()) == Some("yaml"))
|
||||
.collect();
|
||||
paths.sort();
|
||||
|
||||
for path in paths {
|
||||
let content = fs::read_to_string(&path);
|
||||
match content {
|
||||
Ok(content) => {
|
||||
conf_files.push(content);
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Reading: {}: {:?}", path.display(), e)
|
||||
}
|
||||
};
|
||||
Err(e) => {
|
||||
error!("Config read failed, retrying...");
|
||||
last_error = Some(e);
|
||||
}
|
||||
}
|
||||
|
||||
info!("Reading upstreams from {}", d);
|
||||
data
|
||||
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Reading: {}: {:?}", d, e);
|
||||
if data.is_empty() {
|
||||
let err_msg = match last_error {
|
||||
Some(e) => {
|
||||
error!("Reading: {}: {:?}", d, e);
|
||||
e.to_string()
|
||||
}
|
||||
None => {
|
||||
error!("Reading: {}: File is empty after retries", d);
|
||||
"File is empty".to_string()
|
||||
}
|
||||
};
|
||||
warn!("Running with empty upstreams list, update it via API");
|
||||
return (None, e.to_string());
|
||||
return (None, err_msg);
|
||||
}
|
||||
},
|
||||
|
||||
let mut confdir = Path::new(d).parent().unwrap().to_path_buf();
|
||||
let mut autocfg = Path::new(d).parent().unwrap().to_path_buf();
|
||||
|
||||
autocfg.push("autoconfigs");
|
||||
if fs::metadata(autocfg.clone()).is_err() {
|
||||
fs::create_dir_all(autocfg.clone()).ok();
|
||||
}
|
||||
autocfg.push("domains.json");
|
||||
if autocfg.exists() {
|
||||
let json: Option<Vec<String>> = fs::read_to_string(autocfg).ok().and_then(|s| serde_json::from_str(&s).ok());
|
||||
if let Some(domains) = json {
|
||||
for domain in domains {
|
||||
DOMAINS.insert(domain, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
confdir.push("conf.d");
|
||||
|
||||
if let Ok(entries) = fs::read_dir(&confdir) {
|
||||
let mut paths: Vec<_> = entries
|
||||
.flatten()
|
||||
.map(|e| e.path())
|
||||
.filter(|p| p.extension().and_then(|e| e.to_str()) == Some("yaml"))
|
||||
.collect();
|
||||
paths.sort();
|
||||
|
||||
for path in paths {
|
||||
let content = fs::read_to_string(&path);
|
||||
match content {
|
||||
Ok(content) => {
|
||||
conf_files.push(content);
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Reading: {}: {:?}", path.display(), e)
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
info!("Reading upstreams from {}", d);
|
||||
data
|
||||
}
|
||||
"content" => {
|
||||
info!("Reading upstreams from API post body");
|
||||
d.to_string()
|
||||
@@ -233,7 +258,7 @@ async fn populate_file_upstreams(config: &mut Configuration, parsed: &Config) {
|
||||
clone_dashmap_into(&r, &config.upstreams);
|
||||
}
|
||||
info!("Upstream Config:");
|
||||
print_upstreams(&config.upstreams);
|
||||
print_upstreams(&config.upstreams, &config.extraparams);
|
||||
}
|
||||
}
|
||||
pub fn parce_main_config(path: &str) -> AppConfig {
|
||||
@@ -320,7 +345,8 @@ fn log_builder(conf: &AppConfig, location: &Option<String>) {
|
||||
LevelFilter::Info
|
||||
}
|
||||
};
|
||||
let pattern = "{d(%Y-%m-%d %H:%M:%S)} {l} {t} - {m}{n}";
|
||||
// let pattern = "{d(%Y-%m-%d %H:%M:%S)} {l} {t} - {m}{n}";
|
||||
let pattern = "{d(%Y-%m-%d %H:%M:%S)} {l} {t} - {m}\n";
|
||||
if let Some(location) = location {
|
||||
let file = FileAppender::builder().encoder(Box::new(PatternEncoder::new(pattern))).build(location).unwrap();
|
||||
let config = Log4rsConfig::builder()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::tls::load;
|
||||
use crate::tls::load::CertificateConfig;
|
||||
use crate::utils::structs::{InnerMap, InnerMapForJson, UpstreamSnapshotForJson, UpstreamsDashMap, UpstreamsIdMap};
|
||||
use crate::utils::structs::{InnerMap, InnerMapForJson, Extraparams, UpstreamSnapshotForJson, UpstreamsDashMap, UpstreamsIdMap};
|
||||
use dashmap::DashMap;
|
||||
use log::{error, info};
|
||||
use notify::{event::ModifyKind, Config, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
|
||||
@@ -20,7 +20,7 @@ use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
use std::{fs, process, thread, time};
|
||||
|
||||
pub fn print_upstreams(upstreams: &UpstreamsDashMap) {
|
||||
pub fn print_upstreams(upstreams: &UpstreamsDashMap, extraparams: &Extraparams) {
|
||||
let mut out = String::new();
|
||||
for host_entry in upstreams.iter() {
|
||||
writeln!(out, "Hostname: {}", host_entry.key()).unwrap();
|
||||
@@ -35,8 +35,8 @@ pub fn print_upstreams(upstreams: &UpstreamsDashMap) {
|
||||
f.is_ssl,
|
||||
f.is_http2,
|
||||
f.to_https,
|
||||
f.rate_limit.unwrap_or(0),
|
||||
f.x4xx_limit.unwrap_or(0)
|
||||
f.rate_limit.unwrap_or(extraparams.rate_limit.unwrap_or(0)),
|
||||
f.x4xx_limit.unwrap_or(extraparams.x4xx_limit.unwrap_or(0))
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ impl ProxyHttp for LB {
|
||||
session.set_keepalive(None);
|
||||
session.write_response_header(Box::new(header), true).await?;
|
||||
if let (Some(oi), Some(oa)) = (&_ctx.hostname, rate_key) {
|
||||
warn!("Limit 4XX: {}-rps exceed on {} from {}", rate, oi, oa);
|
||||
warn!("Limit 4XX: {}-rps exceed on {} from {} path {}", rate, oi, oa, session.req_header().uri.path());
|
||||
}
|
||||
return Ok(true);
|
||||
}
|
||||
@@ -229,11 +229,11 @@ impl ProxyHttp for LB {
|
||||
}
|
||||
|
||||
async fn upstream_request_filter(&self, session: &mut Session, upstream_request: &mut RequestHeader, ctx: &mut Self::CTX) -> Result<()> {
|
||||
if let Some(client_ip) = session.client_addr() {
|
||||
if let Some(ip) = session.client_addr().and_then(|a| a.as_inet()).map(|i| i.ip()) {
|
||||
IP_BUFFER.with(|buffer| {
|
||||
let mut buf = buffer.borrow_mut();
|
||||
buf.clear();
|
||||
write!(buf, "{}", client_ip).unwrap_or(());
|
||||
write!(buf, "{}", ip).unwrap_or(());
|
||||
upstream_request.append_header("X-Forwarded-For", buf.as_str()).unwrap_or(false);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
// use std::net::SocketAddr;
|
||||
use crate::tls::acme::order::CHALLENGES;
|
||||
// use axum_server::tls_openssl::OpenSSLConfig;
|
||||
use crate::tls::acme::{account, order};
|
||||
use crate::utils::discovery::APIUpstreamProvider;
|
||||
use crate::utils::jwt::Claims;
|
||||
use crate::utils::metrics::{get_memory_usage, get_open_files, MEMORY_USAGE, OPEN_FILES};
|
||||
use crate::utils::structs::{Config, Configuration, UpstreamsDashMap};
|
||||
use crate::utils::tools::{upstreams_liveness_json, upstreams_to_json};
|
||||
use axum::body::Body;
|
||||
@@ -56,11 +55,6 @@ pub async fn run_server(config: &APIUpstreamProvider, mut to_return: Sender<Conf
|
||||
};
|
||||
let app = Router::new()
|
||||
// .route("/{*wildcard}", get(senderror))
|
||||
// .route("/{*wildcard}", post(senderror))
|
||||
// .route("/{*wildcard}", put(senderror))
|
||||
// .route("/{*wildcard}", head(senderror))
|
||||
// .route("/{*wildcard}", delete(senderror))
|
||||
// .nest_service("/static", static_files)
|
||||
.route("/jwt", post(jwt_gen))
|
||||
.route("/acme_create", any(acme_create))
|
||||
.route("/acme_order/{*domain}", any(acme_order))
|
||||
@@ -69,19 +63,6 @@ pub async fn run_server(config: &APIUpstreamProvider, mut to_return: Sender<Conf
|
||||
.route("/metrics", get(metrics))
|
||||
.route("/status", get(status))
|
||||
.with_state(app_state);
|
||||
|
||||
// if let Some(value) = &config.tls_address {
|
||||
// let cf = OpenSSLConfig::from_pem_file(config.tls_certificate.clone().unwrap(), config.tls_key_file.clone().unwrap()).unwrap();
|
||||
// let addr: SocketAddr = value.parse().expect("Unable to parse socket address");
|
||||
// let tls_app = app.clone();
|
||||
// tokio::spawn(async move {
|
||||
// if let Err(e) = axum_server::bind_openssl(addr, cf).serve(tls_app.into_make_service()).await {
|
||||
// eprintln!("TLS server failed: {}", e);
|
||||
// }
|
||||
// });
|
||||
// info!("Starting the TLS API server on: {}", value);
|
||||
// }
|
||||
|
||||
if let (Some(address), Some(folder)) = (&config.file_server_address, &config.file_server_folder) {
|
||||
let static_files = ServeDir::new(folder);
|
||||
let static_serve: Router = Router::new().fallback_service(static_files);
|
||||
@@ -171,6 +152,9 @@ async fn jwt_gen(State(state): State<AppState>, Json(payload): Json<Claims>) ->
|
||||
}
|
||||
|
||||
async fn metrics() -> impl IntoResponse {
|
||||
MEMORY_USAGE.set(get_memory_usage() as i64);
|
||||
OPEN_FILES.set(get_open_files() as i64);
|
||||
|
||||
let metric_families = gather();
|
||||
let encoder = TextEncoder::new();
|
||||
let mut buffer = Vec::new();
|
||||
@@ -281,13 +265,4 @@ pub async fn http01_challenge(axum::extract::Path(token): axum::extract::Path<St
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
// fn key_authorization(headers: &HeaderMap, params: &HashMap<String, String>, masterkey: &str) -> bool {
|
||||
// if let Some(s) = headers.get("x-api-key").and_then(|v| v.to_str().ok()).or(params.get("key").map(|s| s.as_str())) {
|
||||
// if s.as_bytes().ct_eq(masterkey.as_bytes()).into() {
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
// false
|
||||
// }
|
||||
|
||||
// -- ⚝ by Dave -- in NeoVim ⚝ --
|
||||
|
||||
Reference in New Issue
Block a user