Esempio n. 1
0
def _install_reactor():
    reactor_qual_name = "twisted.internet.reactor"
    import sys
    if reactor_qual_name not in sys.modules:
        if config.core.mode == "proxy":
            import asyncio
            from twisted.internet import asyncioreactor
            loop = asyncio.get_event_loop()
            asyncioreactor.install(eventloop=loop)
        elif config.core.mode == "wallet":
            # TODO, add qmuash support
            import asyncio
            from quamash import QEventLoop, QThreadExecutor
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
            from twisted.internet import asyncioreactor
            asyncioreactor.install(eventloop=loop)
            from twisted.internet import reactor
    else:
        import asyncio
        from quamash import QEventLoop, QThreadExecutor
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)

        from twisted.internet import asyncioreactor
        import twisted.internet
        reactor = asyncioreactor.AsyncioSelectorReactor(loop)
        twisted.internet.reactor = reactor
        sys.modules[reactor_qual_name] = reactor

    return sys.modules.get(reactor_qual_name)
Esempio n. 2
0
        elif k in loaded_config:
            result[k] = loaded_config[k]

    # copy things with no defaults
    for k, v in loaded_config.items():
        if k not in result:
            result[k] = v

    return result


if __name__ == "__main__":
    # TODO we don't want to have to install the reactor, when we can get away with
    #   it
    asyncioreactor.install()

    # we remove the global reactor to make it evident when it has accidentally
    # been used:
    # ! twisted.internet.reactor = None
    # TODO can't do this ^ yet, since twisted.internet.task.{coiterate,cooperate}
    #   (indirectly) depend on the globally-installed reactor and there's no way
    #   to pass in a custom one.
    #   and twisted.web.client uses twisted.internet.task.cooperate

    config = parse_config()
    config = merge_left_with_defaults(CONFIG_DEFAULTS, config)
    check_config(config)
    custom_reactor = cast(SygnalReactor, asyncioreactor.AsyncioSelectorReactor())
    sygnal = Sygnal(config, custom_reactor)
    sygnal.run()
Esempio n. 3
0
def main(argv):
    argv = parser.parse_args(argv)
    argv.cert = getattr(argv, "cert",
                        "%s.cert" % argv.keys[0].rsplit(".", 1)[0])
    argv.hash = getattr(hashes, argv.hash)()
    cert = None
    if not os.path.exists(argv.keys[0]) and not argv.no_gen:
        pkey = rsa.generate_private_key(public_exponent=65537,
                                        key_size=argv.keysize,
                                        backend=default_backend())
        with open(argv.keys[0], "wb") as f:
            f.write(
                pkey.private_bytes(
                    encoding=serialization.Encoding.PEM,
                    format=serialization.PrivateFormat.TraditionalOpenSSL,
                    encryption_algorithm=serialization.NoEncryption(),
                ))
        subject = x509.Name([
            x509.NameAttribute(NameOID.ORGANIZATION_NAME, "spkcspider"),
            x509.NameAttribute(NameOID.COMMON_NAME, argv.address),
        ])

        cert = (x509.CertificateBuilder().subject_name(subject).issuer_name(
            subject).public_key(pkey.public_key()).serial_number(
                x509.random_serial_number()).not_valid_before(
                    dt.utcnow()).not_valid_after(dt.utcnow() +
                                                 td(days=365 * 20)))
        # cert = cert.add_extension(
        #    x509.SubjectAlternativeName([x509.DNSName("localhost")]),
        #    critical=False
        # )

        cert = cert.sign(pkey, hashes.SHA512(), default_backend())
        del pkey

        with open(argv.cert, "wb") as f:
            f.write(cert.public_bytes(serialization.Encoding.PEM))
    if not all(map(os.path.exists, argv.keys)):
        argv.exit(1, "invalid keys")
    key_list = {}
    pw = None
    for num, key_path in enumerate(argv.keys):
        pkey = None
        with open(key_path, "rb") as f:
            if num == 0:
                pkey, pw = load_priv_key(f.read())
            else:
                pkey = load_priv_key(f.read())[0]

            if not pkey:
                argv.exit(1, "invalid key: %s" % key_path)
            pem_public = pkey.public_key().public_bytes(
                encoding=serialization.Encoding.PEM,
                format=serialization.PublicFormat.SubjectPublicKeyInfo).strip(
                )
            digest = hashes.Hash(argv.hash, backend=default_backend())
            digest.update(pem_public)
            key_list[digest.finalize().hex()] = pkey
        del pkey
    ctx = None
    if os.path.exists(argv.cert):
        pubdata = None
        with open(argv.cert, "rb") as f:
            pubdata = f.read()
        privdata = None
        with open(argv.keys[0], "rb") as f:
            privdata = f.read()

        ctx = ssl.PrivateCertificate.load(pubdata, privdata,
                                          crypto.FILETYPE_PEM)

    loop = asyncio.new_event_loop()
    reactor = asyncioreactor.AsyncioSelectorReactor(loop)
    smtp_factory = SMTPFactory()
    smtp_factory.domain = argv.address
    smtp_factory.delivery = SpiderDelivery(argv.postbox)
    smtp_factory.encryption_required = not argv.unencrypted
    if ctx:
        smtp_factory.cert_options = ssl.optionsForClientTLS(argv.address, ctx)
    reactor.listenTCP(argv.smtp_port, smtp_factory, interface=argv.address)

    pop3_factory = POP3Factory()
    pop3_factory.domain = argv.address
    pop3_factory.mbox = SpiderPostbox(argv.postbox)
    pop3_factory.encryption_required = not argv.unencrypted
    if ctx:
        pop3_factory.cert_options = ssl.optionsForClientTLS(argv.address, ctx)
    reactor.listenTCP(argv.pop3_port, pop3_factory, interface=argv.address)
    logger.info("spkcspider pipeline started")
    loop.run_forever()
Esempio n. 4
0
import asyncio
import sys
import pdb

import twisted.internet
from twisted.internet import asyncioreactor

try:
    import uvloop
    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
except ImportError:
    pass

# Force install the reactor. In some cases some pre-initialization code will
# create the wrong reactor. This ensures that its always the asyncio reactor.
reactor = asyncioreactor.AsyncioSelectorReactor()
twisted.internet.reactor = reactor
sys.modules['twisted.internet.reactor'] = reactor

from twisted.python import usage
from twisted.scripts._twistd_unix import ServerOptions, UnixApplicationRunner

# Load the available MAAS plugins.
twistd_plugins = []
try:
    from provisioningserver.plugin import ProvisioningServiceMaker
except ImportError:
    pass
else:
    # Rackd service that twisted will spawn.
    twistd_plugins.append(
Esempio n. 5
0
        elif k in loaded_config:
            result[k] = loaded_config[k]

    # copy things with no defaults
    for k, v in loaded_config.items():
        if k not in result:
            result[k] = v

    return result


if __name__ == "__main__":
    # TODO we don't want to have to install the reactor, when we can get away with
    #   it
    asyncioreactor.install()

    # we remove the global reactor to make it evident when it has accidentally
    # been used:
    # ! twisted.internet.reactor = None
    # TODO can't do this ^ yet, since twisted.internet.task.{coiterate,cooperate}
    #   (indirectly) depend on the globally-installed reactor and there's no way
    #   to pass in a custom one.
    #   and twisted.web.client uses twisted.internet.task.cooperate

    config = parse_config()
    config = merge_left_with_defaults(CONFIG_DEFAULTS, config)
    check_config(config)
    sygnal = Sygnal(config,
                    custom_reactor=asyncioreactor.AsyncioSelectorReactor())
    sygnal.run()