Skip to content

Commit 78567b7

Browse files
committed
make ports depend on network zones
1 parent 807bfaf commit 78567b7

File tree

8 files changed

+116
-49
lines changed

8 files changed

+116
-49
lines changed

binaries/cuprated/src/config.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ mod tracing_config;
4040

4141
#[macro_use]
4242
mod macros;
43+
mod default;
4344

45+
pub use crate::config::p2p::p2p_port;
4446
use fs::FileSystemConfig;
4547
use p2p::P2PConfig;
4648
use rayon::RayonConfig;
47-
pub use rpc::RpcConfig;
49+
pub use rpc::{restricted_rpc_port, unrestricted_rpc_port, RpcConfig};
4850
pub use storage::{StorageConfig, TxpoolConfig};
4951
use tokio::TokioConfig;
5052
use tor::TorConfig;
@@ -228,8 +230,8 @@ impl Config {
228230
extra_outbound_connections: self.p2p.clear_net.extra_outbound_connections,
229231
max_inbound_connections: self.p2p.clear_net.max_inbound_connections,
230232
gray_peers_percent: self.p2p.clear_net.gray_peers_percent,
231-
p2p_port: self.p2p.clear_net.p2p_port,
232-
rpc_port: self.rpc.restricted.port_for_p2p(),
233+
p2p_port: p2p_port(self.p2p.clear_net.p2p_port, self.network),
234+
rpc_port: self.rpc.restricted.port_for_p2p(self.network),
233235
address_book_config: self.p2p.clear_net.address_book_config.address_book_config(
234236
&self.fs.cache_directory,
235237
self.network,
@@ -241,12 +243,15 @@ impl Config {
241243
/// The [`Tor`], [`cuprate_p2p::P2PConfig`].
242244
pub fn tor_p2p_config(&self, ctx: &TorContext) -> cuprate_p2p::P2PConfig<Tor> {
243245
let inbound_enabled = self.p2p.tor_net.inbound_onion;
246+
247+
let tor_p2p_port = p2p_port(self.p2p.tor_net.p2p_port, self.network);
248+
244249
let our_onion_address = match ctx.mode {
245250
TorMode::Off => None,
246251
TorMode::Daemon => inbound_enabled.then(||
247252
OnionAddr::new(
248253
&self.tor.daemon.anonymous_inbound,
249-
self.p2p.tor_net.p2p_port
254+
tor_p2p_port
250255
).expect("Unable to parse supplied `anonymous_inbound` onion address. Please make sure the address is correct.")),
251256
TorMode::Arti => inbound_enabled.then(|| {
252257
let addr = ctx.arti_onion_service
@@ -257,7 +262,7 @@ impl Config {
257262
.display_unredacted()
258263
.to_string();
259264

260-
OnionAddr::new(&addr, self.p2p.tor_net.p2p_port).unwrap()
265+
OnionAddr::new(&addr, tor_p2p_port).unwrap()
261266
})
262267
};
263268

@@ -268,7 +273,7 @@ impl Config {
268273
extra_outbound_connections: self.p2p.tor_net.extra_outbound_connections,
269274
max_inbound_connections: self.p2p.tor_net.max_inbound_connections,
270275
gray_peers_percent: self.p2p.tor_net.gray_peers_percent,
271-
p2p_port: self.p2p.tor_net.p2p_port,
276+
p2p_port: tor_p2p_port,
272277
rpc_port: 0,
273278
address_book_config: self.p2p.tor_net.address_book_config.address_book_config(
274279
&self.fs.cache_directory,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
4+
pub enum DefaultOrCustom<T> {
5+
Default,
6+
#[serde(untagged)]
7+
Custom(T),
8+
}

binaries/cuprated/src/config/p2p.rs

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ use cuprate_p2p_core::{
2121
use cuprate_p2p_transport::{Arti, ArtiClientConfig, ArtiServerConfig};
2222
use cuprate_wire::OnionAddr;
2323

24-
use crate::{p2p::ProxySettings, tor::TorMode};
25-
2624
use super::macros::config_struct;
25+
use crate::config::default::DefaultOrCustom;
26+
use crate::{p2p::ProxySettings, tor::TorMode};
2727

2828
config_struct! {
2929
/// P2P config.
@@ -167,7 +167,7 @@ config_struct! {
167167
/// Type | Number
168168
/// Valid values | 0..65534
169169
/// Examples | 18080, 9999, 5432
170-
pub p2p_port: u16,
170+
pub p2p_port: DefaultOrCustom<u16>,
171171

172172
#[child = true]
173173
/// The address book config.
@@ -259,10 +259,40 @@ config_struct! {
259259
}
260260
}
261261

262+
pub const fn p2p_port(setting: DefaultOrCustom<u16>, network: Network) -> u16 {
263+
match setting {
264+
DefaultOrCustom::Default => match network {
265+
Network::Mainnet => 18080,
266+
Network::Stagenet => 38080,
267+
Network::Testnet => 28080,
268+
},
269+
DefaultOrCustom::Custom(port) => port,
270+
}
271+
}
272+
273+
impl ClearNetConfig {
274+
pub fn tor_transport_config(&self, network: Network) -> TransportConfig<ClearNet, Tcp> {
275+
let server_config = if self.enable_inbound {
276+
let mut sc = TcpServerConfig::default();
277+
sc.ipv4 = Some(self.listen_on);
278+
sc.ipv6 = self.enable_inbound_v6.then_some(self.listen_on_v6);
279+
sc.port = p2p_port(self.p2p_port, network);
280+
Some(sc)
281+
} else {
282+
None
283+
};
284+
285+
TransportConfig {
286+
client_config: (),
287+
server_config,
288+
}
289+
}
290+
}
291+
262292
impl Default for ClearNetConfig {
263293
fn default() -> Self {
264294
Self {
265-
p2p_port: 18080,
295+
p2p_port: DefaultOrCustom::Default,
266296
enable_inbound: true,
267297
listen_on: Ipv4Addr::UNSPECIFIED,
268298
enable_inbound_v6: false,
@@ -282,7 +312,7 @@ impl Default for TorNetConfig {
282312
Self {
283313
enabled: false,
284314
inbound_onion: false,
285-
p2p_port: 18080,
315+
p2p_port: DefaultOrCustom::Default,
286316
outbound_connections: 12,
287317
extra_outbound_connections: 2,
288318
max_inbound_connections: 128,
@@ -292,25 +322,6 @@ impl Default for TorNetConfig {
292322
}
293323
}
294324

295-
impl From<&ClearNetConfig> for TransportConfig<ClearNet, Tcp> {
296-
fn from(value: &ClearNetConfig) -> Self {
297-
let server_config = if value.p2p_port != 0 {
298-
let mut sc = TcpServerConfig::default();
299-
sc.ipv4 = Some(value.listen_on);
300-
sc.ipv6 = value.enable_inbound_v6.then_some(value.listen_on_v6);
301-
sc.port = value.p2p_port;
302-
Some(sc)
303-
} else {
304-
None
305-
};
306-
307-
Self {
308-
client_config: (),
309-
server_config,
310-
}
311-
}
312-
}
313-
314325
config_struct! {
315326
/// The addressbook config exposed to users.
316327
#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)]

binaries/cuprated/src/config/rpc.rs

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
use crate::config::default::DefaultOrCustom;
2+
use crate::config::macros::config_struct;
3+
use cuprate_helper::network::Network;
4+
use serde::{Deserialize, Serialize};
5+
use std::net::IpAddr;
16
use std::{
27
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
38
time::Duration,
49
};
510

6-
use serde::{Deserialize, Serialize};
7-
8-
use crate::config::macros::config_struct;
9-
1011
config_struct! {
1112
/// RPC config.
1213
#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
@@ -24,11 +25,18 @@ config_struct! {
2425

2526
config_struct! {
2627
Shared {
27-
/// The address and port the RPC server will listen on.
28+
/// The address the RPC server will listen on.
2829
///
29-
/// Type | IPv4/IPv6 address + port
30+
/// Type | IPv4/IPv6 address
3031
/// Examples | "", "127.0.0.1:18081", "192.168.1.50:18085"
31-
pub address: SocketAddr,
32+
pub address: IpAddr,
33+
34+
/// The port the RPC server will listen on.
35+
///
36+
/// Type | Number
37+
/// Valid values | 0..65534
38+
/// Examples | 18080, 9999, 5432
39+
pub port: DefaultOrCustom<u16>,
3240

3341
/// Toggle the RPC server.
3442
///
@@ -88,7 +96,8 @@ impl Default for UnrestrictedRpcConfig {
8896
fn default() -> Self {
8997
Self {
9098
i_know_what_im_doing_allow_public_unrestricted_rpc: false,
91-
address: SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 18081)),
99+
address: IpAddr::V4(Ipv4Addr::LOCALHOST),
100+
port: DefaultOrCustom::Default,
92101
enable: true,
93102
request_byte_limit: 0,
94103
}
@@ -99,7 +108,8 @@ impl Default for RestrictedRpcConfig {
99108
fn default() -> Self {
100109
Self {
101110
advertise: false,
102-
address: SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 18089)),
111+
address: IpAddr::V4(Ipv4Addr::UNSPECIFIED),
112+
port: DefaultOrCustom::Default,
103113
enable: false,
104114
// 1 megabyte.
105115
// <https://github.com/monero-project/monero/blob/3b01c490953fe92f3c6628fa31d280a4f0490d28/src/cryptonote_config.h#L134>
@@ -108,11 +118,33 @@ impl Default for RestrictedRpcConfig {
108118
}
109119
}
110120

121+
pub const fn restricted_rpc_port(config: DefaultOrCustom<u16>, network: Network) -> u16 {
122+
match config {
123+
DefaultOrCustom::Default => match network {
124+
Network::Mainnet => 18089,
125+
Network::Stagenet => 38089,
126+
Network::Testnet => 28089,
127+
},
128+
DefaultOrCustom::Custom(port) => port,
129+
}
130+
}
131+
132+
pub const fn unrestricted_rpc_port(config: DefaultOrCustom<u16>, network: Network) -> u16 {
133+
match config {
134+
DefaultOrCustom::Default => match network {
135+
Network::Mainnet => 18081,
136+
Network::Stagenet => 38081,
137+
Network::Testnet => 28081,
138+
},
139+
DefaultOrCustom::Custom(port) => port,
140+
}
141+
}
142+
111143
impl RestrictedRpcConfig {
112144
/// Return the restricted RPC port for P2P if available and public.
113-
pub const fn port_for_p2p(&self) -> u16 {
145+
pub const fn port_for_p2p(&self, network: Network) -> u16 {
114146
if self.advertise && self.enable {
115-
self.address.port()
147+
restricted_rpc_port(self.port, network)
116148
} else {
117149
0
118150
}

binaries/cuprated/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ fn main() {
168168
// Initialize the RPC server(s).
169169
rpc::init_rpc_servers(
170170
config.rpc,
171+
config.network,
171172
blockchain_read_handle,
172173
context_svc.clone(),
173174
txpool_read_handle,

binaries/cuprated/src/p2p.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub async fn initialize_zones_p2p(
110110
context_svc.clone(),
111111
txpool_read_handle.clone(),
112112
config.clearnet_p2p_config(),
113-
(&config.p2p.clear_net).into(),
113+
config.p2p.clear_net.tor_transport_config(config.network),
114114
)
115115
.await
116116
.unwrap()

binaries/cuprated/src/rpc/server.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ use tracing::{info, warn};
1313

1414
use cuprate_blockchain::service::BlockchainReadHandle;
1515
use cuprate_consensus::BlockchainContextService;
16+
use cuprate_helper::network::Network;
1617
use cuprate_rpc_interface::{RouterBuilder, RpcHandler};
1718
use cuprate_txpool::service::TxpoolReadHandle;
1819

20+
use crate::config::{restricted_rpc_port, unrestricted_rpc_port};
1921
use crate::{
2022
config::RpcConfig,
2123
rpc::{rpc_handler::BlockchainManagerHandle, CupratedRpcHandler},
@@ -31,16 +33,18 @@ use crate::{
3133
/// address without override option
3234
pub fn init_rpc_servers(
3335
config: RpcConfig,
36+
network: Network,
3437
blockchain_read: BlockchainReadHandle,
3538
blockchain_context: BlockchainContextService,
3639
txpool_read: TxpoolReadHandle,
3740
tx_handler: IncomingTxHandler,
3841
) {
39-
for ((enable, addr, request_byte_limit), restricted) in [
42+
for ((enable, addr, port, request_byte_limit), restricted) in [
4043
(
4144
(
4245
config.unrestricted.enable,
4346
config.unrestricted.address,
47+
unrestricted_rpc_port(config.unrestricted.port, network),
4448
config.unrestricted.request_byte_limit,
4549
),
4650
false,
@@ -49,6 +53,7 @@ pub fn init_rpc_servers(
4953
(
5054
config.restricted.enable,
5155
config.restricted.address,
56+
restricted_rpc_port(config.restricted.port, network),
5257
config.restricted.request_byte_limit,
5358
),
5459
true,
@@ -59,7 +64,7 @@ pub fn init_rpc_servers(
5964
continue;
6065
}
6166

62-
if !restricted && !cuprate_helper::net::ip_is_local(addr.ip()) {
67+
if !restricted && !cuprate_helper::net::ip_is_local(addr) {
6368
if config
6469
.unrestricted
6570
.i_know_what_im_doing_allow_public_unrestricted_rpc
@@ -82,9 +87,14 @@ pub fn init_rpc_servers(
8287
);
8388

8489
tokio::task::spawn(async move {
85-
run_rpc_server(rpc_handler, restricted, addr, request_byte_limit)
86-
.await
87-
.unwrap();
90+
run_rpc_server(
91+
rpc_handler,
92+
restricted,
93+
SocketAddr::new(addr, port),
94+
request_byte_limit,
95+
)
96+
.await
97+
.unwrap();
8898
});
8999
}
90100
}

binaries/cuprated/src/tor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ use cuprate_p2p_transport::{
2525
};
2626
use cuprate_wire::OnionAddr;
2727

28+
use crate::config::p2p_port;
2829
use crate::{config::Config, p2p::ProxySettings};
29-
3030
//---------------------------------------------------------------------------------------------------- Initialization
3131

3232
#[derive(Clone, Default, Debug, Copy, PartialEq, Eq, Serialize, Deserialize)]
@@ -148,7 +148,7 @@ pub fn transport_arti_config(config: &Config, ctx: TorContext) -> TransportConfi
148148

149149
ArtiServerConfig::new(
150150
onion_svc,
151-
config.p2p.tor_net.p2p_port,
151+
p2p_port(config.p2p.tor_net.p2p_port, config.network),
152152
&bootstrapped_client,
153153
&client_config,
154154
)

0 commit comments

Comments
 (0)