Skip to content

Commit

Permalink
Tanner develop fixes phase #2 (#263)
Browse files Browse the repository at this point in the history
* JSON and Nonetype error fixes

* Suggested changes

* Suggested changes

* Minor fix
  • Loading branch information
viskey98 authored and afeena committed Jul 18, 2018
1 parent 15181b4 commit e56071b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion tanner/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async def return_snare_info(self, uuid, count=-1):
query_res = []
try:
query_res = await self.redis_client.zrevrangebyscore(
uuid, offset=0, count=count
uuid, offset=0, count=count, encoding='utf-8'
)
except aioredis.ProtocolError as connection_error:
self.logger.error('Can not connect to redis %s', connection_error)
Expand Down
23 changes: 12 additions & 11 deletions tanner/session_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ async def analyze(self, session_key, redis_client):
session = None
await asyncio.sleep(1, loop=self._loop)
try:
session = await redis_client.execute('get', session_key)
session = json.loads(session)
session = await redis_client.get(session_key, encoding='utf-8')
except (aioredis.ProtocolError, TypeError, ValueError) as error:
self.logger.error('Can\'t get session for analyze: %s', error)
else:
Expand Down Expand Up @@ -143,16 +142,17 @@ async def detect_crawler(self, stats, bots_owner, crawler_hosts):
if stats['requests_in_second'] > 10:
if stats['referer'] is not None:
return (0.0, 0.5)
if stats['user_agent'] in bots_owner:
if stats['user_agent'] is not None and stats['user_agent'] in bots_owner:
return (0.85, 0.15)
return (0.5, 0.85)
if stats['user_agent'] in bots_owner:
if stats['user_agent'] is not None and stats['user_agent'] in bots_owner:
hostname, _, _ = await self._loop.run_in_executor(
None, socket.gethostbyaddr, stats['peer_ip']
)
for crawler_host in crawler_hosts:
if crawler_host in hostname:
return (0.75, 0.15)
if hostname is not None:
for crawler_host in crawler_hosts:
if crawler_host in hostname:
return (0.75, 0.15)
return (0.25, 0.15)
return (0.0, 0.0)

Expand All @@ -161,13 +161,14 @@ async def detect_attacker(self, stats, bots_owner, crawler_hosts, attacks):
return 1.0
if stats['requests_in_second'] > 10:
return 0.0
if stats['user_agent'] in bots_owner:
if stats['user_agent'] is not None and stats['user_agent'] in bots_owner:
hostname, _, _ = await self._loop.run_in_executor(
None, socket.gethostbyaddr, stats['peer_ip']
)
for crawler_host in crawler_hosts:
if crawler_host in hostname:
return 0.25
if hostname is not None:
for crawler_host in crawler_hosts:
if crawler_host in hostname:
return 0.25
return 0.75
if stats['hidden_links'] > 0:
return 0.5
Expand Down

0 comments on commit e56071b

Please sign in to comment.