Skip to content

Commit

Permalink
WIP: lib(ws): adding cookie storage support
Browse files Browse the repository at this point in the history
* first login cookies get capture for future use.
* consecutive login use the existing cookie.
  • Loading branch information
dhruvinsh committed Nov 4, 2023
1 parent baec121 commit c343585
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
5 changes: 1 addition & 4 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@
USERNAME_ID: str = "username"
PASSWORD_ID: str = "password"

COOKIES = httpx.Cookies()
COOKIES.set("i_can_has_cookie", "1")
COOKIES.set("ref", "https://windscribe.com/")

COOKIE_PATH = Path("cookie.pkl")

# fmt: off
# some qbit config
Expand Down
3 changes: 3 additions & 0 deletions src/ws/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .ws import Windscribe

__all__ = ["Windscribe"]
36 changes: 36 additions & 0 deletions src/ws/cookie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import pickle

from httpx import Cookies

import config


def default_cookie() -> Cookies:
"""Build default cookie."""
cookie = Cookies()
cookie.set("i_can_has_cookie", "1")
cookie.set("ref", "https://windscribe.com/")
return cookie


def load_cookie() -> None | Cookies:
"""Load existing cookie."""
if not config.COOKIE_PATH.exists():
return None

cookie = Cookies()
with open(config.COOKIE_PATH, "rb") as ck:
cookie_dict = pickle.load(ck)
for k, v in cookie_dict.items():
cookie.set(k, v)
return cookie


def save_cookie(cookie: Cookies):
"""Save the cookie to the file for future use"""
cookie_dict: dict[str, str] = {}
for k, v in cookie.items():
cookie_dict[k] = v

with open(config.COOKIE_PATH, "wb") as ck:
pickle.dump(cookie_dict, ck)
18 changes: 16 additions & 2 deletions src/ws.py → src/ws/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import config

from .cookie import default_cookie, load_cookie, save_cookie


class Csrf(TypedDict):
"""CSRF type dict"""
Expand All @@ -38,8 +40,15 @@ def __init__(self, username: str, password: str) -> None:
# ruff: noqa: E501
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36",
}

self.cookie_found = True
cookie = load_cookie()
if cookie is None:
self.cookie_found = False
cookie = default_cookie()

self.client = httpx.Client(
headers=headers, cookies=config.COOKIES, timeout=config.REQUEST_TIMEOUT
headers=headers, cookies=cookie, timeout=config.REQUEST_TIMEOUT
)

# we will populate this later in the login call
Expand Down Expand Up @@ -93,6 +102,10 @@ def _login(self) -> None:
"code": "",
}
self.client.post(config.LOGIN_URL, data=data)

# save the cookie for the future use.
save_cookie(self.client.cookies)

self.logger.debug("login successful")

def _delete_ephm_port(self) -> dict[str, Union[bool, int]]:
Expand Down Expand Up @@ -137,7 +150,8 @@ def _set_matching_port(self) -> int:

def setup(self) -> int:
"""perform ephemeral port setup here"""
self._login()
if not self.cookie_found:
self._login()

# after login we need to update the csrf token agian,
# windscribe puts new csrf token in the javascript
Expand Down

0 comments on commit c343585

Please sign in to comment.