-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.rs
204 lines (172 loc) · 6.4 KB
/
config.rs
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
use caracat::rate_limiter::RateLimitingMethod;
use config::Config;
use std::net::{Ipv4Addr, Ipv6Addr};
use crate::utils::generate_id;
#[derive(Debug, Clone)]
pub struct AgentConfig {
/// Agent identifier.
/// Default: random
pub id: String,
}
#[derive(Debug, Clone)]
pub struct CaracatConfig {
/// Number of probes to send before calling the rate limiter.
/// Default: 100
pub batch_size: u64,
/// Identifier encoded in the probes (random by default).
/// Default: 0
pub instance_id: u16,
/// Whether to actually send the probes on the network or not.
/// Default: false
pub dry_run: bool,
/// Do not send probes with ttl < min_ttl.
/// Default: None
pub min_ttl: Option<u8>,
/// Do not send probes with ttl > max_ttl.
/// Default: None
pub max_ttl: Option<u8>,
/// Check that replies match valid probes.
/// Default: false
pub integrity_check: bool,
/// Interface from which to send the packets.
/// Default: default interface
pub interface: String,
/// Source IPv4 address
/// Default: None
pub src_ipv4_addr: Option<Ipv4Addr>,
/// Source IPv6 address
/// Default: None
pub src_ipv6_addr: Option<Ipv6Addr>,
/// Maximum number of probes to send (unlimited by default).
/// Default: None
pub max_probes: Option<u64>,
/// Number of packets to send per probe.
/// Default: 1
pub packets: u64,
/// Probing rate in packets per second.
/// Default: 100
pub probing_rate: u64,
/// Method to use to limit the packets rate.
/// Default: Auto
pub rate_limiting_method: RateLimitingMethod,
}
#[derive(Debug, Clone)]
pub struct KafkaConfig {
/// Kafka brokers
/// Default: localhost:9092
pub brokers: String,
/// Kafka Authentication Protocol
/// Default: PLAINTEXT
pub auth_protocol: String,
/// Kafka Authentication SASL Username
/// Default: saimiris
pub auth_sasl_username: String,
/// Kafka Authentication SASL Password
/// Default: saimiris
pub auth_sasl_password: String,
/// Kafka Authentication SASL Mechanism
/// Default: SCRAM-SHA-512
pub auth_sasl_mechanism: String,
/// Kafka message max bytes
/// Default: 1048576
pub message_max_bytes: usize,
/// Kafka consumer topics (comma separated)
/// Default: saimiris-targets
pub in_topics: String,
/// Kafka consumer group ID
/// Default: saimiris-agent
pub in_group_id: String,
/// Enable Kafka producer
/// Default: true
pub out_enable: bool,
/// Kafka producer topic
/// Default: saimiris-results
pub out_topic: String,
/// Kafka producer batch wait time
/// Default: 1000
pub out_batch_wait_time: u64,
/// Kafka producer batch wait interval
/// Default: 100
pub out_batch_wait_interval: u64,
}
#[derive(Debug, Clone)]
pub struct AppConfig {
pub agent: AgentConfig,
pub caracat: CaracatConfig,
pub kafka: KafkaConfig,
}
fn load_config(config_path: &str) -> Config {
Config::builder()
.add_source(config::File::with_name(config_path))
.add_source(config::Environment::with_prefix("SAIMIRIS"))
.build()
.unwrap()
}
pub fn app_config(config_path: &str) -> AppConfig {
let config = load_config(config_path);
AppConfig {
// Agent configuration
agent: AgentConfig {
id: config
.get_string("agent.id")
.unwrap_or(generate_id(None, None)),
},
// Caracat configuration
caracat: CaracatConfig {
batch_size: config.get_int("caracat.batch_size").unwrap_or(100) as u64,
instance_id: config.get_int("caracat.instance_id").unwrap_or(0) as u16,
dry_run: config.get_bool("caracat.dry_run").unwrap_or(false),
min_ttl: config.get_int("caracat.min_ttl").ok().map(|x| x as u8),
max_ttl: config.get_int("caracat.max_ttl").ok().map(|x| x as u8),
integrity_check: config.get_bool("caracat.integrity_check").unwrap_or(false),
interface: config
.get_string("caracat.interface")
.unwrap_or(caracat::utilities::get_default_interface()),
src_ipv4_addr: config
.get_string("caracat.src_ipv4_addr")
.ok()
.and_then(|x| x.parse().ok()),
src_ipv6_addr: config
.get_string("caracat.src_ipv6_addr")
.ok()
.and_then(|x| x.parse().ok()),
max_probes: config.get_int("caracat.max_probes").ok().map(|x| x as u64),
packets: config.get_int("caracat.packets").unwrap_or(1) as u64,
probing_rate: config.get_int("caracat.probing_rate").unwrap_or(100) as u64,
rate_limiting_method: caracat::rate_limiter::RateLimitingMethod::Auto, // TODO
},
// Kafka configuration
kafka: KafkaConfig {
brokers: config
.get_string("kafka.brokers")
.unwrap_or("localhost:9092".to_string()),
auth_protocol: config
.get_string("kafka.auth_protocol")
.unwrap_or("PLAINTEXT".to_string()),
auth_sasl_username: config
.get_string("kafka.auth_sasl_username")
.unwrap_or("saimiris".to_string()),
auth_sasl_password: config
.get_string("kafka.auth_sasl_password")
.unwrap_or("saimiris".to_string()),
auth_sasl_mechanism: config
.get_string("kafka.auth_sasl_mechanism")
.unwrap_or("SCRAM-SHA-512".to_string()),
message_max_bytes: config.get_int("kafka.message_max_bytes").unwrap_or(990000) as usize,
in_topics: config
.get_string("kafka.in_topics")
.unwrap_or("saimiris-targets".to_string()),
in_group_id: config
.get_string("kafka.in_group_id")
.unwrap_or("saimiris-agent".to_string()),
out_enable: config.get_bool("kafka.out_enable").unwrap_or(true),
out_topic: config
.get_string("kafka.out_topic")
.unwrap_or("saimiris-results".to_string()),
out_batch_wait_time: config.get_int("kafka.out_batch_wait_time").unwrap_or(1000) as u64,
out_batch_wait_interval: config
.get_int("kafka.out_batch_wait_interval")
.unwrap_or(100) as u64,
},
}
}