Filter DNS results by common ASN
[dnsseed-rust] / src / datastore.rs
index 05d8a1e9ba0ef54fc0fa539899afda08ddf40a4e..c8ef0e0aed812f52599ac6d1ca47dcde0692e48b 100644 (file)
@@ -16,6 +16,8 @@ use tokio::io::write_all;
 
 use regex::Regex;
 
+use crate::bgp_client::BGPClient;
+
 #[derive(Clone, Copy, Hash, PartialEq, Eq)]
 pub enum AddressState {
        Untested,
@@ -434,8 +436,12 @@ impl Store {
                        tokio::fs::rename(nodes_file.clone() + ".tmp", nodes_file)
                });
 
+               settings_future.join(nodes_future).then(|_| { future::ok(()) })
+       }
+
+       pub fn write_dns(&'static self, bgp_client: Arc<BGPClient>) -> impl Future<Item=(), Error=()> {
                let dns_file = self.store.clone() + "/nodes.dump";
-               let dns_future = File::create(dns_file.clone() + ".tmp").and_then(move |f| {
+               File::create(dns_file.clone() + ".tmp").and_then(move |f| {
                        let mut dns_buff = String::new();
                        {
                                let mut rng = thread_rng();
@@ -492,10 +498,12 @@ impl Store {
                                                                .filter(|e| e.is_ipv6() && e.port() == 8333).map(|e| e.ip()).collect();
                                                }
                                        }
-                                       for a in v4_set.iter().choose_multiple(&mut rng, 21) {
+                                       let mut asn_set = HashSet::with_capacity(cmp::max(v4_set.len(), v6_set.len()));
+                                       for a in v4_set.iter().filter(|a| asn_set.insert(bgp_client.get_asn(**a))).choose_multiple(&mut rng, 21) {
                                                dns_buff += &format!("x{:x}.dnsseed\tIN\tA\t{}\n", i, a);
                                        }
-                                       for a in v6_set.iter().choose_multiple(&mut rng, 12) {
+                                       asn_set.clear();
+                                       for a in v6_set.iter().filter(|a| asn_set.insert(bgp_client.get_asn(**a))).choose_multiple(&mut rng, 12) {
                                                dns_buff += &format!("x{:x}.dnsseed\tIN\tAAAA\t{}\n", i, a);
                                        }
                                }
@@ -505,9 +513,7 @@ impl Store {
                        f.poll_sync_all()
                }).and_then(|_| {
                        tokio::fs::rename(dns_file.clone() + ".tmp", dns_file)
-               });
-
-               settings_future.join3(nodes_future, dns_future).then(|_| { future::ok(()) })
+               }).then(|_| { future::ok(()) })
        }
 
        pub fn get_next_scan_nodes(&self) -> Vec<SocketAddr> {