redirectionio 3.1.0

Redirection IO Library to handle matching rule, redirect and filtering headers and body.
Documentation
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
extern crate cbindgen;
extern crate libtool;
extern crate serde;
extern crate serde_yaml;

use std::{
    collections::HashMap,
    env,
    fs::{DirEntry, read_dir, read_to_string},
    path::Path,
};

use glob::glob;
use html_to_markdown_rs::ConversionOptions;
use linked_hash_set::LinkedHashSet;
use serde::{Deserialize, Serialize};
use serde_yaml::from_str as yaml_decode;
use tera::{Context, Tera, Value, try_get_value};
#[derive(Serialize, Deserialize, Debug, Clone)]
struct RuleSet {
    #[serde(default)]
    config: RouterConfig,
    rules: HashMap<String, RuleInput>,
    tests: Vec<RuleTest>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct RouterConfig {
    #[serde(default = "default_as_false")]
    pub ignore_host_case: bool,
    #[serde(default = "default_as_false")]
    pub ignore_header_case: bool,
    #[serde(default = "default_as_false")]
    pub ignore_path_and_query_case: bool,
    #[serde(default)]
    pub ignore_marketing_query_params: bool,
    #[serde(default = "default_as_false")]
    pub ignore_all_query_parameters: bool,
    #[serde(default = "default_marketing_parameters")]
    pub marketing_query_params: LinkedHashSet<String>,
    #[serde(default)]
    pub pass_marketing_query_params_to_target: bool,
    #[serde(default = "default_as_false")]
    pub always_match_any_host: bool,
    #[serde(default = "default_as_true")]
    pub ignore_query_param_order: bool,
}

fn default_as_false() -> bool {
    false
}

fn default_as_true() -> bool {
    true
}

fn default_marketing_parameters() -> LinkedHashSet<String> {
    let mut parameters = LinkedHashSet::new();

    parameters.insert("utm_source".to_string());
    parameters.insert("utm_medium".to_string());
    parameters.insert("utm_campaign".to_string());
    parameters.insert("utm_term".to_string());
    parameters.insert("utm_content".to_string());

    parameters
}

impl Default for RouterConfig {
    fn default() -> Self {
        let mut parameters = LinkedHashSet::new();

        parameters.insert("utm_source".to_string());
        parameters.insert("utm_medium".to_string());
        parameters.insert("utm_campaign".to_string());
        parameters.insert("utm_term".to_string());
        parameters.insert("utm_content".to_string());

        Self {
            ignore_host_case: false,
            ignore_header_case: false,
            ignore_path_and_query_case: false,
            ignore_marketing_query_params: true,
            ignore_all_query_parameters: false,
            marketing_query_params: parameters,
            pass_marketing_query_params_to_target: true,
            always_match_any_host: false,
            ignore_query_param_order: true,
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct RuleInput {
    #[serde(rename = "agentInput")]
    agent_input: Rule,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct Rule {
    id: Option<String>,
    source: Source,
    #[serde(skip_serializing_if = "Option::is_none")]
    target: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(alias = "redirect_code")]
    status_code: Option<u16>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    header_filters: Vec<HeaderFilter>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    body_filters: Vec<BodyFilter>,
    rank: Option<u16>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    markers: Vec<Marker>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    variables: Vec<Variable>,
    #[serde(skip_serializing_if = "Option::is_none")]
    log_override: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    reset: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    stop: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    peer_override: Option<Peer>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct Source {
    #[serde(skip_serializing_if = "Option::is_none")]
    host: Option<String>,
    path: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    query: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    headers: Option<Vec<SourceHeader>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    methods: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    exclude_methods: Option<bool>,
    #[serde(skip_serializing_if = "Vec::is_empty", default, with = "serde_yaml::with::singleton_map_recursive")]
    ips: Vec<IpConstraint>,
    #[serde(skip_serializing_if = "Option::is_none")]
    datetime: Option<Vec<DateTimeConstraint>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    time: Option<Vec<DateTimeConstraint>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    weekdays: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    response_status_codes: Option<Vec<u16>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    exclude_response_status_codes: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    sampling: Option<u32>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct SourceHeader {
    name: String,
    #[serde(rename = "type")]
    kind: String,
    value: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct HeaderFilter {
    action: String,
    header: String,
    value: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct BodyFilter {
    action: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    content: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    value: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    element_tree: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    css_selector: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    ignore_css_selector: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    options: Option<ConversionOptions>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct Marker {
    name: String,
    regex: String,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    transformers: Vec<Transformer>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct Transformer {
    #[serde(rename = "type")]
    transformer_type: String,
    options: Option<HashMap<String, String>>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub enum VariableKind {
    Marker(String),
    RequestHeader { name: String, default: Option<String> },
    RequestHost,
    RequestMethod,
    RequestPath,
    RequestRemoteAddress,
    RequestScheme,
    RequestTime,
    HtmlBody { selector: String, default: Option<String> },
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Variable {
    pub name: String,
    #[serde(rename = "type")]
    #[serde(with = "serde_yaml::with::singleton_map")]
    kind: VariableKind,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    transformers: Vec<Transformer>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub enum IpConstraint {
    InRange(String),
    NotInRange(String),
    NotOneOf(Vec<String>),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct DateTimeConstraint(pub Option<String>, pub Option<String>);

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Peer {
    pub address: String,
    pub sni_host: String,
    pub request_host: String,
    pub allow_invalid_certificates: bool,
    pub tls: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct RuleTest {
    uri: String,
    host: Option<String>,
    scheme: Option<String>,
    remote_ip: Option<String>,
    datetime: Option<String>,
    method: Option<String>,
    headers: Option<Vec<RuleTestHeader>>,
    response_status_code: Option<u16>,
    #[serde(rename = "match")]
    should_match: bool,
    location: Option<String>,
    status: Option<u16>,
    should_filter_body: Option<ShouldFilterBody>,
    should_filter_header: Option<ShouldFilterHeader>,
    should_not_log: Option<bool>,
    sampling_override: Option<bool>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct RuleTestHeader {
    name: String,
    value: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct ShouldFilterBody {
    enable: bool,
    #[serde(default = "default_false")]
    is_binary: bool,
    original_body: String,
    expected_body: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum Body {
    String(String),
    Bytes(Vec<u8>),
}

fn default_false() -> bool {
    false
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct ShouldFilterHeader {
    enable: bool,
    #[serde(default)]
    original_headers: Vec<RuleTestHeader>,
    #[serde(default)]
    expected_headers: Vec<RuleTestHeader>,
    #[serde(default)]
    not_expected_headers: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct RuleSetList {
    rule_sets: HashMap<String, RuleSet>,
}

fn main() {
    let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
    let target_dir_str = env::var("CARGO_TARGET_DIR").ok().unwrap_or_else(|| format!("{}/target", crate_dir));
    let package_name = env::var("CARGO_PKG_NAME").unwrap();
    let target_dir = Path::new(target_dir_str.as_str());
    let output_file = target_dir.join(format!("{package_name}.h"));

    if env::var("DOCS_RS").is_ok() {
        return;
    }

    cbindgen::generate(crate_dir)
        .expect("Unable to generate bindings")
        .write_to_file(output_file);

    make_router_tests();
    make_test_examples_tests();
}

fn make_test_examples_tests() {
    let mut names: Vec<String> = Vec::new();
    for path in glob("tests/test_examples/*.in.json")
        .expect("invalid glob pattern")
        .filter_map(Result::ok)
    {
        let path = path.to_str().unwrap();
        let name = path.replace("tests/test_examples/", "").replace(".in.json", "");
        names.push(name);
    }

    let templating = Tera::new("tests/templates/**/*").expect("cannot load templates");
    let test_path = Path::new("tests/redirectionio_test_examples.rs");
    let mut context = Context::default();
    context.insert("names", &names);
    let test_content = templating
        .render("redirectionio_test_examples.rs.j2", &context)
        .expect("cannot generate");

    // we avoid rewriting the file to keep rust cache as must as possible
    if test_path.exists() {
        let existing_content = std::fs::read_to_string(test_path).expect("cannot read");

        if existing_content != test_content {
            std::fs::write(test_path, test_content).expect("cannot write");
        }
    } else {
        std::fs::write(test_path, test_content).expect("cannot write");
    }
}

fn make_router_tests() {
    let rule_sets = read_router_tests("../../tests/rules");

    if rule_sets.is_empty() {
        return;
    }

    let mut templating = match Tera::new("tests/templates/**/*") {
        Ok(t) => t,
        Err(e) => panic!("{}", e),
    };

    templating.register_filter("as_bytes", filter_as_bytes_str);

    let rule_sets_list = RuleSetList { rule_sets };
    let test_path = Path::new("tests/redirectionio_router_test.rs");

    let context = Context::from_serialize(&rule_sets_list).expect("cannot serialize");
    let test_content = templating
        .render("redirectionio_router_test.rs.j2", &context)
        .expect("cannot generate");

    // we avoid rewriting the file to keep rust cache as much as possible
    if test_path.exists() {
        let existing_content = std::fs::read_to_string(test_path).expect("cannot read");

        if existing_content != test_content {
            std::fs::write(test_path, test_content).expect("cannot write");
        }
    } else {
        std::fs::write(test_path, test_content).expect("cannot write");
    }
}

fn filter_as_bytes_str(value: &Value, _: &HashMap<String, Value>) -> tera::Result<Value> {
    let body = try_get_value!("as_bytes", "value", String, value);

    Ok(Value::String(
        body.as_bytes()
            .iter()
            .map(|b| format!("\\x{b:02x}"))
            .collect::<Vec<String>>()
            .join(""),
    ))
}

fn read_router_tests(path: &str) -> HashMap<String, RuleSet> {
    let mut rule_sets = HashMap::new();

    match read_dir(path) {
        Err(_) => return rule_sets,
        Ok(directory) => {
            for file in directory.flatten() {
                if let Ok(file_type) = file.file_type() {
                    if file_type.is_dir() {
                        rule_sets.extend(read_router_tests(file.path().to_str().unwrap()))
                    } else if file_type.is_file() {
                        match file.path().extension() {
                            None => (),
                            Some(ext) => {
                                if ext == "yml" {
                                    let (key, rule_set) = build_router_test_file(file).expect("");

                                    rule_sets.insert(key, rule_set);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    rule_sets
}

fn build_router_test_file(file: DirEntry) -> std::io::Result<(String, RuleSet)> {
    let content = read_to_string(file.path())?;
    let mut rule_set: RuleSet = yaml_decode(content.as_str()).expect("error");

    for (id, rule) in &mut rule_set.rules {
        rule.agent_input.id = Some(id.clone());
        rule.agent_input.rank = Some(rule.agent_input.rank.unwrap_or(0));
    }

    let name = file
        .path()
        .file_name()
        .unwrap()
        .to_str()
        .unwrap()
        .to_string()
        .replace(".yml", "")
        .replace('-', "_");

    Ok((name, rule_set))
}