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

Proxy reset #160

Merged
merged 5 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ sphinx-rtd-theme = "*"
m2r2 = "*"
sphinx-autoapi = "*"
sphinx-copybutton = "*"
bandit = "*"

[packages]
aiohttp = "*"
backoff = "*"
beautifulsoup4 = "*"
wrapt = "*"
authcaptureproxy = "~=0.4.2"
authcaptureproxy = "~=0.5.0"

[pipenv]
allow_prereleases = true
130 changes: 90 additions & 40 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions teslajsonpy/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def valid_result(result):
)
except TypeError as exception:
_LOGGER.error("Result: %s, %s", result, exception)
return False

retries = 0
sleep_delay = 2
Expand Down
30 changes: 20 additions & 10 deletions teslajsonpy/teslaproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, proxy_url: URL, host_url: URL) -> None:
self._config_flow_id = None
self._callback_url = None
self.waf_retry = 0
self.waf_limit = 7
self.waf_limit = 30
self.tests = {"test_url": self.test_url}

self.headers = {
Expand Down Expand Up @@ -78,17 +78,19 @@ async def test_url(
Optional[Union[URL, Text]]: URL for a http 302 redirect or Text to display on success. None indicates test did not pass.

"""

if str(resp.url) == "https://auth.tesla.com/static/404.html":
code: Text = ""
if resp.url.path == "/void/callback":
code = resp.url.query.get("code")
if resp.url.path == "/static/404.html":
code = URL(resp.history[-1].url).query.get("code")
if code:
username = data.get("identity")
self._callback_url = self.init_query.get("callback_url")
if code:
_LOGGER.debug("Success! Oauth code %s for %s captured.", code, username)
# 302 redirect
return URL(self._callback_url).update_query(
{"code": code, "username": username}
)
_LOGGER.debug("Success! Oauth code %s for %s captured.", code, username)
# 302 redirect
return URL(self._callback_url).update_query(
{"code": code, "username": username}
)
if resp.content_type == "text/html":
text = await resp.text()
if "<noscript>Please enable JavaScript to view the page content." in text:
Expand All @@ -97,7 +99,7 @@ async def test_url(
return return_timer_countdown_refresh_html(
max(30 * (self.waf_retry - self.waf_limit), 120)
if self.waf_retry > self.waf_limit
else random.random() * self.waf_retry + 10,
else random.random() * self.waf_retry + 5,
f"Detected Tesla web application firewall block #{self.waf_retry}. Please wait and then reload the page or wait for the auto reload.",
)
self.waf_retry = 0
Expand Down Expand Up @@ -130,6 +132,14 @@ async def prepend_relative_urls(self, base_url: URL, html: Text) -> Text:
html=html,
)

async def reset_data(self) -> None:
"""Reset all stored data.

A proxy may need to service multiple login requests if the route is not torn down. This function will reset all data between logins.
"""
self.waf_retry = 0
await super().reset_data()

async def prepend_i18n_path(self, base_url: URL, html: Text) -> Text:
"""Prepend path for i18n loadPath so it'll reach the proxy.

Expand Down