Skip to content

Commit 652cb18

Browse files
committed
Refactor mail notifications, add support for SSL servers, add default mail config
1 parent ad72456 commit 652cb18

File tree

3 files changed

+28
-33
lines changed

3 files changed

+28
-33
lines changed

tor2web/t2w.py

+2-16
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
from tor2web.utils.daemon import Daemon, set_pdeathsig, set_proctitle
5555
from tor2web.utils.hostsmap import HostsMap
5656
from tor2web.utils.lists import LimitedSizeDict, List, TorExitNodeList
57-
from tor2web.utils.mail import sendmail, sendexceptionmail
57+
from tor2web.utils.mail import sendmail, MailException
5858
from tor2web.utils.misc import listenTCPonExistingFD, listenSSLonExistingFD, re_sub, verify_onion
5959
from tor2web.utils.socks import SOCKSError, SOCKS5ClientEndpoint, TLSWrapClientEndpoint
6060
from tor2web.utils.ssl import T2WSSLContextFactory, HTTPSVerifyingContextFactory
@@ -810,8 +810,6 @@ def process(self):
810810
defer.returnValue(self.contentFinish(content))
811811

812812
elif staticpath == "notification" and config.smtpmailto_notifications != '':
813-
# if config.smtp_mail is configured we accept notifications
814-
815813
# ################################################################
816814
# Here we need to parse POST data in x-www-form-urlencoded format
817815
# ################################################################
@@ -843,13 +841,7 @@ def process(self):
843841
message = StringIO(''.join(tmp))
844842

845843
try:
846-
sendmail(config.smtpuser,
847-
config.smtppass,
848-
config.smtpmail,
849-
config.smtpmailto_notifications,
850-
message,
851-
config.smtpdomain,
852-
config.smtpport)
844+
sendmail(config, message)
853845
except Exception:
854846
pass
855847

@@ -1300,9 +1292,6 @@ def daemon_main(self):
13001292
subprocess = spawnT2W(self, self.childFDs, self.fds_https, self.fds_http)
13011293
self.subprocesses.append(subprocess.pid)
13021294

1303-
def MailException(etype, value, tb):
1304-
sendexceptionmail(self.config, etype, value, tb)
1305-
13061295
if self.config.smtpmailto_exceptions:
13071296
# if self.config.smtp_mail is configured we change the excepthook
13081297
sys.excepthook = MailException
@@ -1364,9 +1353,6 @@ def start_worker():
13641353
fd=fd,
13651354
factory=factory))
13661355

1367-
def MailException(etype, value, tb):
1368-
sendexceptionmail(config, etype, value, tb)
1369-
13701356
if config.smtpmailto_exceptions:
13711357
# if config.smtp_mail is configured we change the excepthook
13721358
sys.excepthook = MailException

tor2web/utils/config.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,14 @@ def __init__(self):
9191
self.__dict__['disable_tor_redirection'] = False
9292
self.__dict__['disable_gettor'] = False
9393
self.__dict__['avoid_rewriting_visible_data'] = False
94-
self.__dict__['smtp_user'] = ''
95-
self.__dict__['smtp_pass'] = ''
96-
self.__dict__['smtp_mail'] = ''
94+
self.__dict__['smtpuser'] = 'hey_you_should_change_me'
95+
self.__dict__['smtppass'] = 'yes_you_really_should_change_me'
96+
self.__dict__['smtpmail'] = 'notification@demo.globaleaks.org'
9797
self.__dict__['smtpmailto_exceptions'] = 'stackexception@lists.tor2web.org'
9898
self.__dict__['smtpmailto_notifications'] = 'tor2web-abuse@lists.tor2web.org'
99-
self.__dict__['smtpdomain'] = ''
100-
self.__dict__['smtpport'] = 587
99+
self.__dict__['smtpdomain'] = 'demo.globaleaks.org'
100+
self.__dict__['smtpport'] = 9267
101+
self.__dict__['smtpsecurity'] = 'TLS'
101102
self.__dict__['exit_node_list_refresh'] = 600
102103
self.__dict__['automatic_blocklist_updates_source'] = ''
103104
self.__dict__['automatic_blocklist_updates_refresh'] = 600
@@ -143,7 +144,6 @@ def load(self):
143144
exit(1)
144145

145146
try:
146-
147147
self._parser.read([self._file])
148148

149149
for name in self._parser.options(self._section):
@@ -199,7 +199,6 @@ def splitlist(self, line):
199199

200200
def parse(self, name):
201201
try:
202-
203202
value = self._parser.get(self._section, name)
204203

205204
# strip any boundry whitespace just in case
@@ -231,7 +230,6 @@ def __setattr__(self, name, value):
231230
return
232231

233232
try:
234-
235233
# XXX: Automagically discover variable type
236234
self._parser.set(self._section, name, value)
237235

tor2web/utils/mail.py

+20-9
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from tor2web import __version__
2626

2727

28-
def sendmail(authenticationUsername, authenticationSecret, fromAddress, toAddress, messageFile, smtpHost, smtpPort=25):
28+
def sendmail(config, messageFile):
2929
"""
3030
Sends an email using SSLv3 over SMTP
3131
@@ -47,18 +47,26 @@ def sendmail(authenticationUsername, authenticationSecret, fromAddress, toAddres
4747
resultDeferred = defer.Deferred()
4848

4949
senderFactory = ESMTPSenderFactory(
50-
authenticationUsername,
51-
authenticationSecret,
52-
fromAddress,
53-
toAddress,
50+
config.smtpuser.encode('utf-8'),
51+
config.smtppass.encode('utf-8'),
52+
config.smtpmail,
53+
config.smtpmailto_exceptions,
5454
messageFile,
5555
resultDeferred,
56-
contextFactory=contextFactory)
56+
contextFactory=contextFactory,
57+
requireAuthentication=True,
58+
requireTransportSecurity=(config.smtpsecurity != 'SSL'),
59+
retries=0,
60+
timeout=15)
5761

58-
reactor.connectTCP(smtpHost, smtpPort, senderFactory)
62+
if config.security == "SSL":
63+
senderFactory = tls.TLSMemoryBIOFactory(contextFactory, True, senderFactory)
64+
65+
reactor.connectTCP(config.smtpdomain, config.smtpport, senderFactory)
5966

6067
return resultDeferred
6168

69+
6270
def sendexceptionmail(config, etype, value, tb):
6371
"""
6472
Formats traceback and exception data and emails the error
@@ -67,7 +75,6 @@ def sendexceptionmail(config, etype, value, tb):
6775
@param value: Exception string value
6876
@param tb: Traceback string data
6977
"""
70-
7178
exc_type = re.sub("(<(type|class ')|'exceptions.|'>|__main__.)", "", str(etype))
7279

7380
tmp = ["From: Tor2web Node %s.%s <%s>\n" % (config.nodename, config.basehost, config.smtpmail),
@@ -86,4 +93,8 @@ def sendexceptionmail(config, etype, value, tb):
8693
info_string = ''.join(tmp)
8794
message = StringIO(info_string)
8895

89-
sendmail(config.smtpuser, config.smtppass, config.smtpmail, config.smtpmailto_exceptions, message, config.smtpdomain, config.smtpport)
96+
sendmail(config, message)
97+
98+
99+
def MailException(etype, value, tb):
100+
sendexceptionmail(config, etype, value, tb)

0 commit comments

Comments
 (0)