def __init__(self, sock, host, port): """ init the class """ Telnet.__init__(self, host=host, port=port, sock=sock) self.ttype = 'Client' self.connectedtime = None self.pwtries = 0 self.banned = False self.viewonly = False if sock: self.connected = True self.connectedtime = time.localtime() self.api('events.register')('to_client_event', self.addtooutbufferevent, prio=99) self.api('options.prepareclient')(self) self.state = PASSWORD self.addtooutbufferevent({'original':self.api('colors.convertcolors')( '@R#BP@w: @RPlease enter the proxy password:@w'), 'dtype':'passwd'})
def addtooutbuffer(self, args, raw=False): """ add to the outbuffer required: args - a string or a dictionary that contains a data key and a raw key optional: raw - set a raw flag, which means IAC will not be doubled """ data = '' dtype = 'fromclient' if isinstance(args, dict): data = args['data'] dtype = args['dtype'] if 'raw' in args: raw = args['raw'] else: data = args if len(dtype) == 1 and ord(dtype) in self.options: Telnet.addtooutbuffer(self, data, raw) elif dtype == 'fromclient': Telnet.addtooutbuffer(self, data, raw)
def __init__(self, sock, host, port): """ init the class """ Telnet.__init__(self, host=host, port=port, sock=sock) self.ttype = 'Client' self.connectedtime = None self.pwtries = 0 self.banned = False self.viewonly = False if sock: self.connected = True self.connectedtime = time.localtime() self.api('events.register')('to_client_event', self.addtooutbufferevent, prio=99) self.api('options.prepareclient')(self) self.state = PASSWORD self.addtooutbufferevent({ 'original': self.api('colors.convertcolors')( '@R#BP@w: @RPlease enter the proxy password:@w'), 'dtype': 'passwd' })
def handle_close(self): """ handle a close """ self.api('send.client')("%s - %s: Client Disconnected" % \ (self.host, self.port)) self.api('managers.getm')('proxy').removeclient(self) self.api('events.eraise')('client_disconnected', {'client': self}) self.api('events.unregister')('to_client_event', self.addtooutbuffer) Telnet.handle_close(self)
def handle_close(self): """ hand closing the connection """ self.api('send.msg')('Disconnected from mud', 'net') self.api('send.client')(self.api('colors.convertcolors')( '@R#BP@w: The mud closed the connection')) self.api('options.resetoptions')(self, True) Telnet.handle_close(self) self.connectedtime = None self.api('events.eraise')('muddisconnect', {}, calledfrom="mud")
def handle_read(self): """ handle a read """ Telnet.handle_read(self) data = self.getdata() if data: ndata = self.lastmsg + data alldata = ndata.replace("\r", "") ndatal = alldata.split('\n') self.lastmsg = ndatal[-1] for i in ndatal[:-1]: tosend = i try: tnoansi = self.api('colors.stripansi')(tosend) except AttributeError: tnoansi = tosend try: tconvertansi = self.api('colors.convertansi')(tosend) except AttributeError: tconvertansi = tosend if tosend != tconvertansi: self.api('send.msg')('converted %s to %s' % (repr(tosend), tconvertansi), 'ansi') newdata = self.api('events.eraise')('from_mud_event', { 'original': tosend, 'dtype': 'frommud', 'noansi': tnoansi, 'convertansi': tconvertansi }) if 'original' in newdata: tosend = newdata['original'] if 'omit' in newdata and newdata['omit']: tosend = None if tosend != None: #data cannot be transformed here if self.api('api.has')('colors.stripansi'): tnoansi = self.api('colors.stripansi')(tosend) else: tnoansi = tosend if self.api('api.has')('colors.convertansi'): tconvertansi = self.api('colors.convertansi')(tosend) else: tconvertansi = tosend self.api('events.eraise')('to_client_event', { 'original': tosend, 'dtype': 'frommud', 'noansi': tnoansi, 'convertansi': tconvertansi })
def addtooutbuffer(self, data, raw=False): """ add to the outbuffer required: data - a string or a dictionary that contains a data key and a raw key optional: raw - set a raw flag, which means IAC will not be doubled """ dtype = 'fromclient' datastr = "" trace = None if isinstance(data, dict): datastr = data['data'] dtype = data['dtype'] if 'raw' in data: raw = data['raw'] if 'trace' in data: trace = data['trace'] else: datastr = data if len(dtype) == 1 and ord(dtype) in self.options: if trace: trace['changes'].append({ 'flag': 'Sent', 'data': '"%s" to mud with raw: %s and datatype: %s' % (repr(datastr.strip()), raw, dtype), 'plugin': 'proxy', 'callstack': self.api('api.callstack')() }) Telnet.addtooutbuffer(self, datastr, raw) elif dtype == 'fromclient': if trace: trace['changes'].append({ 'flag': 'Sent', 'data': '"%s" to mud with raw: %s and datatype: %s' % (datastr.strip(), raw, dtype), 'plugin': 'proxy', 'callstack': self.api('api.callstack')() }) Telnet.addtooutbuffer(self, datastr, raw)
def __init__(self): """ init the class """ Telnet.__init__(self) self.lastmsg = '' self.ttype = 'BastProxy' self.connectedtime = None self.api('events.register')('to_mud_event', self.addtooutbuffer, prio=99) self.api('options.prepareserver')(self) self.api('managers.add')('mud', self) self.api('log.adddtype')('rawmud')
def handle_close(self): """ hand closing the connection """ self.api('send.msg')('Disconnected from mud', 'net') self.api('events.eraise')('to_client_event', { 'original': self.api('colors.convertcolors') ('@R#BP@w: The mud closed the connection'), 'dtype': 'fromproxy' }) self.api('options.resetoptions')(self, True) Telnet.handle_close(self) self.connectedtime = None self.api('events.eraise')('muddisconnect', {})
def handle_close(self): """ handle a close """ if self.state != CLOSING: self.state = CLOSING self.api('send.client')("%s - %s: Client Disconnected" % \ (self.host, self.port)) self.api('send.msg')("%s - %s: Client Disconnected" % \ (self.host, self.port), primary='net') self.api('events.eraise')('client_disconnected', {'client':self}, calledfrom="client") self.api('events.unregister')('to_client_event', self.addtooutbufferevent) if self.connected: while self.outbuffer: self.handle_write() Telnet.handle_close(self)
def fill_rawq(self): """ Fill raw queue from exactly one recv() system call. Block if no data is immediately available. Set self.eof when connection is closed. """ buf = Telnet.fill_rawq(self) self.api('log.writefile')('rawmud', buf) return buf
def __init__(self): """ init the class """ Telnet.__init__(self) self.username = None self.password = None self.api = API() self.lastmsg = '' self.clients = [] self.vclients = [] self.ttype = 'BastProxy' self.banned = {} self.connectedtime = None self.api('events.register')('to_mud_event', self.addtooutbuffer, prio=99) self.api('options.prepareserver')(self)
def handle_close(self): """ handle a close """ if self.state != CLOSING: self.state = CLOSING self.api('send.client')("%s - %s: Client Disconnected" % \ (self.host, self.port)) self.api('send.msg')("%s - %s: Client Disconnected" % \ (self.host, self.port), primary='net') self.api('events.eraise')('client_disconnected', { 'client': self }, calledfrom="client") self.api('events.unregister')('to_client_event', self.addtooutbufferevent) if self.connected: while self.outbuffer: self.handle_write() Telnet.handle_close(self)
def addtooutbufferevent(self, args): """ this function adds to the output buffer """ if 'client' in args and args['client'] and args['client'] != self: return outbuffer = args['original'] dtype = None raw = False if 'dtype' in args: dtype = args['dtype'] if not dtype: dtype = 'fromproxy' if 'raw' in args: raw = args['raw'] if outbuffer is not None: if (dtype == 'fromproxy' or dtype == 'frommud') \ and self.state == CONNECTED: outbuffer = "".join([outbuffer, '\r\n']) Telnet.addtooutbuffer(self, outbuffer, raw) elif len(dtype) == 1 and ord(dtype) in self.options \ and self.state == CONNECTED: Telnet.addtooutbuffer(self, outbuffer, raw) elif dtype == 'passwd' and self.state == PASSWORD: outbuffer = "".join([outbuffer, '\r\n']) Telnet.addtooutbuffer(self, outbuffer, raw)
def addtooutbuffer(self, data, raw=False): """ add to the outbuffer required: data - a string or a dictionary that contains a data key and a raw key optional: raw - set a raw flag, which means IAC will not be doubled """ dtype = 'fromclient' datastr = "" trace = None if isinstance(data, dict): datastr = data['data'] dtype = data['dtype'] if 'raw' in data: raw = data['raw'] if 'trace' in data: trace = data['trace'] else: datastr = data if len(dtype) == 1 and ord(dtype) in self.options: if trace: trace['changes'].append({'flag':'Sent', 'data':'"%s" to mud with raw: %s and datatype: %s' % (repr(datastr.strip()), raw, dtype), 'plugin':'proxy', 'callstack':self.api('api.callstack')()}) Telnet.addtooutbuffer(self, datastr, raw) elif dtype == 'fromclient': if trace: trace['changes'].append({'flag':'Sent', 'data':'"%s" to mud with raw: %s and datatype: %s' % (datastr.strip(), raw, dtype), 'plugin':'proxy', 'callstack':self.api('api.callstack')()}) Telnet.addtooutbuffer(self, datastr, raw)
def addtooutbufferevent(self, args): """ this function adds to the output buffer """ outbuffer = args['original'] dtype = None raw = False if 'dtype' in args: dtype = args['dtype'] if not dtype: dtype = 'fromproxy' if 'raw' in args: raw = args['raw'] if outbuffer != None: if (dtype == 'fromproxy' or dtype == 'frommud') \ and self.state == CONNECTED: outbuffer = outbuffer + '\r\n' Telnet.addtooutbuffer(self, outbuffer, raw) elif len(dtype) == 1 and ord(dtype) in self.options \ and self.state == CONNECTED: Telnet.addtooutbuffer(self, outbuffer, raw) elif dtype == 'passwd' and self.state == PASSWORD: outbuffer = outbuffer + '\r\n' Telnet.addtooutbuffer(self, outbuffer, raw)
def handle_read(self): """ handle a read """ Telnet.handle_read(self) data = self.getdata() if data: ndata = "".join([self.lastmsg, data]) # don't care about \r alldata = ndata.replace("\r", "") # split on \n ndatal = alldata.split('\n') self.lastmsg = ndatal[-1] for i in ndatal[:-1]: tosend = i try: tnoansi = self.api('colors.stripansi')(tosend) except AttributeError: tnoansi = tosend try: tconvertansi = self.api('colors.convertansi')(tosend) except AttributeError: tconvertansi = tosend if tosend != tconvertansi: self.api('send.msg')('converted %s to %s' % (repr(tosend), tconvertansi), 'ansi') trace = {} trace['dtype'] = 'frommud' trace['original'] = tosend trace['changes'] = [] data = { 'original': tosend, 'data': tosend, 'dtype': 'frommud', 'noansi': tnoansi, 'convertansi': tconvertansi, 'trace': trace } self.api('events.eraise')('muddata_trace_started', data, calledfrom='proxy') # this event can be used to transform the data newdata = self.api('events.eraise')('from_mud_event', data, calledfrom="mud") self.api('events.eraise')('muddata_trace_finished', data, calledfrom='proxy') # use the original key in the returned dictionary # TODO: make this so that it uses a key just named data if 'original' in newdata: tosend = newdata['original'] # omit the data if it has been flagged if 'omit' in newdata and newdata['omit']: tosend = None if tosend != None: #data cannot be transformed here, it goes straight to the client if self.api('api.has')('colors.stripansi'): tnoansi = self.api('colors.stripansi')(tosend) else: tnoansi = tosend if self.api('api.has')('colors.convertansi'): tconvertansi = self.api('colors.convertansi')(tosend) else: tconvertansi = tosend self.api('send.client')(tosend, dtype='frommud')
def handle_read(self): """ handle a read """ if not self.connected: return Telnet.handle_read(self) data = self.getdata() if data: if self.state == CONNECTED: if self.viewonly: self.addtooutbufferevent({ 'todata': self.api('colors.convertcolors')( '@R#BP@w: @RYou are in view mode!@w') }) else: if data: self.api('send.execute')(data, fromclient=True) elif self.state == PASSWORD: data = data.strip() proxyp = self.api('plugins.getp')('proxy') dpw = proxyp.api('proxy.proxypw')() vpw = proxyp.api('proxy.proxypwview')() if dpw and data == dpw: self.api('send.msg')('Successful password from %s : %s' % \ (self.host, self.port), 'net') self.state = CONNECTED self.api('events.eraise')('client_connected', { 'client': self }, calledfrom="client") self.api('send.client')("%s - %s: Client Connected" % \ (self.host, self.port)) elif vpw and data == vpw: self.api('send.msg')('Successful view password from %s : %s' % \ (self.host, self.port), 'net') self.state = CONNECTED self.viewonly = True self.addtooutbufferevent({ 'original': self.api('colors.convertcolors')( '@R#BP@W: @GYou are connected in view mode@w') }) self.api('events.eraise')('client_connected_view', { 'client': self }, calledfrom="client") self.api('send.client')( "%s - %s: Client Connected (View Mode)" % \ (self.host, self.port)) else: self.pwtries += 1 if self.pwtries == 5: self.addtooutbufferevent({ 'original': self.api('colors.convertcolors') ('@R#BP@w: @RYou have been BANNED for 10 minutes:@w' ), 'dtype': 'passwd' }) self.api('send.msg')('%s has been banned.' % self.host, 'net') self.api('clients.addbanned')(self.host) self.handle_close() else: self.addtooutbufferevent({ 'original': self.api('colors.convertcolors') ('@R#BP@w: @RPlease try again! Proxy Password:@w'), 'dtype': 'passwd' })
def handle_read(self): """ handle a read """ Telnet.handle_read(self) data = self.getdata() if data: ndata = "".join([self.lastmsg, data]) # don't care about \r alldata = ndata.replace("\r", "") # split on \n ndatal = alldata.split('\n') self.lastmsg = ndatal[-1] for i in ndatal[:-1]: tosend = i try: tnoansi = self.api('colors.stripansi')(tosend) except AttributeError: tnoansi = tosend try: tconvertansi = self.api('colors.convertansi')(tosend) except AttributeError: tconvertansi = tosend if tosend != tconvertansi: self.api('send.msg')('converted %s to %s' % (repr(tosend), tconvertansi), 'ansi') trace = {} trace['dtype'] = 'frommud' trace['original'] = tosend trace['changes'] = [] data = {'original':tosend, 'data':tosend, 'dtype':'frommud', 'noansi':tnoansi, 'convertansi':tconvertansi, 'trace':trace} self.api('events.eraise')('muddata_trace_started', data, calledfrom='proxy') # this event can be used to transform the data newdata = self.api('events.eraise')('from_mud_event', data, calledfrom="mud") self.api('events.eraise')('muddata_trace_finished', data, calledfrom='proxy') # use the original key in the returned dictionary # TODO: make this so that it uses a key just named data if 'original' in newdata: tosend = newdata['original'] # omit the data if it has been flagged if 'omit' in newdata and newdata['omit']: tosend = None if tosend != None: #data cannot be transformed here, it goes straight to the client if self.api('api.has')('colors.stripansi'): tnoansi = self.api('colors.stripansi')(tosend) else: tnoansi = tosend if self.api('api.has')('colors.convertansi'): tconvertansi = self.api('colors.convertansi')(tosend) else: tconvertansi = tosend self.api('send.client')(tosend, dtype='frommud')
def handle_read(self): """ handle a read """ if not self.connected: return Telnet.handle_read(self) data = self.getdata() if data: if self.state == CONNECTED: if self.viewonly: self.addtooutbufferevent( {'todata':self.api('colors.convertcolors')( '@R#BP@w: @RYou are in view mode!@w')}) else: if data: self.api('send.execute')(data, fromclient=True) elif self.state == PASSWORD: data = data.strip() proxyp = self.api('plugins.getp')('proxy') dpw = proxyp.api('proxy.proxypw')() vpw = proxyp.api('proxy.proxypwview')() if dpw and data == dpw: self.api('send.msg')('Successful password from %s : %s' % \ (self.host, self.port), 'net') self.state = CONNECTED self.api('events.eraise')('client_connected', {'client':self}, calledfrom="client") self.api('send.client')("%s - %s: Client Connected" % \ (self.host, self.port)) elif vpw and data == vpw: self.api('send.msg')('Successful view password from %s : %s' % \ (self.host, self.port), 'net') self.state = CONNECTED self.viewonly = True self.addtooutbufferevent( {'original':self.api('colors.convertcolors')( '@R#BP@W: @GYou are connected in view mode@w')}) self.api('events.eraise')('client_connected_view', {'client':self}, calledfrom="client") self.api('send.client')( "%s - %s: Client Connected (View Mode)" % \ (self.host, self.port)) else: self.pwtries += 1 if self.pwtries == 5: self.addtooutbufferevent( {'original':self.api('colors.convertcolors')( '@R#BP@w: @RYou have been BANNED for 10 minutes:@w'), 'dtype':'passwd'}) self.api('send.msg')('%s has been banned.' % self.host, 'net') self.api('clients.addbanned')(self.host) self.handle_close() else: self.addtooutbufferevent( {'original':self.api('colors.convertcolors')( '@R#BP@w: @RPlease try again! Proxy Password:@w'), 'dtype':'passwd'})