use std::collections::BTreeMap;
use std::net::{Ipv4Addr, Ipv6Addr};
use coip_builder::{BuildOptions, CoipBuilder, PruneConfig};
use coip_format::{AliasRule, Value};
fn make_record(org: &str, country: &str, name: &str, risk: u8) -> BTreeMap<String, Value> {
let mut country_map = BTreeMap::new();
country_map.insert("iso_code".into(), Value::Utf8(country.into()));
country_map.insert("name".into(), Value::Utf8(name.into()));
let mut root = BTreeMap::new();
root.insert("org".into(), Value::Utf8(org.into()));
root.insert("country".into(), Value::Map(country_map));
root.insert("risk_score".into(), Value::U8(risk));
root
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut builder = CoipBuilder::new(BuildOptions::compact());
builder.set_metadata_field("database_type", serde_json::json!("Custom-Threat-IP"));
builder.add_ipv6_alias(AliasRule {
prefix: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 0, 0, 0],
prefix_len: 96,
v4_bit_offset: 96,
});
builder.insert_ipv4_cidr(
Ipv4Addr::new(10, 20, 0, 0),
16,
make_record("Corp-Default", "US", "United States", 10),
)?;
builder.insert_ipv4_cidr(
Ipv4Addr::new(10, 20, 30, 0),
24,
make_record("Corp-DMZ", "DE", "Germany", 85),
)?;
builder.delete_ipv4_cidr(Ipv4Addr::new(10, 20, 30, 1), 32)?;
builder.insert_ipv6_cidr(
"2001:db8:abcd::".parse::<Ipv6Addr>()?,
48,
make_record("IPv6-Pool", "JP", "Japan", 20),
)?;
builder.insert_ipv6_cidr(
"2001:db8:abcd::99".parse::<Ipv6Addr>()?,
128,
make_record("IPv6-Honeypot", "JP", "Japan", 99),
)?;
let (full_bytes, full_stats) = builder.build()?;
println!(
"Built full .coi: {} bytes ({} unique records, {} IPv4 transitions, {} IPv6 transitions)",
full_bytes.len(),
full_stats.record_count,
full_stats.v4_transitions,
full_stats.v6_transitions
);
let mut pruned_opts = BuildOptions::ultrafast();
pruned_opts.prune = Some(PruneConfig {
keep_fields: Some(vec![
"org".into(),
"country.iso_code".into(),
"country.name".into(),
]),
});
let mut builder2 = CoipBuilder::new(pruned_opts);
builder2.insert_ipv4_cidr(
Ipv4Addr::new(10, 20, 0, 0),
16,
make_record("Corp-Default", "US", "United States", 10),
)?;
let (pruned_bytes, pruned_stats) = builder2.build()?;
println!(
"Built pruned UltraFast .coi: {} bytes ({} unique records)",
pruned_bytes.len(),
pruned_stats.record_count
);
Ok(())
}