Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion sentry_telegram/plugin.py
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 _
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Copy link
Owner

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:

response = safe_urlopen(
    method='POST',
    url=url,
    json=payload,
    params=self.get_request_params(),
)

Where get_request_params() will return proxies settings, if it required.

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))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to refrain from the direct use of requests. Sentry has internal, unified utils to interact with network.
Please, look for safe_urlopen() built-in function. It supports params to pass additional options, like proxy-settings.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use params because this argument for query data https://github.com/requests/requests/blob/master/requests/sessions.py#L455
Is it worth to add PR to sentry for adding proxy functionality?

Copy link
Author

Choose a reason for hiding this comment

The 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)
Expand All @@ -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)