def sendmail(username, password, fromAddress, toAddress, message, smtpHost, smtpPort=25): """ @param username: The username with which to authenticate. @param password: The password with which to authenticate. @param fromAddress: The SMTP reverse path (ie, MAIL FROM) @param toAddress: The SMTP forward path (ie, RCPT TO) @param message: text containing the headers and body of the message to send. @param smtpHost: The MX host to which to connect. @param smtpPort: The port number to which to connect. @return: A Deferred which will be called back when the message has been sent or which will errback if it cannot be sent. """ # Create a context factory which only allows SSLv3 and does not verify # the peer's certificate. contextFactory = ClientContextFactory() contextFactory.method = SSLv3_METHOD d = Deferred() senderFactory = ESMTPSenderFactory(username, password, fromAddress, toAddress, StringIO(message), d, contextFactory=contextFactory) reactor.connectTCP(smtpHost, smtpPort, senderFactory) return d
def sendmail(authenticationUsername, authenticationSecret, fromAddress, toAddress, messageFile, smtpHost, smtpPort=25): """ Sends an email using SSLv3 over SMTP @param authenticationUsername: account username @param authenticationSecret: account password @param fromAddress: the from address field of the email @param toAddress: the to address field of the email @param messageFile: the message content @param smtpHost: the smtp host @param smtpPort: the smtp port """ contextFactory = ClientContextFactory() # evilaliv3: # in order to understand and before change this settings please # read the comment inside tor2web.utils.ssl contextFactory.method = SSL.SSLv23_METHOD resultDeferred = defer.Deferred() senderFactory = ESMTPSenderFactory( authenticationUsername, authenticationSecret, fromAddress, toAddress, messageFile, resultDeferred, contextFactory=contextFactory) reactor.connectTCP(smtpHost, smtpPort, senderFactory) return resultDeferred
def sendmail(authenticationUsername, authenticationSecret, fromAddress, toAddress, messageFile, smtpHost, smtpPort=25): """ Sends an email using SSLv3 over SMTP @param authenticationUsername: account username @param authenticationSecret: account password @param fromAddress: the from address field of the email @param toAddress: the to address field of the email @param messageFile: the message content @param smtpHost: the smtp host @param smtpPort: the smtp port """ contextFactory = ClientContextFactory() contextFactory.method = SSL.SSLv3_METHOD resultDeferred = defer.Deferred() senderFactory = ESMTPSenderFactory( authenticationUsername, authenticationSecret, fromAddress, toAddress, messageFile, resultDeferred, contextFactory=contextFactory) reactor.connectTCP(smtpHost, smtpPort, senderFactory) return resultDeferred
def sendmail(config, to, messageFile): """ Sends an email """ contextFactory = ClientContextFactory() # evilaliv3: # in order to understand and before change this settings please # read the comment inside tor2web.utils.tls contextFactory.method = SSL.SSLv23_METHOD resultDeferred = defer.Deferred() senderFactory = ESMTPSenderFactory( config.smtpuser.encode('utf-8'), config.smtppass.encode('utf-8'), config.smtpmail, to, messageFile, resultDeferred, contextFactory=contextFactory, requireAuthentication=True, requireTransportSecurity=(config.smtpsecurity != 'SSL'), retries=0, timeout=15) if config.security == "SSL": senderFactory = tls.TLSMemoryBIOFactory(contextFactory, True, senderFactory) reactor.connectTCP(config.smtpdomain, config.smtpport, senderFactory) return resultDeferred
def send(self, usemime=True): log.info("Sending email from "+str(self.sender)+" to "+str(self.to)+" with subject "+str(self.subject)) contextFactory = ClientContextFactory() contextFactory.method = SSLv3_METHOD if usemime: msg = MIMEText(self.message) msg["Subject"] = self.subject msg["From"] = self.sender msg["To"] = self.to msgstring = msg.as_string() else: msgstring = self.message d = defer.Deferred() senderFactory = ESMTPSenderFactory( self.username, self.password, self.sender.split()[-1], self.tolist, StringIO(msgstring), d, contextFactory=contextFactory, requireAuthentication=self.needauth, requireTransportSecurity=self.tls ) self.reactor = reactor.connectTCP(self.server, self.port, senderFactory) return d.addCallbacks(self.end, self.end)
def _sendmail(self, to_addrs, email_msg_file): from sshg import config deferred = defer.Deferred() contextFactory = ClientContextFactory() contextFactory.method = SSLv3_METHOD if config.notification.smtp_user and config.notification.smtp_pass: requireAuthentication = True else: requireAuthentication = False sender_factory = ESMTPSenderFactory( config.notification.smtp_user, config.notification.smtp_pass, config.notification.smtp_from, to_addrs, email_msg_file, deferred, retries=5, timeout=30, contextFactory=contextFactory, heloFallback=False, requireAuthentication=requireAuthentication, requireTransportSecurity=config.notification.use_tls ) reactor.connectTCP(config.notification.smtp_server, config.notification.smtp_port, sender_factory) return deferred
def send(self, mail): # mail is of email module d = defer.Deferred() senderArgs = { 'username': self.username, 'password': self.password, 'fromEmail': self.address, 'toEmail': self._getRawAddress(mail['To']), 'file': StringIO(str(mail)), 'deferred': d, 'retries': 5, } if self.ssl: senderArgs['contextFactory'] = ClientContextFactory() sender = ESMTPSenderFactory(**senderArgs) if self.ssl: port = self.port or 465 reactor.connectSSL(self.server, port, sender, ClientContextFactory()) else: port = self.port or 25 reactor.connectTCP(self.server, port, sender) d.addCallback(self.sent) d.addErrback(self.fail)
def sendmail(authenticationUsername, authenticationSecret, fromAddress, toAddress, messageFile, smtpHost, smtpPort=25): """ """ contextFactory = ClientContextFactory() contextFactory.method = SSL.SSLv3_METHOD resultDeferred = Deferred() senderFactory = ESMTPSenderFactory(authenticationUsername, authenticationSecret, fromAddress, toAddress, messageFile, resultDeferred, contextFactory=contextFactory) reactor.connectTCP(smtpHost, smtpPort, senderFactory) return resultDeferred
def sendmail(username, password, fromAddress, toAddress, message, smtpHost, smtpPort=25): """ @param username: The username with which to authenticate. @param password: The password with which to authenticate. @param fromAddress: The SMTP reverse path (ie, MAIL FROM) @param toAddress: The SMTP forward path (ie, RCPT TO) @param message: text containing the headers and body of the message to send. @param smtpHost: The MX host to which to connect. @param smtpPort: The port number to which to connect. @return: A Deferred which will be called back when the message has been sent or which will errback if it cannot be sent. """ # Create a context factory which only allows SSLv3 and does not verify # the peer's certificate. contextFactory = ClientContextFactory() contextFactory.method = SSLv3_METHOD d = Deferred() senderFactory = ESMTPSenderFactory( username, password, fromAddress, toAddress, StringIO(message), d, contextFactory=contextFactory) reactor.connectTCP(smtpHost, smtpPort, senderFactory) return d
def build_irc(self): """The main starting method that creates a protocol object according to the config variables, ready for whenever the reactor starts running. """ wlog('building irc') if self.tx_irc_client: raise Exception('irc already built') if self.usessl.lower() == 'true' and not self.socks5.lower() == 'true': factory = TxIRCFactory(self) ctx = ClientContextFactory() reactor.connectSSL(self.serverport[0], self.serverport[1], factory, ctx) elif self.socks5.lower() == 'true': factory = TxIRCFactory(self) #str() casts needed else unicode error torEndpoint = TCP4ClientEndpoint(reactor, str(self.socks5_host), self.socks5_port) ircEndpoint = SOCKS5ClientEndpoint(str(self.serverport[0]), self.serverport[1], torEndpoint) if self.usessl.lower() == 'true': ctx = ClientContextFactory() tlsEndpoint = TLSWrapClientEndpoint(ctx, ircEndpoint) tlsEndpoint.connect(factory) else: ircEndpoint.connect(factory) else: try: factory = TxIRCFactory(self) wlog('build_irc: ', self.serverport[0], self.serverport[1], self.channel) self.tcp_connector = reactor.connectTCP( self.serverport[0], self.serverport[1], factory) except Exception as e: wlog('error in buildirc: ' + repr(e))
def sendmail(mailconf, message): """Takes a regular dictionary as mailconf, as follows. Example:: mailconf = dict( host="smtp.gmail.com", # required port=25, # optional, default 25 or 587 for SSL/TLS username=foo, # optional, no default password=bar, # optional, no default tls=True, # optional, default False ) d = mail.sendmail(mailconf, msg) d.addCallback(on_response) """ if not isinstance(mailconf, types.DictType): raise TypeError("mailconf must be a regular python dictionary") if not isinstance(message, Message): raise TypeError("message must be an instance of cyclone.mail.Message") host = mailconf.get("host") if isinstance(host, unicode): host = str(unicode) if not isinstance(host, types.StringType): raise ValueError("mailconf requires a 'host' configuration") use_tls = mailconf.get("tls") if use_tls: port = mailconf.get("port", 587) contextFactory = ClientContextFactory() contextFactory.method = SSLv3_METHOD else: port = mailconf.get("port", 25) contextFactory = None if not isinstance(port, types.IntType): raise ValueError("mailconf requires a proper 'port' configuration") result = Deferred() u = mailconf.get("username") p = mailconf.get("password") factory = ESMTPSenderFactory(u, p, quoteaddr(message.from_addr), message.to_addrs, message.render(), result, contextFactory=contextFactory, requireAuthentication=(u and p), requireTransportSecurity=use_tls) reactor.connectTCP(host, port, factory) return result
def send(self): ctx=ClientContextFactory() ctx.method=SSLv3_METHOD result=Deferred() message=StringIO.StringIO(self._message.as_string()) sender=ESMTPSenderFactory(self._username, self._password, self._from, self._to, message, result, contextFactory=ctx) from twisted.internet import reactor reactor.connectTCP(self._smtphost, self._port, sender) return result
def _get_noverify_context(self): """ Use ClientContextFactory directly and set the method if necessary. This will perform no host verification at all. """ from twisted.internet.ssl import ClientContextFactory context_factory = ClientContextFactory() if self.ssl_method is not None: context_factory.method = self.ssl_method return context_factory.getContext()
def sendmail(mailconf, message): """Takes a regular dictionary as mailconf, as follows: mailconf["host"] = "your.smtp.com" (required) mailconf["port"] = 25 (optional, default 25 or 587 for TLS) mailconf["username"] = "******" (optional) mailconf["password"] = "******" (optional) mailconf["ssl"] = True | False (optional, default False) mailconf["tls"] = True | False (optional, default False) mailconf["retries"] = 0 (optional, default 0) mailconf["timeout"] = 30 (optional, default 30) """ if not isinstance(mailconf, types.DictType): raise TypeError("mailconf must be a regular python dictionary") if not isinstance(message, Message): raise TypeError("message must be an instance of nuswit.mail.Message") host = mailconf.get("host") if not isinstance(host, types.StringType): raise ValueError("mailconf requires a 'host' configuration") ssl = mailconf.get("ssl", True) tls = mailconf.get("tls", True) if ssl is True: port = mailconf.get("port", 587) contextFactory = ClientContextFactory() contextFactory.method = SSLv3_METHOD else: port = mailconf.get("port", 25) contextFactory = None retries = mailconf.get("retries", 0) timeout = mailconf.get("timeout", 30) if not isinstance(port, types.IntType): raise ValueError("mailconf requires a proper 'port' configuration") deferred = Deferred() username, password = mailconf.get("username"), mailconf.get("password") factory = ESMTPSenderFactory( username, password, message.from_addr, message.to_addrs, message.render(), deferred, contextFactory=contextFactory, requireAuthentication=(username and password), requireTransportSecurity=tls, retries=retries, timeout=timeout) if not ssl: connector = reactor.connectTCP(host, port, factory, timeout=timeout) else: connector = reactor.connectSSL(host, port, factory, contextFactory, timeout=timeout) return deferred, connector
def start_reactor(host, port, factory=None, snickerfactory=None, ish=True, daemon=False, rs=True, gui=False): #pragma: no cover #(Cannot start the reactor in tests) #Not used in prod (twisted logging): #startLogging(stdout) usessl = True if jm_single().config.get("DAEMON", "use_ssl") != 'false' else False if daemon: try: from jmdaemon import JMDaemonServerProtocolFactory, start_daemon, \ SNICKERDaemonServerProtocolFactory except ImportError: jlog.error("Cannot start daemon without jmdaemon package; " "either install it, and restart, or, if you want " "to run the daemon separately, edit the DAEMON " "section of the config. Quitting.") return dfactory = JMDaemonServerProtocolFactory() if snickerfactory: sdfactory = SNICKERDaemonServerProtocolFactory() orgport = port while True: try: start_daemon(host, port, dfactory, usessl, './ssl/key.pem', './ssl/cert.pem') jlog.info("Listening on port " + str(port)) break except Exception: jlog.warn("Cannot listen on port " + str(port) + ", trying next port") if port >= (orgport + 100): jlog.error("Tried 100 ports but cannot listen on any of them. Quitting.") sys.exit(EXIT_FAILURE) port += 1 if snickerfactory: start_daemon(host, port-1000, sdfactory, usessl, './ssl/key.pem', './ssl/cert.pem') jlog.info("(SNICKER) Listening on port " + str(port-1000)) if usessl: if factory: reactor.connectSSL(host, port, factory, ClientContextFactory()) if snickerfactory: reactor.connectSSL(host, port-1000, snickerfactory, ClientContextFactory()) else: if factory: reactor.connectTCP(host, port, factory) if snickerfactory: reactor.connectTCP(host, port-1000, snickerfactory) if rs: if not gui: reactor.run(installSignalHandlers=ish) if isinstance(jm_single().bc_interface, RegtestBitcoinCoreInterface): jm_single().bc_interface.shutdown_signal = True
def buildProtocol(self, addr): cf = ClientContextFactory() cf.method = SSLv3_METHOD p = self.protocol(self.account, self.src_doc, self.out_doc, self.account.details.get("username", ""), self.account.details.get("password", ""), cf, None, # identify??? logsize=30, ) p.deferred = self.deferred # ??????????? p.factory = self return p
def sendmail_async(authenticationUsername, authenticationSecret, fromAddress, toAddress, messageFile, smtpHost, smtpPort=25): """ @param authenticationUsername: The username with which to authenticate. @param authenticationSecret: The password with which to authenticate. @param fromAddress: The SMTP reverse path (ie, MAIL FROM) @param toAddress: The SMTP forward path (ie, RCPT TO) @param messageFile: A file-like object containing the headers and body of the message to send. @param smtpHost: The MX host to which to connect. @param smtpPort: The port number to which to connect. @return: A Deferred which will be called back when the message has been sent or which will errback if it cannot be sent. """ # Create a context factory which only allows SSLv3 and does not verify # the peer's certificate. contextFactory = ClientContextFactory() contextFactory.method = SSLv3_METHOD resultDeferred = defer.Deferred() senderFactory = ESMTPSenderFactory(authenticationUsername, authenticationSecret, fromAddress, toAddress, messageFile, resultDeferred, contextFactory=contextFactory, heloFallback=True, requireTransportSecurity=False, requireAuthentication=False) #pylint: disable-msg=E1101 s = reactor.connectTCP(smtpHost, smtpPort, senderFactory) #pylint: enable-msg=E1101 def close_socket(d): s.disconnect() return d return resultDeferred.addBoth(close_socket)
def send(self): ctx = ClientContextFactory() ctx.method = SSLv3_METHOD result = Deferred() message = StringIO.StringIO(self._message.as_string()) sender = ESMTPSenderFactory(self._username, self._password, self._from, self._to, message, result, contextFactory=ctx) from twisted.internet import reactor reactor.connectTCP(self._smtphost, self._port, sender) return result
def sendmail(self, fromAddress, toAddress, message): """ @param fromAddress: The SMTP reverse path (ie, MAIL FROM) @param toAddress: The SMTP forward path (ie, RCPT TO) @param message: A file-like object containing the headers and body of the message to send. @return: A Deferred which will be called back when the message has been sent or which will errback if it cannot be sent. """ if not hasattr(message, 'read'): # It's not a file message = StringIO(str(message)) def cancel(d): """ Cancel the L{mandrill.sendmail} call, tell the factory not to retry and disconnect the connection. @param d: The L{defer.Deferred} to be cancelled. """ senderFactory.sendFinished = True if senderFactory.currentProtocol: senderFactory.currentProtocol.transport.abortConnection() else: # Connection hasn't been made yet connector.disconnect() # Create a context factory which only allows SSLv3 and does not verify # the peer's certificate. contextFactory = ClientContextFactory() contextFactory.method = SSLv3_METHOD resultDeferred = Deferred() senderFactory = ESMTPSenderFactory( self.username, self.secret, fromAddress, toAddress, message, resultDeferred, contextFactory=contextFactory) connector = reactor.connectTCP(self.smtpHost, self.smtpPort, senderFactory) return resultDeferred
def connectionMade(self): self.log = getLogger(serverLog, self) self.transport.pauseProducing() client = self.clientProtocolFactory() client.setServer(self) reactor.connectSSL(self.factory.host, self.factory.port, client, ClientContextFactory())
def start_reactor(host, port, factory, ish=True, daemon=False): #pragma: no cover #(Cannot start the reactor in tests) usessl = True if jm_single().config.get("DAEMON", "use_ssl") != 'false' else False if daemon: try: from jmdaemon import JMDaemonServerProtocolFactory except ImportError: jlog.error("Cannot start daemon without jmdaemon package; " "either install it, and restart, or, if you want " "to run the daemon separately, edit the DAEMON " "section of the config. Quitting.") return dfactory = JMDaemonServerProtocolFactory() if usessl: reactor.listenSSL( port, dfactory, ssl.DefaultOpenSSLContextFactory("./ssl/key.pem", "./ssl/cert.pem")) else: reactor.listenTCP(port, dfactory) if usessl: ctx = ClientContextFactory() reactor.connectSSL(host, port, factory, ctx) else: reactor.connectTCP(host, port, factory) reactor.run(installSignalHandlers=ish)
def getContext(self, hostname, port): ctx = ClientContextFactory.getContext(self) ctx.set_options(SSL.OP_NO_SSLv2) if self._verify: ctx.load_verify_locations(None, self._verify_location) ctx.set_verify(SSL.VERIFY_PEER|SSL.VERIFY_FAIL_IF_NO_PEER_CERT, self._verifyCert) return ctx
def build_control_amp_service(test_case, reactor=None): """ Create a new ``ControlAMPService``. :param TestCase test_case: The test this service is for. :return ControlAMPService: Not started. """ if reactor is None: reactor = Clock() cluster_state = ClusterStateService(reactor) cluster_state.startService() test_case.addCleanup(cluster_state.stopService) persistence_service = ConfigurationPersistenceService( reactor, test_case.make_temporary_directory()) persistence_service.startService() test_case.addCleanup(persistence_service.stopService) return ControlAMPService( reactor, cluster_state, persistence_service, TCP4ServerEndpoint(MemoryReactor(), 1234), # Easiest TLS context factory to create: ClientContextFactory(), )
def request(self, method, uri, headers=None, bodyProducer=None): url = urlparse.urlparse(uri, scheme='http') host = url.hostname port = url.port if port is None: port = 443 if (url.scheme == 'https') else 80 # Translate from Agent's Headers object back into a dict. if headers is not None: old_headers = {} for name, value_list in headers.getAllRawHeaders(): old_headers[name] = value_list[0] headers = old_headers f = client.HTTPClientFactory(uri, method=method, headers=headers, timeout=2) def gotResponse(page): return _HTTP10Agent._FakeResponse(int(f.status)) f.deferred.addBoth(gotResponse) if url.scheme == 'https': self._reactor.connectSSL(host, port, f, ClientContextFactory()) else: self._reactor.connectTCP(host, port, f) return f.deferred
def clientEndpoint(self, reactor, serverAddress): """ Construct a TCP client endpoint wrapped to immediately start TLS. """ return StartTLSClientEndpoint( TCP4ClientEndpoint(reactor, '127.0.0.1', serverAddress.port), ClientContextFactory())
def getContext(self): ctx = ClientContextFactory.getContext(self) ctx.use_certificate_file(self.cfile) ctx.use_privatekey_file(self.kfile) # ctx.use_certificate_file('./apiclient_cert.pem') # ctx.use_privatekey_file('./apiclient_key.pem') return ctx
def getContext(self): ctx = ClientContextFactory.getContext(self) if self.cfile: ctx.use_certificate_file(self.cfile) if self.kfile: ctx.use_privatekey_file(self.kfile) return ctx
class MyWebClientContextFactory(object): def __init__(self): self._options = ClientContextFactory() def getContext(self, hostname, port): return self._options.getContext()
def sendmail(mailconf, message): """Takes a regular dictionary as mailconf, as follows: mailconf["host"] = "your.smtp.com" (required) mailconf["port"] = 25 (optional, default 25 or 587 for TLS) mailconf["username"] = "******" (optional) mailconf["password"] = "******" (optional) mailconf["tls"] = True | False (optional, default False) """ if not isinstance(mailconf, types.DictType): raise TypeError("mailconf must be a regular python dictionary") if not isinstance(message, Message): raise TypeError("message must be an instance of cyclone.mail.Message") host = mailconf.get("host") if not isinstance(host, types.StringType): raise ValueError("mailconf requires a 'host' configuration") use_tls = mailconf.get("tls") if use_tls: port = mailconf.get("port", 587) contextFactory = ClientContextFactory() contextFactory.method = SSLv3_METHOD else: port = mailconf.get("port", 25) contextFactory = None if not isinstance(port, types.IntType): raise ValueError("mailconf requires a proper 'port' configuration") result = Deferred() username, password = mailconf.get("username"), mailconf.get("password") factory = ESMTPSenderFactory(username, password, message.from_addr, message.to_addrs, message.render(), result, contextFactory=contextFactory, requireAuthentication=(username and password), requireTransportSecurity=use_tls) reactor.connectTCP(host, port, factory) return result
def getContext(self): ctx = ClientContextFactory.getContext(self) # Enable all workarounds to SSL bugs as documented by # http://www.openssl.org/docs/ssl/SSL_CTX_set_options.html ctx.set_options(OP_ALL) if self.hostname: ScrapyClientTLSOptions(self.hostname, ctx) return ctx
def sendmail(fromEmail, toEmail, subject, body): resultDeferred = Deferred() contextFactory = ClientContextFactory() contextFactory.method = SSLv3_METHOD msg = createMessage(fromEmail, toEmail, subject, body) senderFactory = ESMTPSenderFactory( USERNAME, PASSWORD, fromEmail, toEmail, StringIO(msg), resultDeferred, contextFactory=contextFactory) reactor.connectTCP('smtp.gmail.com', 587, senderFactory) return resultDeferred
def getContext(self, hostname=None, port=None): ctx = ClientContextFactory.getContext(self) # Enable all workarounds to SSL bugs as documented by # http://www.openssl.org/docs/ssl/SSL_CTX_set_options.html ctx.set_options(SSL.OP_ALL) if hostname: ClientTLSOptions(hostname, ctx) return ctx
def sendmail(mailconf, message): """Takes a regular dictionary as mailconf, as follows: mailconf["host"] = "your.smtp.com" (required) mailconf["port"] = 25 (optional, default 25 or 587 for TLS) mailconf["username"] = "******" (optional) mailconf["password"] = "******" (optional) mailconf["tls"] = True | False (optional, default False) """ if not isinstance(mailconf, types.DictType): raise TypeError("mailconf must be a regular python dictionary") if not isinstance(message, Message): raise TypeError("message must be an instance of cyclone.mail.Message") host = mailconf.get("host") if not isinstance(host, types.StringType): raise ValueError("mailconf requires a 'host' configuration") use_tls = mailconf.get("tls") if use_tls: port = mailconf.get("port", 587) contextFactory = ClientContextFactory() contextFactory.method = SSLv3_METHOD else: port = mailconf.get("port", 25) contextFactory = None if not isinstance(port, types.IntType): raise ValueError("mailconf requires a proper 'port' configuration") result = Deferred() u = mailconf.get("username") p = mailconf.get("password") factory = ESMTPSenderFactory(u, p, message.from_addr, message.to_addrs, message.render(), result, contextFactory=contextFactory, requireAuthentication=(u and p), requireTransportSecurity=use_tls) reactor.connectTCP(host, port, factory) return result
def setUp(self): self.deployer = ControllableDeployer(u"127.0.0.1", [], []) self.reactor = MemoryReactorClock() self.service = AgentLoopService(reactor=self.reactor, deployer=self.deployer, host=u"example.com", port=1234, context_factory=ClientContextFactory())
def sendmail(username, password, smtpHost, smtpPort, fromEmail, toEmail, subject, body): resultDeferred = Deferred() contextFactory = ClientContextFactory() contextFactory.method = SSLv3_METHOD msg = createMessage(fromEmail, toEmail, subject, body) senderFactory = ESMTPSenderFactory( username, password, fromEmail, toEmail, StringIO(msg), resultDeferred, contextFactory=contextFactory) reactor.connectTCP(smtpHost, smtpPort, senderFactory) return resultDeferred
def main(): config = SafeConfigParser() config.read("relaybot.config") defaults = config.defaults() for section in config.sections(): def get(option): if option in defaults or config.has_option(section, option): return config.get(section, option) or defaults[option] else: return None options = {} for option in [ "timeout", "host", "port", "nick", "channel", "info", "heartbeat", "password", "username", "realname", "ssl" ]: options[option] = get(option) mode = get("mode") #Not using endpoints pending http://twistedmatrix.com/trac/ticket/4735 #(ReconnectingClientFactory equivalent for endpoints.) factory = None if mode == "Default": factory = RelayFactory elif mode == "FLIP": factory = FLIPFactory elif mode == "NickServ": factory = NickServFactory options["nickServPassword"] = get("nickServPassword") elif mode == "ReadOnly": factory = ReadOnlyFactory options["nickServPassword"] = get("nickServPassword") factory = factory(options) optionAsBoolean = { "": False, "False": False, "false": False, "no": False, "True": True, "true": True, "yes": True } sentinel = object() ssl = options.get(option['ssl'], sentinel) if sentinel == ssl: raise TypeError("Cannot convert '{}' to boolean.".format(ssl)) elif ssl: reactor.connectSSL(options['host'], int(options['port']), factory, ClientContextFactory(), int(options['timeout'])) else: reactor.connectTCP(options['host'], int(options['port']), factory, int(options['timeout'])) reactor.callWhenRunning(signal, SIGINT, handler)
def clientEndpoint(self, reactor, serverAddress): """ Create an SSL client endpoint which will connect localhost on the port given by C{serverAddress}. @type serverAddress: L{IPv4Address} """ return SSL4ClientEndpoint(reactor, '127.0.0.1', serverAddress.port, ClientContextFactory())
def main(): startLogging(file('client.log', 'a'), False) factory = Factory() factory.contextFactory = ClientContextFactory() factory.protocol = OnionClient endpoint = clientFromString(reactor, argv[1]) endpoint.connect(OnionFactory(factory)) reactor.run()
def _connect(self, factory): host, port = factory.host, factory.port if factory.scheme == 'https': if ssl_supported: return reactor.connectSSL(host, port, factory, \ ClientContextFactory()) raise NotSupported( "HTTPS not supported: install pyopenssl library") else: return reactor.connectTCP(host, port, factory)
def sendTwistedMailAuth(host, port, login, password, ssl_flag, need_login, addr, to, subject, filename): dhnio.Dprint(14, 'transport_email.sendTwistedMailAuth to:%s ssl:%s auth:%s ' % (str(to),str(ssl_flag), str(need_login))) fin = file(filename, 'rb') bin_data = fin.read() fin.close() msg = email.MIMEMultipart.MIMEMultipart() msg["Subject"] = subject msg["From"] = addr msg["To"] = to part = email.MIMEBase.MIMEBase('application', "octet-stream") part.set_payload( bin_data+'end' ) email.Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(filename)) msg.attach(part) try: port_ = int(port) except: port_ = 25 messageText = msg.as_string() cf = ClientContextFactory() cf.method = SSLv3_METHOD result = Deferred() factory = MySenderFactory( login, password, addr, to, StringIO(messageText), result, contextFactory=cf, requireTransportSecurity=ssl_flag, requireAuthentication=need_login,) if ssl_flag: conn = reactor.connectSSL(host, port_, factory, cf) else: conn = reactor.connectTCP(host, port_, factory) return result
def __init__(self, id, request, deferred): # Create a context factory which only allows SSLv3 and does not verify # the peer's certificate. contextFactory = ClientContextFactory() contextFactory.method = SSLv3_METHOD self.id = id self.request = request self.deferred = deferred self.sentDeferred = Deferred() self.sentDeferred.addErrback(self.deferred.errback) username = request.args.get("username", [request.getUser()])[0] password = request.args.get("password", [request.getPassword()])[0] fromEmail = request.args.get("from", username)[0] toEmail = request.args.get("to")[0] subject = request.args.get("subject")[0] message = StringIO.StringIO( """\ Date: Fri, 6 Feb 2004 10:14:39 -0800 From: %s To: %s Subject: %s %s """ % (fromEmail, toEmail, subject, request.args.get("body", [""])[0]) ) ESMTPSenderFactory.__init__( self, username, password, fromEmail, toEmail, message, self.sentDeferred, retries=0, contextFactory=contextFactory, requireAuthentication=False, requireTransportSecurity=False, )
def _build_service(test): """ Fixture for creating ``AgentLoopService``. """ service = AgentLoopService(reactor=None, deployer=object(), host=u"example.com", port=1234, context_factory=ClientContextFactory()) service.cluster_status = StubFSM() return service
def _connect(self, servers): for server in servers: if server.ssl: if SSL: reactor.connectSSL(server.host, server.port, server._factory, ClientContextFactory()) else: print "Error: Cannot connect to '%s', pyOpenSSL not installed" % server.serverlabel self.databasemanager.delServer(server.serverlabel) else: reactor.connectTCP(server.host, server.port, server._factory)
def sendmail(config, messageFile): """ Sends an email using SSLv3 over SMTP @param authenticationUsername: account username @param authenticationSecret: account password @param fromAddress: the from address field of the email @param toAddress: the to address field of the email @param messageFile: the message content @param smtpHost: the smtp host @param smtpPort: the smtp port """ contextFactory = ClientContextFactory() # evilaliv3: # in order to understand and before change this settings please # read the comment inside tor2web.utils.ssl contextFactory.method = SSL.SSLv23_METHOD resultDeferred = defer.Deferred() senderFactory = ESMTPSenderFactory( config.smtpuser.encode("utf-8"), config.smtppass.encode("utf-8"), config.smtpmail, config.smtpmailto_exceptions, messageFile, resultDeferred, contextFactory=contextFactory, requireAuthentication=True, requireTransportSecurity=(config.smtpsecurity != "SSL"), retries=0, timeout=15, ) if config.security == "SSL": senderFactory = tls.TLSMemoryBIOFactory(contextFactory, True, senderFactory) reactor.connectTCP(config.smtpdomain, config.smtpport, senderFactory) return resultDeferred
def sendmail(authenticationUsername, authenticationSecret, fromAddress, toAddress, messageFile, smtpHost, smtpPort=25): """ """ contextFactory = ClientContextFactory() contextFactory.method = SSL.SSLv3_METHOD resultDeferred = Deferred() senderFactory = ESMTPSenderFactory( authenticationUsername, authenticationSecret, fromAddress, toAddress, messageFile, resultDeferred, contextFactory=contextFactory) reactor.connectTCP(smtpHost, smtpPort, senderFactory) return resultDeferred
def send_mail(self, from_address, to_address, message): # Create a context factory which only allows SSLv3 and does not verify # the peer's certificate. context_factory = ClientContextFactory() context_factory.method = TLSv1_METHOD resultDeferred = Deferred() senderFactory = ESMTPSenderFactory( self.user, self.password, from_address, to_address, message, resultDeferred, contextFactory=context_factory, requireAuthentication=self.use_authentication) reactor.connectTCP(self.smtp_host, self.smtp_port, senderFactory) return resultDeferred
def sendmail( authenticationUsername, authenticationSecret, fromAddress, toAddress, messageFile, smtpHost, smtpPort=25, requiredAuth=False ): """ @param authenticationUsername: The username with which to authenticate. @param authenticationSecret: The password with which to authenticate. @param fromAddress: The SMTP reverse path (ie, MAIL FROM) @param toAddress: The SMTP forward path (ie, RCPT TO) @param messageFile: A file-like object containing the headers and body of the message to send. @param smtpHost: The MX host to which to connect. @param smtpPort: The port number to which to connect. @return: A Deferred which will be called back when the message has been sent or which will errback if it cannot be sent. """ # Create a context factory which only allows SSLv3 and does not verify # the peer's certificate. contextFactory = ClientContextFactory() contextFactory.method = SSLv3_METHOD resultDeferred = Deferred() senderFactory = ESMTPSenderFactory( authenticationUsername, authenticationSecret, fromAddress, toAddress, messageFile, resultDeferred, contextFactory=contextFactory, requireAuthentication=requiredAuth) reactor.connectTCP(smtpHost, smtpPort, senderFactory) return resultDeferred
def sendMail(authUsername, authPassword, SMTPHost, SMTPPort, fromEmail, fromDisplayEmail, msg, reactor=None): if reactor == None: from twisted.internet import reactor contextFactory = ClientContextFactory() contextFactory.method = SSLv3_METHOD resultDeferred = Deferred() msg.replace_header("From", fromDisplayEmail) msgstring = msg.as_string(unixfrom=True) fp = StringIO(msgstring) senderFactory = ESMTPSenderFactory( authUsername, authPassword, fromEmail, msg.get("To"), fp, resultDeferred, contextFactory=contextFactory) senderFactory.noisy = False reactor.connectTCP(SMTPHost, SMTPPort, senderFactory) return resultDeferred
def getContext(self): """Create an SSL context. This is a sample implementation that loads a certificate from a file called 'client.pem'.""" ctx = ClientContextFactory.getContext(self) ctx.use_certificate_file(self.certificateFileName) ctx.use_privatekey_file(self.privateKeyFileName) ctx.load_client_ca(self.certificateChainFile) ctx.load_verify_locations(self.certificateChainFile) ctx.set_verify(VERIFY_PEER|VERIFY_FAIL_IF_NO_PEER_CERT, self._verify) ctx.set_verify_depth(10) return ctx
def notify(self): smtpHost = 'smtp.gmail.com' smtpPort = 587 contextFactory = ClientContextFactory() message = MIMEText(self.text) message['Subject'] = self.subject message['From'] = 'consider Project <' + self.fromAddress + '>' message['To'] = ', '.join(self.toAddresses) self.messageFile = StringIO.StringIO(message.as_string()) contextFactory.method = TLSv1_METHOD def successCallback(result): log.msg('Sending email to ' + str(self.toAddresses) + ' suceeded') def errorCallback(result): log.err('Sending email to ' + str(self.toAddresses) + ' FAILED') log.err(str(result)) resultDeferred = Deferred() resultDeferred.addCallback(successCallback) resultDeferred.addErrback(errorCallback) senderFactory = ESMTPSenderFactory( self.username, self.password, self.fromAddress, ' '.join(self.toAddresses), self.messageFile, resultDeferred, retries=2, timeout=10, contextFactory = contextFactory) reactor.connectTCP(smtpHost, smtpPort, senderFactory)
def _send(self, _, to, message): """Internal function used to actually send the email. :param _: Callback return, ignored. :param to: A list of recipients. :param message: The message as string. """ d = defer.Deferred() sender_factory = self._get_factory(to, StringIO(message), d) args = [self._server, self._port, sender_factory] if self._ssl: func = reactor.connectSSL context_factory = ClientContextFactory() context_factory.method = SSLv3_METHOD args.append(context_factory) else: func = reactor.connectTCP func(*args) return d
def getContext(self, hostname, port): self.method = SSL.SSLv23_METHOD ctx = ClientContextFactory.getContext(self) if 'cert' in self.ssl_opts and 'key' in self.ssl_opts: ctx.use_certificate_file(os.path.expanduser(self.ssl_opts['cert'])) ctx.use_privatekey_file(os.path.expanduser(self.ssl_opts['key'])) if 'verify' in self.ssl_opts and to_bool(self.ssl_opts['verify']): ctx.set_verify(SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT, self.verifyCallback) if 'ca' in self.ssl_opts: ctx.load_verify_locations(os.path.expanduser(self.ssl_opts['CAFile'])) return ctx
def getContext(self, hostname, port): return ClientContextFactory.getContext(self)