cfx_config/
rpc_server_config.rs

1use jsonrpc_http_server::{AccessControlAllowOrigin, DomainsValidation};
2use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
3
4#[derive(Debug, PartialEq)]
5pub struct TcpConfiguration {
6    pub enabled: bool,
7    pub address: SocketAddr,
8}
9
10impl TcpConfiguration {
11    pub fn new(ip: Option<(u8, u8, u8, u8)>, port: Option<u16>) -> Self {
12        let ipv4 = match ip {
13            Some(ip) => Ipv4Addr::new(ip.0, ip.1, ip.2, ip.3),
14            None => Ipv4Addr::new(0, 0, 0, 0),
15        };
16        TcpConfiguration {
17            enabled: port.is_some(),
18            address: SocketAddr::V4(SocketAddrV4::new(ipv4, port.unwrap_or(0))),
19        }
20    }
21}
22
23#[derive(Debug, PartialEq)]
24pub struct HttpConfiguration {
25    pub enabled: bool,
26    pub address: SocketAddr,
27    pub cors_domains: DomainsValidation<AccessControlAllowOrigin>,
28    pub keep_alive: bool,
29    // If it's Some, we will manually set the number of threads of HTTP RPC
30    // server
31    pub threads: Option<usize>,
32}
33
34impl HttpConfiguration {
35    pub fn new(
36        ip: Option<(u8, u8, u8, u8)>, port: Option<u16>, cors: Option<String>,
37        keep_alive: bool, threads: Option<usize>,
38    ) -> Self {
39        let ipv4 = match ip {
40            Some(ip) => Ipv4Addr::new(ip.0, ip.1, ip.2, ip.3),
41            None => Ipv4Addr::new(0, 0, 0, 0),
42        };
43        HttpConfiguration {
44            enabled: port.is_some(),
45            address: SocketAddr::V4(SocketAddrV4::new(ipv4, port.unwrap_or(0))),
46            cors_domains: match cors {
47                None => DomainsValidation::Disabled,
48                Some(cors_list) => match cors_list.as_str() {
49                    "none" => DomainsValidation::Disabled,
50                    "all" => DomainsValidation::AllowOnly(vec![
51                        AccessControlAllowOrigin::Any,
52                    ]),
53                    _ => DomainsValidation::AllowOnly(
54                        cors_list.split(',').map(Into::into).collect(),
55                    ),
56                },
57            },
58            keep_alive,
59            threads,
60        }
61    }
62}
63
64#[derive(Debug, PartialEq)]
65pub struct WsConfiguration {
66    pub enabled: bool,
67    pub address: SocketAddr,
68    pub max_payload_bytes: usize,
69}
70
71impl WsConfiguration {
72    pub fn new(
73        ip: Option<(u8, u8, u8, u8)>, port: Option<u16>,
74        max_payload_bytes: usize,
75    ) -> Self {
76        let ipv4 = match ip {
77            Some(ip) => Ipv4Addr::new(ip.0, ip.1, ip.2, ip.3),
78            None => Ipv4Addr::new(0, 0, 0, 0),
79        };
80        WsConfiguration {
81            enabled: port.is_some(),
82            address: SocketAddr::V4(SocketAddrV4::new(ipv4, port.unwrap_or(0))),
83            max_payload_bytes,
84        }
85    }
86}