2008-08-07 04:32:28 +00:00
|
|
|
# Copyright (c) 2008, Thomas Hurst <tom@hur.st>
|
|
|
|
#
|
|
|
|
# Use of this file is unrestricted provided this notice is retained.
|
|
|
|
# If you use it, it'd be nice if you dropped me a note. Also beer.
|
|
|
|
|
2009-08-08 00:22:31 +00:00
|
|
|
from terminatorlib.util import dbg, err
|
2008-08-07 04:32:28 +00:00
|
|
|
from terminatorlib.version import APP_NAME, APP_VERSION
|
|
|
|
|
|
|
|
import socket
|
|
|
|
import threading
|
|
|
|
import sys
|
2020-05-01 15:38:34 +00:00
|
|
|
if sys.version_info < (3, 0):
|
|
|
|
import SocketServer as socketserver
|
|
|
|
else:
|
|
|
|
import socketserver
|
|
|
|
import code
|
2008-08-07 04:32:28 +00:00
|
|
|
import readline
|
|
|
|
import rlcompleter
|
|
|
|
import re
|
|
|
|
|
2008-08-07 16:26:46 +00:00
|
|
|
def ddbg(msg):
|
2008-08-07 17:20:45 +00:00
|
|
|
# uncomment this to get lots of spam from debugserver
|
2008-08-07 16:26:46 +00:00
|
|
|
return
|
2008-08-07 17:20:45 +00:00
|
|
|
dbg(msg)
|
2008-08-07 16:26:46 +00:00
|
|
|
|
2018-04-24 18:22:10 +00:00
|
|
|
class PythonConsoleServer(socketserver.BaseRequestHandler):
|
2008-08-07 13:59:10 +00:00
|
|
|
env = None
|
2008-08-07 04:32:28 +00:00
|
|
|
def setup(self):
|
2008-08-07 17:20:45 +00:00
|
|
|
dbg('debugserver: connect from %s' % str(self.client_address))
|
2008-11-04 17:01:00 +00:00
|
|
|
ddbg('debugserver: env=%r' % PythonConsoleServer.env)
|
2008-08-07 13:59:10 +00:00
|
|
|
self.console = TerminatorConsole(PythonConsoleServer.env)
|
2008-08-07 04:32:28 +00:00
|
|
|
|
|
|
|
def handle(self):
|
2008-08-07 16:26:46 +00:00
|
|
|
ddbg("debugserver: handling")
|
2008-08-07 04:32:28 +00:00
|
|
|
try:
|
|
|
|
self.socketio = self.request.makefile()
|
|
|
|
sys.stdout = self.socketio
|
|
|
|
sys.stdin = self.socketio
|
2008-08-07 16:26:46 +00:00
|
|
|
sys.stderr = self.socketio
|
2008-08-07 04:32:28 +00:00
|
|
|
self.console.run(self)
|
|
|
|
finally:
|
|
|
|
sys.stdout = sys.__stdout__
|
|
|
|
sys.stdin = sys.__stdin__
|
2008-08-07 16:26:46 +00:00
|
|
|
sys.stderr = sys.__stderr__
|
2008-08-07 04:32:28 +00:00
|
|
|
self.socketio.close()
|
2008-08-07 16:26:46 +00:00
|
|
|
ddbg("debugserver: done handling")
|
2008-08-07 04:32:28 +00:00
|
|
|
|
|
|
|
def verify_request(self, request, client_address):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def finish(self):
|
2008-08-07 16:26:46 +00:00
|
|
|
ddbg('debugserver: disconnect from %s' % str(self.client_address))
|
|
|
|
|
|
|
|
# rfc1116/rfc1184
|
|
|
|
LINEMODE = chr(34) # Linemode negotiation
|
|
|
|
|
|
|
|
NULL = chr(0)
|
|
|
|
ECHO = chr(1)
|
|
|
|
CR = chr(13)
|
|
|
|
LF = chr(10)
|
|
|
|
SE = chr(240) # End subnegotiation
|
|
|
|
NOP = chr(241)
|
|
|
|
DM = chr(242) # Data Mark
|
|
|
|
BRK = chr(243) # Break
|
|
|
|
IP = chr(244) # Interrupt Process
|
|
|
|
AO = chr(245) # Abort Output
|
|
|
|
AYT = chr(246) # Are You There
|
|
|
|
EC = chr(247) # Erase Character
|
|
|
|
EL = chr(248) # Erase Line
|
|
|
|
GA = chr(249) # Go Ahead
|
|
|
|
SB = chr(250) # Subnegotiation follows
|
|
|
|
WILL = chr(251) # Subnegotiation commands
|
|
|
|
WONT = chr(252)
|
|
|
|
DO = chr(253)
|
|
|
|
DONT = chr(254)
|
|
|
|
IAC = chr(255) # Interpret As Command
|
|
|
|
|
|
|
|
UIAC = '(^|[^' + IAC + '])' + IAC # Unescaped IAC
|
|
|
|
BareLF = re.compile('([^' + CR + '])' + CR)
|
|
|
|
DoDont = re.compile(UIAC +'[' + DO + DONT + '](.)')
|
|
|
|
WillWont = re.compile(UIAC + '[' + WILL + WONT + '](.)')
|
|
|
|
AreYouThere = re.compile(UIAC + AYT)
|
|
|
|
IpTelnet = re.compile(UIAC + IP)
|
|
|
|
OtherTelnet = re.compile(UIAC + '[^' + IAC + ']')
|
2008-08-07 04:32:28 +00:00
|
|
|
|
|
|
|
# See http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/205335 for telnet bits
|
|
|
|
# Python doesn't make this an especially neat conversion :(
|
|
|
|
class TerminatorConsole(code.InteractiveConsole):
|
|
|
|
def parse_telnet(self, data):
|
|
|
|
odata = data
|
|
|
|
data = re.sub(BareLF, '\\1', data)
|
2008-08-07 16:26:46 +00:00
|
|
|
data = data.replace(CR + NULL, '')
|
|
|
|
data = data.replace(NULL, '')
|
2008-08-07 04:32:28 +00:00
|
|
|
|
|
|
|
bits = re.findall(DoDont, data)
|
2008-11-04 17:01:00 +00:00
|
|
|
ddbg("bits = %r" % bits)
|
2008-08-07 04:32:28 +00:00
|
|
|
if bits:
|
|
|
|
data = re.sub(DoDont, '\\1', data)
|
2008-08-07 16:26:46 +00:00
|
|
|
ddbg("telnet: DO/DON'T answer")
|
2008-08-07 04:32:28 +00:00
|
|
|
# answer DO and DON'T with WON'T
|
|
|
|
for bit in bits:
|
2008-08-07 16:26:46 +00:00
|
|
|
self.write(IAC + WONT + bit[1])
|
2008-08-07 04:32:28 +00:00
|
|
|
|
|
|
|
bits = re.findall(WillWont, data)
|
|
|
|
if bits:
|
|
|
|
data = re.sub(WillWont, '\\1', data)
|
2008-08-07 16:26:46 +00:00
|
|
|
ddbg("telnet: WILL/WON'T answer")
|
2008-08-07 04:32:28 +00:00
|
|
|
for bit in bits:
|
|
|
|
# answer WILLs and WON'T with DON'Ts
|
2008-08-07 16:26:46 +00:00
|
|
|
self.write(IAC + DONT + bit[1])
|
2008-08-07 04:32:28 +00:00
|
|
|
|
|
|
|
bits = re.findall(AreYouThere, data)
|
|
|
|
if bits:
|
2008-08-07 16:26:46 +00:00
|
|
|
ddbg("telnet: am I there answer")
|
2008-08-07 04:32:28 +00:00
|
|
|
data = re.sub(AreYouThere, '\\1', data)
|
|
|
|
for bit in bits:
|
|
|
|
self.write("Yes, I'm still here, I think.\n")
|
|
|
|
|
2008-08-07 16:26:46 +00:00
|
|
|
(data, interrupts) = re.subn(IpTelnet, '\\1', data)
|
|
|
|
if interrupts:
|
|
|
|
ddbg("debugserver: Ctrl-C detected")
|
2008-08-07 04:32:28 +00:00
|
|
|
raise KeyboardInterrupt
|
|
|
|
|
|
|
|
data = re.sub(OtherTelnet, '\\1', data) # and any other Telnet codes
|
2008-08-07 16:26:46 +00:00
|
|
|
data = data.replace(IAC + IAC, IAC) # and handle escapes
|
2008-08-07 04:32:28 +00:00
|
|
|
|
|
|
|
if data != odata:
|
2008-11-04 17:01:00 +00:00
|
|
|
ddbg("debugserver: Replaced %r with %r" % (odata, data))
|
2008-08-07 04:32:28 +00:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
def raw_input(self, prompt = None):
|
2008-11-04 17:01:00 +00:00
|
|
|
ddbg("debugserver: raw_input prompt = %r" % prompt)
|
2008-08-07 04:32:28 +00:00
|
|
|
if prompt:
|
|
|
|
self.write(prompt)
|
|
|
|
|
|
|
|
buf = ''
|
|
|
|
compstate = 0
|
|
|
|
while True:
|
2008-08-07 16:26:46 +00:00
|
|
|
data = self.server.socketio.read(1)
|
2008-11-04 17:01:00 +00:00
|
|
|
ddbg('raw_input: char=%r' % data)
|
2008-08-07 16:26:46 +00:00
|
|
|
if data == LF or data == '\006':
|
2008-08-07 20:34:47 +00:00
|
|
|
buf = self.parse_telnet(buf + data)
|
2008-08-07 04:32:28 +00:00
|
|
|
if buf != '':
|
|
|
|
return buf
|
|
|
|
elif data == '\004' or data == '': # ^D
|
|
|
|
raise EOFError
|
|
|
|
else:
|
|
|
|
buf += data
|
|
|
|
|
|
|
|
def write(self, data):
|
2008-11-04 17:01:00 +00:00
|
|
|
ddbg("debugserver: write %r" % data)
|
2008-08-07 04:32:28 +00:00
|
|
|
self.server.socketio.write(data)
|
|
|
|
self.server.socketio.flush()
|
|
|
|
|
|
|
|
def run(self, server):
|
|
|
|
self.server = server
|
|
|
|
|
|
|
|
self.write("Welcome to the %s-%s debug server, have a nice stay\n" % (APP_NAME, APP_VERSION))
|
|
|
|
self.interact()
|
|
|
|
try:
|
|
|
|
self.write("Time to go. Bye!\n")
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2008-08-07 13:59:10 +00:00
|
|
|
def spawn(env):
|
|
|
|
PythonConsoleServer.env = env
|
2018-04-24 18:22:10 +00:00
|
|
|
tcpserver = socketserver.TCPServer(('127.0.0.1', 0), PythonConsoleServer)
|
2008-08-07 16:26:46 +00:00
|
|
|
dbg("debugserver: listening on %s" % str(tcpserver.server_address))
|
2008-08-07 04:32:28 +00:00
|
|
|
debugserver = threading.Thread(target=tcpserver.serve_forever, name="DebugServer")
|
|
|
|
debugserver.setDaemon(True)
|
|
|
|
debugserver.start()
|
|
|
|
return(debugserver, tcpserver)
|
|
|
|
|