bgpexplorer 0.2.0

This is a BGP route explorer for routing information database with ability to drill-down routes change history
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
use crate::*;
use crate::config::*;
use chrono::prelude::*;
use dnssector::*;
use regex::Regex;
use serde::de::{Deserialize, Deserializer, MapAccess, SeqAccess, Visitor};
use serde::ser::SerializeStruct;
use std::collections::HashMap;
use whois_rust::{WhoIs, WhoIsError, WhoIsLookupOptions, WhoIsServerValue};

#[derive(Debug)]
enum WhoisKey {
    ExtWhois(String),
    DNS(String),
}
impl WhoisKey {
    fn whois_query(s: String) -> WhoisKey {
        WhoisKey::ExtWhois(s)
    }
    fn dns_query(s: String) -> WhoisKey {
        WhoisKey::DNS(s)
    }
}
impl serde::Serialize for WhoisKey {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut state = serializer.serialize_struct("WK", 1)?;
        match self {
            WhoisKey::ExtWhois(s) => {
                state.serialize_field("whois", s)?;
            }
            WhoisKey::DNS(s) => {
                state.serialize_field("dns", s)?;
            }
        }
        state.end()
    }
}
impl From<WhoisKey> for sled::IVec {
    fn from(sv: WhoisKey) -> sled::IVec {
        serde_json::to_vec(&sv).unwrap().into()
    }
}
#[derive(Debug)]
struct WhoisRec {
    ts: DateTime<Local>,
    val: String,
}
impl WhoisRec {
    fn new(vl: String) -> WhoisRec {
        WhoisRec {
            ts: chrono::Local::now(),
            val: vl,
        }
    }
    fn mkfrom(gts: i64, vl: String) -> WhoisRec {
        WhoisRec {
            ts: DateTime::<Local>::from_utc(
                NaiveDateTime::from_timestamp(gts, 0),
                chrono::FixedOffset::east(0),
            ),
            val: vl,
        }
    }
    fn modified(&self) -> DateTime<Local> {
        self.ts
    }
}
impl From<String> for WhoisRec {
    fn from(s: String) -> WhoisRec {
        WhoisRec {
            ts: chrono::Local::now(),
            val: s,
        }
    }
}
impl serde::Serialize for WhoisRec {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut state = serializer.serialize_struct("WR", 2)?;
        state.serialize_field("ts", &self.ts.timestamp())?;
        state.serialize_field("val", &self.val)?;
        state.end()
    }
}
impl<'de> Deserialize<'de> for WhoisRec {
    fn deserialize<D>(deserializer: D) -> Result<WhoisRec, D::Error>
    where
        D: Deserializer<'de>,
    {
        enum Field {
            Ts,
            Val,
        }

        // This part could also be generated independently by:
        //
        //    #[derive(Deserialize)]
        //    #[serde(field_identifier, rename_all = "lowercase")]
        //    enum Field { Secs, Nanos }
        impl<'de> Deserialize<'de> for Field {
            fn deserialize<D>(deserializer: D) -> Result<Field, D::Error>
            where
                D: Deserializer<'de>,
            {
                struct FieldVisitor;

                impl<'de> Visitor<'de> for FieldVisitor {
                    type Value = Field;

                    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                        formatter.write_str("`ts` or `val`")
                    }

                    fn visit_str<E>(self, value: &str) -> Result<Field, E>
                    where
                        E: serde::de::Error,
                    {
                        match value {
                            "ts" => Ok(Field::Ts),
                            "val" => Ok(Field::Val),
                            _ => Err(serde::de::Error::unknown_field(value, FIELDS)),
                        }
                    }
                }

                deserializer.deserialize_identifier(FieldVisitor)
            }
        }

        struct WhoisRecVisitor;

        impl<'de> Visitor<'de> for WhoisRecVisitor {
            type Value = WhoisRec;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("struct WhoisRec")
            }

            fn visit_seq<V>(self, mut seq: V) -> Result<WhoisRec, V::Error>
            where
                V: SeqAccess<'de>,
            {
                let gts = seq
                    .next_element()?
                    .ok_or_else(|| serde::de::Error::invalid_length(0, &self))?;
                let gval = seq
                    .next_element()?
                    .ok_or_else(|| serde::de::Error::invalid_length(1, &self))?;
                Ok(WhoisRec::mkfrom(gts, gval))
            }

            fn visit_map<V>(self, mut map: V) -> Result<WhoisRec, V::Error>
            where
                V: MapAccess<'de>,
            {
                let mut ts = None;
                let mut val = None;
                while let Some(key) = map.next_key()? {
                    match key {
                        Field::Ts => {
                            if ts.is_some() {
                                return Err(serde::de::Error::duplicate_field("ts"));
                            }
                            ts = Some(map.next_value()?);
                        }
                        Field::Val => {
                            if val.is_some() {
                                return Err(serde::de::Error::duplicate_field("val"));
                            }
                            val = Some(map.next_value()?);
                        }
                    }
                }
                let ts = ts.ok_or_else(|| serde::de::Error::missing_field("ts"))?;
                let val = val.ok_or_else(|| serde::de::Error::missing_field("val"))?;
                Ok(WhoisRec::mkfrom(ts, val))
            }
        }

        const FIELDS: &'static [&'static str] = &["ts", "val"];
        deserializer.deserialize_struct("WR", FIELDS, WhoisRecVisitor)
    }
}
impl From<sled::IVec> for WhoisRec {
    fn from(sv: sled::IVec) -> WhoisRec {
        serde_json::from_slice(&sv).unwrap()
    }
}
impl From<WhoisRec> for sled::IVec {
    fn from(sv: WhoisRec) -> sled::IVec {
        serde_json::to_vec(&sv).unwrap().into()
    }
}
pub struct WhoisSvr {
    whs: WhoIs,
    dns: Vec<std::net::SocketAddr>,
    req_timeout: std::time::Duration,
    cache_valid: chrono::Duration,
    //cache: RwLock<HashMap<String, WhoisRec>>,
    db: sled::Db,
}
static INVALID_WHOIS: &[u8] = b"Invalid WHOIS query";

impl WhoisSvr {
    pub fn new(conf:&SvcConfig) -> WhoisSvr {
        WhoisSvr {
            whs: conf.whoisconfig.clone(),
            dns: conf.whoisdnses.clone(),
            req_timeout: std::time::Duration::from_secs(conf.whoisreqtimeout),
            cache_valid: chrono::Duration::seconds(conf.whoiscachesecs),
            db: sled::Config::default()
                .flush_every_ms(Some(10000))
                .path(conf.whoisdb.clone())
                .open()
                .unwrap(),
        }
    }
    pub fn invalid_query() -> Response<Body> {
        Response::builder()
            .status(StatusCode::OK)
            .body(INVALID_WHOIS.into())
            .unwrap()
    }
    pub async fn bindany() -> Result<tokio::net::UdpSocket, WhoIsError> {
        let mut bindport: u16 = 10000;
        for _i in 0..19 {
            match tokio::net::UdpSocket::bind(std::net::SocketAddr::new(
                std::net::IpAddr::V4(std::net::Ipv4Addr::new(0, 0, 0, 0)),
                bindport,
            ))
            .await
            {
                Ok(s) => {
                    return Ok(s);
                }
                Err(_) => {}
            };
            bindport += 1;
        }
        return Err(WhoIsError::MapError("Unable to bind socket"));
    }
    pub async fn do_query_dns_ptr(
        self: &Arc<WhoisSvr>,
        target: String,
    ) -> Result<String, WhoIsError> {
        lazy_static! {
            static ref RE_IPV4: Regex =
                Regex::new(r"([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)").unwrap();
        }
        match RE_IPV4.captures(target.as_str()) {
            None => {}
            Some(caps) => {
                if let (Some(c1), Some(c2), Some(c3), Some(c4)) =
                    (caps.get(1), caps.get(2), caps.get(3), caps.get(4))
                {
                    let trg = String::new()
                        + c4.as_str()
                        + "."
                        + c3.as_str()
                        + "."
                        + c2.as_str()
                        + "."
                        + c1.as_str()
                        + ".IN-ADDR.ARPA.";
                    let res = match self.do_query_dns("PTR", trg).await {
                        Ok(q) => q,
                        Err(e) => return Err(e),
                    };
                    let lkey: sled::IVec = WhoisKey::dns_query(target.clone()).into();
                    self.db.insert(lkey, WhoisRec::new(res.clone())).unwrap();
                    return Ok(res);
                };
            }
        };
        Err(WhoIsError::MapError("Invalid IPv4"))
    }
    pub async fn query_dns_ptr(self: &Arc<WhoisSvr>, target: String) -> Result<String, WhoIsError> {
        let lkey: sled::IVec = WhoisKey::dns_query(target.clone()).into();
        match self.db.get(lkey.clone()) {
            Ok(r) => {
                if let Some(v) = r {
                    if v.len() > 0 {
                        match serde_json::from_slice::<WhoisRec>(&v) {
                            Ok(q) => {
                                if chrono::Local::now().signed_duration_since(q.modified())
                                    > self.cache_valid
                                {
                                    // run separate task to refresh cache data
                                    let slf = self.clone();
                                    tokio::spawn(async move { slf.do_query_dns_ptr(target).await });
                                }
                                return Ok(q.val);
                            }
                            Err(e) => {
                                eprintln!("Deserialize error: {:?}", e);
                            }
                        };
                    };
                };
            }
            Err(e) => eprintln!("sled error: {:?}", e),
        };
        self.do_query_dns_ptr(target).await
    }
    pub async fn do_query_dns(
        self: &Arc<WhoisSvr>,
        qtype: &str,
        target: String,
    ) -> Result<String, WhoIsError> {
        let mut parsed_query = dnssector::gen::query(
            target.as_bytes(),
            Type::from_string(qtype).unwrap(),
            Class::from_string("IN").unwrap(),
        )
        .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e.to_string()))?;
        let query_tid = parsed_query.tid();
        let query_question = parsed_query.question();
        if query_question.is_none() || parsed_query.flags() & DNS_FLAG_QR != 0 {
            return Err(WhoIsError::MapError("No DNS question"));
        }
        let valid_query = parsed_query.into_packet();
        let socket = Self::bindany().await?;
        socket
            .connect(self.dns[(target.as_bytes()[0] as usize) % self.dns.len()])
            .await?;
        socket.send(&valid_query).await?;
        let mut response = vec![0; DNS_MAX_COMPRESSED_SIZE];
        let response_len = socket
            .recv(&mut response)
            .await
            .map_err(|_| io::Error::new(io::ErrorKind::WouldBlock, "Timeout"))?;
        response.truncate(response_len);
        let mut parsed_response = DNSSector::new(response)
            .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e.to_string()))?
            .parse()
            .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e.to_string()))?;
        if parsed_response.tid() != query_tid || parsed_response.question() != query_question {
            return Err(WhoIsError::MapError("Unexpected DNS response"));
        }
        {
            let mut it = parsed_response.into_iter_answer();
            while let Some(item) = it {
                if item.rr_type() == 12 {
                    //ptr
                    let mut res = String::new();
                    let rdata = item.rdata_slice();
                    let mut p: usize = DNS_RR_HEADER_SIZE;
                    while p < rdata.len() {
                        if (p + (rdata[p] as usize)) > rdata.len() {
                            break;
                        }
                        if rdata[p] > 0 {
                            //TODO: idna
                            res +=
                                String::from_utf8_lossy(&rdata[p + 1..p + 1 + (rdata[p] as usize)])
                                    .to_string()
                                    .as_str();
                            res += ".";
                        }
                        p += 1 + (rdata[p] as usize);
                    }
                    return Ok(res);
                }
                println!("DNS: {:?} - {:?}", item.rr_type(), item.rdata_slice());
                it = item.next();
            }
        };
        Err(WhoIsError::MapError("Not found"))
    }
    pub async fn do_query_whois(
        self: &Arc<WhoisSvr>,
        target: String,
        checkitem: Arc<Option<Regex>>,
    ) -> Result<String, WhoIsError> {
        lazy_static! {
            static ref RE_WHOIS: Regex = Regex::new(r"\b(whois\.[\.a-z0-9\-]+)\b").unwrap();
        }
        let lkey: sled::IVec = WhoisKey::whois_query(target.clone()).into();
        let mut deep: usize = 16;
        let mut whoises: HashMap<String, bool> = HashMap::new();
        while deep > 0 {
            deep -= 1;
            let mut opts = WhoIsLookupOptions::from_string(target.clone())?;
            opts.timeout = Some(self.req_timeout);
            if whoises.len() > 0 {
                loop {
                    let whfnd = match whoises.iter().find(|x| *x.1) {
                        None => return Ok(String::from("")),
                        Some(v) => v.0.clone(),
                    };
                    if whfnd.len() > 0 {
                        whoises.insert(whfnd.clone(), false);
                        opts.server = Some(match WhoIsServerValue::from_string(whfnd) {
                            Ok(s) => s,
                            Err(e) => {
                                eprintln!("Invalid whois server: {:?}", e);
                                continue;
                            }
                        });
                    };
                    break;
                }
            }
            let res = match self.whs.lookup_async(opts).await {
                Ok(v) => v,
                Err(e) => return Err(e),
            };
            match *checkitem {
                None => {
                    self.db.insert(lkey, WhoisRec::new(res.clone())).unwrap();
                    return Ok(res);
                }
                Some(_) => {
                    let v = Self::findstr(res.as_str(), &checkitem);
                    if v.len() > 0 {
                        self.db.insert(lkey, WhoisRec::new(res.clone())).unwrap();
                        return Ok(res);
                    };
                }
            };
            for i in RE_WHOIS.find_iter(res.as_str()) {
                let whoissvr = i.as_str();
                if !whoises.contains_key(whoissvr) {
                    whoises.insert(whoissvr.to_string(), true);
                };
            }
            if whoises.len() < 1 {
                return Ok(res);
            };
        }
        Err(WhoIsError::MapError("Search failed"))
    }
    pub async fn query_whois(
        self: &Arc<WhoisSvr>,
        target: String,
        checkitem: Arc<Option<Regex>>,
    ) -> Result<String, WhoIsError> {
        let lkey: sled::IVec = WhoisKey::whois_query(target.clone()).into();
        match self.db.get(lkey.clone()) {
            Ok(r) => {
                if let Some(v) = r {
                    if v.len() > 0 {
                        match serde_json::from_slice::<WhoisRec>(&v) {
                            Ok(q) => {
                                if chrono::Local::now().signed_duration_since(q.modified())
                                    > self.cache_valid
                                {
                                    let slf = self.clone();
                                    tokio::spawn(async move {
                                        slf.do_query_whois(target, checkitem).await
                                    });
                                }
                                return Ok(q.val);
                            }
                            Err(e) => {
                                eprintln!("Deserialize error: {:?}", e);
                            }
                        };
                    };
                };
            }
            Err(e) => eprintln!("sled error: {:?}", e),
        };
        self.do_query_whois(target, checkitem).await
    }
    fn filterout_comments<'a>(s: &'a str) -> Vec<&'a str> {
        s.split('\n')
            .filter(|q| {
                if q.len() > 0 {
                    if let Some(fc) = q.chars().next() {
                        return fc != '%';
                    }
                };
                false
            })
            .collect()
    }
    fn findstr<'a>(s: &'a str, tofind: &Option<Regex>) -> Vec<&'a str> {
        match tofind {
            None => Self::filterout_comments(s),
            Some(fnd) => s
                .split('\n')
                .filter(|q| {
                    if q.len() > 0 {
                        if let Some(fc) = q.chars().next() {
                            return fc != '%' && fc != '#';
                        }
                    };
                    false
                })
                .skip_while(|x| !fnd.is_match(x))
                .collect(),
        }
    }
    pub async fn handle_query(
        self: &Arc<WhoisSvr>,
        req: &Request<Body>,
    ) -> Result<Response<Body>, hyper::http::Error> {
        let requri = req.uri().path();
        let urlparts: Vec<&str> = requri.split('/').collect();
        if urlparts.len() < 3 {
            return Ok(not_found());
        }
        if urlparts.len() > 3 && urlparts[1] == "api" && urlparts[2] == "dns" {
            let rsp = match self.query_dns_ptr(urlparts[3].to_string()).await {
                Ok(v) => v,
                Err(e) => {
                    return Response::builder()
                        .status(StatusCode::from_u16(500).unwrap())
                        .header("Content-type", "text/plain")
                        .body(format!("Error: {:?}", e).into());
                }
            };
            return Response::builder()
                .status(StatusCode::OK)
                .header("Content-type", "text/plain")
                .body(rsp.into());
        }
        if urlparts[1] != "api" || urlparts[2] != "whois" {
            return Ok(not_found());
        }
        let params = get_url_params(req);
        let query = match get_url_param::<String>(&params, "query") {
            Some(s) => s,
            None => {
                return Ok(WhoisSvr::invalid_query());
            }
        };
        if query.len() < 1 {
            return Ok(WhoisSvr::invalid_query());
        };
        let checkstr = Arc::new(if urlparts.len() >= 4 {
            match urlparts[3] {
                "aut-num" | "as" => Some(Regex::new(r"(aut-num|ASNumber):").unwrap()),
                "r" | "r4" | "route" => Some(Regex::new(r"route:").unwrap()),
                "r6" | "route6" => Some(Regex::new(r"route6:").unwrap()),
                _ => None,
            }
        } else {
            None
        });
        let mut rsp = match self.query_whois(query, checkstr.clone()).await {
            Ok(v) => v,
            Err(e) => {
                return Response::builder()
                    .status(StatusCode::from_u16(500).unwrap())
                    .header("Content-type", "text/plain")
                    .body(format!("Error: {:?}", e).into());
            }
        };
        if urlparts.len() >= 4 {
            match urlparts[3] {
                "raw" => {}
                _ => {
                    rsp = {
                        let v = Self::findstr(rsp.as_str(), &*checkstr);
                        if v.len() > 0 {
                            v.join("\n")
                        } else {
                            Self::filterout_comments(rsp.as_str()).join("\n")
                        }
                    };
                }
            }
        }
        Response::builder()
            .status(StatusCode::OK)
            .header("Content-type", "text/plain")
            .body(rsp.into())
    }
    pub async fn response_fn(
        self: &Arc<WhoisSvr>,
        req: &Request<Body>,
    ) -> Result<Response<Body>, hyper::Error> {
        //Ok(not_found())
        match self.handle_query(req).await {
            Ok(v) => Ok(v),
            Err(e) => Ok(Response::builder()
                .status(StatusCode::NOT_FOUND)
                .body(format!("{:?}", e).into())
                .unwrap()),
        }
    }
}