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 ;
1
6
use std:: {
2
7
net:: { Ipv4Addr , SocketAddr , SocketAddrV4 } ,
3
8
time:: Duration ,
4
9
} ;
5
10
6
- use serde:: { Deserialize , Serialize } ;
7
-
8
- use crate :: config:: macros:: config_struct;
9
-
10
11
config_struct ! {
11
12
/// RPC config.
12
13
#[ derive( Clone , Debug , Default , Deserialize , Serialize , PartialEq , Eq ) ]
@@ -24,11 +25,18 @@ config_struct! {
24
25
25
26
config_struct ! {
26
27
Shared {
27
- /// The address and port the RPC server will listen on.
28
+ /// The address the RPC server will listen on.
28
29
///
29
- /// Type | IPv4/IPv6 address + port
30
+ /// Type | IPv4/IPv6 address
30
31
/// 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 >,
32
40
33
41
/// Toggle the RPC server.
34
42
///
@@ -88,7 +96,8 @@ impl Default for UnrestrictedRpcConfig {
88
96
fn default ( ) -> Self {
89
97
Self {
90
98
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 ,
92
101
enable : true ,
93
102
request_byte_limit : 0 ,
94
103
}
@@ -99,7 +108,8 @@ impl Default for RestrictedRpcConfig {
99
108
fn default ( ) -> Self {
100
109
Self {
101
110
advertise : false ,
102
- address : SocketAddr :: V4 ( SocketAddrV4 :: new ( Ipv4Addr :: UNSPECIFIED , 18089 ) ) ,
111
+ address : IpAddr :: V4 ( Ipv4Addr :: UNSPECIFIED ) ,
112
+ port : DefaultOrCustom :: Default ,
103
113
enable : false ,
104
114
// 1 megabyte.
105
115
// <https://github.com/monero-project/monero/blob/3b01c490953fe92f3c6628fa31d280a4f0490d28/src/cryptonote_config.h#L134>
@@ -108,11 +118,33 @@ impl Default for RestrictedRpcConfig {
108
118
}
109
119
}
110
120
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
+
111
143
impl RestrictedRpcConfig {
112
144
/// 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 {
114
146
if self . advertise && self . enable {
115
- self . address . port ( )
147
+ restricted_rpc_port ( self . port , network )
116
148
} else {
117
149
0
118
150
}
0 commit comments