From a3986fd0a7ac2d902bd3a0a7828d2fc912fbe497 Mon Sep 17 00:00:00 2001 From: Alwx Date: Mon, 16 Apr 2018 16:09:15 +0300 Subject: [PATCH 1/2] add socks5 proxy support --- sentry_telegram/plugin.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/sentry_telegram/plugin.py b/sentry_telegram/plugin.py index 09116d6..0ded265 100644 --- a/sentry_telegram/plugin.py +++ b/sentry_telegram/plugin.py @@ -1,5 +1,6 @@ # coding: utf-8 import logging +import requests from django import forms from django.utils.translation import ugettext_lazy as _ @@ -29,6 +30,11 @@ class TelegramNotificationsOptionsForm(notify.NotificationConfigurationForm): '{project_name}, {url}, {title}, {message}, {tag[%your_tag%]}'), initial='*[Sentry]* {project_name} {tag[level]}: *{title}*\n```{message}```\n{url}' ) + socks_proxy = forms.CharField( + label=_('Socks5 proxy'), + widget=forms.TextInput(attrs={'placeholder': 'socks5://{user}:{pass}@{host}:{port}'}), + help_text=_('Enter socks5 proxy.') + ) class TelegramNotificationsPlugin(notify.NotificationPlugin): @@ -82,6 +88,14 @@ def get_config(self, project, **kwargs): 'required': True, 'default': '*[Sentry]* {project_name} {tag[level]}: *{title}*\n```{message}```\n{url}' }, + { + 'name': 'socks_proxy', + 'label': 'Socks Proxy', + 'type': 'text', + 'help': 'Enter socks5 proxy.', + 'validators': [], + 'required': False, + }, ] def build_message(self, group, event): @@ -124,6 +138,12 @@ def send_message(self, url, payload, receiver): ) self.logger.debug('Response code: %s, content: %s' % (response.status_code, response.content)) + def send_message_through_proxy(self, url, payload, receiver, proxy): + payload['chat_id'] = receiver + self.logger.debug('Sending message to %s . Using proxy' % receiver) + response = requests.post(url, data=payload, proxies=dict(http=proxy, https=proxy)) + self.logger.debug('Response code: %s, content: %s' % (response.status_code, response.content)) + def notify_users(self, group, event, fail_silently=False): self.logger.debug('Received notification for event: %s' % event) receivers = self.get_receivers(group.project) @@ -132,5 +152,9 @@ def notify_users(self, group, event, fail_silently=False): self.logger.debug('Built payload: %s' % payload) url = self.build_url(group.project) self.logger.debug('Built url: %s' % url) + proxy = self.get_option('socks_proxy', group.project) for receiver in receivers: - safe_execute(self.send_message, url, payload, receiver, _with_transaction=False) + if proxy: + safe_execute(self.send_message, url, payload, receiver, proxy, _with_transaction=False) + else: + safe_execute(self.send_message, url, payload, receiver, _with_transaction=False) From 7ed32234ccc6205381b8c38e6e590f6fc14a7250 Mon Sep 17 00:00:00 2001 From: Alwx Date: Mon, 16 Apr 2018 17:23:27 +0300 Subject: [PATCH 2/2] use send_message_through_proxy for proxy, not send_message --- sentry_telegram/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentry_telegram/plugin.py b/sentry_telegram/plugin.py index 0ded265..7a1b4a3 100644 --- a/sentry_telegram/plugin.py +++ b/sentry_telegram/plugin.py @@ -155,6 +155,6 @@ def notify_users(self, group, event, fail_silently=False): proxy = self.get_option('socks_proxy', group.project) for receiver in receivers: if proxy: - safe_execute(self.send_message, url, payload, receiver, proxy, _with_transaction=False) + safe_execute(self.send_message_through_proxy, url, payload, receiver, proxy, _with_transaction=False) else: safe_execute(self.send_message, url, payload, receiver, _with_transaction=False)