Skip to content

Commit 9beea74

Browse files
Mariusz Reichertm-reichert
authored andcommitted
IP anonymization implementation
1 parent 47804f4 commit 9beea74

File tree

5 files changed

+138
-29
lines changed

5 files changed

+138
-29
lines changed

Cargo.lock

Lines changed: 61 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ tracing = { version = "0.1.40", default-features = false, features = ["attribute
7575
electrum-client = { version = "0.8", optional = true }
7676
zmq = "0.10.0"
7777
electrs_macros = { path = "electrs_macros", default-features = false }
78+
rand = "0.9.0"
79+
hex = "0.4"
7880

7981
[dev-dependencies]
8082
bitcoind = { version = "0.36", features = ["25_0"] }

src/bin/electrs.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ extern crate electrs;
66

77
use crossbeam_channel::{self as channel};
88
use error_chain::ChainedError;
9-
use std::process;
9+
use std::{process, thread};
1010
use std::sync::{Arc, RwLock};
1111
use std::time::Duration;
12+
use rand;
13+
use hex;
1214

1315
use electrs::{
1416
config::Config,
@@ -44,7 +46,7 @@ fn fetch_from(config: &Config, store: &Store) -> FetchFrom {
4446
}
4547
}
4648

47-
fn run_server(config: Arc<Config>) -> Result<()> {
49+
fn run_server(config: Arc<Config>, salt_rwlock: Arc<RwLock<String>>) -> Result<()> {
4850
let (block_hash_notify, block_hash_receive) = channel::bounded(1);
4951
let signal = Waiter::start(block_hash_receive);
5052
let metrics = Metrics::new(config.monitoring_addr);
@@ -116,7 +118,12 @@ fn run_server(config: Arc<Config>) -> Result<()> {
116118

117119
// TODO: configuration for which servers to start
118120
let rest_server = rest::start(Arc::clone(&config), Arc::clone(&query));
119-
let electrum_server = ElectrumRPC::start(Arc::clone(&config), Arc::clone(&query), &metrics);
121+
let electrum_server = ElectrumRPC::start(
122+
Arc::clone(&config),
123+
Arc::clone(&query),
124+
&metrics,
125+
Arc::clone(&salt_rwlock),
126+
);
120127

121128
let main_loop_count = metrics.gauge(MetricOpts::new(
122129
"electrs_main_loop_count",
@@ -151,9 +158,32 @@ fn run_server(config: Arc<Config>) -> Result<()> {
151158
Ok(())
152159
}
153160

161+
fn generate_salt() -> String {
162+
let random_bytes: [u8; 32] = rand::random();
163+
hex::encode(random_bytes)
164+
}
165+
166+
fn rotate_salt(salt: &mut String) {
167+
*salt = generate_salt();
168+
}
169+
154170
fn main_() {
171+
let salt = generate_salt();
172+
let salt_rwlock = Arc::new(RwLock::new(salt));
173+
let writer_arc = Arc::clone(&salt_rwlock);
174+
thread::spawn(move || {
175+
loop {
176+
thread::sleep(Duration::from_secs(24 * 3600)); // 24 hours
177+
{
178+
let mut guard = writer_arc.write().unwrap();
179+
rotate_salt(&mut *guard);
180+
info!("Salt rotated");
181+
}
182+
}
183+
});
184+
155185
let config = Arc::new(Config::from_args());
156-
if let Err(e) = run_server(config) {
186+
if let Err(e) = run_server(config, Arc::clone(&salt_rwlock)) {
157187
error!("server failed: {}", e.display_chain());
158188
process::exit(1);
159189
}

src/config.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,10 @@ impl Config {
421421
electrum_rpc_addr,
422422
electrum_txs_limit: value_t_or_exit!(m, "electrum_txs_limit", usize),
423423
electrum_banner,
424-
rpc_logging_modes: m
425-
.values_of("rpc_logging_modes")
426-
.map(|vals| {
427-
let collected = vals.collect::<Vec<_>>();
428-
RpcLoggingModes::from_values(&collected)
429-
}),
424+
rpc_logging_modes: m.values_of("rpc_logging_modes").map(|vals| {
425+
let collected = vals.collect::<Vec<_>>();
426+
RpcLoggingModes::from_values(&collected)
427+
}),
430428
http_addr,
431429
http_socket_file,
432430
monitoring_addr,
@@ -471,27 +469,31 @@ impl Config {
471469
#[derive(Debug, Default, Clone)]
472470
pub struct RpcLoggingModes {
473471
pub with_params: bool,
474-
pub no_params: bool,
475472
pub anonymised: bool,
476473
}
477474

478475
impl RpcLoggingModes {
479476
pub fn from_values(values: &[&str]) -> Self {
480477
let mut modes = Self::default();
478+
let mut no_params = false;
481479

482480
for v in values {
483481
match *v {
484482
"with-params" => modes.with_params = true,
485-
"no-params" => modes.no_params = true,
486-
"anonymised" => modes.anonymised = true,
483+
"no-params" => no_params = true,
484+
"anonymised" => modes.anonymised = true,
487485
_ => panic!("Unsupported RPC logging option: {}", v),
488486
}
489487
}
490488

491-
if modes.with_params && modes.no_params {
489+
if modes.with_params && no_params {
492490
panic!("Contradiction: 'with-params' and 'no-params' cannot both be set.");
493491
}
494492

493+
if no_params {
494+
modes.with_params = false;
495+
}
496+
495497
modes
496498
}
497499

0 commit comments

Comments
 (0)