medved/lib/plugin/iscan/tasks/gopher.py

47 lines
1.2 KiB
Python
Raw Normal View History

"""
Basic tasks for Gopher services
"""
2018-08-02 17:42:09 +00:00
import socket
2018-08-02 17:42:09 +00:00
from lib.exec import Task
2018-08-02 17:42:09 +00:00
class GopherFindTask(Task): # pylint: disable=too-few-public-methods
"""Tries to connect Gopher service"""
@staticmethod
def _recv(sck):
2018-08-02 17:42:09 +00:00
total_data = []
while True:
data = sck.recv(2048)
if not data:
break
total_data.append(data.decode('utf-8'))
return ''.join(total_data)
def _process(self, item):
sock = socket.socket()
sock.settimeout(self.lcnf.get('timeout', 20))
sock.connect((item['data']['ip'], int(70)))
sock.sendall(b'\n\n')
response = self._recv(sock)
sock.close()
self._logger.debug("Parsing result")
item['data']['files'] = []
item['data']['filter'] = False
for s in [s for s in response.split("\r\n") if s]:
node = {}
fields = s.split("\t")
self._logger.debug(fields)
node['type'] = fields[0][0]
if len(fields) == 4:
node['name'] = fields[0][1:]
node['path'] = fields[1]
node['serv'] = f"{fields[2]}:{fields[3]}"
item['data']['files'].append(node)
if not item['data']['files']:
raise Exception("Empty server (not Gopher?)")
2018-08-04 11:44:27 +00:00
item['steps'][self._id] = True