noodelapi 0.1.0__py3-none-any.whl
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.
- noodelapi/__init__.py +3 -0
- noodelapi/__main__.py +4 -0
- noodelapi/browser.py +219 -0
- noodelapi/cli.py +490 -0
- noodelapi/client.py +500 -0
- noodelapi/config.py +182 -0
- noodelapi/hash.py +242 -0
- noodelapi/pow.py +28 -0
- noodelapi/server.py +573 -0
- noodelapi-0.1.0.dist-info/METADATA +105 -0
- noodelapi-0.1.0.dist-info/RECORD +15 -0
- noodelapi-0.1.0.dist-info/WHEEL +5 -0
- noodelapi-0.1.0.dist-info/entry_points.txt +2 -0
- noodelapi-0.1.0.dist-info/licenses/LICENSE +21 -0
- noodelapi-0.1.0.dist-info/top_level.txt +1 -0
noodelapi/__init__.py
ADDED
noodelapi/__main__.py
ADDED
noodelapi/browser.py
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import re
|
|
5
|
+
import sys
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Iterable, List, Optional
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass
|
|
12
|
+
class BrowserToken:
|
|
13
|
+
profile: Path
|
|
14
|
+
token: str
|
|
15
|
+
modified: float
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _varint(buf: bytes, index: int):
|
|
19
|
+
value = 0
|
|
20
|
+
shift = 0
|
|
21
|
+
while index < len(buf) and shift < 64:
|
|
22
|
+
byte = buf[index]
|
|
23
|
+
index += 1
|
|
24
|
+
value |= (byte & 0x7F) << shift
|
|
25
|
+
if not (byte & 0x80):
|
|
26
|
+
return value, index
|
|
27
|
+
shift += 7
|
|
28
|
+
return None, index
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _snappy_decode(buf: bytes, start: int, limit: int = 4_000_000) -> Optional[bytes]:
|
|
32
|
+
"""Decode a raw Snappy block at ``start``. Returns None if it is not valid."""
|
|
33
|
+
length, pos = _varint(buf, start)
|
|
34
|
+
if not length or length <= 0 or length > limit:
|
|
35
|
+
return None
|
|
36
|
+
out = bytearray()
|
|
37
|
+
while len(out) < length and pos < len(buf):
|
|
38
|
+
tag = buf[pos]
|
|
39
|
+
pos += 1
|
|
40
|
+
kind = tag & 3
|
|
41
|
+
if kind == 0:
|
|
42
|
+
size = tag >> 2
|
|
43
|
+
if size < 60:
|
|
44
|
+
size += 1
|
|
45
|
+
else:
|
|
46
|
+
extra = size - 59
|
|
47
|
+
if pos + extra > len(buf):
|
|
48
|
+
return None
|
|
49
|
+
size = int.from_bytes(buf[pos : pos + extra], "little") + 1
|
|
50
|
+
pos += extra
|
|
51
|
+
if pos + size > len(buf) or size > limit:
|
|
52
|
+
return None
|
|
53
|
+
out += buf[pos : pos + size]
|
|
54
|
+
pos += size
|
|
55
|
+
else:
|
|
56
|
+
if kind == 1:
|
|
57
|
+
if pos >= len(buf):
|
|
58
|
+
return None
|
|
59
|
+
size = 4 + ((tag >> 2) & 7)
|
|
60
|
+
offset = ((tag & 0xE0) << 3) | buf[pos]
|
|
61
|
+
pos += 1
|
|
62
|
+
elif kind == 2:
|
|
63
|
+
if pos + 2 > len(buf):
|
|
64
|
+
return None
|
|
65
|
+
size = (tag >> 2) + 1
|
|
66
|
+
offset = int.from_bytes(buf[pos : pos + 2], "little")
|
|
67
|
+
pos += 2
|
|
68
|
+
else:
|
|
69
|
+
if pos + 4 > len(buf):
|
|
70
|
+
return None
|
|
71
|
+
size = (tag >> 2) + 1
|
|
72
|
+
offset = int.from_bytes(buf[pos : pos + 4], "little")
|
|
73
|
+
pos += 4
|
|
74
|
+
if offset == 0 or offset > len(out):
|
|
75
|
+
return None
|
|
76
|
+
for _ in range(size):
|
|
77
|
+
out.append(out[-offset])
|
|
78
|
+
return bytes(out) if len(out) == length else None
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
_TOKEN_RE = re.compile(rb"userToken.{0,300}?\"value\":\"([^\"]+)\"", re.S)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _extract_token_from_blob(blob: bytes) -> Optional[str]:
|
|
85
|
+
match = _TOKEN_RE.search(blob)
|
|
86
|
+
if match:
|
|
87
|
+
try:
|
|
88
|
+
value = match.group(1).decode("utf-8")
|
|
89
|
+
except UnicodeDecodeError:
|
|
90
|
+
return None
|
|
91
|
+
if value and len(value) < 4096:
|
|
92
|
+
return value
|
|
93
|
+
return None
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _read_file(path: Path) -> Optional[bytes]:
|
|
97
|
+
"""Read a browser database, copying it first if the OS locks it."""
|
|
98
|
+
try:
|
|
99
|
+
return path.read_bytes()
|
|
100
|
+
except OSError:
|
|
101
|
+
pass
|
|
102
|
+
try:
|
|
103
|
+
import shutil
|
|
104
|
+
import tempfile
|
|
105
|
+
|
|
106
|
+
target = Path(tempfile.gettempdir()) / f"noodelapi-{path.name}"
|
|
107
|
+
shutil.copyfile(path, target)
|
|
108
|
+
data = target.read_bytes()
|
|
109
|
+
try:
|
|
110
|
+
target.unlink()
|
|
111
|
+
except OSError:
|
|
112
|
+
pass
|
|
113
|
+
return data
|
|
114
|
+
except OSError:
|
|
115
|
+
return None
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def extract_token_from_leveldb(leveldb: Path) -> Optional[str]:
|
|
119
|
+
files = sorted(
|
|
120
|
+
[p for p in leveldb.iterdir() if p.suffix in {".ldb", ".log"} and p.is_file()],
|
|
121
|
+
key=lambda p: p.stat().st_mtime,
|
|
122
|
+
reverse=True,
|
|
123
|
+
)
|
|
124
|
+
for path in files:
|
|
125
|
+
data = _read_file(path)
|
|
126
|
+
if not data:
|
|
127
|
+
continue
|
|
128
|
+
|
|
129
|
+
token = _extract_token_from_blob(data)
|
|
130
|
+
if token:
|
|
131
|
+
return token
|
|
132
|
+
|
|
133
|
+
# LevelDB table blocks are Snappy-compressed. The key is near the start
|
|
134
|
+
# of the block, so only try starts shortly before each occurrence.
|
|
135
|
+
for match in re.finditer(b"userToken", data):
|
|
136
|
+
begin = max(0, match.start() - 256)
|
|
137
|
+
for start in range(begin, match.start() + 1):
|
|
138
|
+
decoded = _snappy_decode(data, start)
|
|
139
|
+
if not decoded or b"userToken" not in decoded:
|
|
140
|
+
continue
|
|
141
|
+
token = _extract_token_from_blob(decoded)
|
|
142
|
+
if token:
|
|
143
|
+
return token
|
|
144
|
+
return None
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _profile_roots() -> List[Path]:
|
|
148
|
+
home = Path.home()
|
|
149
|
+
override = os.environ.get("NOODELAPI_BROWSER_PROFILE")
|
|
150
|
+
if override:
|
|
151
|
+
return [Path(override).expanduser()]
|
|
152
|
+
|
|
153
|
+
if sys.platform == "darwin":
|
|
154
|
+
base = home / "Library" / "Application Support"
|
|
155
|
+
return [
|
|
156
|
+
base / "BraveSoftware" / "Brave-Browser",
|
|
157
|
+
base / "Google" / "Chrome",
|
|
158
|
+
base / "Microsoft Edge",
|
|
159
|
+
base / "Chromium",
|
|
160
|
+
base / "Arc",
|
|
161
|
+
base / "Vivaldi",
|
|
162
|
+
]
|
|
163
|
+
|
|
164
|
+
if sys.platform == "win32":
|
|
165
|
+
local = Path(os.environ.get("LOCALAPPDATA") or (Path.home() / "AppData" / "Local"))
|
|
166
|
+
return [
|
|
167
|
+
local / "BraveSoftware" / "Brave-Browser" / "User Data",
|
|
168
|
+
local / "Google" / "Chrome" / "User Data",
|
|
169
|
+
local / "Microsoft" / "Edge" / "User Data",
|
|
170
|
+
local / "Chromium" / "User Data",
|
|
171
|
+
local / "Vivaldi" / "User Data",
|
|
172
|
+
local / "Arc" / "User Data",
|
|
173
|
+
]
|
|
174
|
+
|
|
175
|
+
base = Path(os.environ.get("XDG_CONFIG_HOME", home / ".config"))
|
|
176
|
+
return [
|
|
177
|
+
base / "BraveSoftware" / "Brave-Browser",
|
|
178
|
+
base / "google-chrome",
|
|
179
|
+
base / "microsoft-edge",
|
|
180
|
+
base / "chromium",
|
|
181
|
+
base / "vivaldi",
|
|
182
|
+
]
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def _leveldb_dirs(root: Path) -> Iterable[Path]:
|
|
186
|
+
if root.name == "leveldb" and root.is_dir():
|
|
187
|
+
yield root
|
|
188
|
+
return
|
|
189
|
+
if not root.is_dir():
|
|
190
|
+
return
|
|
191
|
+
direct = root / "Local Storage" / "leveldb"
|
|
192
|
+
if direct.is_dir():
|
|
193
|
+
yield direct
|
|
194
|
+
try:
|
|
195
|
+
for child in root.iterdir():
|
|
196
|
+
if child.is_dir():
|
|
197
|
+
candidate = child / "Local Storage" / "leveldb"
|
|
198
|
+
if candidate.is_dir():
|
|
199
|
+
yield candidate
|
|
200
|
+
except OSError:
|
|
201
|
+
return
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def find_browser_tokens() -> List[BrowserToken]:
|
|
205
|
+
found: List[BrowserToken] = []
|
|
206
|
+
seen = set()
|
|
207
|
+
for root in _profile_roots():
|
|
208
|
+
for leveldb in _leveldb_dirs(root):
|
|
209
|
+
token = extract_token_from_leveldb(leveldb)
|
|
210
|
+
if not token or token in seen:
|
|
211
|
+
continue
|
|
212
|
+
seen.add(token)
|
|
213
|
+
try:
|
|
214
|
+
modified = max(p.stat().st_mtime for p in leveldb.iterdir() if p.is_file())
|
|
215
|
+
except OSError:
|
|
216
|
+
modified = 0.0
|
|
217
|
+
found.append(BrowserToken(profile=leveldb, token=token, modified=modified))
|
|
218
|
+
found.sort(key=lambda item: item.modified, reverse=True)
|
|
219
|
+
return found
|