From 2557cadd2760eafc51a072055cf3c83747b2f40d Mon Sep 17 00:00:00 2001 From: Jose Carlos Recuero Date: Wed, 25 Aug 2021 16:46:38 -0700 Subject: [PATCH 1/4] Node without use of proxy Provide new attribute when Node is created that allows to don't use proxy for any request for that connection. Default value is set to use proxy. --- pyaci/core.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pyaci/core.py b/pyaci/core.py index 6c230df..ae27a08 100755 --- a/pyaci/core.py +++ b/pyaci/core.py @@ -75,8 +75,9 @@ def _elementToString(e): class Api(object): - def __init__(self, parentApi=None): + def __init__(self, parentApi=None, no_proxies=None): self._parentApi = parentApi + self._no_proxies = no_proxies def GET(self, format=None, **kwargs): return self._performRequest('GET', format=format, **kwargs) @@ -142,7 +143,9 @@ def _performRequest(self, method, format=None, needData=False, **kwargs): if "subscription" not in kwargs: self._x509Prep(rootApi, prepped, data) send_kwargs = rootApi._session.merge_environment_settings( - prepped.url, proxies={},stream=None, verify=rootApi._verify, cert=None) + prepped.url, proxies={}, stream=None, verify=rootApi._verify, cert=None) + if rootApi._no_proxies: + send_kwargs["proxies"] = {} response = rootApi._session.send( prepped, timeout=rootApi._timeout, **send_kwargs) @@ -178,8 +181,8 @@ def _rootApi(self): class Node(Api): def __init__(self, url, session=None, verify=False, disableWarnings=True, - timeout=None, aciMetaFilePath=None): - super(Node, self).__init__() + timeout=None, aciMetaFilePath=None, no_proxies=None): + super(Node, self).__init__(no_proxies=no_proxies) self._url = url if session is not None: self._session = session From c4a67a8d2ae48244a5f53eccdd6c14789a119dc5 Mon Sep 17 00:00:00 2001 From: Jose Carlos Recuero Date: Mon, 30 Aug 2021 18:40:56 -0700 Subject: [PATCH 2/4] Pass proxies to be used for a given Node When Node is created, proxies to be used for the give connection can be defined at this time. Any request will make use of those proxies. --- pyaci/core.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pyaci/core.py b/pyaci/core.py index ae27a08..0814fec 100755 --- a/pyaci/core.py +++ b/pyaci/core.py @@ -75,10 +75,9 @@ def _elementToString(e): class Api(object): - def __init__(self, parentApi=None, no_proxies=None): + def __init__(self, parentApi=None, user_proxies=None): self._parentApi = parentApi - self._no_proxies = no_proxies - + self._user_proxies = user_proxies def GET(self, format=None, **kwargs): return self._performRequest('GET', format=format, **kwargs) @@ -144,8 +143,9 @@ def _performRequest(self, method, format=None, needData=False, **kwargs): self._x509Prep(rootApi, prepped, data) send_kwargs = rootApi._session.merge_environment_settings( prepped.url, proxies={}, stream=None, verify=rootApi._verify, cert=None) - if rootApi._no_proxies: - send_kwargs["proxies"] = {} + + if rootApi._user_proxies is not None: + send_kwargs["proxies"] = rootApi._user_proxies response = rootApi._session.send( prepped, timeout=rootApi._timeout, **send_kwargs) @@ -181,8 +181,8 @@ def _rootApi(self): class Node(Api): def __init__(self, url, session=None, verify=False, disableWarnings=True, - timeout=None, aciMetaFilePath=None, no_proxies=None): - super(Node, self).__init__(no_proxies=no_proxies) + timeout=None, aciMetaFilePath=None, user_proxies=None): + super(Node, self).__init__(user_proxies=user_proxies) self._url = url if session is not None: self._session = session From 0c7f951b7673e2b29baf6bc09b8544275c5f88ae Mon Sep 17 00:00:00 2001 From: Praveen Kumar Date: Tue, 7 Sep 2021 14:59:56 -0700 Subject: [PATCH 3/4] Fix casing to match with PyACI convention --- pyaci/core.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pyaci/core.py b/pyaci/core.py index 0814fec..955bbe7 100755 --- a/pyaci/core.py +++ b/pyaci/core.py @@ -75,9 +75,9 @@ def _elementToString(e): class Api(object): - def __init__(self, parentApi=None, user_proxies=None): + def __init__(self, parentApi=None, userProxies=None): self._parentApi = parentApi - self._user_proxies = user_proxies + self._userProxies = userProxies def GET(self, format=None, **kwargs): return self._performRequest('GET', format=format, **kwargs) @@ -144,8 +144,8 @@ def _performRequest(self, method, format=None, needData=False, **kwargs): send_kwargs = rootApi._session.merge_environment_settings( prepped.url, proxies={}, stream=None, verify=rootApi._verify, cert=None) - if rootApi._user_proxies is not None: - send_kwargs["proxies"] = rootApi._user_proxies + if rootApi._userProxies is not None: + send_kwargs['proxies'] = rootApi._userProxies response = rootApi._session.send( prepped, timeout=rootApi._timeout, **send_kwargs) @@ -181,8 +181,8 @@ def _rootApi(self): class Node(Api): def __init__(self, url, session=None, verify=False, disableWarnings=True, - timeout=None, aciMetaFilePath=None, user_proxies=None): - super(Node, self).__init__(user_proxies=user_proxies) + timeout=None, aciMetaFilePath=None, userProxies=None): + super(Node, self).__init__(userProxies=userProxies) self._url = url if session is not None: self._session = session From 44e52ca95da0b60e6880a51805db111e7d7d08cd Mon Sep 17 00:00:00 2001 From: Jose Carlos Recuero Date: Wed, 1 Dec 2021 12:49:05 -0800 Subject: [PATCH 4/4] Update websocket proxy properly when user/password used When user/password is provided in the proxy http://username:password@hostname:port hostname and port have to be extracted properly in order to pass them to the websocket. --- pyaci/core.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pyaci/core.py b/pyaci/core.py index bb93481..ddfc131 100755 --- a/pyaci/core.py +++ b/pyaci/core.py @@ -266,9 +266,16 @@ def startWsListener(self): try: proxyUrl = self._userProxies.get("https", self._userProxies.get("http", None)) if proxyUrl: + # proxy format: http://username:password@host:port + # if username:password is provided, it has to be removed in + # order to retrive host and port properly. + if "@" in proxyUrl: + proxyUrl = "http://{}".format(proxyUrl.split("@")[1]) + logger.info("URL proxyUrl {}".format(proxyUrl)) runForeverKwargs["http_proxy_host"] = urlparse(proxyUrl).netloc.split(":")[0] runForeverKwargs["http_proxy_port"] = int(urlparse(proxyUrl).netloc.split(":")[1]) runForeverKwargs["proxy_type"] = "http" + logger.info("URL kwargs {}".format(runForeverKwargs)) except ValueError: logger.info("http(s) proxy unavailable for {}".format(self.webSocketUrl)) wst = threading.Thread(target=lambda: ws.run_forever(