Working POC for authenticating telnet console

This commit is contained in:
Daniel Graña 2018-09-05 10:49:46 -03:00
parent 0007cd032d
commit e65f7e0c91
1 changed files with 22 additions and 3 deletions

View File

@ -22,6 +22,7 @@ from scrapy import signals
from scrapy.utils.trackref import print_live_refs
from scrapy.utils.engine import print_engine_status
from scrapy.utils.reactor import listen_tcp
from scrapy.utils.decorators import defers
try:
import guppy
@ -49,6 +50,8 @@ class TelnetConsole(protocol.ServerFactory):
self.noisy = False
self.portrange = [int(x) for x in crawler.settings.getlist('TELNETCONSOLE_PORT')]
self.host = crawler.settings['TELNETCONSOLE_HOST']
self.username = crawler.settings.get('TELNETCONSOLE_USERNAME', 'scrapy')
self.password = crawler.settings.get('TELNETCONSOLE_PASSWORD', 'scrapy')
self.crawler.signals.connect(self.start_listening, signals.engine_started)
self.crawler.signals.connect(self.stop_listening, signals.engine_stopped)
@ -67,9 +70,25 @@ class TelnetConsole(protocol.ServerFactory):
self.port.stopListening()
def protocol(self):
telnet_vars = self._get_telnet_vars()
return telnet.TelnetTransport(telnet.TelnetBootstrapProtocol,
insults.ServerProtocol, manhole.Manhole, telnet_vars)
class Portal:
"""An implementation of IPortal"""
@defers
def login(self_, credentials, mind, *interfaces):
if not (credentials.username == self.username
and credentials.checkPassword(self.password)):
raise ValueError("Invalid credentials")
protocol = telnet.TelnetBootstrapProtocol(
insults.ServerProtocol,
manhole.Manhole,
self._get_telnet_vars()
)
return (interfaces[0], protocol, lambda: None)
return telnet.TelnetTransport(
telnet.AuthenticatingTelnetProtocol,
Portal()
)
def _get_telnet_vars(self):
# Note: if you add entries here also update topics/telnetconsole.rst