1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use std::net::SocketAddr as StdSocketAddr;

use koibumi_core::{
    message::{StreamNumbers, UserAgent},
    net::SocketAddrExt,
    Config as CoreConfig,
};

/// A set of configurations for a Koibumi Bitmessage node.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Config {
    core: CoreConfig,
    channel_buffer: usize,

    server: Option<StdSocketAddr>,
    socks: Option<StdSocketAddr>,
    connect_to_onion: bool,
    connect_to_ip: bool,
    connect_to_myself: bool,
    user_agent: UserAgent,
    stream_numbers: StreamNumbers,

    max_outgoing_initiated: usize,
    max_outgoing_established: usize,

    max_nodes: usize,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            core: CoreConfig::default(),
            channel_buffer: 0x10000,

            server: None,
            socks: None,
            connect_to_onion: false,
            connect_to_ip: false,
            connect_to_myself: false,
            user_agent: DEFAULT_USER_AGENT_STR.to_vec().into(),
            stream_numbers: vec![1_u32.into()].into(),

            max_outgoing_initiated: 32,
            max_outgoing_established: 8,

            max_nodes: 20000,
        }
    }
}

impl Config {
    /// Constructs a builder for building a configuration set.
    pub fn builder() -> Builder {
        Builder::new()
    }

    /// Constructs a default configuration set.
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns the core configuration set
    /// which this configuration set contains.
    /// The default is the default of the core configuration set.
    pub fn core(&self) -> &CoreConfig {
        &self.core
    }

    /// Returns the buffer size of channels
    /// which will be created in this node.
    /// The default is `0x10000`.
    pub fn channel_buffer(&self) -> usize {
        self.channel_buffer
    }

    /// Returns the `Option` of the socket address of the server
    /// which listens incomming connections.
    /// If this is `None`, no server will be launched.
    /// The default is `None`.
    pub fn server(&self) -> &Option<StdSocketAddr> {
        &self.server
    }

    /// Returns the `Option` of the socket address of the SOCKS5 server
    /// which is used by outgoing connections.
    /// If this is `None`, connections are directly to Clearnet.
    /// The default is `None`.
    pub fn socks(&self) -> &Option<StdSocketAddr> {
        &self.socks
    }

    /// Returns the flag whether this node connects to remote nodes
    /// that have Onion addresses.
    /// Only outgoing connections are affected.
    /// The default is `false`.
    pub fn connect_to_onion(&self) -> bool {
        self.connect_to_onion
    }

    /// Returns the flag whether this node connects to remote nodes
    /// that have IP addresses.
    /// Only outgoing connections are affected.
    /// The default is `false`.
    pub fn connect_to_ip(&self) -> bool {
        self.connect_to_ip
    }

    /// Returns whether this node can connect to the node
    /// specified by the socket address.
    /// It is affected by the type of the address
    /// which is Onion or IP.
    pub fn is_connectable_to(&self, addr: &SocketAddrExt) -> bool {
        match addr {
            SocketAddrExt::Ipv4(_) => self.connect_to_ip,
            SocketAddrExt::Ipv6(_) => self.connect_to_ip,
            SocketAddrExt::OnionV2(_) => self.connect_to_onion,
            SocketAddrExt::OnionV3(_) => self.connect_to_onion,
        }
    }

    /// Returns the flag whether this node can connect to this node itself.
    /// The default is `false`.
    pub fn connect_to_myself(&self) -> bool {
        self.connect_to_myself
    }

    /// Returns the user agent which is sent to the nodes
    /// this node connected to.
    /// The default is `"/PyBitmessage:0.6.3.2/"`.
    pub fn user_agent(&self) -> &UserAgent {
        &self.user_agent
    }

    /// Returns the set of stream numbers
    /// that this node is interested in.
    /// The default is `[1]`.
    pub fn stream_numbers(&self) -> &StreamNumbers {
        &self.stream_numbers
    }

    /// Returns the maximum number of outgoing conections
    /// this node initiates.
    /// The default is `32`.
    pub fn max_outgoing_initiated(&self) -> usize {
        self.max_outgoing_initiated
    }

    /// Returns the maximum number of outgoing established connections
    /// this node keeps.
    /// The default is `8`.
    pub fn max_outgoing_established(&self) -> usize {
        self.max_outgoing_established
    }

    /// Returns the maximum number of node addresses
    /// this node memorizes.
    /// The default is `20000`.
    pub fn max_nodes(&self) -> usize {
        self.max_nodes
    }
}

/// A builder for building a configuration set
/// for a Koibumi Bitmessage node.
#[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct Builder {
    config: Config,
}

const DEFAULT_USER_AGENT_STR: &[u8] = b"/PyBitmessage:0.6.3.2/";

impl Builder {
    fn new() -> Self {
        Self::default()
    }

    /// Sets the core configuration set
    /// which the configuration set this builder builds contains.
    /// The default is the default of the core configuration set.
    pub fn core(&mut self, core: CoreConfig) -> &mut Self {
        self.config.core = core;
        self
    }

    /// Sets the buffer size of channels
    /// which created in this node.
    /// The default is `0x10000`.
    pub fn channel_buffer(&mut self, v: usize) -> &mut Self {
        self.config.channel_buffer = v;
        self
    }

    /// Sets the `Option` of the socket address of the server
    /// which listens incomming connections.
    /// If this is `None`, no server will be launched.
    /// The default is `None`.
    pub fn server(&mut self, addr: Option<StdSocketAddr>) -> &mut Self {
        self.config.server = addr;
        self
    }

    /// Sets the `Option` of the socket address of the SOCKS5 server
    /// which is used by outgoing connections.
    /// If this is `None`, connections are directly to Clearnet.
    /// The default is `None`.
    pub fn socks(&mut self, addr: Option<StdSocketAddr>) -> &mut Self {
        self.config.socks = addr;
        self
    }

    /// Sets the flag whether this node connects to remote nodes
    /// that have Onion addresses.
    /// Only outgoing connections are affected.
    /// The default is `false`.
    pub fn connect_to_onion(&mut self, b: bool) -> &mut Self {
        self.config.connect_to_onion = b;
        self
    }

    /// Sets the flag whether this node connects to remote nodes
    /// that have IP addresses.
    /// Only outgoing connections are affected.
    /// The default is `false`.
    pub fn connect_to_ip(&mut self, b: bool) -> &mut Self {
        self.config.connect_to_ip = b;
        self
    }

    /// Sets the flag whether this node can connect to this node itself.
    /// The default is `false`.
    pub fn connect_to_myself(&mut self, b: bool) -> &mut Self {
        self.config.connect_to_myself = b;
        self
    }

    /// Sets the user agent which is sent to the nodes
    /// this node connected to.
    /// The default is `"/PyBitmessage:0.6.3.2/"`.
    pub fn user_agent(&mut self, s: UserAgent) -> &mut Self {
        self.config.user_agent = s;
        self
    }

    /// Sets the set of stream numbers
    /// that this node is interested in.
    /// The default is `[1]`.
    pub fn stream_numbers(&mut self, list: StreamNumbers) -> &mut Self {
        self.config.stream_numbers = list;
        self
    }

    /// Sets the maximum number of outgoing conections
    /// this node initiates.
    /// The default is `32`.
    pub fn max_outgoing_initiated(&mut self, n: usize) -> &mut Self {
        self.config.max_outgoing_initiated = n;
        self
    }

    /// Sets the maximum number of outgoing established connections
    /// this node keeps.
    /// The default is `8`.
    pub fn max_outgoing_established(&mut self, n: usize) -> &mut Self {
        self.config.max_outgoing_established = n;
        self
    }

    /// Sets the maximum number of node addresses
    /// this node memorizes.
    /// The default is `20000`.
    pub fn max_nodes(&mut self, n: usize) -> &mut Self {
        self.config.max_nodes = n;
        self
    }

    /// Returns the configuration set this builder represents.
    pub fn build(&self) -> Config {
        self.config.clone()
    }
}