def main(): logging.basicConfig( level=logging.DEBUG, format="%(asctime)s %(levelname)s %(message)s", datefmt="%Y-%m-%d %H:%M:%SZ" ) parser = optparse.OptionParser() parser.set_usage("usage: %prog [options] handler [path ...]") options, args = parser.parse_args() if len(args) < 1: parser.error("expected handler") try: handler_spec = json.loads(args[0]) except ValueError: handler_spec = args[0] handler_type = handlers.load_handler(handler_spec) def generate_events(): for line in fileinput.input(args[1:]): line = line.strip() if not line: continue yield events.Event(json.loads(line)) idiokit.main_loop(idiokit.pipe( _feed(generate_events()), handler_type(log=logging).transform(), _print_events() ))
def test_should_allow_disabling_certificate_verification(self): @idiokit.stream def test(): server, url = yield create_https_server("localhost") _, fileobj = yield idiokit.pipe(server, utils.fetch_url(url, verify=False)) self.assertEqual(fileobj.read(), "ok") idiokit.main_loop(test())
def test_should_allow_passing_custom_ca_certs_for_verification(self): @idiokit.stream def test(): server, url = yield create_https_server("localhost") with tmpfile(ca_data) as ca_certs: _, fileobj = yield idiokit.pipe(server, utils.fetch_url(url, verify=ca_certs)) self.assertEqual(fileobj.read(), "ok") idiokit.main_loop(test())
def test_should_allow_passing_Request_objects_as_url_parameter(self): @idiokit.stream def test(): server, url = yield create_https_server("localhost") request = urllib2.Request(url) with tmpfile(ca_data) as ca_certs: _, fileobj = yield idiokit.pipe(server, utils.fetch_url(request, verify=ca_certs)) self.assertEqual(fileobj.read(), "ok") idiokit.main_loop(test())
def test_should_verify_certificates_when_verify_is_True(self): @idiokit.stream def test(): server, url = yield create_https_server("localhost") try: yield idiokit.pipe(server, utils.fetch_url(url, verify=True)) except utils.FetchUrlFailed: return self.fail("fetch_url should fail due to a certificate verification error") idiokit.main_loop(test())
def test_should_check_hostname_when_verifying_certificates(self): @idiokit.stream def test(): server, url = yield create_https_server("127.0.0.1") with tmpfile(ca_data) as ca_certs: try: yield idiokit.pipe(server, utils.fetch_url(url, verify=ca_certs)) except idiokit.ssl.SSLCertificateError: return self.fail("fetch_url should fail due to a wrong hostname") idiokit.main_loop(test())
def test_should_allow_passing_custom_ca_certs_for_verification(self): @idiokit.stream def test(): server, url = yield create_https_server("localhost") with tmpfile(ca_data) as ca_certs: _, fileobj = yield idiokit.pipe( server, utils.fetch_url(url, verify=ca_certs)) self.assertEqual(fileobj.read(), "ok") idiokit.main_loop(test())
def test_should_allow_passing_Request_objects_as_url_parameter(self): @idiokit.stream def test(): server, url = yield create_https_server("localhost") request = urllib2.Request(url) with tmpfile(ca_data) as ca_certs: _, fileobj = yield idiokit.pipe( server, utils.fetch_url(request, verify=ca_certs)) self.assertEqual(fileobj.read(), "ok") idiokit.main_loop(test())
def test_should_verify_certificates_when_verify_is_True(self): @idiokit.stream def test(): server, url = yield create_https_server("localhost") try: yield idiokit.pipe(server, utils.fetch_url(url, verify=True)) except utils.FetchUrlFailed: return self.fail( "fetch_url should fail due to a certificate verification error" ) idiokit.main_loop(test())
def run(self): @idiokit.stream def throw_stop_on_signal(): try: yield idiokit.consume() except idiokit.Signal: raise services.Stop() return idiokit.main_loop(throw_stop_on_signal() | self._run())
def run(): @idiokit.stream def main(socket_path, process_id, callable, args, keys): sock = socket.Socket(socket.AF_UNIX) try: yield sock.connect(socket_path) yield sock.sendall(process_id) yield decode(sock) | callable(*args, **keys) | encode(sock) finally: yield sock.close() socket_path, process_id, callable, args, keys = cPickle.load(sys.stdin) try: idiokit.main_loop(main(socket_path, process_id, callable, args, keys)) except (idiokit.Signal, _ConnectionLost): pass
def main(): parser = argparse.ArgumentParser( description= 'Run a simple IRCBot (default values are in server_configs.py') parser.add_argument('--cc-host', type=str, help='The C&C IRC server host', default=cc_host) parser.add_argument('--cc-port', type=str, help='The C&C IRC server port', default=cc_port) parser.add_argument('--cc-nick', type=str, help='The C&C IRC server nick', default=cc_nick) parser.add_argument('--cc-channel', type=str, help='The C&C nick for your bot', default=cc_channel) parser.add_argument('--whois-host', type=str, help='The target IRC server host', default=whois_host) parser.add_argument('--whois-port', type=str, help='The target IRC server port', default=whois_port) parser.add_argument('--whois-nick', type=str, help='The target IRC server nick', default=whois_nick) args = parser.parse_args() idiokit.main_loop(ircbot(args.cc_host, args.cc_port, args.cc_nick, args.cc_channel, \ args.whois_host, args.whois_port, args.whois_nick))
def test_http_adapter(self): @idiokit.stream def main(test_string, client): s = socket.Socket(socket.AF_INET) try: yield s.bind(("127.0.0.1", 0)) yield s.listen(1) _, port = yield s.getsockname() result = yield serve(test_string, s) | get(client, "http://127.0.0.1:{0}/".format(port)) finally: yield s.close() idiokit.stop(result) client = Client() self.assertEqual("this is a test", idiokit.main_loop(main("this is a test", client)))
def handle(handler_spec, msg_data): r""" Return a list of event dictionaries collected by handling msg_data using handler_spec. >>> from abusehelper.core.events import Event >>> from abusehelper.core.mail import Handler >>> >>> class MyHandler(Handler): ... @idiokit.stream ... def handle_text_plain(self, msg): ... yield idiokit.send(Event(a="test")) ... >>> handle(MyHandler, "From: [email protected]\n\nThis is the payload.") [{u'a': [u'test']}] Note that to simplify testing the output is a list of dictionaries instead of abusehelper.core.events.Event objects. msg_data will be cleaned using inspect.cleandoc, so the previous example can be expressed with triple quotes: >>> handle(MyHandler, ''' ... From: [email protected] ... ... This is the payload. ... ''') [{u'a': [u'test']}] """ handler_type = handlers.load_handler(handler_spec) msg = message_from_string(inspect.cleandoc(msg_data)) log = logging.getLogger("Null") log_handler = _NullHandler() log.addHandler(log_handler) try: handler = handler_type(log=log) return idiokit.main_loop(handler.handle(msg) | _collect_events()) finally: log.removeHandler(log_handler)
def handle(handler_spec, input_events): r""" Return a list of event dictionaries collected by handling input_events using handler_spec. >>> from abusehelper.core.events import Event >>> from abusehelper.core.transformation import Handler >>> >>> class MyHandler(Handler): ... @idiokit.stream ... def transform(self): ... while True: ... event = yield idiokit.next() ... event.add("a", "b") ... yield idiokit.send(event) ... >>> handle(MyHandler, [{}]) [{u'a': [u'b']}] Note that to simplify testing the output is a list of dictionaries instead of abusehelper.core.events.Event objects. """ handler_type = handlers.load_handler(handler_spec) log = logging.getLogger("Null") log_handler = _NullHandler() log.addHandler(log_handler) try: handler = handler_type(log=log) return idiokit.main_loop(idiokit.pipe( _feed(itertools.imap(events.Event, input_events)), handler.transform(), _collect_events() )) finally: log.removeHandler(log_handler)
def test_http_unix_adapter(self): @idiokit.stream def main(test_string, client): s = socket.Socket(socket.AF_UNIX) try: tmpdir = tempfile.mkdtemp() try: # Ensure that the path contains both upper and lower case # characters to test case-sensitivity. path = os.path.join(tmpdir, "socket.SOCKET") url = "http+unix://{0}".format(urllib.quote(path, "")) yield s.bind(path) yield s.listen(1) result = yield serve(test_string, s) | get(client, url) finally: shutil.rmtree(tmpdir) finally: yield s.close() idiokit.stop(result) client = Client() client.mount("http+unix://", HTTPUnixAdapter()) self.assertEqual("this is a test", idiokit.main_loop(main("this is a test", client)))
def run(self): return idiokit.main_loop(self.main())
@idiokit.stream def client(host, port, cafile): sock = socket.Socket() yield sock.connect((host, port)) if cafile: ssl_sock = yield ssl.wrap_socket(sock, require_cert=True, ca_certs=cafile) else: ssl_sock = yield ssl.wrap_socket(sock, require_cert=True) cert = yield ssl_sock.getpeercert() ssl.match_hostname(cert, host) if len(sys.argv) < 3 or len(sys.argv) > 4: exit("Usage: %s <HOST> <PORT> [CA_FILE]" % sys.argv[0]) host = sys.argv[1] port = sys.argv[2] cafile = sys.argv[3] if len(sys.argv) > 3 else None try: idiokit.main_loop(client(host, int(port), cafile)) print "ACCEPT" except idiokit.ssl.SSLError: print "REJECT" else: pass
def execute(self): return idiokit.main_loop(self._execute())
def handle_msg(msg): handler = handler_type(log=logging) idiokit.main_loop(handler.handle(msg) | _print_events())
def _host_lookup(self, lookup): hl = _hostlookup.HostLookup(hosts_file=self._hosts.name) return idiokit.main_loop(hl.host_lookup(lookup))
def run(self): try: return idiokit.main_loop(self.main()) except idiokit.Signal: pass
def run(self): return idiokit.main_loop(self.configs() | self.read() | self.main())
port = int(args[1]) cert = None if len(args) == 3: cert = args[2] print "Using certificate(s) from file: {0}".format(cert) try: sock = idiokit.socket.Socket() yield sock.connect((host, port)) sock = yield idiokit.ssl.wrap_socket( sock, require_cert=True, ca_certs=cert ) peer_cert = yield sock.getpeercert() idiokit.ssl.match_hostname(peer_cert, host) except idiokit.socket.SocketError as e: print "connection failed ({0})".format(e) except idiokit.socket.SocketGAIError as e: print "connection failed ({0})".format(e) except idiokit.ssl.SSLError as e: print "idiokit strongly disapproves! ({0})".format(e) else: print "idiokit approves this connection" finally: sock.close() if __name__ == '__main__': idiokit.main_loop(main())
import idiokit from idiokit.http.server import serve_http from idiokit.http.handlers.router import Router from idiokit.http.handlers.redirect import redirect from idiokit.http.handlers.filehandler import filehandler, BakedFileSystem @idiokit.stream def ping(addr, request, response): yield response.write("PONG\n") if __name__ == "__main__": router = Router({ "ping": ping, "pong": redirect("/ping"), "/": filehandler( BakedFileSystem({ "index.html": "Hello world!\n" }), index="index.html" ) }) print "Serving http://127.0.0.1:8080/" idiokit.main_loop(serve_http(router, "127.0.0.1", 8080))
import sys import idiokit from idiokit import socket, ssl @idiokit.stream def client(host, port): sock = socket.Socket() yield sock.connect((host, port)) ssl_sock = yield ssl.wrap_socket(sock, require_cert=True) cert = yield ssl_sock.getpeercert() ssl.match_hostname(cert, host) host = sys.argv[1] port = sys.argv[2] try: idiokit.main_loop(client(host, int(port))) print "VERIFY SUCCESS" except idiokit.ssl.SSLError: print "VERIFY FAILURE"