aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: f84d97329e178764db18071a0aae2522c6fb9630 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#[macro_use]
extern crate anyhow;

use log::*;
use std::collections::BTreeMap;
use std::sync::Arc;
use std::time::Instant;

use futures::{FutureExt, TryFutureExt};
use std::net::SocketAddr;
use structopt::StructOpt;
use tokio::select;
use tokio::sync::watch;

mod cert;
mod cert_store;
mod http;
mod https;
mod metrics;
mod proxy_config;
mod reverse_proxy;
mod tls_util;

pub use df_consul as consul;
use proxy_config::ProxyConfig;

#[cfg(feature = "dhat-heap")]
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;

#[derive(StructOpt, Debug)]
#[structopt(name = "tricot")]
struct Opt {
	/// Address of consul server
	#[structopt(
		long = "consul-addr",
		env = "TRICOT_CONSUL_HOST",
		default_value = "http://127.0.0.1:8500"
	)]
	pub consul_addr: String,

	/// CA certificate for Consul server with TLS
	#[structopt(long = "consul-ca-cert", env = "TRICOT_CONSUL_CA_CERT")]
	pub consul_ca_cert: Option<String>,

	/// Skip TLS verification for Consul
	#[structopt(long = "consul-tls-skip-verify", env = "TRICOT_CONSUL_TLS_SKIP_VERIFY")]
	pub consul_tls_skip_verify: bool,

	/// Client certificate for Consul server with TLS
	#[structopt(long = "consul-client-cert", env = "TRICOT_CONSUL_CLIENT_CERT")]
	pub consul_client_cert: Option<String>,

	/// Client key for Consul server with TLS
	#[structopt(long = "consul-client-key", env = "TRICOT_CONSUL_CLIENT_KEY")]
	pub consul_client_key: Option<String>,

	/// Prefix of Tricot's entries in Consul KV space
	#[structopt(
		long = "consul-kv-prefix",
		env = "TRICOT_CONSUL_KV_PREFIX",
		default_value = "tricot/"
	)]
	pub consul_kv_prefix: String,

	/// Node name
	#[structopt(long = "node-name", env = "TRICOT_NODE_NAME", default_value = "<none>")]
	pub node_name: String,

	/// Bind address for HTTP server
	#[structopt(
		long = "http-bind-addr",
		env = "TRICOT_HTTP_BIND_ADDR",
		default_value = "0.0.0.0:80"
	)]
	pub http_bind_addr: SocketAddr,

	/// Bind address for HTTPS server
	#[structopt(
		long = "https-bind-addr",
		env = "TRICOT_HTTPS_BIND_ADDR",
		default_value = "0.0.0.0:443"
	)]
	pub https_bind_addr: SocketAddr,

	/// Bind address for metrics server (Prometheus format over HTTP)
	#[structopt(long = "metrics-bind-addr", env = "TRICOT_METRICS_BIND_ADDR")]
	pub metrics_bind_addr: Option<SocketAddr>,

	/// E-mail address for Let's Encrypt certificate requests
	#[structopt(long = "letsencrypt-email", env = "TRICOT_LETSENCRYPT_EMAIL")]
	pub letsencrypt_email: String,

	/// Enable compression of responses
	#[structopt(long = "enable-compression", env = "TRICOT_ENABLE_COMPRESSION")]
	pub enable_compression: bool,

	/// Mime types for which to enable compression (comma-separated list)
	#[structopt(
		long = "compress-mime-types",
		env = "TRICOT_COMPRESS_MIME_TYPES",
		default_value = "text/html,text/plain,text/css,text/javascript,text/xml,application/javascript,application/json,application/xml,image/svg+xml,font/ttf"
	)]
	pub compress_mime_types: String,

	#[structopt(
		long = "warmup-cert-memory-store",
		env = "TRICOT_WARMUP_CERT_MEMORY_STORE"
	)]
	pub warmup_cert_memory_store: bool,
}

#[tokio::main(flavor = "multi_thread", worker_threads = 10)]
async fn main() {
	#[cfg(feature = "dhat-heap")]
	let _profiler = dhat::Profiler::new_heap();

	if std::env::var("RUST_LOG").is_err() {
		std::env::set_var("RUST_LOG", "tricot=info")
	}
	pretty_env_logger::init();

	// Abort on panic (same behavior as in Go)
	std::panic::set_hook(Box::new(|panic_info| {
		error!("{}", panic_info.to_string());
		std::process::abort();
	}));

	let opt = Opt::from_args();

	info!("Starting Tricot");

	let (exit_signal, provoke_exit) = watch_ctrl_c();
	let exit_on_err = move |err: anyhow::Error| {
		error!("Error: {}", err);
		let _ = provoke_exit.send(true);
	};

	let metrics_server = metrics::MetricsServer::init(opt.metrics_bind_addr);

	let consul_config = consul::Config {
		addr: opt.consul_addr.clone(),
		ca_cert: opt.consul_ca_cert.clone(),
		tls_skip_verify: opt.consul_tls_skip_verify,
		client_cert: opt.consul_client_cert.clone(),
		client_key: opt.consul_client_key.clone(),
	};

	let consul = consul::Consul::new(consul_config, &opt.consul_kv_prefix)
		.expect("Error creating Consul client");
	let rx_proxy_config = proxy_config::spawn_proxy_config_task(
		consul.clone(),
		opt.node_name.clone(),
		exit_signal.clone(),
	);

	let cert_store = cert_store::CertStore::new(
		consul.clone(),
		opt.node_name.clone(),
		rx_proxy_config.clone(),
		opt.letsencrypt_email.clone(),
		exit_on_err.clone(),
	);
	if opt.warmup_cert_memory_store {
		match cert_store.warmup_memory_store().await {
            Err(e) => error!("An error occured while warming up the certificate memory store with Consul data, continue without caching: {e}"),
            _ => (),
        };
	}

	let metrics_task = tokio::spawn(
		metrics_server
			.run(wait_from(exit_signal.clone()))
			.map_err(exit_on_err.clone())
			.then(|_| async { info!("Metrics server exited") }),
	);

	let http_task = tokio::spawn(
		http::serve_http(
			opt.http_bind_addr,
			consul.clone(),
			wait_from(exit_signal.clone()),
		)
		.map_err(exit_on_err.clone())
		.then(|_| async { info!("HTTP server exited") }),
	);

	let https_config = https::HttpsConfig {
		bind_addr: opt.https_bind_addr,
		enable_compression: opt.enable_compression,
		compress_mime_types: opt
			.compress_mime_types
			.split(',')
			.map(|x| x.to_string())
			.collect(),
		time_origin: Instant::now(),
	};

	let https_task = tokio::spawn(
		https::serve_https(
			https_config,
			cert_store.clone(),
			rx_proxy_config.clone(),
			exit_signal.clone(),
		)
		.map_err(exit_on_err.clone())
		.then(|_| async { info!("HTTPS server exited") }),
	);

	let dump_task = tokio::spawn(dump_config_on_change(rx_proxy_config, exit_signal.clone()));

	metrics_task.await.expect("Tokio task await failure");
	http_task.await.expect("Tokio task await failure");
	https_task.await.expect("Tokio task await failure");
	dump_task.await.expect("Tokio task await failure");
}

async fn dump_config_on_change(
	mut rx_proxy_config: watch::Receiver<Arc<ProxyConfig>>,
	mut must_exit: watch::Receiver<bool>,
) {
	let mut old_cfg: Arc<ProxyConfig> = rx_proxy_config.borrow().clone();

	while !*must_exit.borrow() {
		select!(
			c = rx_proxy_config.changed() => {
				if c.is_err() {
					break;
				}
			}
			_ = must_exit.changed() => continue,
		);

		let cfg: Arc<ProxyConfig> = rx_proxy_config.borrow().clone();
		if cfg != old_cfg {
			let mut cfg_map = BTreeMap::<_, Vec<_>>::new();
			for ent in cfg.entries.iter() {
				cfg_map
					.entry((&ent.host, &ent.path_prefix))
					.or_default()
					.push(ent);
			}

			println!("---- PROXY CONFIGURATION ----");
			for ((host, prefix), ents) in cfg_map.iter_mut() {
				println!("{}{}:", host, prefix.as_deref().unwrap_or_default());
				for ent in ents.iter() {
					println!("   {}", ent);
				}
			}
			println!();

			old_cfg = cfg;
		}
	}
}

/// Creates a watch that contains `false`, and that changes
/// to `true` when a Ctrl+C signal is received.
pub fn watch_ctrl_c() -> (watch::Receiver<bool>, Arc<watch::Sender<bool>>) {
	let (send_cancel, watch_cancel) = watch::channel(false);
	let send_cancel = Arc::new(send_cancel);
	let send_cancel_2 = send_cancel.clone();
	tokio::spawn(async move {
		tokio::signal::ctrl_c()
			.await
			.expect("failed to install CTRL+C signal handler");
		info!("Received CTRL+C, shutting down.");
		send_cancel.send(true).unwrap();
	});
	(watch_cancel, send_cancel_2)
}

async fn wait_from(mut chan: watch::Receiver<bool>) {
	while !*chan.borrow() {
		if chan.changed().await.is_err() {
			return;
		}
	}
}