+ pub fn save_data(&'static self) -> impl Future<Item=(), Error=()> {
+ let settings_file = self.store.clone() + "/settings";
+ let settings_future = File::create(settings_file.clone() + ".tmp").and_then(move |f| {
+ let settings_string = format!("{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}",
+ self.get_u64(U64Setting::RunTimeout),
+ self.get_u64(U64Setting::WasGoodTimeout),
+ self.get_u64(U64Setting::MinProtocolVersion),
+ self.get_u64(U64Setting::RescanInterval(AddressState::Untested)),
+ self.get_u64(U64Setting::RescanInterval(AddressState::LowBlockCount)),
+ self.get_u64(U64Setting::RescanInterval(AddressState::HighBlockCount)),
+ self.get_u64(U64Setting::RescanInterval(AddressState::LowVersion)),
+ self.get_u64(U64Setting::RescanInterval(AddressState::BadVersion)),
+ self.get_u64(U64Setting::RescanInterval(AddressState::NotFullNode)),
+ self.get_u64(U64Setting::RescanInterval(AddressState::ProtocolViolation)),
+ self.get_u64(U64Setting::RescanInterval(AddressState::Timeout)),
+ self.get_u64(U64Setting::RescanInterval(AddressState::TimeoutDuringRequest)),
+ self.get_u64(U64Setting::RescanInterval(AddressState::TimeoutAwaitingPong)),
+ self.get_u64(U64Setting::RescanInterval(AddressState::TimeoutAwaitingAddr)),
+ self.get_u64(U64Setting::RescanInterval(AddressState::TimeoutAwaitingBlock)),
+ self.get_u64(U64Setting::RescanInterval(AddressState::Good)),
+ self.get_u64(U64Setting::RescanInterval(AddressState::WasGood)),
+ self.get_u64(U64Setting::RescanInterval(AddressState::EvilNode)),
+ self.get_regex(RegexSetting::SubverRegex).as_str());
+ write_all(f, settings_string).and_then(|(mut f, _)| {
+ f.poll_sync_all()
+ }).and_then(|_| {
+ tokio::fs::rename(settings_file.clone() + ".tmp", settings_file)
+ })
+ });
+
+ let nodes_file = self.store.clone() + "/nodes";
+ let nodes_future = File::create(nodes_file.clone() + ".tmp").and_then(move |f| {
+ let mut nodes_buff = String::new();
+ {
+ let nodes = self.nodes.read().unwrap();
+ nodes_buff.reserve(nodes.nodes_to_state.len() * 20);
+ for (ref sockaddr, ref node) in nodes.nodes_to_state.iter() {
+ nodes_buff += &sockaddr.to_string();
+ nodes_buff += ",";
+ nodes_buff += &node.state.to_num().to_string();
+ nodes_buff += ",";
+ nodes_buff += &node.last_services.to_string();
+ nodes_buff += "\n";
+ }
+ }
+ write_all(f, nodes_buff)
+ }).and_then(|(mut f, _)| {
+ f.poll_sync_all()
+ }).and_then(|_| {
+ 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";
+ File::create(dns_file.clone() + ".tmp").and_then(move |f| {
+ let mut dns_buff = String::new();
+ {
+ let mut rng = thread_rng();
+ for i in &[1u64, 4, 5, 8, 9, 12, 13, 1024, 1025, 1028, 1029, 1032, 1033, 1036, 1037] {
+ let mut tor_set: Vec<Ipv6Addr> = Vec::new();
+ let mut v6_set: Vec<Ipv6Addr> = Vec::new();
+ let mut v4_set: Vec<Ipv4Addr> = Vec::new();
+ macro_rules! add_addr { ($addr: expr) => {
+ match $addr.ip() {
+ IpAddr::V4(v4addr) => v4_set.push(v4addr),
+ IpAddr::V6(v6addr) if v6addr.octets()[..6] == [0xFD,0x87,0xD8,0x7E,0xEB,0x43][..] => tor_set.push(v6addr),
+ IpAddr::V6(v6addr) => v6_set.push(v6addr),
+ }
+ } }
+ {
+ let nodes = self.nodes.read().unwrap();
+ if i.count_ones() == 1 {
+ for j in 0..64 {
+ if i & (1 << j) != 0 {
+ let set_ref = &nodes.good_node_services[j];
+ for a in set_ref.iter().filter(|e| e.port() == 8333) {
+ add_addr!(a);
+ }
+ break;
+ }
+ }
+ } else if i.count_ones() == 2 {
+ let mut first_set = None;
+ let mut second_set = None;
+ for j in 0..64 {
+ if i & (1 << j) != 0 {
+ if first_set == None {
+ first_set = Some(&nodes.good_node_services[j]);
+ } else {
+ second_set = Some(&nodes.good_node_services[j]);
+ break;
+ }
+ }
+ }
+ for a in first_set.unwrap().intersection(&second_set.unwrap()).filter(|e| e.port() == 8333) {
+ add_addr!(a);
+ }
+ } else {
+ //TODO: Could optimize this one a bit
+ let mut intersection;
+ let mut intersection_set_ref = None;
+ for j in 0..64 {
+ if i & (1 << j) != 0 {
+ if intersection_set_ref == None {
+ intersection_set_ref = Some(&nodes.good_node_services[j]);
+ } else {
+ let new_intersection = intersection_set_ref.unwrap()
+ .intersection(&nodes.good_node_services[j]).map(|e| (*e).clone()).collect();
+ intersection = Some(new_intersection);
+ intersection_set_ref = Some(intersection.as_ref().unwrap());
+ }
+ }
+ }
+ for a in intersection_set_ref.unwrap().iter().filter(|e| e.port() == 8333) {
+ add_addr!(a);
+ }
+ }
+ }
+ let mut asn_set = HashSet::with_capacity(cmp::max(v4_set.len(), v6_set.len()));
+ asn_set.insert(0);
+ for a in v4_set.iter().filter(|a| asn_set.insert(bgp_client.get_asn(IpAddr::V4(**a)))).choose_multiple(&mut rng, 21) {
+ dns_buff += &format!("x{:x}.dnsseed\tIN\tA\t{}\n", i, a);
+ }
+ asn_set.clear();
+ asn_set.insert(0);
+ for a in v6_set.iter().filter(|a| asn_set.insert(bgp_client.get_asn(IpAddr::V6(**a)))).choose_multiple(&mut rng, 10) {
+ dns_buff += &format!("x{:x}.dnsseed\tIN\tAAAA\t{}\n", i, a);
+ }
+ for a in tor_set.iter().choose_multiple(&mut rng, 2) {
+ dns_buff += &format!("x{:x}.dnsseed\tIN\tAAAA\t{}\n", i, a);
+ }
+ }
+ }
+ write_all(f, dns_buff)
+ }).and_then(|(mut f, _)| {
+ f.poll_sync_all()
+ }).and_then(|_| {
+ tokio::fs::rename(dns_file.clone() + ".tmp", dns_file)
+ }).then(|_| { future::ok(()) })