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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use ipnet::Ipv4Net;
use serde::{de, Deserialize, Deserializer};

#[allow(unused_imports)]
use crate::_prelude::*;
use crate::{
	resolver::{AsyncTrustDnsResolver, Resolver, RESERVED_V4_SUBNETS, RESERVED_V6_SUBNETS},
	types,
};

type Result<T> = types::Result<T>;

#[derive(Clone, Debug)]
pub struct CLevel(pub Level);

impl Deref for CLevel {
	type Target = Level;

	fn deref(&self) -> &Self::Target {
		&self.0
	}
}

impl<'de> Deserialize<'de> for CLevel {
	fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<CLevel, D::Error> {
		let s: String = Deserialize::deserialize(deserializer)?;
		Level::from_str(&s).map(CLevel).map_err(de::Error::custom)
	}
}

#[derive(Clone, Debug)]
pub struct CIP4Addr(pub Ipv4Addr);

impl Deref for CIP4Addr {
	type Target = Ipv4Addr;

	fn deref(&self) -> &Self::Target {
		&self.0
	}
}

impl<'de> Deserialize<'de> for CIP4Addr {
	fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<CIP4Addr, D::Error> {
		let s: String = Deserialize::deserialize(deserializer)?;
		let addr: Ipv4Addr = s.parse().map_err(de::Error::custom)?;
		Ok(CIP4Addr(addr))
	}
}

#[derive(Clone, Debug)]
pub struct CIP6Addr(pub Ipv6Addr);

impl Deref for CIP6Addr {
	type Target = Ipv6Addr;

	fn deref(&self) -> &Self::Target {
		&self.0
	}
}

impl<'de> Deserialize<'de> for CIP6Addr {
	fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<CIP6Addr, D::Error> {
		let s: String = Deserialize::deserialize(deserializer)?;
		let addr: Ipv6Addr = s.parse().map_err(de::Error::custom)?;
		Ok(CIP6Addr(addr))
	}
}

#[derive(Clone, Debug)]
pub struct CIpv4Net(pub Ipv4Net);

impl Deref for CIpv4Net {
	type Target = Ipv4Net;

	fn deref(&self) -> &Self::Target {
		&self.0
	}
}

impl<'de> Deserialize<'de> for CIpv4Net {
	fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<CIpv4Net, D::Error> {
		let s: String = Deserialize::deserialize(deserializer)?;
		s.parse::<Ipv4Net>().map(CIpv4Net).map_err(de::Error::custom)
	}
}

#[derive(Clone, Debug)]
pub struct CBytes(pub usize);

impl Deref for CBytes {
	type Target = usize;

	fn deref(&self) -> &Self::Target {
		&self.0
	}
}

impl<'de> Deserialize<'de> for CBytes {
	fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<CBytes, D::Error> {
		let s: String = Deserialize::deserialize(deserializer)?;
		let s = s.replace("_", "");
		let v = s.parse::<humanize_rs::bytes::Bytes>();
		let r = v.map_err(de::Error::custom)?;
		Ok(CBytes(r.size()))
	}
}

#[derive(Clone, Debug)]
pub struct CDuration(Duration);

impl Deref for CDuration {
	type Target = Duration;

	fn deref(&self) -> &Self::Target {
		&self.0
	}
}

impl CDuration {
	pub fn from_secs(secs: u64) -> Self {
		CDuration(Duration::from_secs(secs))
	}

	pub fn from_millis(millis: u64) -> Self {
		CDuration(Duration::from_millis(millis))
	}
}

impl<'de> Deserialize<'de> for CDuration {
	fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<CDuration, D::Error> {
		let s: String = Deserialize::deserialize(deserializer)?;
		let s = s.replace("_", "");
		let v = humanize_rs::duration::parse(&s);
		let r = v.map_err(de::Error::custom)?;
		Ok(CDuration(r))
	}
}

pub type AsyncTrustDnsResolverConfig = trust_dns_resolver::config::ResolverConfig;
pub struct AsyncTrustDnsResolverOptsMapping(trust_dns_resolver::config::ResolverOpts);

impl Deref for AsyncTrustDnsResolverOptsMapping {
	type Target = trust_dns_resolver::config::ResolverOpts;

	fn deref(&self) -> &Self::Target {
		&self.0
	}
}

#[derive(Clone, Debug, Deserialize)]
#[serde(default)]
pub struct AsyncTrustDnsResolverOpts {
	pub ndots:                  usize,
	pub timeout:                CDuration,
	pub attempts:               usize,
	pub rotate:                 bool,
	pub check_names:            bool,
	pub edns0:                  bool,
	pub validate:               bool,
	pub ip_strategy:            trust_dns_resolver::config::LookupIpStrategy,
	pub cache_size:             usize,
	pub use_hosts_file:         bool,
	pub positive_min_ttl:       Option<CDuration>,
	pub negative_min_ttl:       Option<CDuration>,
	pub positive_max_ttl:       Option<CDuration>,
	pub negative_max_ttl:       Option<CDuration>,
	pub num_concurrent_reqs:    usize,
	pub preserve_intermediates: bool,
}

impl From<AsyncTrustDnsResolverOpts> for AsyncTrustDnsResolverOptsMapping {
	fn from(p: AsyncTrustDnsResolverOpts) -> AsyncTrustDnsResolverOptsMapping {
		AsyncTrustDnsResolverOptsMapping(trust_dns_resolver::config::ResolverOpts {
			ndots:                  p.ndots,
			timeout:                *p.timeout,
			attempts:               p.attempts,
			rotate:                 p.rotate,
			check_names:            p.check_names,
			edns0:                  p.edns0,
			validate:               p.validate,
			ip_strategy:            p.ip_strategy,
			cache_size:             p.cache_size,
			use_hosts_file:         p.use_hosts_file,
			positive_min_ttl:       p.positive_min_ttl.map(|v| *v),
			negative_min_ttl:       p.negative_min_ttl.map(|v| *v),
			positive_max_ttl:       p.positive_max_ttl.map(|v| *v),
			negative_max_ttl:       p.negative_max_ttl.map(|v| *v),
			num_concurrent_reqs:    p.num_concurrent_reqs,
			preserve_intermediates: p.preserve_intermediates,
		})
	}
}

impl From<AsyncTrustDnsResolverOptsMapping> for AsyncTrustDnsResolverOpts {
	fn from(p: AsyncTrustDnsResolverOptsMapping) -> AsyncTrustDnsResolverOpts {
		AsyncTrustDnsResolverOpts {
			ndots:                  p.ndots,
			timeout:                CDuration(p.timeout),
			attempts:               p.attempts,
			rotate:                 p.rotate,
			check_names:            p.check_names,
			edns0:                  p.edns0,
			validate:               p.validate,
			ip_strategy:            p.ip_strategy,
			cache_size:             p.cache_size,
			use_hosts_file:         p.use_hosts_file,
			positive_min_ttl:       p.positive_min_ttl.map(CDuration),
			negative_min_ttl:       p.negative_min_ttl.map(CDuration),
			positive_max_ttl:       p.positive_max_ttl.map(CDuration),
			negative_max_ttl:       p.negative_max_ttl.map(CDuration),
			num_concurrent_reqs:    p.num_concurrent_reqs,
			preserve_intermediates: p.preserve_intermediates,
		}
	}
}

impl Default for AsyncTrustDnsResolverOpts {
	fn default() -> Self {
		AsyncTrustDnsResolverOptsMapping(trust_dns_resolver::config::ResolverOpts::default()).into()
	}
}

#[derive(Clone, Debug, Deserialize)]
pub struct ConcurrencyProfile {
	pub parser_concurrency: usize,
	pub parser_pin:         usize,
	pub domain_concurrency: usize,
}

impl Default for ConcurrencyProfile {
	fn default() -> Self {
		let physical_cores = num_cpus::get_physical();
		Self { parser_concurrency: physical_cores, parser_pin: 0, domain_concurrency: physical_cores * 40 }
	}
}

impl ConcurrencyProfile {
	pub fn transit_buffer_size(&self) -> usize {
		self.domain_concurrency * 10
	}

	pub fn job_tx_buffer_size(&self) -> usize {
		self.domain_concurrency
	}

	pub fn job_update_buffer_size(&self) -> usize {
		self.domain_concurrency * 2
	}
}

#[derive(Clone, Debug, Deserialize)]
pub struct NetworkingProfileValues {
	pub connect_timeout:          Option<CDuration>,
	pub socket_read_buffer_size:  Option<CBytes>,
	pub socket_write_buffer_size: Option<CBytes>,
	pub bind_local_ipv4:          Vec<CIP4Addr>,
	pub bind_local_ipv6:          Vec<CIP6Addr>,
}

impl Default for NetworkingProfileValues {
	fn default() -> Self {
		Self {
			connect_timeout:          Some(CDuration::from_secs(5)),
			socket_write_buffer_size: Some(CBytes(32 * 1024)),
			socket_read_buffer_size:  Some(CBytes(32 * 1024)),
			bind_local_ipv4:          vec![],
			bind_local_ipv6:          vec![],
		}
	}
}

#[derive(Clone, Debug, Default, Deserialize)]
pub struct ResolverConfig {
	#[serde(default)]
	config:  Option<AsyncTrustDnsResolverConfig>,
	#[serde(default)]
	options: Option<AsyncTrustDnsResolverOpts>,
}

#[derive(Clone, Debug, Deserialize)]
pub struct NetworkingProfile {
	pub values:           NetworkingProfileValues,
	pub resolver_config:  ResolverConfig,
	pub net_v4_blacklist: Vec<CIpv4Net>,

	#[serde(skip)]
	resolver: Option<Arc<Box<dyn Resolver>>>,
}

impl Default for NetworkingProfile {
	fn default() -> Self {
		Self {
			values:           NetworkingProfileValues::default(),
			resolver:         None,
			resolver_config:  ResolverConfig::default(),
			net_v4_blacklist: vec![],
		}
	}
}

impl NetworkingProfile {
	pub fn resolve(self) -> Result<ResolvedNetworkingProfile> {
		ResolvedNetworkingProfile::new(self)
	}
}

#[derive(Clone, Debug)]
pub struct ResolvedNetworkingProfile {
	pub values: NetworkingProfileValues,

	pub resolver: Arc<Box<dyn Resolver>>,
}

impl ResolvedNetworkingProfile {
	fn new(p: NetworkingProfile) -> Result<Self> {
		let values = p.values;

		if let Some(r) = p.resolver {
			return Ok(Self { values, resolver: r })
		}

		let (config, options) =
			if let (Some(config), Some(options)) = (p.resolver_config.config, p.resolver_config.options) {
				(config, options.into())
			} else {
				let (config, options) = trust_dns_resolver::system_conf::read_system_conf()
					.context("cannot read resolver config settings from system")?;
				(config, AsyncTrustDnsResolverOptsMapping(options))
			};

		let mut resolver = AsyncTrustDnsResolver::new(config, *options).context("cannot create default resolver")?;

		let reserved_v4 = RESERVED_V4_SUBNETS
			.clone()
			.into_iter()
			.chain(p.net_v4_blacklist.into_iter().map(|a| *a))
			.collect::<Vec<_>>();
		resolver.with_net_v4_blacklist(reserved_v4);
		resolver.with_net_v6_blacklist(RESERVED_V6_SUBNETS.to_vec());
		Ok(Self { values, resolver: Arc::new(Box::new(resolver)) })
	}
}

#[derive(Clone, Debug, Deserialize)]
pub struct CrawlingSettings {
	pub internal_read_buffer_size: CBytes,
	pub concurrency:               usize,
	pub max_response_size:         CBytes,
	pub delay:                     CDuration,
	pub delay_jitter:              CDuration,
	pub status_timeout:            CDuration,
	pub load_timeout:              CDuration,
	pub job_soft_timeout:          CDuration,
	pub job_hard_timeout:          CDuration,
	pub custom_headers:            HashMap<String, Vec<String>>,
	pub user_agent:                Option<String>,
	pub compression:               bool,
}

impl fmt::Display for CrawlingSettings {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		write!(
			f,
			"concurrency: {}, delay: {:?}, job hard timeout: {:?}, job soft timeout: {:?}, irbs: {:?}, load timeout: {:?}, max_response_size: {:?}, custom headers: {:?}",
			self.concurrency, self.delay, self.job_hard_timeout, self.job_soft_timeout, self.internal_read_buffer_size, self.load_timeout, self.max_response_size, self.custom_headers,
		)
	}
}

impl Default for CrawlingSettings {
	fn default() -> Self {
		Self {
			concurrency:               2,
			internal_read_buffer_size: CBytes(32 * 1024),
			delay:                     CDuration::from_secs(1),
			delay_jitter:              CDuration::from_millis(1000),
			job_hard_timeout:          CDuration::from_secs(60),
			job_soft_timeout:          CDuration::from_secs(30),
			status_timeout:            CDuration::from_secs(5),
			load_timeout:              CDuration::from_secs(10),
			user_agent:                Some(String::from("crusty-core/0.66.1")),
			compression:               true,
			custom_headers:            HashMap::new(),
			max_response_size:         CBytes(1024 * 1024 * 2),
		}
		.build_headers()
	}
}

impl CrawlingSettings {
	pub fn build_headers(mut self) -> Self {
		if let Some(user_agent) = &self.user_agent {
			self.custom_headers.insert(http::header::USER_AGENT.to_string(), vec![user_agent.clone()]);
		}
		if self.compression {
			self.custom_headers.insert(http::header::ACCEPT_ENCODING.to_string(), vec!["gzip, deflate".into()]);
		}
		self.custom_headers.insert(
			http::header::ACCEPT.to_string(),
			vec!["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9".into()],
		);
		self
	}
}