koibumi_node/
config.rs

1use std::net::SocketAddr as StdSocketAddr;
2
3use serde::{Deserialize, Serialize};
4
5use koibumi_core::{
6    message::{StreamNumbers, UserAgent},
7    net::SocketAddrExt,
8    Config as CoreConfig,
9};
10
11use crate::net::SocketAddrNode;
12
13/// Represents a username and a password used to authenticate SOCKS5 connection.
14#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
15pub struct SocksAuth {
16    username: String,
17    password: String,
18}
19
20impl SocksAuth {
21    /// Creates a authentication object consists of a username and a password.
22    pub fn new(username: String, password: String) -> Self {
23        Self { username, password }
24    }
25
26    /// Returns the username.
27    pub fn username(&self) -> &str {
28        &self.username
29    }
30
31    /// Returns the password.
32    pub fn password(&self) -> &str {
33        &self.password
34    }
35}
36
37/// A set of configurations for a Koibumi Bitmessage node.
38#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
39pub struct Config {
40    #[serde(default = "default_channel_buffer")]
41    channel_buffer: usize,
42
43    #[serde(default = "default_server")]
44    server: Option<StdSocketAddr>,
45    #[serde(default = "default_socks")]
46    socks: Option<StdSocketAddr>,
47    #[serde(default = "default_connect_to_onion")]
48    connect_to_onion: bool,
49    #[serde(default = "default_connect_to_ip")]
50    connect_to_ip: bool,
51    #[serde(default = "default_connect_to_myself")]
52    connect_to_myself: bool,
53    #[serde(default = "default_user_agent")]
54    user_agent: UserAgent,
55    #[serde(default = "default_stream_numbers")]
56    stream_numbers: StreamNumbers,
57
58    #[serde(default = "default_seeds")]
59    seeds: Vec<SocketAddrExt>,
60    #[serde(default = "default_bootstraps")]
61    bootstraps: Vec<SocketAddrNode>,
62
63    #[serde(default = "default_max_incoming_connected")]
64    max_incoming_connected: usize,
65    #[serde(default = "default_max_incoming_established")]
66    max_incoming_established: usize,
67    #[serde(default = "default_max_outgoing_initiated")]
68    max_outgoing_initiated: usize,
69    #[serde(default = "default_max_outgoing_established")]
70    max_outgoing_established: usize,
71
72    #[serde(default = "default_max_nodes")]
73    max_nodes: usize,
74
75    #[serde(default = "default_own_nodes")]
76    own_nodes: Vec<SocketAddrExt>,
77
78    #[serde(default = "default_socks_auth")]
79    socks_auth: Option<SocksAuth>,
80
81    #[serde(default = "default_core")]
82    core: CoreConfig,
83}
84
85fn default_core() -> CoreConfig {
86    CoreConfig::default()
87}
88
89fn default_channel_buffer() -> usize {
90    0x10000
91}
92
93fn default_server() -> Option<StdSocketAddr> {
94    None
95}
96
97fn default_socks() -> Option<StdSocketAddr> {
98    None
99}
100
101fn default_socks_auth() -> Option<SocksAuth> {
102    None
103}
104
105fn default_connect_to_onion() -> bool {
106    false
107}
108
109fn default_connect_to_ip() -> bool {
110    false
111}
112
113fn default_connect_to_myself() -> bool {
114    false
115}
116
117fn default_user_agent() -> UserAgent {
118    DEFAULT_USER_AGENT_STR.to_vec().into()
119}
120
121fn default_stream_numbers() -> StreamNumbers {
122    vec![1_u32.into()].into()
123}
124
125fn default_seeds() -> Vec<SocketAddrExt> {
126    Vec::with_capacity(0)
127}
128
129fn default_bootstraps() -> Vec<SocketAddrNode> {
130    Vec::with_capacity(0)
131}
132
133fn default_max_incoming_connected() -> usize {
134    160
135}
136
137fn default_max_incoming_established() -> usize {
138    128
139}
140
141fn default_max_outgoing_initiated() -> usize {
142    32
143}
144
145fn default_max_outgoing_established() -> usize {
146    8
147}
148
149fn default_max_nodes() -> usize {
150    20000
151}
152
153fn default_own_nodes() -> Vec<SocketAddrExt> {
154    Vec::with_capacity(0)
155}
156
157impl Default for Config {
158    fn default() -> Self {
159        Self {
160            core: default_core(),
161            channel_buffer: default_channel_buffer(),
162
163            server: default_server(),
164            socks: default_socks(),
165            socks_auth: default_socks_auth(),
166            connect_to_onion: default_connect_to_onion(),
167            connect_to_ip: default_connect_to_ip(),
168            connect_to_myself: default_connect_to_myself(),
169            user_agent: default_user_agent(),
170            stream_numbers: default_stream_numbers(),
171
172            seeds: default_seeds(),
173            bootstraps: default_bootstraps(),
174
175            max_incoming_connected: default_max_incoming_connected(),
176            max_incoming_established: default_max_incoming_established(),
177            max_outgoing_initiated: default_max_outgoing_initiated(),
178            max_outgoing_established: default_max_outgoing_established(),
179
180            max_nodes: default_max_nodes(),
181
182            own_nodes: default_own_nodes(),
183        }
184    }
185}
186
187impl Config {
188    /// Constructs a builder for building a configuration set.
189    pub fn builder() -> Builder {
190        Builder::new()
191    }
192
193    /// Constructs a default configuration set.
194    pub fn new() -> Self {
195        Self::default()
196    }
197
198    /// Returns the core configuration set
199    /// which this configuration set contains.
200    /// The default is the default of the core configuration set.
201    pub fn core(&self) -> &CoreConfig {
202        &self.core
203    }
204
205    /// Returns the buffer size of channels
206    /// which will be created in this node.
207    /// The default is `0x10000`.
208    pub fn channel_buffer(&self) -> usize {
209        self.channel_buffer
210    }
211
212    /// Returns the `Option` of the socket address of the server
213    /// which listens incomming connections.
214    /// If this is `None`, no server will be launched.
215    /// The default is `None`.
216    pub fn server(&self) -> &Option<StdSocketAddr> {
217        &self.server
218    }
219
220    /// Returns the `Option` of the socket address of the SOCKS5 server
221    /// which is used by outgoing connections.
222    /// If this is `None`, connections are directly to Clearnet.
223    /// The default is `None`.
224    pub fn socks(&self) -> &Option<StdSocketAddr> {
225        &self.socks
226    }
227
228    /// Returns the `Option` of the authentication method of the SOCKS5
229    /// which is used by outgoing connections.
230    /// The default is `None`.
231    pub fn socks_auth(&self) -> &Option<SocksAuth> {
232        &self.socks_auth
233    }
234
235    /// Returns the flag whether this node connects to remote nodes
236    /// that have Onion addresses.
237    /// Only outgoing connections are affected.
238    /// The default is `false`.
239    pub fn connect_to_onion(&self) -> bool {
240        self.connect_to_onion
241    }
242
243    /// Returns the flag whether this node connects to remote nodes
244    /// that have IP addresses.
245    /// Only outgoing connections are affected.
246    /// The default is `false`.
247    pub fn connect_to_ip(&self) -> bool {
248        self.connect_to_ip
249    }
250
251    /// Returns whether this node can connect to the node
252    /// specified by the socket address.
253    /// It is affected by the type of the address
254    /// which is Onion or IP.
255    pub fn is_connectable_to(&self, addr: &SocketAddrNode) -> bool {
256        match addr {
257            SocketAddrNode::AddrExt(addr) => match addr {
258                SocketAddrExt::Ipv4(_addr) => self.connect_to_ip,
259                SocketAddrExt::Ipv6(_addr) => self.connect_to_ip,
260                SocketAddrExt::OnionV2(_addr) => self.connect_to_onion,
261                SocketAddrExt::OnionV3(_addr) => self.connect_to_onion,
262            },
263            SocketAddrNode::Domain(_domain) => self.connect_to_ip,
264        }
265    }
266
267    /// Returns the flag whether this node can connect to this node itself.
268    /// The default is `false`.
269    pub fn connect_to_myself(&self) -> bool {
270        self.connect_to_myself
271    }
272
273    /// Returns the user agent which is sent to the nodes
274    /// this node connected to.
275    /// The default is `"/PyBitmessage:0.6.3.2/"`.
276    pub fn user_agent(&self) -> &UserAgent {
277        &self.user_agent
278    }
279
280    /// Returns the set of stream numbers
281    /// that this node is interested in.
282    /// The default is `[1]`.
283    pub fn stream_numbers(&self) -> &StreamNumbers {
284        &self.stream_numbers
285    }
286
287    /// Returns the list of socket addresses of the seed nodes.
288    /// The default is `[]`.
289    pub fn seeds(&self) -> &[SocketAddrExt] {
290        &self.seeds
291    }
292
293    /// Returns the list of socket addresses of the bootstrap domain names.
294    /// The default is `[]`.
295    pub fn bootstraps(&self) -> &[SocketAddrNode] {
296        &self.bootstraps
297    }
298
299    /// Returns the maximum number of incoming connections
300    /// this node accepts.
301    /// The default is `160`.
302    pub fn max_incoming_connected(&self) -> usize {
303        self.max_incoming_connected
304    }
305
306    /// Returns the maximum number of incoming established connections
307    /// this node accepts.
308    /// The default is `128`.
309    pub fn max_incoming_established(&self) -> usize {
310        self.max_incoming_established
311    }
312
313    /// Returns the maximum number of outgoing conections
314    /// this node initiates.
315    /// The default is `32`.
316    pub fn max_outgoing_initiated(&self) -> usize {
317        self.max_outgoing_initiated
318    }
319
320    /// Returns the maximum number of outgoing established connections
321    /// this node keeps.
322    /// The default is `8`.
323    pub fn max_outgoing_established(&self) -> usize {
324        self.max_outgoing_established
325    }
326
327    /// Returns the maximum number of node addresses
328    /// this node memorizes.
329    /// The default is `20000`.
330    pub fn max_nodes(&self) -> usize {
331        self.max_nodes
332    }
333
334    /// Returns the list of the socket addresses of the own node.
335    /// The default is `[]`.
336    pub fn own_nodes(&self) -> &[SocketAddrExt] {
337        &self.own_nodes
338    }
339}
340
341/// A builder for building a configuration set
342/// for a Koibumi Bitmessage node.
343#[derive(Clone, PartialEq, Eq, Debug, Default)]
344pub struct Builder {
345    config: Config,
346}
347
348const DEFAULT_USER_AGENT_STR: &[u8] = b"/PyBitmessage:0.6.3.2/";
349
350impl Builder {
351    fn new() -> Self {
352        Self::default()
353    }
354
355    /// Sets the core configuration set
356    /// which the configuration set this builder builds contains.
357    /// The default is the default of the core configuration set.
358    pub fn core(&mut self, core: CoreConfig) -> &mut Self {
359        self.config.core = core;
360        self
361    }
362
363    /// Sets the buffer size of channels
364    /// which created in this node.
365    /// The default is `0x10000`.
366    pub fn channel_buffer(&mut self, v: usize) -> &mut Self {
367        self.config.channel_buffer = v;
368        self
369    }
370
371    /// Sets the `Option` of the socket address of the server
372    /// which listens incomming connections.
373    /// If this is `None`, no server will be launched.
374    /// The default is `None`.
375    pub fn server(&mut self, addr: Option<StdSocketAddr>) -> &mut Self {
376        self.config.server = addr;
377        self
378    }
379
380    /// Sets the `Option` of the socket address of the SOCKS5 server
381    /// which is used by outgoing connections.
382    /// If this is `None`, connections are directly to Clearnet.
383    /// The default is `None`.
384    pub fn socks(&mut self, addr: Option<StdSocketAddr>) -> &mut Self {
385        self.config.socks = addr;
386        self
387    }
388
389    /// Sets the `Option` of the authentication method of the SOCKS5
390    /// which is used by outgoing connections.
391    /// The default is `None`.
392    pub fn socks_auth(&mut self, auth: Option<SocksAuth>) -> &mut Self {
393        self.config.socks_auth = auth;
394        self
395    }
396
397    /// Sets the flag whether this node connects to remote nodes
398    /// that have Onion addresses.
399    /// Only outgoing connections are affected.
400    /// The default is `false`.
401    pub fn connect_to_onion(&mut self, b: bool) -> &mut Self {
402        self.config.connect_to_onion = b;
403        self
404    }
405
406    /// Sets the flag whether this node connects to remote nodes
407    /// that have IP addresses.
408    /// Only outgoing connections are affected.
409    /// The default is `false`.
410    pub fn connect_to_ip(&mut self, b: bool) -> &mut Self {
411        self.config.connect_to_ip = b;
412        self
413    }
414
415    /// Sets the flag whether this node can connect to this node itself.
416    /// The default is `false`.
417    pub fn connect_to_myself(&mut self, b: bool) -> &mut Self {
418        self.config.connect_to_myself = b;
419        self
420    }
421
422    /// Sets the user agent which is sent to the nodes
423    /// this node connected to.
424    /// The default is `"/PyBitmessage:0.6.3.2/"`.
425    pub fn user_agent(&mut self, s: UserAgent) -> &mut Self {
426        self.config.user_agent = s;
427        self
428    }
429
430    /// Sets the set of stream numbers
431    /// that this node is interested in.
432    /// The default is `[1]`.
433    pub fn stream_numbers(&mut self, list: StreamNumbers) -> &mut Self {
434        self.config.stream_numbers = list;
435        self
436    }
437
438    /// Sets the list of socket addresses of the seed nodes.
439    /// The default is `[]`.
440    pub fn seeds(&mut self, list: Vec<SocketAddrExt>) -> &mut Self {
441        self.config.seeds = list;
442        self
443    }
444
445    /// Sets the list of socket addresses of the bootstrap domain names.
446    /// The default is `[]`.
447    pub fn bootstraps(&mut self, list: Vec<SocketAddrNode>) -> &mut Self {
448        self.config.bootstraps = list;
449        self
450    }
451
452    /// Sets the maximum number of incoming connections
453    /// this node accepts.
454    /// The default is `160`.
455    pub fn max_incoming_connected(&mut self, n: usize) -> &mut Self {
456        self.config.max_incoming_connected = n;
457        self
458    }
459
460    /// Sets the maximum number of incoming established connections
461    /// this node accepts.
462    /// The default is `128`.
463    pub fn max_incoming_established(&mut self, n: usize) -> &mut Self {
464        self.config.max_incoming_established = n;
465        self
466    }
467
468    /// Sets the maximum number of outgoing conections
469    /// this node initiates.
470    /// The default is `32`.
471    pub fn max_outgoing_initiated(&mut self, n: usize) -> &mut Self {
472        self.config.max_outgoing_initiated = n;
473        self
474    }
475
476    /// Sets the maximum number of outgoing established connections
477    /// this node keeps.
478    /// The default is `8`.
479    pub fn max_outgoing_established(&mut self, n: usize) -> &mut Self {
480        self.config.max_outgoing_established = n;
481        self
482    }
483
484    /// Sets the maximum number of node addresses
485    /// this node memorizes.
486    /// The default is `20000`.
487    pub fn max_nodes(&mut self, n: usize) -> &mut Self {
488        self.config.max_nodes = n;
489        self
490    }
491
492    /// Sets the list of the socket addresses of the own node.
493    /// The default is `[]`.
494    pub fn own_nodes(&mut self, list: Vec<SocketAddrExt>) -> &mut Self {
495        self.config.own_nodes = list;
496        self
497    }
498
499    /// Returns the configuration set this builder represents.
500    pub fn build(&self) -> Config {
501        self.config.clone()
502    }
503}