Skip to content

Commit

Permalink
Feat: Log client IP during REST and Electrum requests
Browse files Browse the repository at this point in the history
  • Loading branch information
junderw committed Oct 8, 2023
1 parent 18e041c commit a34e353
Show file tree
Hide file tree
Showing 9 changed files with 460 additions and 101 deletions.
39 changes: 39 additions & 0 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 @@ -35,8 +35,10 @@ libc = "0.2.81"
log = "0.4.11"
socket2 = { version = "0.4", features = ["all"] }
num_cpus = "1.12.0"
memchr = "2.4.1"
page_size = "0.4.2"
prometheus = "0.13"
proxy-protocol = { version = "0.5", features = ["always_exhaustive"] }
rayon = "1.5.0"
rocksdb = "0.21.0"
serde = "1.0.118"
Expand Down
9 changes: 9 additions & 0 deletions src/bin/electrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ fn run_server(config: Arc<Config>) -> Result<()> {
);
}

if std::env::var("ELECTRS_PERIODIC_THREAD_LOGGER").is_ok() {
electrs::util::spawn_thread("periodic_thread_logger", || loop {
electrs::util::with_spawned_threads(|threads| {
debug!("THREADS: {:?}", threads);
});
std::thread::sleep(std::time::Duration::from_millis(5000));
});
}

loop {
if let Err(err) = signal.wait(Duration::from_millis(500), true) {
info!("stopping server: {}", err);
Expand Down
16 changes: 16 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct Config {
pub daemon_rpc_addr: SocketAddr,
pub cookie: Option<String>,
pub electrum_rpc_addr: SocketAddr,
pub electrum_proxy_depth: usize,
pub http_addr: SocketAddr,
pub http_socket_file: Option<PathBuf>,
pub rpc_socket_file: Option<PathBuf>,
Expand Down Expand Up @@ -139,6 +140,15 @@ impl Config {
.help("Electrum server JSONRPC 'addr:port' to listen on (default: '127.0.0.1:50001' for mainnet, '127.0.0.1:60001' for testnet and '127.0.0.1:60401' for regtest)")
.takes_value(true),
)
.arg(
Arg::with_name("electrum_proxy_depth")
.long("electrum-proxy-depth")
.help("Electrum server's PROXY protocol header depth. \
ie. a value of 2 means the 2nd closest hop's PROXY header \
will be used to find the source IP. A value of 0 means all \
IPs are ignored. (default: 0)")
.takes_value(true),
)
.arg(
Arg::with_name("http_addr")
.long("http-addr")
Expand Down Expand Up @@ -393,6 +403,11 @@ impl Config {
.unwrap_or(&format!("127.0.0.1:{}", default_electrum_port)),
"Electrum RPC",
);
let electrum_proxy_depth = m
.value_of("electrum_proxy_depth")
.unwrap_or("0")
.parse::<usize>()
.expect("invalid electrum_proxy_depth");
let http_addr: SocketAddr = str_to_socketaddr(
m.value_of("http_addr")
.unwrap_or(&format!("127.0.0.1:{}", default_http_port)),
Expand Down Expand Up @@ -465,6 +480,7 @@ impl Config {
cookie,
utxos_limit: value_t_or_exit!(m, "utxos_limit", usize),
electrum_rpc_addr,
electrum_proxy_depth,
electrum_txs_limit: value_t_or_exit!(m, "electrum_txs_limit", usize),
electrum_banner,
http_addr,
Expand Down
Loading

0 comments on commit a34e353

Please sign in to comment.