1use std::collections::{BTreeMap, BTreeSet};
8use std::net::{Ipv4Addr, Ipv6Addr};
9
10use coip_builder::{BuildOptions, CoipBuilder, PruneConfig};
11use coip_format::{AliasRule, Value};
12
13fn make_record(
14 org: &str,
15 country: &str,
16 en_name: &str,
17 zh_name: &str,
18 risk: u8,
19) -> BTreeMap<String, Value> {
20 let mut names = BTreeMap::new();
21 names.insert("en".into(), Value::Utf8(en_name.into()));
22 names.insert("zh-CN".into(), Value::Utf8(zh_name.into()));
23 names.insert("de".into(), Value::Utf8(format!("DE-{en_name}")));
24
25 let mut country_map = BTreeMap::new();
26 country_map.insert("iso_code".into(), Value::Utf8(country.into()));
27 country_map.insert("names".into(), Value::Map(names));
28
29 let mut root = BTreeMap::new();
30 root.insert("org".into(), Value::Utf8(org.into()));
31 root.insert("country".into(), Value::Map(country_map));
32 root.insert("risk_score".into(), Value::U8(risk));
33 root
34}
35
36fn main() -> Result<(), Box<dyn std::error::Error>> {
37 let mut builder = CoipBuilder::new(BuildOptions::compact());
38 builder.set_metadata_field("database_type", serde_json::json!("Custom-Threat-IP"));
39 builder.add_ipv6_alias(AliasRule {
40 prefix: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 0, 0, 0],
41 prefix_len: 96,
42 v4_bit_offset: 96,
43 });
44
45 builder.insert_ipv4_cidr(
47 Ipv4Addr::new(10, 20, 0, 0),
48 16,
49 make_record("Corp-Default", "US", "United States", "美国", 10),
50 )?;
51 builder.insert_ipv4_cidr(
52 Ipv4Addr::new(10, 20, 30, 0),
53 24,
54 make_record("Corp-DMZ", "DE", "Germany", "德国", 85),
55 )?;
56 builder.delete_ipv4_cidr(Ipv4Addr::new(10, 20, 30, 1), 32)?;
57
58 builder.insert_ipv6_cidr(
60 "2001:db8:abcd::".parse::<Ipv6Addr>()?,
61 48,
62 make_record("IPv6-Pool", "JP", "Japan", "日本", 20),
63 )?;
64 builder.insert_ipv6_cidr(
65 "2001:db8:abcd::99".parse::<Ipv6Addr>()?,
66 128,
67 make_record("IPv6-Honeypot", "JP", "Japan", "日本", 99),
68 )?;
69
70 let (full_bytes, full_stats) = builder.build()?;
71 println!(
72 "Built full .coi: {} bytes ({} unique records, {} IPv4 transitions, {} IPv6 transitions)",
73 full_bytes.len(),
74 full_stats.record_count,
75 full_stats.v4_transitions,
76 full_stats.v6_transitions
77 );
78
79 let mut pruned_opts = BuildOptions::ultrafast();
81 pruned_opts.prune = Some(PruneConfig {
82 keep_fields: Some(vec![
83 "org".into(),
84 "country.iso_code".into(),
85 "country.names".into(),
86 ]),
87 languages: Some(BTreeSet::from(["en".into(), "zh-CN".into()])),
88 });
89 let mut builder2 = CoipBuilder::new(pruned_opts);
90 builder2.insert_ipv4_cidr(
91 Ipv4Addr::new(10, 20, 0, 0),
92 16,
93 make_record("Corp-Default", "US", "United States", "美国", 10),
94 )?;
95 let (pruned_bytes, pruned_stats) = builder2.build()?;
96 println!(
97 "Built pruned UltraFast .coi: {} bytes ({} unique records)",
98 pruned_bytes.len(),
99 pruned_stats.record_count
100 );
101
102 Ok(())
103}