From bec746b76f3322dfc5935dd4163abde44409f0da Mon Sep 17 00:00:00 2001 From: Alexey Minnekhanov Date: Tue, 15 May 2018 12:48:18 +0300 Subject: [PATCH 1/2] Use proxies from Pyfa's "Network settings" for ESI requests --- service/esiAccess.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/service/esiAccess.py b/service/esiAccess.py index 2d4605f69..7a1fe262b 100644 --- a/service/esiAccess.py +++ b/service/esiAccess.py @@ -18,7 +18,7 @@ import base64 import datetime from eos.enum import Enum -from service.settings import EsiSettings +from service.settings import EsiSettings, NetworkSettings from requests import Session from urllib.parse import urlencode, quote @@ -85,6 +85,7 @@ class EsiAccess(object): 'pyfa v{}'.format(config.version) ) }) + self._session.proxies = self.get_requests_proxies() @property def sso_url(self): @@ -132,6 +133,26 @@ class EsiAccess(object): if 'refresh_token' in tokenResponse: char.refreshToken = config.cipher.encrypt(tokenResponse['refresh_token'].encode()) + @staticmethod + def get_requests_proxies(): + proxies = {} + proxy_settings = NetworkSettings.getInstance().getProxySettings() + # proxy_settings is a tuple of (host, port), like ('192.168.20.1', 3128), or None + if proxy_settings is not None: + # form proxy address in format "http://host:port + proxy_host_port = '{}:{}'.format(proxy_settings[0], proxy_settings[1]) + proxy_auth_details = NetworkSettings.getInstance().getProxyAuthDetails() + # proxy_auth_details is a tuple of (login, password), or None + user_pass = '' + if proxy_auth_details is not None: + # construct prefix in form "user:password@" + user_pass = '{}:{}@'.format(proxy_auth_details[0], proxy_auth_details[1]) + proxies = { + 'http': 'http://' + user_pass + proxy_host_port, + 'https': 'http://' + user_pass + proxy_host_port + } + return proxies + def getLoginURI(self, redirect=None): self.state = str(uuid.uuid4()) From 63074a2b8579382964b645ecc8b1cde3a60788e6 Mon Sep 17 00:00:00 2001 From: Alexey Min Date: Tue, 15 May 2018 22:13:04 +0300 Subject: [PATCH 2/2] Reduce code duplication: Network settings class now provides its own method to get proxy settings dict in requests format. This method can be used both in network service and esi access, reducing code duplication. --- service/esiAccess.py | 22 +--------------------- service/network.py | 20 +------------------- service/settings.py | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 40 deletions(-) diff --git a/service/esiAccess.py b/service/esiAccess.py index 7a1fe262b..bbddc6384 100644 --- a/service/esiAccess.py +++ b/service/esiAccess.py @@ -85,7 +85,7 @@ class EsiAccess(object): 'pyfa v{}'.format(config.version) ) }) - self._session.proxies = self.get_requests_proxies() + self._session.proxies = NetworkSettings.getInstance().getProxySettingsInRequestsFormat() @property def sso_url(self): @@ -133,26 +133,6 @@ class EsiAccess(object): if 'refresh_token' in tokenResponse: char.refreshToken = config.cipher.encrypt(tokenResponse['refresh_token'].encode()) - @staticmethod - def get_requests_proxies(): - proxies = {} - proxy_settings = NetworkSettings.getInstance().getProxySettings() - # proxy_settings is a tuple of (host, port), like ('192.168.20.1', 3128), or None - if proxy_settings is not None: - # form proxy address in format "http://host:port - proxy_host_port = '{}:{}'.format(proxy_settings[0], proxy_settings[1]) - proxy_auth_details = NetworkSettings.getInstance().getProxyAuthDetails() - # proxy_auth_details is a tuple of (login, password), or None - user_pass = '' - if proxy_auth_details is not None: - # construct prefix in form "user:password@" - user_pass = '{}:{}@'.format(proxy_auth_details[0], proxy_auth_details[1]) - proxies = { - 'http': 'http://' + user_pass + proxy_host_port, - 'https': 'http://' + user_pass + proxy_host_port - } - return proxies - def getLoginURI(self, redirect=None): self.state = str(uuid.uuid4()) diff --git a/service/network.py b/service/network.py index c1eeb7d15..0c3bfb2b9 100644 --- a/service/network.py +++ b/service/network.py @@ -93,25 +93,7 @@ class Network(object): # or with HTTP Basic auth support: proxies = {'http': 'http://user:pass@10.10.1.10:3128/'} # then you do: requests.get('http://example.org', proxies=proxies) - proxies = None - proxy_settings = NetworkSettings.getInstance().getProxySettings() - # proxy_settings is a tuple of (host, port), like ('192.168.20.1', 3128), or None - - if proxy_settings is not None: - # form proxy address in format "http://host:port - proxy_host_port = '{}:{}'.format(proxy_settings[0], proxy_settings[1]) - proxy_auth_details = NetworkSettings.getInstance().getProxyAuthDetails() - # proxy_auth_details is a tuple of (login, password), or None - user_pass = '' - if proxy_auth_details is not None: - # construct prefix in form "user:password@" - user_pass = '{}:{}@'.format(proxy_auth_details[0], proxy_auth_details[1]) - proxies = { - 'http': 'http://' + user_pass + proxy_host_port, - 'https': 'http://' + user_pass + proxy_host_port - } - # final form: { 'http': 'http://user:password@host:port', ... }, or - # { 'http': 'http://host:port', ... } if no auth info. + proxies = NetworkSettings.getInstance().getProxySettingsInRequestsFormat() try: resp = requests.get(url, headers=headers, proxies=proxies, **kwargs) diff --git a/service/settings.py b/service/settings.py index 6852dddcb..75bdb5405 100644 --- a/service/settings.py +++ b/service/settings.py @@ -284,6 +284,23 @@ class NetworkSettings(object): self.serviceNetworkSettings["login"] = login self.serviceNetworkSettings["password"] = password + def getProxySettingsInRequestsFormat(self) -> dict: + proxies = {} + proxy_settings = self.getProxySettings() + if proxy_settings is not None: + # form proxy address in format "http://host:port + proxy_host_port = '{}:{}'.format(proxy_settings[0], proxy_settings[1]) + proxy_auth_details = self.getProxyAuthDetails() + user_pass = '' + if proxy_auth_details is not None: + # construct prefix in form "user:password@" + user_pass = '{}:{}@'.format(proxy_auth_details[0], proxy_auth_details[1]) + proxies = { + 'http': 'http://' + user_pass + proxy_host_port, + 'https': 'http://' + user_pass + proxy_host_port + } + return proxies + class HTMLExportSettings(object): """