Socks proxy support

This commit is contained in:
TreeNode 2014-12-10 23:37:34 +02:00
parent 150fd1b5bc
commit 50b80b2a08
3 changed files with 40 additions and 0 deletions

View File

@ -15,6 +15,8 @@ import imaplib
import mimetypes import mimetypes
import poplib import poplib
import re import re
import socks
import socket
from datetime import timedelta from datetime import timedelta
from email.header import decode_header from email.header import decode_header
@ -84,6 +86,17 @@ def process_queue(q, quiet=False):
if not quiet: if not quiet:
print "Processing: %s" % q print "Processing: %s" % q
if q.socks_proxy_type and q.socks_proxy_host and q.socks_proxy_port:
proxy_type = {
'socks4': socks.SOCKS4,
'socks5': socks.SOCKS5,
}.get(q.socks_proxy_type)
socks.set_default_proxy(proxy_type=proxy_type, addr=q.socks_proxy_host, port=q.socks_proxy_port)
socket.socket = socks.socksocket
else:
socket.socket = socket._socketobject
email_box_type = settings.QUEUE_EMAIL_BOX_TYPE if settings.QUEUE_EMAIL_BOX_TYPE else q.email_box_type email_box_type = settings.QUEUE_EMAIL_BOX_TYPE if settings.QUEUE_EMAIL_BOX_TYPE else q.email_box_type
if email_box_type == 'pop3': if email_box_type == 'pop3':

View File

@ -185,6 +185,10 @@ class Migration(migrations.Migration):
('email_box_imap_folder', models.CharField(help_text='If using IMAP, what folder do you wish to fetch messages from? This allows you to use one IMAP account for multiple queues, by filtering messages on your IMAP server into separate folders. Default: INBOX.', max_length=100, null=True, verbose_name='IMAP Folder', blank=True)), ('email_box_imap_folder', models.CharField(help_text='If using IMAP, what folder do you wish to fetch messages from? This allows you to use one IMAP account for multiple queues, by filtering messages on your IMAP server into separate folders. Default: INBOX.', max_length=100, null=True, verbose_name='IMAP Folder', blank=True)),
('email_box_interval', models.IntegerField(default=b'5', help_text='How often do you wish to check this mailbox? (in Minutes)', null=True, verbose_name='E-Mail Check Interval', blank=True)), ('email_box_interval', models.IntegerField(default=b'5', help_text='How often do you wish to check this mailbox? (in Minutes)', null=True, verbose_name='E-Mail Check Interval', blank=True)),
('email_box_last_check', models.DateTimeField(null=True, editable=False, blank=True)), ('email_box_last_check', models.DateTimeField(null=True, editable=False, blank=True)),
('socks_proxy_type', models.CharField(choices=[(b'socks4', 'SOCKS4'), (b'socks5', 'SOCKS5')], max_length=8, blank=True, help_text='SOCKS4 or SOCKS5 allows you to proxy your connections through a SOCKS server', null=True, verbose_name='Socks Proxy Type')),
('socks_proxy_host', models.GenericIPAddressField(blank=True, help_text='Socks proxy IP address.', null=True, verbose_name='Socks Proxy Host')),
('socks_proxy_port', models.IntegerField(blank=True, help_text='Socks proxy port number.', null=True, verbose_name='Socks Proxy Port')),
], ],
options={ options={
'ordering': ('title',), 'ordering': ('title',),

View File

@ -178,6 +178,29 @@ class Queue(models.Model):
# This is updated by management/commands/get_mail.py. # This is updated by management/commands/get_mail.py.
) )
socks_proxy_type = models.CharField(
_('Socks Proxy Type'),
max_length=8,
choices=(('socks4', _('SOCKS4')), ('socks5', _('SOCKS5'))),
blank=True,
null=True,
help_text=_('SOCKS4 or SOCKS5 allows you to proxy your connections through a SOCKS server.'),
)
socks_proxy_host = models.GenericIPAddressField(
_('Socks Proxy Host'),
blank=True,
null=True,
help_text=_('Socks proxy IP address.'),
)
socks_proxy_port = models.IntegerField(
_('Socks Proxy Port'),
blank=True,
null=True,
help_text=_('Socks proxy port number.'),
)
def __unicode__(self): def __unicode__(self):
return u"%s" % self.title return u"%s" % self.title