@1outres/dsh-bot-connector-browser 0.3.0 → 0.4.0
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.
- package/README.md +12 -1
- package/lib/index.js +507 -41
- package/package.json +3 -3
- package/src/host/connector.ts +15 -7
- package/src/host/preparation.ts +68 -8
- package/src/host/session.ts +20 -5
- package/src/host/tools.ts +19 -10
- package/src/host/vault/failures.ts +31 -0
- package/src/host/vault/helper.ts +97 -0
- package/src/host/vault/marks.ts +22 -0
- package/src/host/vault/passkeys.ts +124 -0
- package/src/host/vault/protocol.ts +143 -0
- package/src/host/vault/tools.ts +218 -0
- package/src/host/vm/layout.ts +7 -1
- package/src/index.ts +22 -8
- package/vm/helper/bytes.mjs +74 -0
- package/vm/helper/cdp.mjs +259 -0
- package/vm/helper/channel.mjs +99 -0
- package/vm/helper/dsh-browser-fill.mjs +154 -0
- package/vm/helper/dsh-browser-passkey.mjs +192 -0
- package/vm/helper/page.mjs +135 -0
- package/vm/install-helper.sh +12 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// Finding what the Host marked through Playwright MCP: one element (or a tab's document) carrying
|
|
2
|
+
// a one-time attribute, searched in an isolated world of every frame of every tab (out-of-process
|
|
3
|
+
// frames included), with the frame's origin as the browser reports it, not as the page says.
|
|
4
|
+
|
|
5
|
+
const WORLD = "dsh-browser-vault"
|
|
6
|
+
const MARK = /^[0-9a-f]{32}$/
|
|
7
|
+
const ORIGIN = /^https?:\/\/[^/\s]+$/
|
|
8
|
+
|
|
9
|
+
export class PageError extends Error {
|
|
10
|
+
constructor(code) {
|
|
11
|
+
super(code)
|
|
12
|
+
this.name = "PageError"
|
|
13
|
+
this.code = code
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function checkMark(value) {
|
|
18
|
+
if (typeof value !== "string" || !MARK.test(value)) throw new PageError("protocol")
|
|
19
|
+
return value
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const findExpression = (attribute, mark) => `(() => {
|
|
23
|
+
const found = [];
|
|
24
|
+
const visit = (root) => {
|
|
25
|
+
for (const element of root.querySelectorAll("*")) {
|
|
26
|
+
if (element.getAttribute("${attribute}") === "${mark}") found.push(element);
|
|
27
|
+
if (element.shadowRoot) visit(element.shadowRoot);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
visit(document);
|
|
31
|
+
return found.length === 1 ? found[0] : found.length;
|
|
32
|
+
})()`
|
|
33
|
+
|
|
34
|
+
function framesOf(tree, out = []) {
|
|
35
|
+
out.push(tree.frame)
|
|
36
|
+
for (const child of tree.childFrames ?? []) framesOf(child, out)
|
|
37
|
+
return out
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Attach to every target of the types (flat sessions). */
|
|
41
|
+
export async function attachTargets(cdp, types) {
|
|
42
|
+
const { targetInfos } = await cdp.call("Target.getTargets")
|
|
43
|
+
const sessions = []
|
|
44
|
+
for (const target of targetInfos) {
|
|
45
|
+
if (!types.includes(target.type)) continue
|
|
46
|
+
try {
|
|
47
|
+
const { sessionId } = await cdp.call("Target.attachToTarget", {
|
|
48
|
+
targetId: target.targetId,
|
|
49
|
+
flatten: true,
|
|
50
|
+
})
|
|
51
|
+
sessions.push({ sessionId, targetId: target.targetId, type: target.type })
|
|
52
|
+
} catch {}
|
|
53
|
+
}
|
|
54
|
+
return sessions
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The frame's origin now, as the browser reports it; undefined when the frame is gone. */
|
|
58
|
+
export async function frameOrigin(cdp, sessionId, frameId) {
|
|
59
|
+
const { frameTree } = await cdp.call("Page.getFrameTree", {}, sessionId)
|
|
60
|
+
return framesOf(frameTree).find((frame) => frame.id === frameId)?.securityOrigin
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** A web origin (http or https with a host); an opaque or other one is refused. */
|
|
64
|
+
export function webOrigin(origin) {
|
|
65
|
+
if (typeof origin !== "string" || !ORIGIN.test(origin)) throw new PageError("opaque-origin")
|
|
66
|
+
return origin
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The one marked node: its session, frame, the frame's origin and a handle in the isolated
|
|
71
|
+
* world. `mainFrameOnly` searches the tabs' top documents only.
|
|
72
|
+
*/
|
|
73
|
+
export async function findMarked(cdp, sessions, { attribute, mark, mainFrameOnly }) {
|
|
74
|
+
const seen = new Set()
|
|
75
|
+
const matches = []
|
|
76
|
+
let count = 0
|
|
77
|
+
for (const session of sessions) {
|
|
78
|
+
let tree
|
|
79
|
+
try {
|
|
80
|
+
tree = (await cdp.call("Page.getFrameTree", {}, session.sessionId)).frameTree
|
|
81
|
+
} catch {
|
|
82
|
+
continue
|
|
83
|
+
}
|
|
84
|
+
const frames = mainFrameOnly ? [tree.frame] : framesOf(tree)
|
|
85
|
+
for (const frame of frames) {
|
|
86
|
+
if (seen.has(frame.id)) continue
|
|
87
|
+
let contextId
|
|
88
|
+
try {
|
|
89
|
+
contextId = (
|
|
90
|
+
await cdp.call(
|
|
91
|
+
"Page.createIsolatedWorld",
|
|
92
|
+
{ frameId: frame.id, worldName: WORLD, grantUniveralAccess: false },
|
|
93
|
+
session.sessionId,
|
|
94
|
+
)
|
|
95
|
+
).executionContextId
|
|
96
|
+
} catch {
|
|
97
|
+
continue
|
|
98
|
+
}
|
|
99
|
+
seen.add(frame.id)
|
|
100
|
+
const { result } = await cdp.call(
|
|
101
|
+
"Runtime.evaluate",
|
|
102
|
+
{ expression: findExpression(attribute, mark), contextId, returnByValue: false },
|
|
103
|
+
session.sessionId,
|
|
104
|
+
)
|
|
105
|
+
if (result.type === "object" && typeof result.objectId === "string") {
|
|
106
|
+
count += 1
|
|
107
|
+
matches.push({
|
|
108
|
+
sessionId: session.sessionId,
|
|
109
|
+
frameId: frame.id,
|
|
110
|
+
origin: frame.securityOrigin,
|
|
111
|
+
objectId: result.objectId,
|
|
112
|
+
})
|
|
113
|
+
} else if (typeof result.value === "number") {
|
|
114
|
+
count += result.value
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (count === 0) throw new PageError("not-found")
|
|
119
|
+
if (count > 1 || matches.length !== 1) throw new PageError("ambiguous")
|
|
120
|
+
return matches[0]
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Run a function on the node in its isolated world and return its JSON value. */
|
|
124
|
+
export async function onNode(cdp, node, functionDeclaration) {
|
|
125
|
+
const { result, exceptionDetails } = await cdp.call(
|
|
126
|
+
"Runtime.callFunctionOn",
|
|
127
|
+
{ objectId: node.objectId, functionDeclaration, returnByValue: true },
|
|
128
|
+
node.sessionId,
|
|
129
|
+
)
|
|
130
|
+
if (exceptionDetails !== undefined) throw new PageError("not-found")
|
|
131
|
+
return result.value
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export const unmark = (cdp, node, attribute) =>
|
|
135
|
+
onNode(cdp, node, `function () { this.removeAttribute("${attribute}"); return true }`)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# Installs the browser's login helpers (the *.mjs files in $1) into $2 and swaps them in. Run as
|
|
3
|
+
# root by the browser connector's preparation and by infra/vm/build-base-image.sh.
|
|
4
|
+
set -eu
|
|
5
|
+
source=$1
|
|
6
|
+
target=$2
|
|
7
|
+
rm -rf -- "$target.new" "$target.old"
|
|
8
|
+
install -d -m 0755 -- "$target.new"
|
|
9
|
+
for file in "$source"/*.mjs; do install -m 0644 -- "$file" "$target.new/"; done
|
|
10
|
+
if [ -e "$target" ]; then mv -- "$target" "$target.old"; fi
|
|
11
|
+
mv -- "$target.new" "$target"
|
|
12
|
+
rm -rf -- "$target.old"
|