binarycookies 2.1.4__tar.gz → 2.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {binarycookies-2.1.4 → binarycookies-2.2.0}/PKG-INFO +62 -62
- {binarycookies-2.1.4 → binarycookies-2.2.0}/README.md +58 -61
- {binarycookies-2.1.4 → binarycookies-2.2.0}/pyproject.toml +17 -5
- binarycookies-2.2.0/src/binarycookies/__main__.py +38 -0
- {binarycookies-2.1.4 → binarycookies-2.2.0}/src/binarycookies/_deserialize.py +18 -3
- binarycookies-2.2.0/src/binarycookies/_output_handlers.py +70 -0
- {binarycookies-2.1.4 → binarycookies-2.2.0}/src/binarycookies/_serialize.py +9 -2
- {binarycookies-2.1.4 → binarycookies-2.2.0}/src/binarycookies/parser.py +1 -1
- binarycookies-2.1.4/setup.py +0 -38
- binarycookies-2.1.4/src/binarycookies/__main__.py +0 -48
- {binarycookies-2.1.4 → binarycookies-2.2.0}/LICENSE +0 -0
- {binarycookies-2.1.4 → binarycookies-2.2.0}/src/binarycookies/__init__.py +0 -0
- {binarycookies-2.1.4 → binarycookies-2.2.0}/src/binarycookies/models.py +0 -0
- {binarycookies-2.1.4 → binarycookies-2.2.0}/src/binarycookies/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
2
|
Name: binarycookies
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.2.0
|
|
4
4
|
Summary: Python Binary Cookies (de)serializer
|
|
5
5
|
Author: Daniel Tom
|
|
6
6
|
Author-email: d.e.tom89@gmail.com
|
|
@@ -10,81 +10,35 @@ Classifier: Programming Language :: Python :: 3.8
|
|
|
10
10
|
Classifier: Programming Language :: Python :: 3.9
|
|
11
11
|
Classifier: Programming Language :: Python :: 3.10
|
|
12
12
|
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
15
|
Requires-Dist: pydantic (>=2.0.0,<3.0.0)
|
|
14
16
|
Requires-Dist: typer (>=0.12.3,<0.17.0)
|
|
15
17
|
Description-Content-Type: text/markdown
|
|
16
18
|
|
|
17
19
|
[](https://github.com/dan1elt0m/binary-cookies-reader/actions/workflows/test.yml)
|
|
20
|
+
<h1>
|
|
21
|
+
<img src="docs/bincook.png" width="30" style="vertical-align: middle; margin-right: 10px;">
|
|
22
|
+
Binary Cookies
|
|
23
|
+
</h1>
|
|
18
24
|
|
|
19
|
-
|
|
25
|
+
CLI tool and Python library for reading and writing Binary Cookies.
|
|
20
26
|
|
|
21
|
-
|
|
27
|
+
### Requirements
|
|
22
28
|
|
|
23
|
-
|
|
29
|
+
- Python >= 3.9
|
|
24
30
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
## Installation
|
|
31
|
+
### Installation
|
|
28
32
|
```bash
|
|
29
33
|
pip install binarycookies
|
|
30
34
|
```
|
|
31
|
-
If you want to use the parser as CLI, it's recommended to use pipx to install the package in an isolated environment.
|
|
32
|
-
```bash
|
|
33
|
-
pipx install binarycookies
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
## Basic Usage CLI
|
|
37
|
-
After installation, you can use the command-line interface to read a binary cookies file:
|
|
38
|
-
|
|
39
|
-
```bash
|
|
40
|
-
bcparser <path_to_binary_cookies_file>
|
|
41
|
-
```
|
|
42
|
-
Replace <path_to_binary_cookies_file> with the path to the binary cookie file you want to read.
|
|
43
|
-
|
|
44
|
-
### Basic Usage Python
|
|
45
|
-
|
|
46
|
-
#### Deserialization
|
|
47
|
-
|
|
48
|
-
```python
|
|
49
|
-
import binarycookies
|
|
50
|
-
|
|
51
|
-
with open("path/to/cookies.binarycookies", "rb") as f:
|
|
52
|
-
cookies = binarycookies.load(f)
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
#### Serialization
|
|
56
|
-
|
|
57
|
-
```python
|
|
58
|
-
import binarycookies
|
|
59
|
-
|
|
60
|
-
cookie = {
|
|
61
|
-
"name": "session_id",
|
|
62
|
-
"value": "abc123",
|
|
63
|
-
"url": "https://example.com",
|
|
64
|
-
"path": "/",
|
|
65
|
-
"create_datetime": "2023-10-01T12:34:56+00:00",
|
|
66
|
-
"expiry_datetime": "2023-12-31T23:59:59+00:00",
|
|
67
|
-
"flag": "Secure"
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
with open("path/to/cookies.binarycookies", "wb") as f:
|
|
71
|
-
binarycookies.dump(cookie, f)
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
## Output Types
|
|
75
|
-
|
|
76
|
-
The `bcparser` CLI supports two output types: `json` (default) and `ascii`.
|
|
77
|
-
|
|
78
|
-
### JSON Output
|
|
79
|
-
|
|
80
|
-
The `json` output type formats the cookies as a JSON array, making it easy to parse and manipulate programmatically.
|
|
81
35
|
|
|
82
|
-
|
|
36
|
+
### CLI example:
|
|
83
37
|
```sh
|
|
84
|
-
bcparser path/to/cookies.binarycookies
|
|
38
|
+
bcparser path/to/cookies.binarycookies
|
|
85
39
|
```
|
|
86
40
|
|
|
87
|
-
|
|
41
|
+
Output:
|
|
88
42
|
```json
|
|
89
43
|
[
|
|
90
44
|
{
|
|
@@ -108,8 +62,10 @@ Example output JSON:
|
|
|
108
62
|
]
|
|
109
63
|
```
|
|
110
64
|
|
|
65
|
+
|
|
111
66
|
### ASCII Output
|
|
112
|
-
|
|
67
|
+
|
|
68
|
+
`ascii` output is also possible with the --output ascii flag.
|
|
113
69
|
|
|
114
70
|
Example usage:
|
|
115
71
|
```sh
|
|
@@ -135,9 +91,53 @@ Expires: 2023-12-31T23:59:59+00:00
|
|
|
135
91
|
Flag: HttpOnly
|
|
136
92
|
----------------------------------------
|
|
137
93
|
```
|
|
94
|
+
### Netscape Output
|
|
95
|
+
For `netscape` output use the --output netscape flag.
|
|
96
|
+
Example usage:
|
|
97
|
+
```sh
|
|
98
|
+
bcparser path/to/cookies.binarycookies --output netscape
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Example output Netscape:
|
|
102
|
+
```netscape
|
|
103
|
+
# Netscape HTTP Cookie File
|
|
104
|
+
example.com FALSE / TRUE 1704067199 session_id abc123
|
|
105
|
+
example.com FALSE /account FALSE 1704067199 user_token xyz789
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Basic Usage Python
|
|
109
|
+
|
|
110
|
+
#### Deserialization
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
import binarycookies
|
|
114
|
+
|
|
115
|
+
with open("path/to/cookies.binarycookies", "rb") as f:
|
|
116
|
+
cookies = binarycookies.load(f)
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
#### Serialization
|
|
120
|
+
|
|
121
|
+
```python
|
|
122
|
+
import binarycookies
|
|
123
|
+
|
|
124
|
+
cookie = {
|
|
125
|
+
"name": "session_id",
|
|
126
|
+
"value": "abc123",
|
|
127
|
+
"url": "https://example.com",
|
|
128
|
+
"path": "/",
|
|
129
|
+
"create_datetime": "2023-10-01T12:34:56+00:00",
|
|
130
|
+
"expiry_datetime": "2023-12-31T23:59:59+00:00",
|
|
131
|
+
"flag": "Secure"
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
with open("path/to/cookies.binarycookies", "wb") as f:
|
|
135
|
+
binarycookies.dump(cookie, f)
|
|
136
|
+
```
|
|
138
137
|
|
|
139
138
|
### License
|
|
140
139
|
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
|
141
140
|
|
|
142
141
|
### Contributing
|
|
143
142
|
Contributions are welcome! If you find a bug or have a feature request, please open an issue on GitHub. Pull requests are also welcome.
|
|
143
|
+
|
|
@@ -1,74 +1,26 @@
|
|
|
1
1
|
[](https://github.com/dan1elt0m/binary-cookies-reader/actions/workflows/test.yml)
|
|
2
|
+
<h1>
|
|
3
|
+
<img src="docs/bincook.png" width="30" style="vertical-align: middle; margin-right: 10px;">
|
|
4
|
+
Binary Cookies
|
|
5
|
+
</h1>
|
|
2
6
|
|
|
3
|
-
|
|
7
|
+
CLI tool and Python library for reading and writing Binary Cookies.
|
|
4
8
|
|
|
5
|
-
|
|
9
|
+
### Requirements
|
|
6
10
|
|
|
7
|
-
|
|
11
|
+
- Python >= 3.9
|
|
8
12
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
## Installation
|
|
13
|
+
### Installation
|
|
12
14
|
```bash
|
|
13
15
|
pip install binarycookies
|
|
14
16
|
```
|
|
15
|
-
If you want to use the parser as CLI, it's recommended to use pipx to install the package in an isolated environment.
|
|
16
|
-
```bash
|
|
17
|
-
pipx install binarycookies
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
## Basic Usage CLI
|
|
21
|
-
After installation, you can use the command-line interface to read a binary cookies file:
|
|
22
|
-
|
|
23
|
-
```bash
|
|
24
|
-
bcparser <path_to_binary_cookies_file>
|
|
25
|
-
```
|
|
26
|
-
Replace <path_to_binary_cookies_file> with the path to the binary cookie file you want to read.
|
|
27
|
-
|
|
28
|
-
### Basic Usage Python
|
|
29
|
-
|
|
30
|
-
#### Deserialization
|
|
31
|
-
|
|
32
|
-
```python
|
|
33
|
-
import binarycookies
|
|
34
|
-
|
|
35
|
-
with open("path/to/cookies.binarycookies", "rb") as f:
|
|
36
|
-
cookies = binarycookies.load(f)
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
#### Serialization
|
|
40
|
-
|
|
41
|
-
```python
|
|
42
|
-
import binarycookies
|
|
43
|
-
|
|
44
|
-
cookie = {
|
|
45
|
-
"name": "session_id",
|
|
46
|
-
"value": "abc123",
|
|
47
|
-
"url": "https://example.com",
|
|
48
|
-
"path": "/",
|
|
49
|
-
"create_datetime": "2023-10-01T12:34:56+00:00",
|
|
50
|
-
"expiry_datetime": "2023-12-31T23:59:59+00:00",
|
|
51
|
-
"flag": "Secure"
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
with open("path/to/cookies.binarycookies", "wb") as f:
|
|
55
|
-
binarycookies.dump(cookie, f)
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
## Output Types
|
|
59
|
-
|
|
60
|
-
The `bcparser` CLI supports two output types: `json` (default) and `ascii`.
|
|
61
|
-
|
|
62
|
-
### JSON Output
|
|
63
17
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
Example usage:
|
|
18
|
+
### CLI example:
|
|
67
19
|
```sh
|
|
68
|
-
bcparser path/to/cookies.binarycookies
|
|
20
|
+
bcparser path/to/cookies.binarycookies
|
|
69
21
|
```
|
|
70
22
|
|
|
71
|
-
|
|
23
|
+
Output:
|
|
72
24
|
```json
|
|
73
25
|
[
|
|
74
26
|
{
|
|
@@ -92,8 +44,10 @@ Example output JSON:
|
|
|
92
44
|
]
|
|
93
45
|
```
|
|
94
46
|
|
|
47
|
+
|
|
95
48
|
### ASCII Output
|
|
96
|
-
|
|
49
|
+
|
|
50
|
+
`ascii` output is also possible with the --output ascii flag.
|
|
97
51
|
|
|
98
52
|
Example usage:
|
|
99
53
|
```sh
|
|
@@ -119,9 +73,52 @@ Expires: 2023-12-31T23:59:59+00:00
|
|
|
119
73
|
Flag: HttpOnly
|
|
120
74
|
----------------------------------------
|
|
121
75
|
```
|
|
76
|
+
### Netscape Output
|
|
77
|
+
For `netscape` output use the --output netscape flag.
|
|
78
|
+
Example usage:
|
|
79
|
+
```sh
|
|
80
|
+
bcparser path/to/cookies.binarycookies --output netscape
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Example output Netscape:
|
|
84
|
+
```netscape
|
|
85
|
+
# Netscape HTTP Cookie File
|
|
86
|
+
example.com FALSE / TRUE 1704067199 session_id abc123
|
|
87
|
+
example.com FALSE /account FALSE 1704067199 user_token xyz789
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Basic Usage Python
|
|
91
|
+
|
|
92
|
+
#### Deserialization
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
import binarycookies
|
|
96
|
+
|
|
97
|
+
with open("path/to/cookies.binarycookies", "rb") as f:
|
|
98
|
+
cookies = binarycookies.load(f)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
#### Serialization
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
import binarycookies
|
|
105
|
+
|
|
106
|
+
cookie = {
|
|
107
|
+
"name": "session_id",
|
|
108
|
+
"value": "abc123",
|
|
109
|
+
"url": "https://example.com",
|
|
110
|
+
"path": "/",
|
|
111
|
+
"create_datetime": "2023-10-01T12:34:56+00:00",
|
|
112
|
+
"expiry_datetime": "2023-12-31T23:59:59+00:00",
|
|
113
|
+
"flag": "Secure"
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
with open("path/to/cookies.binarycookies", "wb") as f:
|
|
117
|
+
binarycookies.dump(cookie, f)
|
|
118
|
+
```
|
|
122
119
|
|
|
123
120
|
### License
|
|
124
121
|
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
|
125
122
|
|
|
126
123
|
### Contributing
|
|
127
|
-
Contributions are welcome! If you find a bug or have a feature request, please open an issue on GitHub. Pull requests are also welcome.
|
|
124
|
+
Contributions are welcome! If you find a bug or have a feature request, please open an issue on GitHub. Pull requests are also welcome.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "binarycookies"
|
|
3
|
-
version = "2.
|
|
3
|
+
version = "2.2.0"
|
|
4
4
|
description = "Python Binary Cookies (de)serializer"
|
|
5
5
|
authors = ["Daniel Tom <d.e.tom89@gmail.com>"]
|
|
6
6
|
readme = "README.md"
|
|
@@ -11,14 +11,25 @@ python = ">=3.8,<4.0"
|
|
|
11
11
|
typer = ">=0.12.3,<0.17.0"
|
|
12
12
|
pydantic = ">=2.0.0,<3.0.0"
|
|
13
13
|
|
|
14
|
-
[tool.poetry.scripts]
|
|
15
|
-
bcparser = "binarycookies.__main__:main"
|
|
16
|
-
|
|
17
14
|
[tool.poetry.group.dev.dependencies]
|
|
18
15
|
pytest = "^8.2.2"
|
|
19
16
|
ruff = ">=0.5.1,<0.8.0"
|
|
20
17
|
pytest-cov = "^5.0.0"
|
|
21
18
|
|
|
19
|
+
[tool.poetry.group.docs.dependencies]
|
|
20
|
+
mkdocs = "^1.4.2"
|
|
21
|
+
mkdocs-material = "^9.1.4"
|
|
22
|
+
mkdocs-gen-files = ">=0.4,<0.6"
|
|
23
|
+
mkdocs-literate-nav = "^0.6.0"
|
|
24
|
+
mkdocstrings = {extras = ["python"], version = ">=0.20,<0.30"}
|
|
25
|
+
mkautodoc = "^0.2.0"
|
|
26
|
+
pymdown-extensions = "^10.0.1"
|
|
27
|
+
|
|
28
|
+
[tool.poetry.scripts]
|
|
29
|
+
bcparser = "binarycookies.__main__:main"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
22
33
|
[build-system]
|
|
23
34
|
requires = ["poetry-core"]
|
|
24
35
|
build-backend = "poetry.core.masonry.api"
|
|
@@ -43,7 +54,8 @@ lint.ignore = [
|
|
|
43
54
|
"RUF012", # causes failures in pydantic models
|
|
44
55
|
"PLR0911", # for some reason can't ignore this on func definition
|
|
45
56
|
"ISC001",
|
|
46
|
-
"COM812"
|
|
57
|
+
"COM812",
|
|
58
|
+
"T201", # WE want to use print in CLI
|
|
47
59
|
]
|
|
48
60
|
lint.extend-select = [
|
|
49
61
|
"I",
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from datetime import datetime
|
|
3
|
+
from typing import Type
|
|
4
|
+
|
|
5
|
+
import typer
|
|
6
|
+
from rich import print as rprint
|
|
7
|
+
|
|
8
|
+
from binarycookies import load
|
|
9
|
+
from binarycookies._output_handlers import OUTPUT_HANDLERS, OutputType
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class DateTimeEncoder(json.JSONEncoder):
|
|
13
|
+
def default(self, obj: Type) -> str:
|
|
14
|
+
if isinstance(obj, datetime):
|
|
15
|
+
return obj.isoformat()
|
|
16
|
+
return super().default(obj)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def cli(file_path: str, output: OutputType = OutputType.json):
|
|
20
|
+
"""CLI entrypoint for reading Binary Cookies"""
|
|
21
|
+
with open(file_path, "rb") as f:
|
|
22
|
+
cookies = load(f)
|
|
23
|
+
|
|
24
|
+
handler = OUTPUT_HANDLERS.get(output)
|
|
25
|
+
if not handler:
|
|
26
|
+
rprint(f"[red]Error:[/red] Unsupported output type: {output}")
|
|
27
|
+
raise typer.Exit(code=1)
|
|
28
|
+
|
|
29
|
+
handler(cookies)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def main():
|
|
33
|
+
"""CLI entrypoint for reading Binary Cookies"""
|
|
34
|
+
typer.run(cli)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
if __name__ == "__main__":
|
|
38
|
+
main()
|
|
@@ -27,8 +27,19 @@ def interpret_flag(flags: int) -> Flag:
|
|
|
27
27
|
|
|
28
28
|
|
|
29
29
|
def mac_epoch_to_date(epoch: int) -> datetime:
|
|
30
|
-
"""
|
|
31
|
-
|
|
30
|
+
"""
|
|
31
|
+
Converts a mac epoch time to a datetime object, handling potential
|
|
32
|
+
overflows on both 32-bit and 64-bit systems.
|
|
33
|
+
"""
|
|
34
|
+
# The Mac epoch starts on 2001-01-01, which is 978307200 seconds after the Unix epoch.
|
|
35
|
+
unix_epoch = epoch + 978307200
|
|
36
|
+
|
|
37
|
+
try:
|
|
38
|
+
return datetime.fromtimestamp(unix_epoch, tz=timezone.utc)
|
|
39
|
+
except (OverflowError, OSError):
|
|
40
|
+
# This handles timestamps that are too large for the system's C library,
|
|
41
|
+
# which can happen for non-expiring cookies or on 32-bit systems (Year 2038 problem).
|
|
42
|
+
return datetime.max.replace(tzinfo=timezone.utc)
|
|
32
43
|
|
|
33
44
|
|
|
34
45
|
def read_string(data: BytesIO, size: int) -> str:
|
|
@@ -111,8 +122,12 @@ def _deserialize_page(page: BytesIO) -> List[Cookie]:
|
|
|
111
122
|
|
|
112
123
|
def load(bf: BinaryIO) -> List[Cookie]:
|
|
113
124
|
"""Deserializes a binary cookie file and returns a list of Cookie objects.
|
|
125
|
+
|
|
114
126
|
Args:
|
|
115
|
-
bf (BinaryIO): A binary file object containing the binary cookie data.
|
|
127
|
+
bf (BinaryIO): A binary file object containing the binary cookie data.
|
|
128
|
+
Returns:
|
|
129
|
+
List[Cookie]: A list of Cookie objects.
|
|
130
|
+
"""
|
|
116
131
|
# Check if the file is empty
|
|
117
132
|
if bf.readable() and bf.read(1) == b"":
|
|
118
133
|
raise BinaryCookiesDecodeError("The file is empty.")
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from datetime import datetime
|
|
3
|
+
from enum import Enum
|
|
4
|
+
from sys import stdout
|
|
5
|
+
from typing import Callable, List, Type
|
|
6
|
+
|
|
7
|
+
from rich import print as rprint
|
|
8
|
+
|
|
9
|
+
from binarycookies._serialize import IS_PYDANTIC_V1
|
|
10
|
+
from binarycookies.models import Cookie
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class DateTimeEncoder(json.JSONEncoder):
|
|
14
|
+
def default(self, obj: Type) -> str:
|
|
15
|
+
if isinstance(obj, datetime):
|
|
16
|
+
return obj.isoformat()
|
|
17
|
+
return super().default(obj)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class OutputType(str, Enum):
|
|
21
|
+
json = "json"
|
|
22
|
+
ascii = "ascii"
|
|
23
|
+
netscape = "netscape"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _output_json(cookies: List[Cookie]):
|
|
27
|
+
"""Outputs cookies in JSON format."""
|
|
28
|
+
output = [cookie.dict() for cookie in cookies] if IS_PYDANTIC_V1 else [cookie.model_dump() for cookie in cookies]
|
|
29
|
+
json.dump(output, indent=2, cls=DateTimeEncoder, fp=stdout)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _output_ascii(cookies: List[Cookie]):
|
|
33
|
+
"""Outputs cookies in a human-readable ASCII format."""
|
|
34
|
+
for cookie in cookies:
|
|
35
|
+
rprint(f"Name: {cookie.name}")
|
|
36
|
+
rprint(f"Value: {cookie.value}")
|
|
37
|
+
rprint(f"URL: {cookie.url}")
|
|
38
|
+
rprint(f"Path: {cookie.path}")
|
|
39
|
+
rprint(f"Created: {cookie.create_datetime.isoformat()}")
|
|
40
|
+
rprint(f"Expires: {cookie.expiry_datetime.isoformat()}")
|
|
41
|
+
rprint(f"Flag: {cookie.flag.value}")
|
|
42
|
+
rprint("-" * 40)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _output_netscape(cookies: List[Cookie]):
|
|
46
|
+
"""Outputs cookies in Netscape cookie file format."""
|
|
47
|
+
# http://www.cookiecentral.com/faq/#3.5
|
|
48
|
+
print("# Netscape HTTP Cookie File")
|
|
49
|
+
for cookie in cookies:
|
|
50
|
+
expiry_ts = int(cookie.expiry_datetime.timestamp())
|
|
51
|
+
domain = cookie.url
|
|
52
|
+
print(
|
|
53
|
+
"%(domain)s\t%(flag)s\t%(path)s\t%(secure)s\t%(expiry)d\t%(name)s\t%(value)s"
|
|
54
|
+
% {
|
|
55
|
+
"domain": domain,
|
|
56
|
+
"flag": str(domain.startswith(".")).upper(),
|
|
57
|
+
"path": cookie.path,
|
|
58
|
+
"secure": str("Secure" in cookie.flag.value).upper(),
|
|
59
|
+
"expiry": expiry_ts,
|
|
60
|
+
"name": cookie.name,
|
|
61
|
+
"value": cookie.value,
|
|
62
|
+
}
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
OUTPUT_HANDLERS: dict[str, Callable[[List[Cookie]], None]] = {
|
|
67
|
+
OutputType.json: _output_json,
|
|
68
|
+
OutputType.ascii: _output_ascii,
|
|
69
|
+
OutputType.netscape: _output_netscape,
|
|
70
|
+
}
|
|
@@ -3,9 +3,13 @@ from io import BufferedWriter, BytesIO
|
|
|
3
3
|
from struct import pack
|
|
4
4
|
from typing import BinaryIO, Dict, List, Tuple, Union
|
|
5
5
|
|
|
6
|
+
from pydantic import __version__ as pydantic_version
|
|
7
|
+
|
|
6
8
|
from binarycookies._deserialize import FLAGS
|
|
7
9
|
from binarycookies.models import BcField, Cookie, CookieFields, FileFields, Format
|
|
8
10
|
|
|
11
|
+
IS_PYDANTIC_V1 = pydantic_version.startswith("1.")
|
|
12
|
+
|
|
9
13
|
CookiesCollection = Union[List[Dict], List[Cookie], Tuple[Dict], Tuple[Cookie], Cookie, Dict[str, str]]
|
|
10
14
|
|
|
11
15
|
|
|
@@ -82,9 +86,12 @@ def dumps(cookies: CookiesCollection) -> bytes:
|
|
|
82
86
|
bytes: The serialized binary cookies data.
|
|
83
87
|
"""
|
|
84
88
|
if isinstance(cookies, dict):
|
|
85
|
-
cookies = [Cookie.model_validate(cookies)]
|
|
89
|
+
cookies = [Cookie.parse_obj(cookies)] if IS_PYDANTIC_V1 else [Cookie.model_validate(cookies)]
|
|
86
90
|
elif isinstance(cookies, (list, tuple)):
|
|
87
|
-
|
|
91
|
+
if IS_PYDANTIC_V1:
|
|
92
|
+
cookies = [Cookie.parse_obj(cookie) for cookie in cookies]
|
|
93
|
+
else:
|
|
94
|
+
cookies = [Cookie.model_validate(cookie) for cookie in cookies]
|
|
88
95
|
elif isinstance(cookies, Cookie):
|
|
89
96
|
cookies = [cookies]
|
|
90
97
|
else:
|
|
@@ -6,7 +6,7 @@ from binarycookies._deserialize import load
|
|
|
6
6
|
from binarycookies.models import Cookie
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
@deprecated("read_binary_cookies_file is deprecated, use
|
|
9
|
+
@deprecated("read_binary_cookies_file is deprecated, use binarycookies.load instead.")
|
|
10
10
|
def read_binary_cookies_file(file_path: str) -> List[Cookie]:
|
|
11
11
|
with open(file_path, "rb") as f:
|
|
12
12
|
return load(f)
|
binarycookies-2.1.4/setup.py
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
from setuptools import setup
|
|
3
|
-
|
|
4
|
-
package_dir = \
|
|
5
|
-
{'': 'src'}
|
|
6
|
-
|
|
7
|
-
packages = \
|
|
8
|
-
['binarycookies']
|
|
9
|
-
|
|
10
|
-
package_data = \
|
|
11
|
-
{'': ['*']}
|
|
12
|
-
|
|
13
|
-
install_requires = \
|
|
14
|
-
['pydantic>=2.0.0,<3.0.0', 'typer>=0.12.3,<0.17.0']
|
|
15
|
-
|
|
16
|
-
entry_points = \
|
|
17
|
-
{'console_scripts': ['bcparser = binarycookies.__main__:main']}
|
|
18
|
-
|
|
19
|
-
setup_kwargs = {
|
|
20
|
-
'name': 'binarycookies',
|
|
21
|
-
'version': '2.1.4',
|
|
22
|
-
'description': 'Python Binary Cookies (de)serializer',
|
|
23
|
-
'long_description': '[](https://github.com/dan1elt0m/binary-cookies-reader/actions/workflows/test.yml)\n\n# Binary Cookies\n\nPython library and CLI tool for reading and writing binary cookies files.\n\n## Requirements\n\n- Python 3.9 or higher\n\n## Installation\n```bash \npip install binarycookies\n```\nIf you want to use the parser as CLI, it\'s recommended to use pipx to install the package in an isolated environment.\n```bash \npipx install binarycookies\n```\n\n## Basic Usage CLI\nAfter installation, you can use the command-line interface to read a binary cookies file:\n\n```bash\nbcparser <path_to_binary_cookies_file>\n```\nReplace <path_to_binary_cookies_file> with the path to the binary cookie file you want to read.\n\n### Basic Usage Python\n\n#### Deserialization\n\n```python\nimport binarycookies \n\nwith open("path/to/cookies.binarycookies", "rb") as f:\n cookies = binarycookies.load(f)\n```\n\n#### Serialization\n\n```python\nimport binarycookies \n\ncookie = {\n "name": "session_id",\n "value": "abc123",\n "url": "https://example.com",\n "path": "/",\n "create_datetime": "2023-10-01T12:34:56+00:00",\n "expiry_datetime": "2023-12-31T23:59:59+00:00",\n "flag": "Secure"\n}\n\nwith open("path/to/cookies.binarycookies", "wb") as f:\n binarycookies.dump(cookie, f)\n```\n\n## Output Types\n\nThe `bcparser` CLI supports two output types: `json` (default) and `ascii`.\n\n### JSON Output\n\nThe `json` output type formats the cookies as a JSON array, making it easy to parse and manipulate programmatically.\n\nExample usage:\n```sh\nbcparser path/to/cookies.binarycookies --output json\n```\n\nExample output JSON:\n```json\n[\n {\n "name": "session_id",\n "value": "abc123",\n "url": "https://example.com",\n "path": "/",\n "create_datetime": "2023-10-01T12:34:56+00:00",\n "expiry_datetime": "2023-12-31T23:59:59+00:00",\n "flag": "Secure"\n },\n {\n "name": "user_token",\n "value": "xyz789",\n "url": "https://example.com",\n "path": "/account",\n "create_datetime": "2023-10-01T12:34:56+00:00",\n "expiry_datetime": "2023-12-31T23:59:59+00:00",\n "flag": "HttpOnly"\n }\n]\n```\n\n### ASCII Output\nThe ascii output type formats the cookies in a simple, line-by-line text format, making it easy to read and pipe to other command-line tools.\n\nExample usage:\n```sh\nbcparser path/to/cookies.binarycookies --output ascii\n```\n\nExample output ASCII:\n```text\nName: session_id\nValue: abc123\nURL: https://example.com\nPath: /\nCreated: 2023-10-01T12:34:56+00:00\nExpires: 2023-12-31T23:59:59+00:00\nFlag: Secure\n----------------------------------------\nName: user_token\nValue: xyz789\nURL: https://example.com\nPath: /account\nCreated: 2023-10-01T12:34:56+00:00\nExpires: 2023-12-31T23:59:59+00:00\nFlag: HttpOnly\n----------------------------------------\n```\n\n### License\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n\n### Contributing\nContributions are welcome! If you find a bug or have a feature request, please open an issue on GitHub. Pull requests are also welcome.',
|
|
24
|
-
'author': 'Daniel Tom',
|
|
25
|
-
'author_email': 'd.e.tom89@gmail.com',
|
|
26
|
-
'maintainer': 'None',
|
|
27
|
-
'maintainer_email': 'None',
|
|
28
|
-
'url': 'None',
|
|
29
|
-
'package_dir': package_dir,
|
|
30
|
-
'packages': packages,
|
|
31
|
-
'package_data': package_data,
|
|
32
|
-
'install_requires': install_requires,
|
|
33
|
-
'entry_points': entry_points,
|
|
34
|
-
'python_requires': '>=3.8,<4.0',
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
setup(**setup_kwargs)
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import json
|
|
2
|
-
from datetime import datetime
|
|
3
|
-
from enum import Enum
|
|
4
|
-
from typing import Type
|
|
5
|
-
|
|
6
|
-
import typer
|
|
7
|
-
from rich import print
|
|
8
|
-
|
|
9
|
-
from binarycookies import load
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class DateTimeEncoder(json.JSONEncoder):
|
|
13
|
-
def default(self, obj: Type) -> str:
|
|
14
|
-
if isinstance(obj, datetime):
|
|
15
|
-
return obj.isoformat()
|
|
16
|
-
return super().default(obj)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
class OutputType(str, Enum):
|
|
20
|
-
json = "json"
|
|
21
|
-
ascii = "ascii"
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
def cli(file_path: str, output: str = "json"):
|
|
25
|
-
"""CLI entrypoint for reading Binary Cookies"""
|
|
26
|
-
with open(file_path, "rb") as f:
|
|
27
|
-
cookies = load(f)
|
|
28
|
-
if output == OutputType.json:
|
|
29
|
-
print(json.dumps([cookie.model_dump() for cookie in cookies], indent=2, cls=DateTimeEncoder))
|
|
30
|
-
elif output == OutputType.ascii:
|
|
31
|
-
for cookie in cookies:
|
|
32
|
-
print(f"Name: {cookie.name}")
|
|
33
|
-
print(f"Value: {cookie.value}")
|
|
34
|
-
print(f"URL: {cookie.url}")
|
|
35
|
-
print(f"Path: {cookie.path}")
|
|
36
|
-
print(f"Created: {cookie.create_datetime.isoformat()}")
|
|
37
|
-
print(f"Expires: {cookie.expiry_datetime.isoformat()}")
|
|
38
|
-
print(f"Flag: {cookie.flag.value}")
|
|
39
|
-
print("-" * 40)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
def main():
|
|
43
|
-
"""CLI entrypoint for reading Binary Cookies"""
|
|
44
|
-
typer.run(cli)
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if __name__ == "__main__":
|
|
48
|
-
main()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|