From 50b80b2a084fe9ca2da0268f631be7a88cfd82e6 Mon Sep 17 00:00:00 2001 From: TreeNode Date: Wed, 10 Dec 2014 23:37:34 +0200 Subject: [PATCH] Socks proxy support --- helpdesk/management/commands/get_email.py | 13 +++++++++++++ helpdesk/migrations/0001_initial.py | 4 ++++ helpdesk/models.py | 23 +++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/helpdesk/management/commands/get_email.py b/helpdesk/management/commands/get_email.py index 8d4fede6..33dcc27c 100644 --- a/helpdesk/management/commands/get_email.py +++ b/helpdesk/management/commands/get_email.py @@ -15,6 +15,8 @@ import imaplib import mimetypes import poplib import re +import socks +import socket from datetime import timedelta from email.header import decode_header @@ -84,6 +86,17 @@ def process_queue(q, quiet=False): if not quiet: 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 if email_box_type == 'pop3': diff --git a/helpdesk/migrations/0001_initial.py b/helpdesk/migrations/0001_initial.py index dfd93b4b..80b3dccc 100644 --- a/helpdesk/migrations/0001_initial.py +++ b/helpdesk/migrations/0001_initial.py @@ -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_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)), + ('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={ 'ordering': ('title',), diff --git a/helpdesk/models.py b/helpdesk/models.py index cbd42e5d..13b188f0 100644 --- a/helpdesk/models.py +++ b/helpdesk/models.py @@ -178,6 +178,29 @@ class Queue(models.Model): # 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): return u"%s" % self.title