-
-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add basic proxy support #19
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer to refrain from the direct use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey, there is already one getsentry/sentry#6784 |
||
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_through_proxy, url, payload, receiver, proxy, _with_transaction=False) | ||
else: | ||
safe_execute(self.send_message, url, payload, receiver, _with_transaction=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method looks like almost copy of
send_message
method.I propose to use one
send_message
, and add proxies like this:Where
get_request_params()
will return proxies settings, if it required.