Skip to content

Commit 5ce5188

Browse files
committed
Make port numbers configurable
1 parent 768df49 commit 5ce5188

8 files changed

Lines changed: 41 additions & 20 deletions

File tree

Cargo.toml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2024"
66
[dependencies]
77
anyhow = "1.0"
88
axum = { version = "0.8", features = ["macros", "ws"] }
9-
axum-extra = { version = "0.10", default-features = false, features = [
9+
axum-extra = { version = "0.12", default-features = false, features = [
1010
"cookie-private",
1111
] }
1212
bytes = "1.8"
@@ -19,16 +19,13 @@ hyper = { version = "1.0", features = ["full"] }
1919
hyper-util = { version = "0.1", features = ["client-legacy"] }
2020
oauth2 = "4.4"
2121
parking_lot = "0.12"
22-
rand = "0.9"
23-
reqwest = { version = "0.12", default-features = false, features = [
24-
"json",
25-
"rustls-tls-webpki-roots",
26-
] }
22+
rand = "0.10"
23+
reqwest = { version = "0.13", default-features = false, features = ["json"] }
2724
serde = { version = "1.0", features = ["derive"] }
2825
serde_json = "1.0"
2926
sha2 = "0.10"
3027
structstruck = "0.5"
31-
sysinfo = "0.37"
28+
sysinfo = "0.38"
3229
tokio = { version = "1", features = ["full", "rt-multi-thread"] }
3330
tokio-util = "0.7"
3431
tracing = "0.1"

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ Environment variables overwrite any options from the configuration file and shou
4747
- `words`: List of words to combine into a unique service name
4848
- `admins`: Github user names / handles of admins
4949

50+
### Optional configuration values
51+
52+
- `max_services`: Maximum number of concurrent services (default: 1000)
53+
- `server_port`: Port for the main HTTP server (default: 3000)
54+
- `proxy_port`: Port for the proxy server (default: 3001)
55+
5056
An example configuration file can be found in this repository.
5157

5258
## Uploading a binary
@@ -78,7 +84,7 @@ GitHub action example:
7884
7985
## Configure reverse proxy for Etes
8086
81-
A reverse proxy that terminates TLS connections should be configured. The base domain should point to port 3000 and all sub-domains should point to port 3001.
87+
A reverse proxy that terminates TLS connections should be configured. The base domain should point to `server_port` (default 3000) and all sub-domains should point to `proxy_port` (default 3001).
8288

8389
Example using caddy:
8490

config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ github_token = "a-personal-access-token"
33
github_owner = "tweedegolf"
44
github_repo = "etes"
55
max_services = 10
6+
server_port = 3000
7+
proxy_port = 3001
68
github_client_id = "some-oauth-client-id"
79
github_client_secret = "some-oauth-secret"
810
authorize_url = "https://example.com/etes/authorize"

src/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ pub struct Config {
3434
pub admins: Vec<String>,
3535
// Maximum number of concurrent services
3636
pub max_services: usize,
37+
// Port for the main HTTP server
38+
pub server_port: u16,
39+
// Port for the proxy server
40+
pub proxy_port: u16,
3741
}
3842

3943
impl Config {
@@ -42,6 +46,8 @@ impl Config {
4246

4347
let config: Config = config::Config::builder()
4448
.set_default("max_services", 1000)?
49+
.set_default("server_port", 3000)?
50+
.set_default("proxy_port", 3001)?
4551
.add_source(config::File::with_name(&config_file))
4652
.add_source(
4753
config::Environment::with_prefix("etes")

src/main.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,12 @@ async fn main() -> Result<()> {
165165

166166
let proxy_app: Router = Router::new()
167167
.fallback(any(proxy::handler))
168-
.with_state(state);
168+
.with_state(state.clone());
169169

170-
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await?;
171-
let proxy_listener = tokio::net::TcpListener::bind("127.0.0.1:3001").await?;
170+
let listener_addr = format!("127.0.0.1:{}", state.config.server_port);
171+
let proxy_listener_addr = format!("127.0.0.1:{}", state.config.proxy_port);
172+
let listener = tokio::net::TcpListener::bind(&listener_addr).await?;
173+
let proxy_listener = tokio::net::TcpListener::bind(&proxy_listener_addr).await?;
172174

173175
info!(
174176
"Starting server on {} and {}",

src/monitor.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use parking_lot::RwLock;
22
use serde::Serialize;
33
use std::sync::Arc;
44
use sysinfo::System;
5+
use tracing::error;
56

6-
use crate::{AppState, events::Event};
7+
use crate::{AppState, events::Event, executable};
78

89
#[derive(Clone, Serialize)]
910
pub struct MemoryState {
@@ -38,11 +39,19 @@ impl SystemMonitor {
3839
// Send regular updates to the event manager and thereby the connected clients
3940
pub async fn send_updates(state: AppState) {
4041
let mut system = System::new_all();
42+
let last_cleanup = std::time::Instant::now();
4143

4244
loop {
45+
// if the last cleanup was more than a day ago, run cleanup
46+
if last_cleanup.elapsed().as_secs() > 24 * 60 * 60
47+
&& let Err(e) = executable::remove_unused_executables(state.clone()).await
48+
{
49+
error!("Failed to remove unused executables: {e:?}");
50+
}
51+
4352
system.refresh_all();
4453

45-
// Store
54+
// Store memory state
4655
state
4756
.monitor
4857
.update(system.used_memory(), system.total_memory());

src/proxy.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use anyhow::{Context, anyhow};
22
use axum::{
3-
RequestExt,
43
extract::{Request, State},
4+
http::header::HOST,
55
response::{Html, IntoResponse, Redirect, Response},
66
};
7-
use axum_extra::extract::Host;
87
use hyper::{StatusCode, Uri};
98

109
use crate::{
@@ -51,17 +50,17 @@ pub async fn handler(
5150
mut req: Request,
5251
) -> Result<Response, AppError> {
5352
let host = req
54-
.extract_parts::<Host>()
55-
.await
53+
.headers()
54+
.get(HOST)
55+
.and_then(|v| v.to_str().ok())
5656
.context("No request host found")?;
5757

5858
let subdomain = host
59-
.0
6059
.split('.')
6160
.next()
6261
.context("Could not determine subdomain")?;
6362

64-
let domain = host.0.split('.').skip(1).collect::<Vec<&str>>().join(".");
63+
let domain = host.split('.').skip(1).collect::<Vec<&str>>().join(".");
6564

6665
if is_valid_hash(subdomain) {
6766
let user = User::from_request(random_string(), user)?;

src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rand::{Rng, distr::Alphanumeric};
1+
use rand::{RngExt, distr::Alphanumeric};
22
use sha2::Digest;
33
use tokio::net::TcpListener;
44

0 commit comments

Comments
 (0)