Skip to content
Merged
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ Logs are written to `<data-dir>/rustock.log` by default (with daily rotation). U
| `--rpc-port` | `4444` | JSON-RPC HTTP port |
| `--rpc-host` | `127.0.0.1` | JSON-RPC bind address |
| `--no-rpc` | `false` | Disable the JSON-RPC server |
| `--external-ip` | none | External IP to advertise in discovery (e.g. `203.0.113.42`) |
| `--external-ip` | looked up | IPv4 address to advertise in discovery (e.g. `203.0.113.42`). Unset on mainnet or testnet, the node asks `--ip-check-url` for its public IPv4 address; if that fails it advertises `127.0.0.1`. Forward the P2P TCP port and the discovery UDP port (P2P + 1) yourself: the lookup finds the outbound address, which behind NAT is not reachable inbound |
| `--ip-check-url` | `https://checkip.amazonaws.com` | `https://` service that returns the caller's IPv4 address as text, requested once per start with certificate verification (5 s timeout). `off` disables the lookup |

Commonly used beyond the basics:

Expand Down
7 changes: 4 additions & 3 deletions crates/alerts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ lettre = { version = "0.11.10", default-features = false, features = ["builder",
tracing = { workspace = true }

[features]
# Off by default. Email pulls in rustls, ring, webpki-roots and the ICU/idna
# stack -- 55 crates, a 13% increase in the dependency tree of a consensus
# node -- which is not a reasonable default for an optional alerting feature.
# Off by default. Email pulls in lettre and the ICU/idna stack -- most of 55
# crates, a 13% increase in the dependency tree of a consensus node -- which
# is not a reasonable default for an optional alerting feature. rustls, ring
# and webpki-roots are in every build, through rustock-cli's external-IP lookup.
# Build with `--features smtp` on the host that needs to send mail. Without it
# the watcher still runs and logs everything; only delivery is unavailable.
default = []
Expand Down
7 changes: 6 additions & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ rocksdb = { workspace = true }
libc = { workspace = true }
alloy-rlp = { workspace = true }
flate2 = "1"
# HTTPS client for the external-IP lookup (src/public_ip.rs): rustls with the
# ring provider and Mozilla's root store, not the lettre/smtp stack.
tokio-rustls = { version = "0.26", default-features = false, features = ["ring", "tls12"] }
webpki-roots = "1"

[features]
# Enable outgoing email for peg-out alerts (pulls in a TLS stack).
# Enable outgoing email for peg-out alerts (adds the SMTP transport to the
# TLS stack the external-IP lookup already carries).
smtp = ["rustock-alerts/smtp"]

[dev-dependencies]
Expand Down
38 changes: 35 additions & 3 deletions crates/cli/src/config_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,12 @@ pub struct NodeSection {
pub data_dir: Option<String>,
pub network_id: Option<u64>,
pub secret_key: Option<String>,
/// Parsed as an IP address, matching the flag's type -- a malformed value
/// is rejected when the file is read rather than misused later.
pub external_ip: Option<std::net::IpAddr>,
/// Parsed as an IPv4 address, matching the flag's type -- an IPv6 or
/// malformed value is rejected when the file is read rather than misused
/// later.
pub external_ip: Option<std::net::Ipv4Addr>,
/// `"off"` or an `https://` URL, as the flag takes.
pub ip_check_url: Option<String>,
pub supply_check: Option<String>,
/// `"on"` / `"off"`, as the flag takes.
pub rskj_multisig_senders: Option<String>,
Expand Down Expand Up @@ -462,6 +465,7 @@ pub const CONFIGURABLE: &[&str] = &[
"network_id",
"secret_key",
"external_ip",
"ip_check_url",
"supply_check",
"execution_threads",
"rskj_multisig_senders",
Expand Down Expand Up @@ -684,6 +688,34 @@ mod tests {
assert!(err.to_string().contains("port"), "{err}");
}

#[test]
fn an_ipv6_external_ip_is_rejected_on_the_command_line() {
let err = Args::command()
.try_get_matches_from(["rustock", "--external-ip", "::1"])
.unwrap_err()
.to_string();
assert!(err.contains("--external-ip") && err.contains("IPv4"), "{err}");
}

#[test]
fn an_ipv6_external_ip_is_rejected_in_the_file() {
let file = write("[node]\nexternal_ip = \"2606:4700::1111\"\n");
let err = format!("{:#}", FileConfig::load(file.path().to_str().unwrap()).unwrap_err());
assert!(err.contains("external_ip") && err.contains("IPv4"), "{err}");
}

#[test]
fn private_ipv4_external_addresses_are_accepted() {
for ip in ["10.1.2.3", "192.168.0.5", "127.0.0.1"] {
let (_, args) = parse(&["rustock", "--external-ip", ip]);
assert_eq!(args.external_ip, Some(ip.parse().unwrap()));
}
let file = write("[node]\nexternal_ip = \"10.1.2.3\"\n");
let (m, mut a) = parse(&["rustock"]);
crate::apply_file_config(&m, &FileConfig::load(file.path().to_str().unwrap()).unwrap(), &mut a);
assert_eq!(a.external_ip, Some("10.1.2.3".parse().unwrap()));
}

/// A missing file is an error naming the path, not a silent fallback to
/// defaults -- an operator who passes --config means it.
#[test]
Expand Down
30 changes: 24 additions & 6 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clap::{CommandFactory, FromArgMatches, Parser};

mod config_file;
mod public_ip;
use rustock_core::config::ChainConfig;
use rustock_core::validation::HeaderVerifier;
use rustock_storage::BlockStore;
Expand Down Expand Up @@ -561,10 +562,19 @@ struct Args {
#[arg(long)]
peg_events_dir: Option<String>,

/// External IP address to advertise to peers (e.g., 203.0.113.42).
/// If not set, 127.0.0.1 is used.
#[arg(long)]
external_ip: Option<std::net::IpAddr>,
/// External IPv4 address to advertise to peers (e.g., 203.0.113.42).
/// If not set, the address is looked up with --ip-check-url on mainnet and
/// testnet; if that fails, or on any other network, 127.0.0.1 is used.
/// The listeners are IPv4-only, so an IPv6 address is rejected.
#[arg(long, value_name = "IPV4")]
external_ip: Option<std::net::Ipv4Addr>,

/// https:// URL of a service that returns the caller's public IPv4 address
/// as plain text, asked for the address to advertise when --external-ip is
/// unset. The certificate is verified. "off" disables the lookup. The
/// request goes out on every start.
#[arg(long, default_value = public_ip::DEFAULT_URL, value_name = "URL")]
ip_check_url: String,

/// Path to a synced rskj `blocks` LevelDB (e.g. ~/.rsk/mainnet/database/blocks).
/// When set, block bodies are read from it before requesting them from
Expand Down Expand Up @@ -1503,6 +1513,7 @@ fn apply_file_config(
apply(matches, "network_id", f.node.network_id.as_ref(), &mut a.network_id);
apply_opt(matches, "secret_key", f.node.secret_key.as_ref(), &mut a.secret_key);
apply_opt(matches, "external_ip", f.node.external_ip.as_ref(), &mut a.external_ip);
apply(matches, "ip_check_url", f.node.ip_check_url.as_ref(), &mut a.ip_check_url);
apply(matches, "supply_check", f.node.supply_check.as_ref(), &mut a.supply_check);
apply(matches, "execution_threads", f.node.execution_threads.as_ref(),
&mut a.execution_threads);
Expand Down Expand Up @@ -2223,7 +2234,7 @@ async fn run(local_offset: Option<time::UtcOffset>) -> Result<()> {
.map(|f| f.number)
.unwrap_or(0);

let node_config = NodeConfig {
let mut node_config = NodeConfig {
client_id: "Rustock/0.1.0".to_string(),
listen_port: args.port,
id: node_id,
Expand Down Expand Up @@ -2253,7 +2264,7 @@ async fn run(local_offset: Option<time::UtcOffset>) -> Result<()> {
secret_key: secret_key_bytes,
discovery_port: args.port + 1,
data_dir: args.data_dir.clone(),
external_ip: args.external_ip,
external_ip: args.external_ip.map(std::net::IpAddr::V4),
max_outbound_peers: args.max_peers,
max_inbound_peers: args.max_inbound_peers,
max_inbound_per_ip: args.max_inbound_per_ip,
Expand Down Expand Up @@ -3298,6 +3309,13 @@ async fn run(local_offset: Option<time::UtcOffset>) -> Result<()> {
.with_events(events.clone()),
);

node_config.external_ip = public_ip::resolve(
args.external_ip.map(std::net::IpAddr::V4),
&args.ip_check_url,
args.network_id,
args.closed_network,
)
.await;
let mut node =
Node::with_peer_store(node_config, peer_store.clone()).with_scoring(scoring.clone());
node.add_handler(sync_handler);
Expand Down
Loading
Loading