def test_all(args): if args.debug: verifying_machine.debug = True good = 0 bad = 0 fbad = BadFile(args.file) for line in open(args.file, 'rb'): if line.startswith('#'): continue lock_script, txraw, index, block_timestamp = line.split() lock_script = HD(lock_script) txraw = HD(txraw) index = int(index) block_timestamp = int(block_timestamp) if args.debug: W('tx: %d %s\n' % ( index, dhash(txraw)[::-1].encode('hex'), )) try: test(lock_script, txraw, index, block_timestamp) good += 1 except: W('error: %r\n' % (coro.compact_traceback(), )) bad += 1 fbad.write(line) if args.debug: W('hit <enter> to continue\n') raw_input() W('%d good, %d bad\n' % (good, bad)) fbad.close()
def handle_request (self, request): parts = request.path.split ('/')[2:] # ignore ['', 'admin'] subcmd = parts[0] if not subcmd: subcmd = 'status' method_name = 'cmd_%s' % (subcmd,) if self.safe_cmd.match (subcmd) and hasattr (self, method_name): request['content-type'] = 'text/html' request.set_deflate() method = getattr (self, method_name) request.push ( '\r\n'.join ([ '<html><head>', css, '</head><body>', '<h1>caesure admin</h1>', ]) ) self.menu (request) try: method (request, parts) except SystemExit: raise except: request.push ('<h1>something went wrong</h1>') request.push ('<pre>%r</pre>' % (coro.compact_traceback(),)) request.push ('<hr>') self.menu (request) request.push ('</body></html>') request.done() else: request.error (400)
def run(self): self.stream = read_stream.sock_stream(self.conn) upgrade = False try: try: for request in request_stream(self, self.stream).gen_requests(): if request.bad: # bad request request.error(400) else: try: handler = self.pick_handler(request) if handler: # XXX with_timeout() ? handler.handle_request(request) else: request.error(404) request.wait_until_done() except (coro.TimeoutError, coro.Interrupted): raise except HTTP_Upgrade: upgrade = True break # XXX use Exception here, avoid catch/raise of coro.TimeoutError/Interrupted? except: tb = coro.compact_traceback() self.server.log('error: %r request=%r tb=%r' % (self.peer, request, tb)) request.error(500, tb) except (OSError, coro.TimeoutError, coro.ClosedError): pass finally: if not upgrade: self.conn.close()
def handle_request (self, request): parts = request.path.split ('/')[2:] subcmd = parts[0] if not subcmd: subcmd = 'status' method_name = 'cmd_%s' % (subcmd,) if self.safe_cmd.match (subcmd) and hasattr (self, method_name): request['content-type'] = 'text/html' request.set_deflate() method = getattr (self, method_name) OB = OutputBuffer (request) PUSH = OB.push PUSH ( elem0 ('html'), HEAD (STYLE (css, type='text/css')), elem0 ('body'), H1 ('caesure admin'), elem0 ('hr'), ) self.menu (PUSH) try: method (request, PUSH, parts) except SystemExit: raise except: request.push ('<h1>something went wrong</h1>') request.push ('<pre>%r</pre>' % (coro.compact_traceback(),)) PUSH (elem0 ('hr')) self.menu (PUSH) PUSH (elems1 ('body', 'html')) OB.flush() request.done() else: request.error (400)
def handle_request(self, request): parts = request.path.split("/")[2:] # ignore ['', 'admin'] subcmd = parts[0] if not subcmd: subcmd = "status" method_name = "cmd_%s" % (subcmd,) if self.safe_cmd.match(subcmd) and hasattr(self, method_name): request["content-type"] = "text/html" request.set_deflate() method = getattr(self, method_name) request.push( "\r\n".join( [ '<!DOCTYPE HTML><html><head><meta charset="UTF-8"><title>Caesure admin</title>', css, "</head><body>", "<h1>caesure admin</h1>", ] ) ) self.menu(request) try: method(request, parts) except SystemExit: raise except: request.push("<strong>something went wrong</strong><br><br>") request.push("<pre>%r</pre>" % (coro.compact_traceback(),)) request.push("<hr>") self.menu(request) request.push("</body></html>") request.done() else: request.error(400)
def run (self, conn, peer): self.conn = conn self.peer = peer self.stream = read_stream.sock_stream (self.conn) try: try: for request in request_stream (self, self.stream).gen_requests(): if request.bad: # bad request request.error (400) else: try: handler = self.pick_handler (request) if handler: # XXX with_timeout() ? handler.handle_request (request) else: request.error (404) request.wait_until_done() except (coro.TimeoutError, coro.Interrupted): raise except: tb = coro.compact_traceback() request.error (500, tb) self.server.log ('error: %r request=%r tb=%r' % (self.peer, request, tb)) except (OSError, coro.TimeoutError, coro.ClosedError): pass finally: self.conn.close()
def run (self, conn, peer): self.conn = conn self.peer = peer self.stream = read_stream.sock_stream (self.conn) upgrade = False try: try: for request in request_stream (self, self.stream).gen_requests(): if request.bad: # bad request request.error (400) else: try: handler = self.pick_handler (request) if handler: # XXX with_timeout() ? handler.handle_request (request) else: request.error (404) request.wait_until_done() except (coro.TimeoutError, coro.Interrupted): raise except HTTP_Upgrade: upgrade = True break # XXX use Exception here, avoid catch/raise of coro.TimeoutError/Interrupted? except: tb = coro.compact_traceback() self.server.log ('error: %r request=%r tb=%r' % (self.peer, request, tb)) request.error (500, tb) except (OSError, coro.TimeoutError, coro.ClosedError): pass finally: if not upgrade: self.conn.close()
def test_all (args): if args.debug: verifying_machine.debug = True good = 0 bad = 0 fbad = BadFile (args.file) for line in open (args.file, 'rb'): if line.startswith ('#'): continue lock_script, txraw, index, block_timestamp = line.split() lock_script = HD (lock_script) txraw = HD (txraw) index = int (index) block_timestamp = int (block_timestamp) if args.debug: W ('tx: %d %s\n' % (index, dhash (txraw)[::-1].encode ('hex'),)) try: test (lock_script, txraw, index, block_timestamp) good += 1 except: W ('error: %r\n' % (coro.compact_traceback(),)) bad += 1 fbad.write (line) if args.debug: W ('hit <enter> to continue\n') raw_input() W ('%d good, %d bad\n' % (good, bad)) fbad.close()
def test_all(): import sys good = 0 bad = 0 for line in open(sys.argv[1], 'rb'): lock_script, txraw, index, block_timestamp = line.split() lock_script = HD(lock_script) txraw = HD(txraw) index = int(index) block_timestamp = int(block_timestamp) try: test(lock_script, txraw, index, block_timestamp) good += 1 except: print coro.compact_traceback() bad += 1 print '%d good, %d bad' % (good, bad)
def test_all(): import sys good = 0 bad = 0 for line in open (sys.argv[1], 'rb'): lock_script, txraw, index, block_timestamp = line.split() lock_script = HD (lock_script) txraw = HD (txraw) index = int (index) block_timestamp = int (block_timestamp) try: test (lock_script, txraw, index, block_timestamp) good += 1 except: print coro.compact_traceback() bad += 1 print '%d good, %d bad' % (good, bad)
def handle_request(self, req): try: handler = self.pick_handler(req) if handler: # XXX with_timeout() handler.handle_request(req) else: req.error(404) except: tb = coro.compact_traceback() req.error(500, tb) self.log('error: %r request=%r tb=%r' % (self.peer, req, tb))
def handle_request(self, req): try: handler = self.pick_handler(req) if handler: # XXX with_timeout() handler.handle_request(req) else: req.error(404) except: tb = coro.compact_traceback() req.error(500, tb) self.log("error: %r request=%r tb=%r" % (self.peer, req, tb))
def broadcast (self, payload, save=True): if save: self.drawing.append (payload) # copy to avoid "Set changed size during iteration" for client in list (self.clients): try: client.send_text (payload) except: self.dead.add (client) tb = coro.compact_traceback() W ('error: tb=%r' % (tb,)) self.clients.difference_update (self.dead) self.dead = set()
def broadcast(self, payload, save=True): if save: self.drawing.append(payload) # copy to avoid "Set changed size during iteration" for client in list(self.clients): try: client.send_text(payload) except: self.dead.add(client) tb = coro.compact_traceback() W('error: tb=%r' % (tb, )) self.clients.difference_update(self.dead) self.dead = set()
def do_command (self, cmd, data): if self.check_command_name (cmd): try: method = getattr (self, 'cmd_%s' % cmd,) except AttributeError: W ('no support for "%s" command\n' % (cmd,)) else: try: method (data) except: W ('caesure error: %r\n' % (coro.compact_traceback(),)) W (' ********** problem processing %r command\n' % (cmd,)) else: W ('bad command: "%r", ignoring\n' % (cmd,))
def run (self): self.thread_id = coro.current().thread_id() while not self.shutdown_flag: try: conn, addr = self.accept() client = self.create_connection() c = coro.spawn (client.run, conn, addr) c.set_name ('%s connection on %r' % (self.__class__.__name__, addr,)) except coro.Shutdown: break except: self.log ('error: %r' % (coro.compact_traceback(),)) coro.sleep_relative (0.25) continue self.sock.close()
def do_command (self, cmd, data): if self.waiting.has_key (cmd): self.waiting[cmd].wake_all ((cmd, data)) if self.check_command_name (cmd): try: method = getattr (self, 'cmd_%s' % cmd,) except AttributeError: G.log ('connection', 'unknown_command', cmd) else: try: method (data) except: G.log ('connection', 'error', cmd, coro.compact_traceback()) else: G.log ('connection', 'bad_command', cmd)
def get_block (self, conn, name): try: self.requested.add (name) strname = str(name) t0 = coro.now_usec G.log ('hoover', 'asked', strname) self.add_block (conn.get_block (name)) G.log ('hoover', 'recv', strname) self.in_flight_sem.release(1) except coro.TimeoutError: # let some other connection try it... G.log ('hoover', 'retry', strname) self.push (name) self.requested.remove (name) except: G.log ('hoover', 'error', coro.compact_traceback())
def get_block(self, conn, name): try: self.requested.add(name) strname = str(name) t0 = coro.now_usec G.log('hoover', 'asked', strname) self.add_block(conn.get_block(name)) G.log('hoover', 'recv', strname) self.in_flight_sem.release(1) except coro.TimeoutError: # let some other connection try it... G.log('hoover', 'retry', strname) self.push(name) self.requested.remove(name) except: G.log('hoover', 'error', coro.compact_traceback())
def do_command (self, cmd, data): G.hoover.live_cv.wake_one (self) if self.waiting.has_key (cmd): self.waiting[cmd].wake_all ((cmd, data)) if self.check_command_name (cmd): try: method = getattr (self, 'cmd_%s' % cmd,) except AttributeError: G.log ('connection', 'unknown_command', cmd) else: try: method (data) except: G.log ('connection', 'error', cmd, coro.compact_traceback()) else: G.log ('connection', 'bad_command', cmd)
def run(self): self.thread_id = coro.current().thread_id() while not self.shutdown_flag: try: conn, addr = self.accept() client = self.create_connection(conn, addr) c = coro.spawn(client.run) c.set_name('%s connection on %r' % ( self.__class__.__name__, addr, )) except coro.Shutdown: break except: self.log('error: %r' % (coro.compact_traceback(), )) coro.sleep_relative(0.25) continue self.sock.close()
def _run (self, server_s): secure = self.is_secure() self.thread_id = coro.current().thread_id() while not self.shutdown_flag: try: conn, addr = server_s.accept() client = http_client() coro.spawn (client.run, conn, addr, self, self._handlers) except coro.Shutdown: # server-shutdown break except: qlog.write('COMMON.APP_FAILURE', ('%s accept handler error %s' % (self.__class__.__name__, coro.compact_traceback()))) coro.sleep_relative(0.25) continue server_s.close() return None
def _run(self, server_s): secure = self.is_secure() self.thread_id = coro.current().thread_id() while not self.shutdown_flag: try: conn, addr = server_s.accept() client = http_client() coro.spawn(client.run, conn, addr, self, self._handlers) except coro.Shutdown: # server-shutdown break except: qlog.write( 'COMMON.APP_FAILURE', ('%s accept handler error %s' % (self.__class__.__name__, coro.compact_traceback()))) coro.sleep_relative(0.25) continue server_s.close() return None
def run(self, conn, peer, server_obj, handlers): self.conn = conn self.server = server_obj self.peer = peer # Note that peer could be a fake address, and server_obj can be None. # These indicate a "backdoor" request from the gui. try: try: count = 0 qlog.write('WEBUI.CONN_INIT', 'http', id(self), peer[0], peer[1]) while 1: if self.server and self.server.shutdown_flag: break try: # We use self.stream to read the header line-by-line # and then switch to reading directly from the socket # for the body (if needed). Reuse the previous # instance if it exists, to support HTTP pipelining. if not self.stream: self.stream = read_stream.stream_reader( self.conn.recv) request_line = self.read_line() if not request_line: break except socket.error: qlog.write('WEBUI.CONN_ERROR', 'http', id(self), 'socket error') break count = count + 1 headers = self.read_header() #print '\n'.join (headers) + '\n\n' request = http_request(self, request_line, headers) request.read_body() if request._error: # Bad Request request.error(400) return else: try: try: handler = self.pick_handler(handlers, request) if handler: handler.handle_request(request) else: self.not_found(request) if not request._done: request.done() except OSError, err: if err[0] == errno.EPIPE: pass # ignore broken pipe error else: raise # process exception in outer try # These exceptions are used inside the coro # stuff and shouldn't be thrown away except (coro.TimeoutError, coro.Interrupted): raise except: tb = coro.compact_traceback() ## sys.stderr.write (repr(tb)) request.error(500, tb) qlog.write('COMMON.APP_FAILURE', tb + ' request: ' + ` request `) tb = None if request._close: # ok, this gets interesting. the connection needs to close # here. the finally clause below isn't getting hit because # the session and client are running in the same coroutine. # that's bad, I think. conn.close() break # this should be a policy decision of the owner of logfp # self.logfp.flush() except read_stream.BufferOverflow: # Indicates a request header that exceeded the line # buffer, which may indicate an attack on the server. # We just close the connection without a response. # TODO:lrosenstein - log this since it may be an attack? qlog.write('WEBUI.CONN_ERROR', 'http', id(self), 'line buffer limit exceeded') pass except sslip.Error, why: # Most likely a problem with SSL negotiation qlog.write('WEBUI.CONN_ERROR', 'https', id(self), why[1]) pass except OSError, err: # We got some kind of I/O error that wasn't handled # elsewhere. Since this seem to happen because the # client closed the connection, it is safe to ignore # the exception. qlog.write('WEBUI.CONN_ERROR', 'http', id(self), 'OS error %s' % str(err[1])) pass
def exception_notifier(): me = coro.current() traceback = coro.compact_traceback() G.log ('exception', me.id, me.name, traceback) WY ('exception: %r %r %r\n' % (me.id, me.name, traceback))
def run (self, conn, peer, server_obj, handlers): self.conn = conn self.server = server_obj self.peer = peer # Note that peer could be a fake address, and server_obj can be None. # These indicate a "backdoor" request from the gui. try: try: count = 0 qlog.write('WEBUI.CONN_INIT', 'http', id(self), peer[0], peer[1]) while 1: if self.server and self.server.shutdown_flag: break try: # We use self.stream to read the header line-by-line # and then switch to reading directly from the socket # for the body (if needed). Reuse the previous # instance if it exists, to support HTTP pipelining. if not self.stream: self.stream = read_stream.stream_reader(self.conn.recv) request_line = self.read_line() if not request_line: break except socket.error: qlog.write('WEBUI.CONN_ERROR', 'http', id(self), 'socket error') break count = count + 1 headers = self.read_header() #print '\n'.join (headers) + '\n\n' request = http_request (self, request_line, headers) request.read_body() if request._error: # Bad Request request.error (400) return else: try: try: handler = self.pick_handler (handlers, request) if handler: handler.handle_request (request) else: self.not_found (request) if not request._done: request.done() except OSError, err: if err[0] == errno.EPIPE: pass # ignore broken pipe error else: raise # process exception in outer try # These exceptions are used inside the coro # stuff and shouldn't be thrown away except (coro.TimeoutError, coro.Interrupted): raise except: tb = coro.compact_traceback() ## sys.stderr.write (repr(tb)) request.error (500, tb) qlog.write('COMMON.APP_FAILURE', tb + ' request: ' + `request`) tb = None if request._close: # ok, this gets interesting. the connection needs to close # here. the finally clause below isn't getting hit because # the session and client are running in the same coroutine. # that's bad, I think. conn.close() break # this should be a policy decision of the owner of logfp # self.logfp.flush() except read_stream.BufferOverflow: # Indicates a request header that exceeded the line # buffer, which may indicate an attack on the server. # We just close the connection without a response. # TODO:lrosenstein - log this since it may be an attack? qlog.write('WEBUI.CONN_ERROR', 'http', id(self), 'line buffer limit exceeded') pass except sslip.Error, why: # Most likely a problem with SSL negotiation qlog.write('WEBUI.CONN_ERROR', 'https', id(self), why[1]) pass except OSError, err: # We got some kind of I/O error that wasn't handled # elsewhere. Since this seem to happen because the # client closed the connection, it is safe to ignore # the exception. qlog.write('WEBUI.CONN_ERROR', 'http', id(self), 'OS error %s' % str(err[1])) pass
def exception_notifier(): me = coro.current() G.log ('exception', me.id, me.name, coro.compact_traceback())