Skip to content

Commit

Permalink
Merge pull request #284 from massanchik/master
Browse files Browse the repository at this point in the history
Socks proxy support
  • Loading branch information
rossp committed Dec 26, 2014
2 parents 150fd1b + ae681dd commit 7b07f56
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ Before django-helpdesk will be much use, you need to do some basic configuration
EMAIL_HOST_USER = 'YYYYYY@ZZZZ.PPP'
EMAIL_HOST_PASSWORD = '123456'

8. If you wish to use SOCKS4/5 proxy with Helpdesk Queue email operations, install PySocks manually.

You're now up and running! Happy ticketing.
17 changes: 17 additions & 0 deletions helpdesk/management/commands/get_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import mimetypes
import poplib
import re
import socket

from datetime import timedelta
from email.header import decode_header
Expand Down Expand Up @@ -84,6 +85,22 @@ 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:
try:
import socks
except ImportError:
raise ImportError("Queue has been configured with proxy settings, but no socks library was installed. Try to install PySocks via pypi.")

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':
Expand Down
32 changes: 32 additions & 0 deletions helpdesk/migrations/0002_socks_proxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('helpdesk', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='queue',
name='socks_proxy_host',
field=models.GenericIPAddressField(help_text='Socks proxy IP address. Default: 127.0.0.1', null=True, verbose_name='Socks Proxy Host', blank=True),
preserve_default=True,
),
migrations.AddField(
model_name='queue',
name='socks_proxy_port',
field=models.IntegerField(help_text='Socks proxy port number. Default: 9150 (default TOR port)', null=True, verbose_name='Socks Proxy Port', blank=True),
preserve_default=True,
),
migrations.AddField(
model_name='queue',
name='socks_proxy_type',
field=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'),
preserve_default=True,
),
]
32 changes: 32 additions & 0 deletions helpdesk/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. Default: 127.0.0.1'),
)

socks_proxy_port = models.IntegerField(
_('Socks Proxy Port'),
blank=True,
null=True,
help_text=_('Socks proxy port number. Default: 9150 (default TOR port)'),
)

def __unicode__(self):
return u"%s" % self.title

Expand All @@ -202,6 +225,15 @@ def save(self, *args, **kwargs):
if self.email_box_type == 'imap' and not self.email_box_imap_folder:
self.email_box_imap_folder = 'INBOX'

if self.socks_proxy_type:
if not self.socks_proxy_host:
self.socks_proxy_host = '127.0.0.1'
if not self.socks_proxy_port:
self.socks_proxy_port = 9150
else:
self.socks_proxy_host = None
self.socks_proxy_port = None

if not self.email_box_port:
if self.email_box_type == 'imap' and self.email_box_ssl:
self.email_box_port = 993
Expand Down

0 comments on commit 7b07f56

Please sign in to comment.