simple_keenetic_client 0.1.1__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.
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Leonid Amirov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: simple_keenetic_client
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Client for KeeneticOS-based routers via HTTP interface
|
|
5
|
+
License: MIT
|
|
6
|
+
Author: Leonid Amirov
|
|
7
|
+
Author-email: leonid.amirov@gmail.com
|
|
8
|
+
Requires-Python: >=3.11,<4.0
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Requires-Dist: aiohttp (>=3.11.7,<4.0.0)
|
|
14
|
+
Project-URL: repository, https://github.com/side2k/simple-keenetic-client
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# Simple Keenetic client
|
|
18
|
+
|
|
19
|
+
HTTP client for KeeneticOS-based routers. Uses web interface to connect.
|
|
20
|
+
Does not do more than authenticate, fetch SMS messages and mark them as read or delete them.
|
|
21
|
+
|
|
22
|
+
Example usage:
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from simple_keenetic_client import SimpleKeeneticClient
|
|
26
|
+
|
|
27
|
+
async def get_unread_sms():
|
|
28
|
+
async with SimpleKeeneticClient(
|
|
29
|
+
"http://keenetic-router.test",
|
|
30
|
+
username="testuser",
|
|
31
|
+
password="testpassword",
|
|
32
|
+
) as client:
|
|
33
|
+
interfaces = await client.get_mobile_interfaces()
|
|
34
|
+
|
|
35
|
+
return await self.get_unread_sms(
|
|
36
|
+
interface_names=interfaces.keys()
|
|
37
|
+
)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
"Real life" usage example: https://github.com/side2k/keensms2mqtt
|
|
41
|
+
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Simple Keenetic client
|
|
2
|
+
|
|
3
|
+
HTTP client for KeeneticOS-based routers. Uses web interface to connect.
|
|
4
|
+
Does not do more than authenticate, fetch SMS messages and mark them as read or delete them.
|
|
5
|
+
|
|
6
|
+
Example usage:
|
|
7
|
+
|
|
8
|
+
```python
|
|
9
|
+
from simple_keenetic_client import SimpleKeeneticClient
|
|
10
|
+
|
|
11
|
+
async def get_unread_sms():
|
|
12
|
+
async with SimpleKeeneticClient(
|
|
13
|
+
"http://keenetic-router.test",
|
|
14
|
+
username="testuser",
|
|
15
|
+
password="testpassword",
|
|
16
|
+
) as client:
|
|
17
|
+
interfaces = await client.get_mobile_interfaces()
|
|
18
|
+
|
|
19
|
+
return await self.get_unread_sms(
|
|
20
|
+
interface_names=interfaces.keys()
|
|
21
|
+
)
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
"Real life" usage example: https://github.com/side2k/keensms2mqtt
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "simple_keenetic_client"
|
|
3
|
+
version = "0.1.1"
|
|
4
|
+
description = "Client for KeeneticOS-based routers via HTTP interface"
|
|
5
|
+
authors = ["Leonid Amirov <leonid.amirov@gmail.com>"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
|
|
9
|
+
[tool.poetry.dependencies]
|
|
10
|
+
python = "^3.11"
|
|
11
|
+
aiohttp = "^3.11.7"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
[tool.poetry.group.dev.dependencies]
|
|
15
|
+
black = "^24.10.0"
|
|
16
|
+
flake8 = "^7.1.1"
|
|
17
|
+
isort = "^5.13.2"
|
|
18
|
+
pytest = "^8.3.3"
|
|
19
|
+
pytest-asyncio = "^0.24.0"
|
|
20
|
+
pytest-mock = "^3.14.0"
|
|
21
|
+
|
|
22
|
+
[tool.poetry.urls]
|
|
23
|
+
repository = "https://github.com/side2k/simple-keenetic-client"
|
|
24
|
+
|
|
25
|
+
[build-system]
|
|
26
|
+
requires = ["poetry-core"]
|
|
27
|
+
build-backend = "poetry.core.masonry.api"
|
|
28
|
+
|
|
29
|
+
[tool.black]
|
|
30
|
+
line-length = 95
|
|
31
|
+
target-version = ['py311']
|
|
32
|
+
exclude = '''
|
|
33
|
+
/(
|
|
34
|
+
\.eggs
|
|
35
|
+
| \.git
|
|
36
|
+
| \.mypy_cache
|
|
37
|
+
| \.venv
|
|
38
|
+
| _build
|
|
39
|
+
| build
|
|
40
|
+
| dist
|
|
41
|
+
)/
|
|
42
|
+
'''
|
|
43
|
+
|
|
44
|
+
[tool.isort]
|
|
45
|
+
profile = 'black'
|
|
46
|
+
line_length = 95
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import logging
|
|
3
|
+
from hashlib import md5, sha256
|
|
4
|
+
from urllib.parse import urljoin
|
|
5
|
+
|
|
6
|
+
import aiohttp
|
|
7
|
+
|
|
8
|
+
logger = logging.getLogger(__name__)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class SimpleKeeneticClient:
|
|
12
|
+
is_authenticated = False
|
|
13
|
+
|
|
14
|
+
def __init__(self, base_url, username, password):
|
|
15
|
+
self._base_url = base_url
|
|
16
|
+
self._username = username
|
|
17
|
+
self._password = password
|
|
18
|
+
|
|
19
|
+
async def __aenter__(self):
|
|
20
|
+
await self.connect()
|
|
21
|
+
return self
|
|
22
|
+
|
|
23
|
+
async def __aexit__(self, exc_type, exc_value, traceback):
|
|
24
|
+
logger.warning("Exiting keenetic loop")
|
|
25
|
+
await self.close()
|
|
26
|
+
|
|
27
|
+
async def connect(self):
|
|
28
|
+
logger.debug("Connecting to {self._base_url}")
|
|
29
|
+
self._session = aiohttp.ClientSession()
|
|
30
|
+
await self.login(self._username, self._password)
|
|
31
|
+
|
|
32
|
+
async def close(self):
|
|
33
|
+
if self.is_authenticated:
|
|
34
|
+
await self.logout()
|
|
35
|
+
await self._session.close()
|
|
36
|
+
|
|
37
|
+
def get_url(self, path):
|
|
38
|
+
return urljoin(self._base_url, path)
|
|
39
|
+
|
|
40
|
+
def get_headers(self, extra_headers):
|
|
41
|
+
headers = {"Content-Type": "application/json"}
|
|
42
|
+
if extra_headers:
|
|
43
|
+
headers.update(extra_headers)
|
|
44
|
+
|
|
45
|
+
return headers
|
|
46
|
+
|
|
47
|
+
async def get(self, path, extra_headers=None):
|
|
48
|
+
return await self._session.get(
|
|
49
|
+
self.get_url(path), headers=self.get_headers(extra_headers)
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
async def post(self, path, data, extra_headers=None):
|
|
53
|
+
url = self.get_url(path)
|
|
54
|
+
serialized_data = json.dumps(data) if data else None
|
|
55
|
+
logger.debug(f"POST {url}: {serialized_data}")
|
|
56
|
+
return await self._session.post(
|
|
57
|
+
url,
|
|
58
|
+
data=serialized_data,
|
|
59
|
+
headers=self.get_headers(extra_headers),
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
def prepare_auth_data(self, username, password, realm, challenge):
|
|
63
|
+
hash1 = md5(f"{username}:{realm}:{password}".encode("utf-8"))
|
|
64
|
+
hash2 = sha256(f"{challenge}{hash1.hexdigest()}".encode("utf-8"))
|
|
65
|
+
|
|
66
|
+
return {"login": username, "password": hash2.hexdigest()}
|
|
67
|
+
|
|
68
|
+
async def login(self, username, password):
|
|
69
|
+
challenge_response = await self.get("/auth")
|
|
70
|
+
auth_params = challenge_response.headers.get("WWW-Authenticate")
|
|
71
|
+
if not auth_params:
|
|
72
|
+
raise Exception(
|
|
73
|
+
"No 'WWW-Authenticate' header in /auth response - is it really a "
|
|
74
|
+
"Keenetic device?"
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
[auth_method, auth_method_params] = auth_params.split(" ", maxsplit=1)
|
|
78
|
+
if auth_method != "x-ndw2-interactive":
|
|
79
|
+
raise Exception(f"This client doesn't support {auth_method=} (hopefully, yet!)")
|
|
80
|
+
|
|
81
|
+
realm = challenge_response.headers.get("X-NDM-Realm")
|
|
82
|
+
challenge = challenge_response.headers.get("X-NDM-Challenge")
|
|
83
|
+
|
|
84
|
+
auth_data = self.prepare_auth_data(username, password, realm, challenge)
|
|
85
|
+
|
|
86
|
+
auth_response = await self.post("/auth", data=auth_data)
|
|
87
|
+
auth_response.raise_for_status()
|
|
88
|
+
|
|
89
|
+
control_response = await self.get("/auth")
|
|
90
|
+
|
|
91
|
+
control_response.raise_for_status()
|
|
92
|
+
self.is_authenticated = True
|
|
93
|
+
|
|
94
|
+
async def logout(self):
|
|
95
|
+
url = self.get_url("/auth")
|
|
96
|
+
logger.debug(f"DELETE {url}")
|
|
97
|
+
response = await self._session.delete(url)
|
|
98
|
+
response.raise_for_status()
|
|
99
|
+
self.is_authenticated = False
|
|
100
|
+
|
|
101
|
+
async def rci_interface(self):
|
|
102
|
+
response = await self.get("/rci/interface")
|
|
103
|
+
response.raise_for_status()
|
|
104
|
+
return await response.json()
|
|
105
|
+
|
|
106
|
+
async def get_interfaces(self):
|
|
107
|
+
response = await self.post("/rci/", data={"show": {"interface": {}}})
|
|
108
|
+
response.raise_for_status()
|
|
109
|
+
return await response.json()
|
|
110
|
+
|
|
111
|
+
def _is_mobile_interface(self, if_data):
|
|
112
|
+
return "Mobile" in if_data.get("traits", [])
|
|
113
|
+
|
|
114
|
+
async def get_mobile_interfaces(self):
|
|
115
|
+
rci_data = await self.get_interfaces()
|
|
116
|
+
return {
|
|
117
|
+
if_name: if_data
|
|
118
|
+
for if_name, if_data in rci_data["show"]["interface"].items()
|
|
119
|
+
if self._is_mobile_interface(if_data)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async def get_sms_by_interface(self, interface_name):
|
|
123
|
+
response = await self.post(
|
|
124
|
+
"/rci/", data={"sms": {"interface": interface_name, "list": {}}}
|
|
125
|
+
)
|
|
126
|
+
return await response.json()
|
|
127
|
+
|
|
128
|
+
async def mark_sms_as_read(self, interface_name: str, msg_ids: list[str]):
|
|
129
|
+
response = await self.post(
|
|
130
|
+
"/rci/",
|
|
131
|
+
data={
|
|
132
|
+
"sms": {
|
|
133
|
+
"interface": interface_name,
|
|
134
|
+
"read": [{"id": msg_id} for msg_id in msg_ids],
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
)
|
|
138
|
+
return await response.json()
|
|
139
|
+
|
|
140
|
+
async def delete_sms(self, interface_name: str, msg_ids: list[str]):
|
|
141
|
+
response = await self.post(
|
|
142
|
+
"/rci/",
|
|
143
|
+
data={
|
|
144
|
+
"sms": {
|
|
145
|
+
"interface": interface_name,
|
|
146
|
+
"delete": [{"id": msg_id} for msg_id in msg_ids],
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
)
|
|
150
|
+
return await response.json()
|