goosebit 0.1.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.
- goosebit-0.1.0/LICENSE +201 -0
- goosebit-0.1.0/PKG-INFO +37 -0
- goosebit-0.1.0/README.md +16 -0
- goosebit-0.1.0/goosebit/__init__.py +62 -0
- goosebit-0.1.0/goosebit/api/__init__.py +1 -0
- goosebit-0.1.0/goosebit/api/devices.py +112 -0
- goosebit-0.1.0/goosebit/api/download.py +20 -0
- goosebit-0.1.0/goosebit/api/firmware.py +64 -0
- goosebit-0.1.0/goosebit/api/routes.py +11 -0
- goosebit-0.1.0/goosebit/auth/__init__.py +123 -0
- goosebit-0.1.0/goosebit/db.py +32 -0
- goosebit-0.1.0/goosebit/models.py +21 -0
- goosebit-0.1.0/goosebit/permissions.py +55 -0
- goosebit-0.1.0/goosebit/realtime/__init__.py +1 -0
- goosebit-0.1.0/goosebit/realtime/logs.py +43 -0
- goosebit-0.1.0/goosebit/realtime/routes.py +13 -0
- goosebit-0.1.0/goosebit/settings.py +55 -0
- goosebit-0.1.0/goosebit/ui/__init__.py +1 -0
- goosebit-0.1.0/goosebit/ui/routes.py +104 -0
- goosebit-0.1.0/goosebit/ui/static/__init__.py +5 -0
- goosebit-0.1.0/goosebit/ui/static/favicon.ico +0 -0
- goosebit-0.1.0/goosebit/ui/static/favicon.svg +1 -0
- goosebit-0.1.0/goosebit/ui/static/js/devices.js +370 -0
- goosebit-0.1.0/goosebit/ui/static/js/firmware.js +131 -0
- goosebit-0.1.0/goosebit/ui/static/js/index.js +161 -0
- goosebit-0.1.0/goosebit/ui/static/js/logs.js +18 -0
- goosebit-0.1.0/goosebit/ui/static/svg/goosebit-logo.svg +1 -0
- goosebit-0.1.0/goosebit/ui/templates/__init__.py +5 -0
- goosebit-0.1.0/goosebit/ui/templates/devices.html +82 -0
- goosebit-0.1.0/goosebit/ui/templates/firmware.html +47 -0
- goosebit-0.1.0/goosebit/ui/templates/index.html +37 -0
- goosebit-0.1.0/goosebit/ui/templates/login.html +34 -0
- goosebit-0.1.0/goosebit/ui/templates/logs.html +21 -0
- goosebit-0.1.0/goosebit/ui/templates/nav.html +64 -0
- goosebit-0.1.0/goosebit/updater/__init__.py +1 -0
- goosebit-0.1.0/goosebit/updater/controller/__init__.py +1 -0
- goosebit-0.1.0/goosebit/updater/controller/routes.py +6 -0
- goosebit-0.1.0/goosebit/updater/controller/v1/__init__.py +1 -0
- goosebit-0.1.0/goosebit/updater/controller/v1/routes.py +92 -0
- goosebit-0.1.0/goosebit/updater/download/__init__.py +1 -0
- goosebit-0.1.0/goosebit/updater/download/routes.py +6 -0
- goosebit-0.1.0/goosebit/updater/download/v1/__init__.py +1 -0
- goosebit-0.1.0/goosebit/updater/download/v1/routes.py +26 -0
- goosebit-0.1.0/goosebit/updater/manager.py +206 -0
- goosebit-0.1.0/goosebit/updater/misc.py +69 -0
- goosebit-0.1.0/goosebit/updater/routes.py +30 -0
- goosebit-0.1.0/goosebit/updater/updates.py +93 -0
- goosebit-0.1.0/pyproject.toml +36 -0
goosebit-0.1.0/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
goosebit-0.1.0/PKG-INFO
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: goosebit
|
3
|
+
Version: 0.1.0
|
4
|
+
Summary:
|
5
|
+
Author: Upstream Data
|
6
|
+
Author-email: brett@upstreamdata.ca
|
7
|
+
Requires-Python: >=3.12,<4.0
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
9
|
+
Classifier: Programming Language :: Python :: 3.12
|
10
|
+
Requires-Dist: aerich (>=0.7.2,<0.8.0)
|
11
|
+
Requires-Dist: aiofiles (>=24.1.0,<25.0.0)
|
12
|
+
Requires-Dist: fastapi[uvicorn] (>=0.111.0,<0.112.0)
|
13
|
+
Requires-Dist: itsdangerous (>=2.2.0,<3.0.0)
|
14
|
+
Requires-Dist: jinja2 (>=3.1.4,<4.0.0)
|
15
|
+
Requires-Dist: passlib[bcrypt] (>=1.7.4,<2.0.0)
|
16
|
+
Requires-Dist: python-jose (>=3.3.0,<4.0.0)
|
17
|
+
Requires-Dist: python-multipart (>=0.0.9,<0.0.10)
|
18
|
+
Requires-Dist: tortoise-orm (>=0.21.4,<0.22.0)
|
19
|
+
Requires-Dist: websockets (>=12.0,<13.0)
|
20
|
+
Description-Content-Type: text/markdown
|
21
|
+
|
22
|
+
# goosebit
|
23
|
+
<img src="docs/img/goosebit-logo.png?raw=true" style="width: 100px; height: 100px; display: block;">
|
24
|
+
|
25
|
+
---
|
26
|
+
|
27
|
+
A simplistic remote update server.
|
28
|
+
|
29
|
+
|
30
|
+
## Setup
|
31
|
+
|
32
|
+
To set up, install the dependencies in `pyproject.toml` with `poetry install`. Then you can run GooseBit by running `main.py`.
|
33
|
+
|
34
|
+
## Initial Startup
|
35
|
+
|
36
|
+
The first time you start GooseBit, you should change the default username and password inside `goosebit/settings.py`, as well as generate a new secret key.
|
37
|
+
The default login credentials for testing are `admin@goosebit.local`, `admin`.
|
goosebit-0.1.0/README.md
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# goosebit
|
2
|
+
<img src="docs/img/goosebit-logo.png?raw=true" style="width: 100px; height: 100px; display: block;">
|
3
|
+
|
4
|
+
---
|
5
|
+
|
6
|
+
A simplistic remote update server.
|
7
|
+
|
8
|
+
|
9
|
+
## Setup
|
10
|
+
|
11
|
+
To set up, install the dependencies in `pyproject.toml` with `poetry install`. Then you can run GooseBit by running `main.py`.
|
12
|
+
|
13
|
+
## Initial Startup
|
14
|
+
|
15
|
+
The first time you start GooseBit, you should change the default username and password inside `goosebit/settings.py`, as well as generate a new secret key.
|
16
|
+
The default login credentials for testing are `admin@goosebit.local`, `admin`.
|
@@ -0,0 +1,62 @@
|
|
1
|
+
from contextlib import asynccontextmanager
|
2
|
+
from typing import Annotated
|
3
|
+
|
4
|
+
from fastapi import Depends, FastAPI
|
5
|
+
from fastapi.requests import Request
|
6
|
+
from fastapi.responses import RedirectResponse
|
7
|
+
from fastapi.security import OAuth2PasswordRequestForm
|
8
|
+
|
9
|
+
from goosebit import api, db, realtime, ui, updater
|
10
|
+
from goosebit.auth import (
|
11
|
+
authenticate_user,
|
12
|
+
auto_redirect,
|
13
|
+
create_session,
|
14
|
+
get_current_user,
|
15
|
+
)
|
16
|
+
from goosebit.ui.static import static
|
17
|
+
from goosebit.ui.templates import templates
|
18
|
+
|
19
|
+
|
20
|
+
@asynccontextmanager
|
21
|
+
async def lifespan(_: FastAPI):
|
22
|
+
await db.init()
|
23
|
+
yield
|
24
|
+
await db.close()
|
25
|
+
|
26
|
+
|
27
|
+
app = FastAPI(lifespan=lifespan)
|
28
|
+
app.include_router(updater.router)
|
29
|
+
app.include_router(ui.router)
|
30
|
+
app.include_router(api.router)
|
31
|
+
app.include_router(realtime.router)
|
32
|
+
app.mount("/static", static, name="static")
|
33
|
+
|
34
|
+
|
35
|
+
@app.middleware("http")
|
36
|
+
async def attach_user(request: Request, call_next):
|
37
|
+
request.scope["user"] = get_current_user(request)
|
38
|
+
return await call_next(request)
|
39
|
+
|
40
|
+
|
41
|
+
@app.get("/", include_in_schema=False)
|
42
|
+
def root_redirect(request: Request):
|
43
|
+
return RedirectResponse(request.url_for("ui_root"))
|
44
|
+
|
45
|
+
|
46
|
+
@app.get("/login", dependencies=[Depends(auto_redirect)], include_in_schema=False)
|
47
|
+
async def login_ui(request: Request):
|
48
|
+
return templates.TemplateResponse("login.html", context={"request": request})
|
49
|
+
|
50
|
+
|
51
|
+
@app.post("/login", include_in_schema=False, dependencies=[Depends(authenticate_user)])
|
52
|
+
async def login(form_data: Annotated[OAuth2PasswordRequestForm, Depends()]):
|
53
|
+
resp = RedirectResponse("/ui/home", status_code=302)
|
54
|
+
resp.set_cookie(key="session_id", value=create_session(form_data.username))
|
55
|
+
return resp
|
56
|
+
|
57
|
+
|
58
|
+
@app.get("/logout", include_in_schema=False)
|
59
|
+
async def logout(request: Request):
|
60
|
+
resp = RedirectResponse("/login", status_code=302)
|
61
|
+
resp.delete_cookie(key="session_id")
|
62
|
+
return resp
|
@@ -0,0 +1 @@
|
|
1
|
+
from .routes import router
|
@@ -0,0 +1,112 @@
|
|
1
|
+
from __future__ import annotations
|
2
|
+
|
3
|
+
import asyncio
|
4
|
+
import time
|
5
|
+
|
6
|
+
from fastapi import APIRouter, Security
|
7
|
+
from fastapi.requests import Request
|
8
|
+
from pydantic import BaseModel
|
9
|
+
|
10
|
+
from goosebit.auth import validate_user_permissions
|
11
|
+
from goosebit.models import Device
|
12
|
+
from goosebit.permissions import Permissions
|
13
|
+
from goosebit.updater.manager import delete_device, get_update_manager
|
14
|
+
from goosebit.updater.misc import get_device_by_uuid
|
15
|
+
|
16
|
+
router = APIRouter(prefix="/devices")
|
17
|
+
|
18
|
+
|
19
|
+
@router.get(
|
20
|
+
"/all",
|
21
|
+
dependencies=[Security(validate_user_permissions, scopes=[Permissions.HOME.READ])],
|
22
|
+
)
|
23
|
+
async def devices_get_all() -> list[dict]:
|
24
|
+
devices = await Device.all()
|
25
|
+
|
26
|
+
async def parse(device: Device) -> dict:
|
27
|
+
manager = await get_update_manager(device.uuid)
|
28
|
+
last_seen = device.last_seen
|
29
|
+
if last_seen is not None:
|
30
|
+
last_seen = round(time.time() - device.last_seen)
|
31
|
+
return {
|
32
|
+
"uuid": device.uuid,
|
33
|
+
"web_pwd": device.web_pwd,
|
34
|
+
"name": device.name,
|
35
|
+
"fw": device.fw_version,
|
36
|
+
"fw_file": device.fw_file,
|
37
|
+
"state": device.last_state,
|
38
|
+
"force_update": manager.force_update,
|
39
|
+
"last_ip": device.last_ip,
|
40
|
+
"last_seen": last_seen,
|
41
|
+
"online": last_seen < 120 if last_seen is not None else None,
|
42
|
+
}
|
43
|
+
|
44
|
+
return list(await asyncio.gather(*[parse(d) for d in devices]))
|
45
|
+
|
46
|
+
|
47
|
+
class UpdateDevicesModel(BaseModel):
|
48
|
+
devices: list[str]
|
49
|
+
firmware: str | None = None
|
50
|
+
name: str | None = None
|
51
|
+
|
52
|
+
|
53
|
+
@router.post(
|
54
|
+
"/update",
|
55
|
+
dependencies=[
|
56
|
+
Security(validate_user_permissions, scopes=[Permissions.DEVICE.WRITE])
|
57
|
+
],
|
58
|
+
)
|
59
|
+
async def devices_update(request: Request, config: UpdateDevicesModel) -> dict:
|
60
|
+
for uuid in config.devices:
|
61
|
+
updater = await get_update_manager(uuid)
|
62
|
+
device = await updater.get_device()
|
63
|
+
if config.firmware is not None:
|
64
|
+
device.fw_file = config.firmware
|
65
|
+
if config.name is not None:
|
66
|
+
device.name = config.name
|
67
|
+
await updater.save()
|
68
|
+
return {"success": True}
|
69
|
+
|
70
|
+
|
71
|
+
class ForceUpdateModel(BaseModel):
|
72
|
+
devices: list[str]
|
73
|
+
|
74
|
+
|
75
|
+
@router.post(
|
76
|
+
"/force_update",
|
77
|
+
dependencies=[
|
78
|
+
Security(validate_user_permissions, scopes=[Permissions.DEVICE.WRITE])
|
79
|
+
],
|
80
|
+
)
|
81
|
+
async def devices_force_update(request: Request, config: ForceUpdateModel) -> dict:
|
82
|
+
for uuid in config.devices:
|
83
|
+
updater = await get_update_manager(uuid)
|
84
|
+
updater.force_update = True
|
85
|
+
return {"success": True}
|
86
|
+
|
87
|
+
|
88
|
+
@router.get(
|
89
|
+
"/logs/{dev_id}",
|
90
|
+
dependencies=[Security(validate_user_permissions, scopes=[Permissions.HOME.READ])],
|
91
|
+
)
|
92
|
+
async def device_logs(request: Request, dev_id: str) -> str:
|
93
|
+
device = await get_device_by_uuid(dev_id)
|
94
|
+
if device.last_log is not None:
|
95
|
+
return device.last_log
|
96
|
+
return "No logs found."
|
97
|
+
|
98
|
+
|
99
|
+
class DeleteModel(BaseModel):
|
100
|
+
devices: list[str]
|
101
|
+
|
102
|
+
|
103
|
+
@router.post(
|
104
|
+
"/delete",
|
105
|
+
dependencies=[
|
106
|
+
Security(validate_user_permissions, scopes=[Permissions.DEVICE.DELETE])
|
107
|
+
],
|
108
|
+
)
|
109
|
+
async def devices_delete(request: Request, config: DeleteModel) -> dict:
|
110
|
+
for uuid in config.devices:
|
111
|
+
await delete_device(uuid)
|
112
|
+
return {"success": True}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
from fastapi import APIRouter, Security
|
2
|
+
from fastapi.requests import Request
|
3
|
+
from fastapi.responses import FileResponse
|
4
|
+
|
5
|
+
from goosebit.auth import validate_user_permissions
|
6
|
+
from goosebit.permissions import Permissions
|
7
|
+
from goosebit.settings import UPDATES_DIR
|
8
|
+
|
9
|
+
router = APIRouter(prefix="/download")
|
10
|
+
|
11
|
+
|
12
|
+
@router.get(
|
13
|
+
"/{file}",
|
14
|
+
dependencies=[
|
15
|
+
Security(validate_user_permissions, scopes=[Permissions.FIRMWARE.READ])
|
16
|
+
],
|
17
|
+
)
|
18
|
+
async def download_file(request: Request, file: str):
|
19
|
+
filename = UPDATES_DIR.joinpath(file)
|
20
|
+
return FileResponse(filename, media_type="application/octet-stream")
|
@@ -0,0 +1,64 @@
|
|
1
|
+
from fastapi import APIRouter, Body, Security
|
2
|
+
from fastapi.requests import Request
|
3
|
+
|
4
|
+
from goosebit.auth import validate_user_permissions
|
5
|
+
from goosebit.permissions import Permissions
|
6
|
+
from goosebit.settings import UPDATES_DIR
|
7
|
+
from goosebit.updater.misc import fw_sort_key, get_newest_fw
|
8
|
+
from goosebit.updater.updates import FirmwareArtifact
|
9
|
+
|
10
|
+
router = APIRouter(prefix="/firmware")
|
11
|
+
|
12
|
+
|
13
|
+
@router.get(
|
14
|
+
"/all",
|
15
|
+
dependencies=[
|
16
|
+
Security(validate_user_permissions, scopes=[Permissions.FIRMWARE.READ])
|
17
|
+
],
|
18
|
+
)
|
19
|
+
async def firmware_get_all() -> list[dict]:
|
20
|
+
UPDATES_DIR.mkdir(parents=True, exist_ok=True)
|
21
|
+
|
22
|
+
firmware = []
|
23
|
+
for file in sorted(
|
24
|
+
[f for f in UPDATES_DIR.iterdir() if f.suffix == ".swu"],
|
25
|
+
key=lambda x: fw_sort_key(x),
|
26
|
+
reverse=True,
|
27
|
+
):
|
28
|
+
artifact = FirmwareArtifact(file.name)
|
29
|
+
firmware.append(
|
30
|
+
{
|
31
|
+
"name": file.name,
|
32
|
+
"size": artifact.path.stat().st_size,
|
33
|
+
"version": artifact.version,
|
34
|
+
}
|
35
|
+
)
|
36
|
+
|
37
|
+
return firmware
|
38
|
+
|
39
|
+
|
40
|
+
@router.get(
|
41
|
+
"/latest",
|
42
|
+
dependencies=[
|
43
|
+
Security(validate_user_permissions, scopes=[Permissions.FIRMWARE.READ])
|
44
|
+
],
|
45
|
+
)
|
46
|
+
async def firmware_get_latest() -> dict:
|
47
|
+
UPDATES_DIR.mkdir(parents=True, exist_ok=True)
|
48
|
+
|
49
|
+
file_data = UPDATES_DIR.joinpath(get_newest_fw())
|
50
|
+
return {"name": file_data.name, "size": file_data.stat().st_size}
|
51
|
+
|
52
|
+
|
53
|
+
@router.post(
|
54
|
+
"/delete",
|
55
|
+
dependencies=[
|
56
|
+
Security(validate_user_permissions, scopes=[Permissions.FIRMWARE.DELETE])
|
57
|
+
],
|
58
|
+
)
|
59
|
+
async def firmware_delete(request: Request, file: str = Body()) -> dict:
|
60
|
+
file_path = UPDATES_DIR.joinpath(file)
|
61
|
+
if file_path.exists():
|
62
|
+
file_path.unlink()
|
63
|
+
return {"success": True}
|
64
|
+
return {"success": False}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
from fastapi import APIRouter, Depends
|
2
|
+
|
3
|
+
from goosebit.api import devices, download, firmware
|
4
|
+
from goosebit.auth import authenticate_api_session
|
5
|
+
|
6
|
+
router = APIRouter(
|
7
|
+
prefix="/api", dependencies=[Depends(authenticate_api_session)], tags=["api"]
|
8
|
+
)
|
9
|
+
router.include_router(firmware.router)
|
10
|
+
router.include_router(devices.router)
|
11
|
+
router.include_router(download.router)
|
@@ -0,0 +1,123 @@
|
|
1
|
+
from __future__ import annotations
|
2
|
+
|
3
|
+
from fastapi import Depends, HTTPException
|
4
|
+
from fastapi.requests import Request
|
5
|
+
from fastapi.security import SecurityScopes
|
6
|
+
from fastapi.websockets import WebSocket
|
7
|
+
from jose import jwt
|
8
|
+
|
9
|
+
from goosebit.settings import PWD_CXT, SECRET, USERS
|
10
|
+
|
11
|
+
|
12
|
+
async def authenticate_user(request: Request):
|
13
|
+
form_data = await request.form()
|
14
|
+
username = form_data.get("username")
|
15
|
+
password = form_data.get("password")
|
16
|
+
user = USERS.get(username)
|
17
|
+
if user is None:
|
18
|
+
raise HTTPException(
|
19
|
+
status_code=302,
|
20
|
+
headers={"location": str(request.url_for("login"))},
|
21
|
+
detail="Invalid credentials",
|
22
|
+
)
|
23
|
+
if not PWD_CXT.verify(password, user.hashed_pwd):
|
24
|
+
raise HTTPException(
|
25
|
+
status_code=302,
|
26
|
+
headers={"location": str(request.url_for("login"))},
|
27
|
+
detail="Invalid credentials",
|
28
|
+
)
|
29
|
+
return user
|
30
|
+
|
31
|
+
|
32
|
+
sessions = {}
|
33
|
+
|
34
|
+
|
35
|
+
def create_session(email: str) -> str:
|
36
|
+
token = jwt.encode({"email": email}, SECRET)
|
37
|
+
sessions[token] = email
|
38
|
+
return token
|
39
|
+
|
40
|
+
|
41
|
+
def authenticate_session(request: Request):
|
42
|
+
session_id = request.cookies.get("session_id")
|
43
|
+
if session_id is None or session_id not in sessions:
|
44
|
+
raise HTTPException(
|
45
|
+
status_code=302,
|
46
|
+
headers={"location": str(request.url_for("login"))},
|
47
|
+
detail="Invalid session ID",
|
48
|
+
)
|
49
|
+
user = get_user_from_session(session_id)
|
50
|
+
return user
|
51
|
+
|
52
|
+
|
53
|
+
def authenticate_api_session(request: Request):
|
54
|
+
session_id = request.cookies.get("session_id")
|
55
|
+
if session_id is None or session_id not in sessions:
|
56
|
+
raise HTTPException(status_code=401, detail="Not logged in")
|
57
|
+
user = get_user_from_session(session_id)
|
58
|
+
return user
|
59
|
+
|
60
|
+
|
61
|
+
def authenticate_ws_session(websocket: WebSocket):
|
62
|
+
session_id = websocket.cookies.get("session_id")
|
63
|
+
if session_id is None or session_id not in sessions:
|
64
|
+
raise HTTPException(status_code=401, detail="Not logged in")
|
65
|
+
user = get_user_from_session(session_id)
|
66
|
+
return user
|
67
|
+
|
68
|
+
|
69
|
+
def auto_redirect(request: Request):
|
70
|
+
session_id = request.cookies.get("session_id")
|
71
|
+
if session_id is None or session_id not in sessions:
|
72
|
+
return request
|
73
|
+
raise HTTPException(
|
74
|
+
status_code=302,
|
75
|
+
headers={"location": str(request.url_for("ui_root"))},
|
76
|
+
detail="Already logged in",
|
77
|
+
)
|
78
|
+
|
79
|
+
|
80
|
+
def get_user_from_session(session_id: str):
|
81
|
+
for username in USERS:
|
82
|
+
if username == sessions.get(session_id):
|
83
|
+
return username
|
84
|
+
|
85
|
+
|
86
|
+
def get_current_user(request: Request):
|
87
|
+
session_id = request.cookies.get("session_id")
|
88
|
+
if session_id is None or session_id not in sessions:
|
89
|
+
return None
|
90
|
+
user = get_user_from_session(session_id)
|
91
|
+
return USERS[user]
|
92
|
+
|
93
|
+
|
94
|
+
def validate_user_permissions(
|
95
|
+
request: Request,
|
96
|
+
security: SecurityScopes,
|
97
|
+
username: str = Depends(authenticate_session),
|
98
|
+
) -> Request:
|
99
|
+
user = USERS[username]
|
100
|
+
if security.scopes is None:
|
101
|
+
return request
|
102
|
+
for scope in security.scopes:
|
103
|
+
if scope not in user.permissions:
|
104
|
+
raise HTTPException(
|
105
|
+
status_code=403,
|
106
|
+
detail="Not enough permissions",
|
107
|
+
)
|
108
|
+
|
109
|
+
|
110
|
+
def validate_ws_user_permissions(
|
111
|
+
websocket: WebSocket,
|
112
|
+
security: SecurityScopes,
|
113
|
+
username: str = Depends(authenticate_ws_session),
|
114
|
+
) -> WebSocket:
|
115
|
+
user = USERS[username]
|
116
|
+
if security.scopes is None:
|
117
|
+
return websocket
|
118
|
+
for scope in security.scopes:
|
119
|
+
if scope not in user.permissions:
|
120
|
+
raise HTTPException(
|
121
|
+
status_code=403,
|
122
|
+
detail="Not enough permissions",
|
123
|
+
)
|
@@ -0,0 +1,32 @@
|
|
1
|
+
from aerich import Command
|
2
|
+
from tortoise import Tortoise, run_async
|
3
|
+
|
4
|
+
from goosebit.settings import DB_MIGRATIONS_LOC, DB_URI
|
5
|
+
|
6
|
+
TORTOISE_CONF = {
|
7
|
+
"connections": {"default": DB_URI},
|
8
|
+
"apps": {
|
9
|
+
"models": {
|
10
|
+
"models": ["goosebit.models", "aerich.models"],
|
11
|
+
},
|
12
|
+
},
|
13
|
+
}
|
14
|
+
|
15
|
+
|
16
|
+
async def init():
|
17
|
+
command = Command(tortoise_config=TORTOISE_CONF, location=DB_MIGRATIONS_LOC)
|
18
|
+
await Tortoise.init(config=TORTOISE_CONF)
|
19
|
+
if not DB_MIGRATIONS_LOC.exists():
|
20
|
+
await command.init_db(safe=True)
|
21
|
+
await command.init()
|
22
|
+
await command.migrate()
|
23
|
+
await command.upgrade(run_in_transaction=True)
|
24
|
+
await Tortoise.generate_schemas(safe=True)
|
25
|
+
|
26
|
+
|
27
|
+
async def close():
|
28
|
+
await Tortoise.close_connections()
|
29
|
+
|
30
|
+
|
31
|
+
if __name__ == "__main__":
|
32
|
+
run_async(init())
|