Skip to content

Commit a34e353

Browse files
committed
Feat: Log client IP during REST and Electrum requests
1 parent 18e041c commit a34e353

File tree

9 files changed

+460
-101
lines changed

9 files changed

+460
-101
lines changed

Cargo.lock

Lines changed: 39 additions & 0 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
@@ -35,8 +35,10 @@ libc = "0.2.81"
3535
log = "0.4.11"
3636
socket2 = { version = "0.4", features = ["all"] }
3737
num_cpus = "1.12.0"
38+
memchr = "2.4.1"
3839
page_size = "0.4.2"
3940
prometheus = "0.13"
41+
proxy-protocol = { version = "0.5", features = ["always_exhaustive"] }
4042
rayon = "1.5.0"
4143
rocksdb = "0.21.0"
4244
serde = "1.0.118"

src/bin/electrs.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ fn run_server(config: Arc<Config>) -> Result<()> {
106106
);
107107
}
108108

109+
if std::env::var("ELECTRS_PERIODIC_THREAD_LOGGER").is_ok() {
110+
electrs::util::spawn_thread("periodic_thread_logger", || loop {
111+
electrs::util::with_spawned_threads(|threads| {
112+
debug!("THREADS: {:?}", threads);
113+
});
114+
std::thread::sleep(std::time::Duration::from_millis(5000));
115+
});
116+
}
117+
109118
loop {
110119
if let Err(err) = signal.wait(Duration::from_millis(500), true) {
111120
info!("stopping server: {}", err);

src/config.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub struct Config {
3939
pub daemon_rpc_addr: SocketAddr,
4040
pub cookie: Option<String>,
4141
pub electrum_rpc_addr: SocketAddr,
42+
pub electrum_proxy_depth: usize,
4243
pub http_addr: SocketAddr,
4344
pub http_socket_file: Option<PathBuf>,
4445
pub rpc_socket_file: Option<PathBuf>,
@@ -139,6 +140,15 @@ impl Config {
139140
.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)")
140141
.takes_value(true),
141142
)
143+
.arg(
144+
Arg::with_name("electrum_proxy_depth")
145+
.long("electrum-proxy-depth")
146+
.help("Electrum server's PROXY protocol header depth. \
147+
ie. a value of 2 means the 2nd closest hop's PROXY header \
148+
will be used to find the source IP. A value of 0 means all \
149+
IPs are ignored. (default: 0)")
150+
.takes_value(true),
151+
)
142152
.arg(
143153
Arg::with_name("http_addr")
144154
.long("http-addr")
@@ -393,6 +403,11 @@ impl Config {
393403
.unwrap_or(&format!("127.0.0.1:{}", default_electrum_port)),
394404
"Electrum RPC",
395405
);
406+
let electrum_proxy_depth = m
407+
.value_of("electrum_proxy_depth")
408+
.unwrap_or("0")
409+
.parse::<usize>()
410+
.expect("invalid electrum_proxy_depth");
396411
let http_addr: SocketAddr = str_to_socketaddr(
397412
m.value_of("http_addr")
398413
.unwrap_or(&format!("127.0.0.1:{}", default_http_port)),
@@ -465,6 +480,7 @@ impl Config {
465480
cookie,
466481
utxos_limit: value_t_or_exit!(m, "utxos_limit", usize),
467482
electrum_rpc_addr,
483+
electrum_proxy_depth,
468484
electrum_txs_limit: value_t_or_exit!(m, "electrum_txs_limit", usize),
469485
electrum_banner,
470486
http_addr,

0 commit comments

Comments
 (0)