Skip to content

Rpc log ip anonymisation #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: new-index
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 61 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ tracing = { version = "0.1.40", default-features = false, features = ["attribute
electrum-client = { version = "0.8", optional = true }
zmq = "0.10.0"
electrs_macros = { path = "electrs_macros", default-features = false }
rand = "0.9.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may have rand accessible from other deps like secp?

hex = "0.4"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have hex functionalities provided by bitcoin crate without adding another dep


[dev-dependencies]
bitcoind = { version = "0.36", features = ["25_0"] }
Expand Down
38 changes: 34 additions & 4 deletions src/bin/electrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ extern crate electrs;

use crossbeam_channel::{self as channel};
use error_chain::ChainedError;
use std::process;
use std::{process, thread};
use std::sync::{Arc, RwLock};
use std::time::Duration;
use rand;
use hex;

use electrs::{
config::Config,
Expand Down Expand Up @@ -44,7 +46,7 @@ fn fetch_from(config: &Config, store: &Store) -> FetchFrom {
}
}

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

// TODO: configuration for which servers to start
let rest_server = rest::start(Arc::clone(&config), Arc::clone(&query));
let electrum_server = ElectrumRPC::start(Arc::clone(&config), Arc::clone(&query), &metrics);
let electrum_server = ElectrumRPC::start(
Arc::clone(&config),
Arc::clone(&query),
&metrics,
Arc::clone(&salt_rwlock),
);

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

fn generate_salt() -> String {
let random_bytes: [u8; 32] = rand::random();
hex::encode(random_bytes)
}

fn rotate_salt(salt: &mut String) {
*salt = generate_salt();
}

fn main_() {
let salt = generate_salt();
let salt_rwlock = Arc::new(RwLock::new(salt));
let writer_arc = Arc::clone(&salt_rwlock);
thread::spawn(move || {
loop {
thread::sleep(Duration::from_secs(24 * 3600)); // 24 hours
{
let mut guard = writer_arc.write().unwrap();
rotate_salt(&mut *guard);
info!("Salt rotated");
}
}
});

let config = Arc::new(Config::from_args());
if let Err(e) = run_server(config) {
if let Err(e) = run_server(config, Arc::clone(&salt_rwlock)) {
error!("server failed: {}", e.display_chain());
process::exit(1);
}
Expand Down
62 changes: 39 additions & 23 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct Config {
pub utxos_limit: usize,
pub electrum_txs_limit: usize,
pub electrum_banner: String,
pub electrum_rpc_logging: Option<RpcLogging>,
pub rpc_logging_modes: Option<RpcLoggingModes>,
pub zmq_addr: Option<SocketAddr>,

/// Enable compaction during initial sync
Expand Down Expand Up @@ -75,8 +75,8 @@ impl Config {
pub fn from_args() -> Config {
let network_help = format!("Select network type ({})", Network::names().join(", "));
let rpc_logging_help = format!(
"Select RPC logging option ({})",
RpcLogging::options().join(", ")
"Select possible RPC logging options: {}",
RpcLoggingModes::get_modes().join(", ")
);

let args = App::new("Electrum Rust Server")
Expand Down Expand Up @@ -201,10 +201,12 @@ impl Config {
.help("Welcome banner for the Electrum server, shown in the console to clients.")
.takes_value(true)
).arg(
Arg::with_name("electrum_rpc_logging")
Arg::with_name("rpc_logging_modes")
.long("electrum-rpc-logging")
.help(&rpc_logging_help)
.takes_value(true),
.takes_value(true)
.multiple(true)
.possible_values(&RpcLoggingModes::get_modes()),
).arg(
Arg::with_name("initial_sync_compaction")
.long("initial-sync-compaction")
Expand Down Expand Up @@ -419,9 +421,10 @@ impl Config {
electrum_rpc_addr,
electrum_txs_limit: value_t_or_exit!(m, "electrum_txs_limit", usize),
electrum_banner,
electrum_rpc_logging: m
.value_of("electrum_rpc_logging")
.map(|option| RpcLogging::from(option)),
rpc_logging_modes: m.values_of("rpc_logging_modes").map(|vals| {
let collected = vals.collect::<Vec<_>>();
RpcLoggingModes::from_values(&collected)
}),
http_addr,
http_socket_file,
monitoring_addr,
Expand Down Expand Up @@ -463,26 +466,39 @@ impl Config {
}
}

#[derive(Debug, Clone)]
pub enum RpcLogging {
Full,
NoParams,
#[derive(Debug, Default, Clone)]
pub struct RpcLoggingModes {
pub with_params: bool,
pub anonymised: bool,
}

impl RpcLogging {
pub fn options() -> Vec<String> {
return vec!["full".to_string(), "no-params".to_string()];
}
}
impl RpcLoggingModes {
pub fn from_values(values: &[&str]) -> Self {
let mut modes = Self::default();
let mut no_params = false;

for v in values {
match *v {
"with-params" => modes.with_params = true,
"no-params" => no_params = true,
"anonymised" => modes.anonymised = true,
_ => panic!("Unsupported RPC logging option: {}", v),
}
}

impl From<&str> for RpcLogging {
fn from(option: &str) -> Self {
match option {
"full" => RpcLogging::Full,
"no-params" => RpcLogging::NoParams,
if modes.with_params && no_params {
panic!("Contradiction: 'with-params' and 'no-params' cannot both be set.");
}

_ => panic!("unsupported RPC logging option: {:?}", option),
if no_params {
modes.with_params = false;
}

modes
}

pub fn get_modes() -> Vec<&'static str> {
vec!["with-params", "no-params", "anonymised"]
}
}

Expand Down
Loading