lockbot 2.10.1__tar.gz → 2.11.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.
- {lockbot-2.10.1/python/lockbot.egg-info → lockbot-2.11.0}/PKG-INFO +1 -1
- {lockbot-2.10.1 → lockbot-2.11.0}/pyproject.toml +1 -1
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/agent_skill/SKILL.md +16 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/bots/router.py +3 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/main.py +3 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/managed_paths.py +1 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_api/discovery.py +18 -1
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_api/ingress.py +1 -1
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_api/skill.py +1 -0
- lockbot-2.11.0/python/lockbot/backend/app/resource_api/view_links.py +128 -0
- lockbot-2.11.0/python/lockbot/backend/app/resource_api/views.py +135 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/base_bot.py +12 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/device_bot.py +2 -4
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/i18n/en.py +1 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/i18n/zh.py +1 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/node_bot.py +1 -1
- {lockbot-2.10.1 → lockbot-2.11.0/python/lockbot.egg-info}/PKG-INFO +1 -1
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot.egg-info/SOURCES.txt +2 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/LICENSE +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/MANIFEST.in +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/README.md +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/__init__.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/__init__.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/__init__.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/admin/__init__.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/admin/router.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/audit/__init__.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/audit/models.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/audit/router.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/audit/service.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/auth/__init__.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/auth/dependencies.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/auth/models.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/auth/router.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/auth/schemas.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/backup/__init__.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/backup/router.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/backup/scheduler.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/backup/service.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/bot_audit/__init__.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/bot_audit/models.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/bot_audit/router.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/bot_audit/service.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/bots/__init__.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/bots/encryption.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/bots/manager.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/bots/models.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/bots/schemas.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/bots/webhook_handler.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/broadcast/__init__.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/broadcast/models.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/broadcast/router.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/broadcast/service.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/config.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/database.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/deployment.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/logs/__init__.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/op_skill/SKILL.md +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/rate_limit.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_api/__init__.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_api/activity.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_api/commands.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_api/errors.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_api/notifications.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_api/observability.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_api/op_skill.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_api/operator.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_api/resources.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_api/snapshot.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_api/sources.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_api/traffic.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_api/usage.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_api/usage_cleanup.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_api/versioning.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_auth/__init__.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_auth/authority.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_auth/credentials.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_auth/dependencies.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_auth/http_errors.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_auth/models.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_auth/pending.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_auth/router.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/resource_auth/service.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/settings/__init__.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/settings/models.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/settings/router.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/sqlite_paths.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/state_migration.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/state_ownership.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/backend/app/static.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/__init__.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/bot_instance.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/command_context.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/config.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/device_usage_alert.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/device_usage_utils.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/entry.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/env.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/handler.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/i18n/__init__.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/io.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/message_adapter.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/msg_utils.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/platforms/__init__.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/platforms/infoflow.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/platforms/registry.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/queue_bot.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/request.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/scheduler.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot/core/utils.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot.egg-info/dependency_links.txt +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot.egg-info/requires.txt +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/python/lockbot.egg-info/top_level.txt +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/setup.cfg +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/tools/create_super_admin.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/tools/gen_keys.py +0 -0
- {lockbot-2.10.1 → lockbot-2.11.0}/tools/reset_super_admin_password.py +0 -0
|
@@ -58,6 +58,22 @@ user-approved origin (and HTTPS where available) remains the credential destinat
|
|
|
58
58
|
The server cannot erase an Agent's earlier memories: keep the scope explicit in its private
|
|
59
59
|
configuration and use this namespaced skill for requests to this implementation.
|
|
60
60
|
|
|
61
|
+
## Share a resource/queue page with the user
|
|
62
|
+
|
|
63
|
+
When the user wants to view resources or queue order in a browser, use `view_url` and
|
|
64
|
+
`view_expires_at` returned by `/api/agent/context`. Share that read-only URL; never put your
|
|
65
|
+
personal key into a browser URL. No separate web command or link-creation endpoint is needed.
|
|
66
|
+
The server lazily creates one link per bot, reuses it for 24 hours from creation, and only
|
|
67
|
+
replaces it when a subsequent authorized request arrives after expiry. Page refreshes do not
|
|
68
|
+
renew it. Ordinary group queries also return the current link. If the link expires, request
|
|
69
|
+
context again. Keep the returned URL on the user-approved origin.
|
|
70
|
+
|
|
71
|
+
The link permits its holder to read this bot's resource/queue/task information and can be
|
|
72
|
+
forwarded; it does not prove current group membership or allow commands. Revoking a personal
|
|
73
|
+
key prevents obtaining new links but does not independently revoke an already shared group
|
|
74
|
+
link before its expiry. A bot identity change invalidates its links. Older servers may omit
|
|
75
|
+
these optional fields: retain the existing API workflow rather than guessing a page URL.
|
|
76
|
+
|
|
61
77
|
## Version checks and compatible updates
|
|
62
78
|
|
|
63
79
|
The manifest separates `server_version`, stable `api_version`, and the opaque content-based
|
|
@@ -323,6 +323,9 @@ def _build_config_dict(bot: Bot, db: Session | None = None) -> dict:
|
|
|
323
323
|
g.strip() for g in (getattr(bot, "group_id", None) or "").split(",") if g.strip()
|
|
324
324
|
]
|
|
325
325
|
config["_AGENT_SKILL_URL"] = lambda bot_id=bot.id, uuid=getattr(bot, "uuid", ""): _agent_skill_url(bot_id, uuid)
|
|
326
|
+
from lockbot.backend.app.resource_api.view_links import group_view_link
|
|
327
|
+
|
|
328
|
+
config["_BOT_VIEW_LINK"] = lambda bot_id=bot.id, uuid=getattr(bot, "uuid", ""): group_view_link(bot_id, uuid)
|
|
326
329
|
config["_IDENTITY_REVISION"] = getattr(bot, "identity_revision", 0)
|
|
327
330
|
config["DATA_DIR"] = str(paths.bots_root)
|
|
328
331
|
config["STATE_FILENAME"] = str(paths.state_file(bot_id))
|
|
@@ -647,6 +647,9 @@ def create_app() -> FastAPI:
|
|
|
647
647
|
app.include_router(skill_router)
|
|
648
648
|
app.include_router(op_skill_router)
|
|
649
649
|
app.include_router(operator_router)
|
|
650
|
+
from lockbot.backend.app.resource_api.views import router as view_router
|
|
651
|
+
|
|
652
|
+
app.include_router(view_router)
|
|
650
653
|
|
|
651
654
|
# ── Integration API docs: personal Agent, operator, enrollment and downloads ──
|
|
652
655
|
@app.get("/api/public-openapi.json", include_in_schema=False)
|
|
@@ -47,7 +47,7 @@ def server_metadata(request: Request, db: Session) -> dict:
|
|
|
47
47
|
"server_version": request.app.version,
|
|
48
48
|
"api_version": API_VERSION,
|
|
49
49
|
"credential_scheme": "lbr_bot_scoped",
|
|
50
|
-
"capabilities": ["credential_context", "namespaced_skill", "skill_version_headers"],
|
|
50
|
+
"capabilities": ["credential_context", "namespaced_skill", "skill_version_headers", "resource_view_link"],
|
|
51
51
|
"context_url": "/api/agent/context",
|
|
52
52
|
"enrollment_url": "/api/resource-auth/bindings",
|
|
53
53
|
"openapi_url": "/api/public-openapi.json",
|
|
@@ -81,6 +81,8 @@ class AgentContextResponse(BaseModel):
|
|
|
81
81
|
credential_scope: Literal["one_user_one_bot"]
|
|
82
82
|
links: dict[str, str]
|
|
83
83
|
skill: dict[str, Any]
|
|
84
|
+
view_url: str | None = None
|
|
85
|
+
view_expires_at: str | None = None
|
|
84
86
|
|
|
85
87
|
|
|
86
88
|
@router.get("/context", response_model=AgentContextResponse)
|
|
@@ -97,11 +99,25 @@ def agent_context(request: Request, response: Response, db: Session = Depends(ge
|
|
|
97
99
|
allowed, retry = credential_budget.check(row.id, "read")
|
|
98
100
|
if not allowed:
|
|
99
101
|
raise errors.rate_limited(retry)
|
|
102
|
+
from lockbot.backend.app.resource_auth.authority import authority_guard
|
|
103
|
+
|
|
104
|
+
bot_id, platform, user_id = row.bound_bot_id, row.platform, row.external_user_id
|
|
105
|
+
with authority_guard(bot_id, platform=platform, external_user_id=user_id):
|
|
106
|
+
db.rollback()
|
|
107
|
+
row = service.authenticate_for_any_bot(db, token=_bearer_token(request))
|
|
108
|
+
if row is None or row.bound_bot_id != bot_id:
|
|
109
|
+
raise ResourceAuthenticationError("Invalid credential", "credential_invalid")
|
|
110
|
+
return _context_payload(request, response, db, row)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _context_payload(request, response, db, row):
|
|
100
114
|
bot = db.get(Bot, row.bound_bot_id)
|
|
101
115
|
from lockbot.backend.app.resource_api.skill import skill_artifacts
|
|
116
|
+
from lockbot.backend.app.resource_api.view_links import view_link
|
|
102
117
|
|
|
103
118
|
response.headers["Cache-Control"] = "no-store"
|
|
104
119
|
response.headers["Vary"] = "Authorization"
|
|
120
|
+
view = view_link(db, bot, str(request.base_url)) if bot.resource_auth_enabled else {}
|
|
105
121
|
return {
|
|
106
122
|
"server": server_metadata(request, db),
|
|
107
123
|
"actor": {"platform": row.platform, "external_user_id": row.external_user_id},
|
|
@@ -115,4 +131,5 @@ def agent_context(request: Request, response: Response, db: Session = Depends(ge
|
|
|
115
131
|
"skill_manifest": "/api/agent-skill",
|
|
116
132
|
},
|
|
117
133
|
"skill": skill_artifacts()[2],
|
|
134
|
+
**view,
|
|
118
135
|
}
|
|
@@ -13,7 +13,7 @@ from collections import OrderedDict
|
|
|
13
13
|
from starlette.responses import JSONResponse
|
|
14
14
|
|
|
15
15
|
_PATH = re.compile(
|
|
16
|
-
r"^/api/(?:agent/context|resource-auth/bindings(?:/[^/]+)?|bots/[^/]+/(?:commands|resources|usage-declarations(?:/[^/]+)?))/?$"
|
|
16
|
+
r"^/api/(?:agent/context|bot-views/[^/]+/state|resource-auth/bindings(?:/[^/]+)?|bots/[^/]+/(?:commands|resources|usage-declarations(?:/[^/]+)?))/?$"
|
|
17
17
|
)
|
|
18
18
|
|
|
19
19
|
|
|
@@ -66,6 +66,7 @@ def public_skill_manifest(kind, request, response, db):
|
|
|
66
66
|
if kind == "operator":
|
|
67
67
|
identity["credential_scheme"] = "lbk_bot_scoped"
|
|
68
68
|
identity["capabilities"].remove("credential_context")
|
|
69
|
+
identity["capabilities"].remove("resource_view_link")
|
|
69
70
|
identity.pop("context_url")
|
|
70
71
|
identity.pop("enrollment_url")
|
|
71
72
|
return {**skill_artifacts(kind)[2], **identity}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"""Bot-scoped read-only links, created lazily and reused for a fixed 24-hour lifetime."""
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
import hmac
|
|
5
|
+
import secrets
|
|
6
|
+
from datetime import datetime, timedelta, timezone
|
|
7
|
+
from urllib.parse import urlsplit
|
|
8
|
+
|
|
9
|
+
from sqlalchemy import DateTime, ForeignKey, Integer, String, or_
|
|
10
|
+
from sqlalchemy.dialects.sqlite import insert
|
|
11
|
+
from sqlalchemy.orm import Mapped, Session, mapped_column
|
|
12
|
+
|
|
13
|
+
from lockbot.backend.app import config
|
|
14
|
+
from lockbot.backend.app.database import Base
|
|
15
|
+
|
|
16
|
+
LIFETIME_SECONDS = 86400
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class BotViewWindow(Base):
|
|
20
|
+
__tablename__ = "bot_view_windows"
|
|
21
|
+
bot_id: Mapped[int] = mapped_column(Integer, ForeignKey("bots.id"), primary_key=True)
|
|
22
|
+
bot_uuid: Mapped[str] = mapped_column(String(36), nullable=False)
|
|
23
|
+
identity_revision: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
24
|
+
nonce: Mapped[str] = mapped_column(String(32), nullable=False)
|
|
25
|
+
issued_at: Mapped[datetime] = mapped_column(DateTime, nullable=False)
|
|
26
|
+
expires_at: Mapped[datetime] = mapped_column(DateTime, nullable=False)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def utcnow():
|
|
30
|
+
return datetime.utcnow()
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _iso(value):
|
|
34
|
+
return value.replace(tzinfo=timezone.utc).isoformat()
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _signature(db, window):
|
|
38
|
+
from lockbot.backend.app.resource_api.discovery import ensure_instance_id
|
|
39
|
+
|
|
40
|
+
key = hmac.digest(config.JWT_SECRET.encode(), b"lockbot:readonly-view:v1", "sha256")
|
|
41
|
+
message = "|".join(
|
|
42
|
+
[
|
|
43
|
+
ensure_instance_id(db),
|
|
44
|
+
str(window.bot_id),
|
|
45
|
+
window.bot_uuid,
|
|
46
|
+
str(window.identity_revision),
|
|
47
|
+
window.nonce,
|
|
48
|
+
_iso(window.issued_at),
|
|
49
|
+
_iso(window.expires_at),
|
|
50
|
+
]
|
|
51
|
+
).encode()
|
|
52
|
+
return base64.urlsafe_b64encode(hmac.digest(key, message, "sha256")).decode().rstrip("=")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _current(window, bot, now):
|
|
56
|
+
return (
|
|
57
|
+
window is not None
|
|
58
|
+
and not bot.is_deleted
|
|
59
|
+
and window.bot_uuid == bot.uuid
|
|
60
|
+
and window.identity_revision == bot.identity_revision
|
|
61
|
+
and window.expires_at > now
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def view_link(db: Session, bot, base_url: str) -> dict:
|
|
66
|
+
"""Only trusted group/Agent entry points call this; state polling never creates a window."""
|
|
67
|
+
parsed = urlsplit(base_url)
|
|
68
|
+
if parsed.scheme not in {"http", "https"} or not parsed.netloc or parsed.username or parsed.password:
|
|
69
|
+
return {}
|
|
70
|
+
if bot.is_deleted or not bot.uuid:
|
|
71
|
+
return {}
|
|
72
|
+
now = utcnow().replace(microsecond=0)
|
|
73
|
+
window = db.get(BotViewWindow, bot.id)
|
|
74
|
+
if not _current(window, bot, now):
|
|
75
|
+
values = {
|
|
76
|
+
"bot_id": bot.id,
|
|
77
|
+
"bot_uuid": bot.uuid,
|
|
78
|
+
"identity_revision": bot.identity_revision,
|
|
79
|
+
"nonce": secrets.token_urlsafe(18),
|
|
80
|
+
"issued_at": now,
|
|
81
|
+
"expires_at": now + timedelta(seconds=LIFETIME_SECONDS),
|
|
82
|
+
}
|
|
83
|
+
db.execute(
|
|
84
|
+
insert(BotViewWindow)
|
|
85
|
+
.values(**values)
|
|
86
|
+
.on_conflict_do_update(
|
|
87
|
+
index_elements=[BotViewWindow.bot_id],
|
|
88
|
+
set_={key: value for key, value in values.items() if key != "bot_id"},
|
|
89
|
+
where=or_(
|
|
90
|
+
BotViewWindow.expires_at <= now,
|
|
91
|
+
BotViewWindow.bot_uuid != bot.uuid,
|
|
92
|
+
BotViewWindow.identity_revision != bot.identity_revision,
|
|
93
|
+
),
|
|
94
|
+
)
|
|
95
|
+
)
|
|
96
|
+
db.commit()
|
|
97
|
+
db.expire_all()
|
|
98
|
+
window = db.get(BotViewWindow, bot.id)
|
|
99
|
+
token = "lbv1." + window.nonce + "." + _signature(db, window)
|
|
100
|
+
base = base_url.split("#", 1)[0].split("?", 1)[0].rstrip("/")
|
|
101
|
+
return {"view_url": f"{base}/#/bot/{bot.id}?view={token}", "view_expires_at": _iso(window.expires_at)}
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def verify_view(db, bot, token):
|
|
105
|
+
"""Read-only verification; an expired or wrong-bot token cannot renew itself."""
|
|
106
|
+
if bot is None or bot.is_deleted or not token or len(token) > 160:
|
|
107
|
+
return None
|
|
108
|
+
pieces = token.split(".")
|
|
109
|
+
if len(pieces) != 3 or pieces[0] != "lbv1":
|
|
110
|
+
return None
|
|
111
|
+
window = db.get(BotViewWindow, bot.id)
|
|
112
|
+
if not _current(window, bot, utcnow()):
|
|
113
|
+
return None
|
|
114
|
+
if not secrets.compare_digest(window.nonce, pieces[1]):
|
|
115
|
+
return None
|
|
116
|
+
return window if secrets.compare_digest(_signature(db, window), pieces[2]) else None
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def group_view_link(bot_id, expected_uuid):
|
|
120
|
+
from lockbot.backend.app.bots.models import Bot
|
|
121
|
+
from lockbot.backend.app.database import SessionLocal
|
|
122
|
+
from lockbot.backend.app.settings.router import get_all_settings
|
|
123
|
+
|
|
124
|
+
with SessionLocal() as db:
|
|
125
|
+
bot = db.get(Bot, bot_id)
|
|
126
|
+
if bot is None or bot.is_deleted or bot.uuid != expected_uuid:
|
|
127
|
+
return {}
|
|
128
|
+
return view_link(db, bot, (get_all_settings(db).get("platform_url") or "").strip())
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"""Narrow read-only snapshots for the per-bot browser page; no account or operation access."""
|
|
2
|
+
|
|
3
|
+
from datetime import datetime, timezone
|
|
4
|
+
|
|
5
|
+
from fastapi import APIRouter, Depends, HTTPException, Request, Response
|
|
6
|
+
from sqlalchemy.orm import Session
|
|
7
|
+
|
|
8
|
+
from lockbot.backend.app.bots.manager import (
|
|
9
|
+
AdmissionFull,
|
|
10
|
+
BotNotRunning,
|
|
11
|
+
InstanceRetiring,
|
|
12
|
+
MayHaveExecuted,
|
|
13
|
+
bot_manager,
|
|
14
|
+
)
|
|
15
|
+
from lockbot.backend.app.bots.models import Bot
|
|
16
|
+
from lockbot.backend.app.database import get_db
|
|
17
|
+
from lockbot.backend.app.rate_limit import limiter
|
|
18
|
+
from lockbot.backend.app.resource_api import errors
|
|
19
|
+
from lockbot.backend.app.resource_api.resources import snapshot_cache
|
|
20
|
+
from lockbot.backend.app.resource_api.snapshot import build_resources, scrub_private
|
|
21
|
+
from lockbot.backend.app.resource_api.usage import active_declarations, attach_declarations, declarations_for_state
|
|
22
|
+
from lockbot.backend.app.resource_api.view_links import verify_view
|
|
23
|
+
from lockbot.core.platforms.registry import normalize_platform_name
|
|
24
|
+
|
|
25
|
+
router = APIRouter(prefix="/api/bot-views", tags=["Read-only resource view"])
|
|
26
|
+
PRIVATE_HEADERS = {"Cache-Control": "no-store", "Vary": "Authorization", "Referrer-Policy": "no-referrer"}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _authorized(db, bot_id, request, *, fresh=False):
|
|
30
|
+
if fresh:
|
|
31
|
+
db.rollback()
|
|
32
|
+
header = request.headers.get("Authorization", "")
|
|
33
|
+
token = header[7:] if header.startswith("Bearer ") else ""
|
|
34
|
+
bot = db.get(Bot, bot_id)
|
|
35
|
+
window = verify_view(db, bot, token)
|
|
36
|
+
if window is None:
|
|
37
|
+
raise HTTPException(
|
|
38
|
+
403,
|
|
39
|
+
{"code": "view_link_invalid", "message": "Get a new link from your group or Agent."},
|
|
40
|
+
headers=PRIVATE_HEADERS,
|
|
41
|
+
)
|
|
42
|
+
return bot, window
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _strip_caller_fields(resources):
|
|
46
|
+
for resource in resources:
|
|
47
|
+
for key in ("caller_queue_position", "caller_is_queue_head", "claimable_by_you", "claim_command"):
|
|
48
|
+
resource.pop(key, None)
|
|
49
|
+
_strip_caller_fields(resource.get("devices", []))
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _display_details(resources, state, aliases):
|
|
53
|
+
for resource in resources:
|
|
54
|
+
raw = state.get(resource["id"], {})
|
|
55
|
+
resource["alias"] = aliases.get(resource["id"], "")
|
|
56
|
+
if isinstance(raw, list):
|
|
57
|
+
by_id = {entry.get("dev_id"): entry for entry in raw}
|
|
58
|
+
pairs = [(device, by_id.get(device["id"], {})) for device in resource.get("devices", [])]
|
|
59
|
+
else:
|
|
60
|
+
pairs = [(resource, raw)]
|
|
61
|
+
for target, entry in pairs:
|
|
62
|
+
if "dev_model" in entry:
|
|
63
|
+
target["dev_model"] = entry["dev_model"]
|
|
64
|
+
waiting = max((holder["remaining_seconds"] for holder in target["holders"]), default=0)
|
|
65
|
+
for booking, original in zip(target.get("booking_list", []), entry.get("booking_list", []), strict=True):
|
|
66
|
+
duration = max(0, int(original.get("duration", 0)))
|
|
67
|
+
booking["duration_seconds"] = duration
|
|
68
|
+
booking["estimated_wait_seconds"] = waiting
|
|
69
|
+
waiting += duration
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@router.get("/{bot_id}/state")
|
|
73
|
+
@limiter.limit("300/minute")
|
|
74
|
+
def view_state(bot_id: int, request: Request, response: Response, db: Session = Depends(get_db)):
|
|
75
|
+
bot, window = _authorized(db, bot_id, request)
|
|
76
|
+
response.headers.update(PRIVATE_HEADERS)
|
|
77
|
+
from lockbot.backend.app.resource_api.view_links import _iso, utcnow
|
|
78
|
+
|
|
79
|
+
metadata = {
|
|
80
|
+
"bot_id": bot.id,
|
|
81
|
+
"name": bot.name,
|
|
82
|
+
"bot_type": bot.bot_type,
|
|
83
|
+
"server_time": _iso(utcnow()),
|
|
84
|
+
"view_expires_at": _iso(window.expires_at),
|
|
85
|
+
}
|
|
86
|
+
try:
|
|
87
|
+
with bot_manager.admit(bot_id, for_read=True) as lease:
|
|
88
|
+
bot, window = _authorized(db, bot_id, request, fresh=True)
|
|
89
|
+
captured_at, state, from_cache = snapshot_cache.get_or_compute(
|
|
90
|
+
bot_id, lease.generation, datetime.now(timezone.utc), lease.bot.state_snapshot
|
|
91
|
+
)
|
|
92
|
+
declarations = []
|
|
93
|
+
offset = 0
|
|
94
|
+
while True:
|
|
95
|
+
rows = active_declarations(db, bot, datetime.utcnow(), limit=500, offset=offset)
|
|
96
|
+
if rows and from_cache:
|
|
97
|
+
captured_at, state = lease.bot.state_snapshot()
|
|
98
|
+
from_cache = False
|
|
99
|
+
declarations.extend(declarations_for_state(rows, state, captured_at))
|
|
100
|
+
if len(rows) < 500:
|
|
101
|
+
break
|
|
102
|
+
offset += 500
|
|
103
|
+
resources = scrub_private(
|
|
104
|
+
build_resources(
|
|
105
|
+
captured_at=captured_at,
|
|
106
|
+
state=state,
|
|
107
|
+
bot_type=bot.bot_type,
|
|
108
|
+
platform=normalize_platform_name(bot.platform),
|
|
109
|
+
caller_id="",
|
|
110
|
+
)
|
|
111
|
+
)
|
|
112
|
+
_strip_caller_fields(resources)
|
|
113
|
+
# The shared page shows task context, never enrollment identifiers or credential notes.
|
|
114
|
+
for declaration in declarations:
|
|
115
|
+
declaration.pop("binding_id", None)
|
|
116
|
+
declaration.pop("authorization_label", None)
|
|
117
|
+
attach_declarations(resources, declarations)
|
|
118
|
+
_display_details(resources, state, lease.bot.config.get_val("NODE_ALIASES", {}))
|
|
119
|
+
# Re-check expiry after assembling the payload; cached state is never cached authorization.
|
|
120
|
+
bot, window = _authorized(db, bot_id, request, fresh=True)
|
|
121
|
+
return {
|
|
122
|
+
**metadata,
|
|
123
|
+
"name": bot.name,
|
|
124
|
+
"health": lease.health or bot.status,
|
|
125
|
+
"server_time": _iso(utcnow()),
|
|
126
|
+
"view_expires_at": _iso(window.expires_at),
|
|
127
|
+
"captured_at": captured_at.isoformat(),
|
|
128
|
+
"age_seconds": max(0, (datetime.now(timezone.utc) - captured_at).total_seconds()),
|
|
129
|
+
"resources": resources,
|
|
130
|
+
}
|
|
131
|
+
except BotNotRunning:
|
|
132
|
+
bot, window = _authorized(db, bot_id, request, fresh=True)
|
|
133
|
+
return {**metadata, "health": bot.status, "captured_at": None, "resources": [], "age_seconds": None}
|
|
134
|
+
except (AdmissionFull, InstanceRetiring, MayHaveExecuted) as exc:
|
|
135
|
+
raise errors.admission_http_error(exc, bot_id) from None
|
|
@@ -413,6 +413,18 @@ class BaseLockBot:
|
|
|
413
413
|
except Exception:
|
|
414
414
|
return ""
|
|
415
415
|
|
|
416
|
+
def _query_reply(self, content, user_id):
|
|
417
|
+
"""Append the current read-only page outside the resource-state lock."""
|
|
418
|
+
getter = self.config.get_val("_BOT_VIEW_LINK")
|
|
419
|
+
if callable(getter):
|
|
420
|
+
try:
|
|
421
|
+
view = getter()
|
|
422
|
+
if view.get("view_url"):
|
|
423
|
+
content = [content.rstrip() + "\n\n", (t("view.link", config=self.config), view["view_url"]), "\n"]
|
|
424
|
+
except Exception:
|
|
425
|
+
self.logger.warning("Could not obtain resource view link for bot %s", self.config.get_val("BOT_ID"))
|
|
426
|
+
return self.adapter.build_reply(content, [user_id])
|
|
427
|
+
|
|
416
428
|
def _help_commands(self):
|
|
417
429
|
"""Return the command-section of the help text. Override in subclasses."""
|
|
418
430
|
return ""
|
|
@@ -147,10 +147,8 @@ class DeviceBot(BaseLockBot):
|
|
|
147
147
|
query current usage
|
|
148
148
|
"""
|
|
149
149
|
with self._lock:
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
)
|
|
153
|
-
return reply
|
|
150
|
+
content = self._msg_with_usage("query.cluster_usage_title", node_key=node_key)
|
|
151
|
+
return self._query_reply(content, user_id)
|
|
154
152
|
|
|
155
153
|
def lock(self, user_id, command, *, acquisition_limit=None):
|
|
156
154
|
"""
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""English (en) message translations for lockbot."""
|
|
2
2
|
|
|
3
3
|
MESSAGES = {
|
|
4
|
+
"view.link": "Resource page: ",
|
|
4
5
|
"help.agent_commands": "{number}. Agent:\n bind <code> (provided by your Agent) / bindings / unbind\n",
|
|
5
6
|
"notify.api_purpose": "\nNote: {purpose}",
|
|
6
7
|
"notify.api_agent_label": "\nAgent: {label}",
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""Chinese (zh) message translations for lockbot."""
|
|
2
2
|
|
|
3
3
|
MESSAGES = {
|
|
4
|
+
"view.link": "网页查看:",
|
|
4
5
|
"help.agent_commands": "{number}. Agent:\n bind <绑定码>(由 Agent 提供) / bindings / unbind\n",
|
|
5
6
|
"notify.api_purpose": "\n备注:{purpose}",
|
|
6
7
|
"notify.api_agent_label": "\nAgent:{label}",
|
|
@@ -101,7 +101,7 @@ class NodeBot(BaseLockBot):
|
|
|
101
101
|
"""
|
|
102
102
|
with self._lock:
|
|
103
103
|
content = self._msg_with_usage("query.cluster_usage_title", node_key=node_key)
|
|
104
|
-
|
|
104
|
+
return self._query_reply(content, user_id)
|
|
105
105
|
|
|
106
106
|
def lock(self, user_id, command, *, acquisition_limit=None):
|
|
107
107
|
"""
|
|
@@ -71,6 +71,8 @@ python/lockbot/backend/app/resource_api/traffic.py
|
|
|
71
71
|
python/lockbot/backend/app/resource_api/usage.py
|
|
72
72
|
python/lockbot/backend/app/resource_api/usage_cleanup.py
|
|
73
73
|
python/lockbot/backend/app/resource_api/versioning.py
|
|
74
|
+
python/lockbot/backend/app/resource_api/view_links.py
|
|
75
|
+
python/lockbot/backend/app/resource_api/views.py
|
|
74
76
|
python/lockbot/backend/app/resource_auth/__init__.py
|
|
75
77
|
python/lockbot/backend/app/resource_auth/authority.py
|
|
76
78
|
python/lockbot/backend/app/resource_auth/credentials.py
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|