grasp-sdk 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.
Potentially problematic release.
This version of grasp-sdk might be problematic. Click here for more details.
- grasp_sdk/__init__.py +262 -0
- grasp_sdk/models/__init__.py +78 -0
- grasp_sdk/sandbox/chrome-stable.mjs +381 -0
- grasp_sdk/sandbox/chromium.mjs +378 -0
- grasp_sdk/sandbox/jsconfig.json +22 -0
- grasp_sdk/services/__init__.py +8 -0
- grasp_sdk/services/browser.py +414 -0
- grasp_sdk/services/sandbox.py +583 -0
- grasp_sdk/utils/__init__.py +31 -0
- grasp_sdk/utils/auth.py +227 -0
- grasp_sdk/utils/config.py +150 -0
- grasp_sdk/utils/logger.py +233 -0
- grasp_sdk-0.1.0.dist-info/METADATA +201 -0
- grasp_sdk-0.1.0.dist-info/RECORD +17 -0
- grasp_sdk-0.1.0.dist-info/WHEEL +5 -0
- grasp_sdk-0.1.0.dist-info/entry_points.txt +2 -0
- grasp_sdk-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
import httpProxy from 'http-proxy';
|
|
2
|
+
import http from 'http';
|
|
3
|
+
import { spawn } from 'child_process';
|
|
4
|
+
|
|
5
|
+
import { Logtail } from '@logtail/node';
|
|
6
|
+
import * as Sentry from "@sentry/node";
|
|
7
|
+
|
|
8
|
+
const logtail = new Logtail(process.env.BS_SOURCE_TOKEN, {
|
|
9
|
+
endpoint: `https://${process.env.BS_INGESTING_HOST}`,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
Sentry.init({
|
|
13
|
+
dsn: process.env.SENTRY_DSN,
|
|
14
|
+
|
|
15
|
+
// Setting this option to true will send default PII data to Sentry.
|
|
16
|
+
// For example, automatic IP address collection on events
|
|
17
|
+
sendDefaultPii: true,
|
|
18
|
+
_experiments: {
|
|
19
|
+
enableLogs: true, // 启用日志功能
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const logger = {
|
|
24
|
+
info: async (message, context) => {
|
|
25
|
+
Sentry.logger.info(message, context);
|
|
26
|
+
return logtail.info(message, context);
|
|
27
|
+
},
|
|
28
|
+
warn: async (message, context) => {
|
|
29
|
+
Sentry.logger.warn(message, context);
|
|
30
|
+
return logtail.warn(message, context);
|
|
31
|
+
},
|
|
32
|
+
error: async (message, context) => {
|
|
33
|
+
Sentry.logger.error(message, context);
|
|
34
|
+
return logtail.error(message, context);
|
|
35
|
+
},
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function parseWebSocketFrame(buffer) {
|
|
39
|
+
if (buffer.length < 2) {
|
|
40
|
+
throw new Error('Incomplete WebSocket frame.');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const firstByte = buffer.readUInt8(0);
|
|
44
|
+
const fin = (firstByte & 0x80) !== 0;
|
|
45
|
+
const opcode = firstByte & 0x0f;
|
|
46
|
+
|
|
47
|
+
// 仅处理文本帧(opcode 为 0x1)
|
|
48
|
+
if (opcode !== 0x1) {
|
|
49
|
+
throw new Error(`Unsupported opcode: ${opcode}`);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const secondByte = buffer.readUInt8(1);
|
|
53
|
+
const isMasked = (secondByte & 0x80) !== 0;
|
|
54
|
+
let payloadLength = secondByte & 0x7f;
|
|
55
|
+
let offset = 2;
|
|
56
|
+
|
|
57
|
+
if (payloadLength === 126) {
|
|
58
|
+
if (buffer.length < offset + 2) {
|
|
59
|
+
throw new Error('Incomplete extended payload length.');
|
|
60
|
+
}
|
|
61
|
+
payloadLength = buffer.readUInt16BE(offset);
|
|
62
|
+
offset += 2;
|
|
63
|
+
} else if (payloadLength === 127) {
|
|
64
|
+
if (buffer.length < offset + 8) {
|
|
65
|
+
throw new Error('Incomplete extended payload length.');
|
|
66
|
+
}
|
|
67
|
+
// 注意:JavaScript 无法精确表示超过 2^53 的整数
|
|
68
|
+
const highBits = buffer.readUInt32BE(offset);
|
|
69
|
+
const lowBits = buffer.readUInt32BE(offset + 4);
|
|
70
|
+
payloadLength = highBits * 2 ** 32 + lowBits;
|
|
71
|
+
offset += 8;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
let maskingKey;
|
|
75
|
+
if (isMasked) {
|
|
76
|
+
if (buffer.length < offset + 4) {
|
|
77
|
+
throw new Error('Incomplete masking key.');
|
|
78
|
+
}
|
|
79
|
+
maskingKey = buffer.slice(offset, offset + 4);
|
|
80
|
+
offset += 4;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (buffer.length < offset + payloadLength) {
|
|
84
|
+
throw new Error('Incomplete payload data.');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const payloadData = buffer.slice(offset, offset + payloadLength);
|
|
88
|
+
|
|
89
|
+
if (isMasked) {
|
|
90
|
+
for (let i = 0; i < payloadLength; i++) {
|
|
91
|
+
payloadData[i] ^= maskingKey[i % 4];
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return payloadData.toString('utf8');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const sandboxId = process.env.SANDBOX_ID;
|
|
99
|
+
const cdpPort = Number(process.env.CDP_PORT);
|
|
100
|
+
const headless = process.env.HEADLESS !== 'false';
|
|
101
|
+
const enableAdblock = process.env.ADBLOCK !== 'false';
|
|
102
|
+
const timeoutMS = process.env.SANBOX_TIMEOUT;
|
|
103
|
+
const workspace = process.env.WORKSPACE;
|
|
104
|
+
const args = [];
|
|
105
|
+
|
|
106
|
+
try {
|
|
107
|
+
const asblockPlugin = '/home/user/.config/google-chrome/Default/Extensions/adblock';
|
|
108
|
+
|
|
109
|
+
args.push(
|
|
110
|
+
'--no-sandbox',
|
|
111
|
+
'--disable-dev-shm-usage',
|
|
112
|
+
'--disable-gpu',
|
|
113
|
+
'--disable-software-rasterizer',
|
|
114
|
+
'--user-data-dir=/home/user/.browser-context'
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
if(headless) {
|
|
118
|
+
args.push('--headless=new');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if(enableAdblock) {
|
|
122
|
+
args.push(...[
|
|
123
|
+
`--disable-extensions-except=${asblockPlugin}`,
|
|
124
|
+
`--load-extension=${asblockPlugin}`,
|
|
125
|
+
])
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
args.push(...[
|
|
129
|
+
`--remote-debugging-port=${cdpPort}`,
|
|
130
|
+
'--remote-debugging-address=0.0.0.0',
|
|
131
|
+
...JSON.parse(process.env.BROWSER_ARGS),
|
|
132
|
+
'about:blank',
|
|
133
|
+
]);
|
|
134
|
+
|
|
135
|
+
console.log(args);
|
|
136
|
+
|
|
137
|
+
let chromePath = '/usr/bin/google-chrome';
|
|
138
|
+
|
|
139
|
+
// 启动 Chrome 并启用远程调试
|
|
140
|
+
const chrome = spawn(chromePath, args, {
|
|
141
|
+
env: { ...process.env, DISPLAY: ':99' }
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
chrome.stdout.on('data', (data) => {
|
|
145
|
+
console.log(`stdout: ${data}`);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
chrome.stderr.on('data', (data) => {
|
|
149
|
+
console.error(`stderr: ${data}`);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
chrome.on('close', (code) => {
|
|
153
|
+
console.log(`Chrome process exited with code ${code}`);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
// Keep the process alive
|
|
157
|
+
process.on('SIGTERM', async () => {
|
|
158
|
+
console.log('Received SIGTERM, closing browser...');
|
|
159
|
+
await logger.warn('Received SIGTERM signal', { sandboxId });
|
|
160
|
+
Sentry.addBreadcrumb({
|
|
161
|
+
category: 'process',
|
|
162
|
+
message: 'Received SIGTERM signal',
|
|
163
|
+
level: 'info',
|
|
164
|
+
data: { sandboxId }
|
|
165
|
+
});
|
|
166
|
+
chrome.kill();
|
|
167
|
+
process.exit(0);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
process.on('SIGINT', async () => {
|
|
171
|
+
console.log('Received SIGINT, closing browser...');
|
|
172
|
+
await logger.warn('Received SIGINT signal', { sandboxId });
|
|
173
|
+
Sentry.addBreadcrumb({
|
|
174
|
+
category: 'process',
|
|
175
|
+
message: 'Received SIGINT signal',
|
|
176
|
+
level: 'info',
|
|
177
|
+
data: { sandboxId }
|
|
178
|
+
});
|
|
179
|
+
chrome.kill();
|
|
180
|
+
process.exit(0);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// 创建代理服务:从 ${this.config.cdpPort! + 1} 转发到 127.0.0.1:${this.config.cdpPort!}
|
|
184
|
+
const proxy = httpProxy.createProxyServer({
|
|
185
|
+
target: `http://127.0.0.1:${cdpPort}`,
|
|
186
|
+
ws: true, // Enable WebSocket support
|
|
187
|
+
changeOrigin: true
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// Log WebSocket messages
|
|
191
|
+
proxy.on('open', () => {
|
|
192
|
+
console.log('🔌 CDP WebSocket connection established');
|
|
193
|
+
logger.info('CDP WebSocket connection established', { sandboxId });
|
|
194
|
+
Sentry.addBreadcrumb({
|
|
195
|
+
category: 'websocket',
|
|
196
|
+
message: 'CDP connection established',
|
|
197
|
+
level: 'info',
|
|
198
|
+
data: { sandboxId }
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
proxy.on('proxyReqWs', (proxyReq, req, socket, options, head) => {
|
|
203
|
+
console.log('📡 New CDP WebSocket connection request:', req.url);
|
|
204
|
+
logger.info('New CDP WebSocket connection request', { url: req.url, sandboxId });
|
|
205
|
+
Sentry.addBreadcrumb({
|
|
206
|
+
category: 'websocket',
|
|
207
|
+
message: 'New CDP connection request',
|
|
208
|
+
level: 'info',
|
|
209
|
+
data: { url: req.url, sandboxId }
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
proxy.on('error', (err, req, res) => {
|
|
214
|
+
console.error('❌ CDP WebSocket proxy error:', err);
|
|
215
|
+
logger.error('CDP WebSocket proxy error', { error: err.message, url: req?.url, sandboxId });
|
|
216
|
+
Sentry.captureException(err, {
|
|
217
|
+
tags: { type: 'websocket_proxy_error', sandboxId },
|
|
218
|
+
extra: { url: req?.url }
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
proxy.on('close', async (req, socket, head) => {
|
|
223
|
+
console.log('🔒 CDP WebSocket connection closed');
|
|
224
|
+
await logger.info('CDP WebSocket connection closed', { sandboxId });
|
|
225
|
+
Sentry.addBreadcrumb({
|
|
226
|
+
category: 'websocket',
|
|
227
|
+
message: 'CDP connection closed',
|
|
228
|
+
level: 'info',
|
|
229
|
+
data: { sandboxId }
|
|
230
|
+
});
|
|
231
|
+
process.exit(0);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
const server = http.createServer(async (req, res) => {
|
|
235
|
+
if (req.url === '/json/version' || req.url === '/json/version/') {
|
|
236
|
+
try {
|
|
237
|
+
// 向本地 CDP 发请求,获取原始 JSON
|
|
238
|
+
const jsonRes = await fetch(`http://127.0.0.1:${cdpPort}/json/version`);
|
|
239
|
+
const data = await jsonRes.json();
|
|
240
|
+
// 替换掉本地的 WebSocket 地址为代理暴露地址
|
|
241
|
+
data.webSocketDebuggerUrl = data.webSocketDebuggerUrl.replace(
|
|
242
|
+
`ws://127.0.0.1:${cdpPort}`,
|
|
243
|
+
`wss://${req.headers.host}`
|
|
244
|
+
);
|
|
245
|
+
await logger.info('CDP version info requested', { url: req.url, response: data, sandboxId });
|
|
246
|
+
Sentry.addBreadcrumb({
|
|
247
|
+
category: 'http',
|
|
248
|
+
message: 'CDP version info requested',
|
|
249
|
+
level: 'info',
|
|
250
|
+
data: { url: req.url, response: data, sandboxId }
|
|
251
|
+
});
|
|
252
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
253
|
+
res.end(JSON.stringify(data));
|
|
254
|
+
} catch(ex) {
|
|
255
|
+
console.error('Failed to fetch CDP version:', ex.message);
|
|
256
|
+
await logger.error('Failed to fetch CDP version', { error: ex.message, sandboxId });
|
|
257
|
+
Sentry.captureException(ex, {
|
|
258
|
+
tags: { type: 'cdp_version_error', sandboxId }
|
|
259
|
+
});
|
|
260
|
+
res.writeHead(500);
|
|
261
|
+
res.end('Internal Server Error');
|
|
262
|
+
}
|
|
263
|
+
} else {
|
|
264
|
+
proxy.web(req, res, {}, async (err) => {
|
|
265
|
+
console.error('Proxy error:', err);
|
|
266
|
+
await logger.error('HTTP proxy error', { error: err.message, url: req.url, sandboxId });
|
|
267
|
+
Sentry.captureException(err, {
|
|
268
|
+
tags: { type: 'proxy_error', sandboxId },
|
|
269
|
+
extra: { url: req.url }
|
|
270
|
+
});
|
|
271
|
+
res.writeHead(502);
|
|
272
|
+
res.end('Bad gateway');
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
server.on('upgrade', (req, socket, head) => {
|
|
278
|
+
// 监听 WebSocket 数据
|
|
279
|
+
let _buffers = [];
|
|
280
|
+
socket.on('data', (data) => {
|
|
281
|
+
let message = '';
|
|
282
|
+
try {
|
|
283
|
+
_buffers.push(data);
|
|
284
|
+
// console.log(`💬 ${_buffers.length}`);
|
|
285
|
+
message = parseWebSocketFrame(Buffer.concat(_buffers)); // 复制data不能破坏原始数据
|
|
286
|
+
_buffers.length = 0;
|
|
287
|
+
if (message.startsWith('{')){ // 只解析 JSON 消息
|
|
288
|
+
const parsed = JSON.parse(message);
|
|
289
|
+
console.log('📨 CDP WebSocket message:', parsed);
|
|
290
|
+
logger.info('CDP WebSocket message received', {
|
|
291
|
+
data: parsed,
|
|
292
|
+
sandboxId: process.env.SANDBOX_ID,
|
|
293
|
+
});
|
|
294
|
+
Sentry.addBreadcrumb({
|
|
295
|
+
category: 'websocket',
|
|
296
|
+
message: 'CDP message received',
|
|
297
|
+
level: 'debug',
|
|
298
|
+
data: { ...parsed, sandboxId }
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
} catch (err) {
|
|
302
|
+
const msg = err.message;
|
|
303
|
+
if(!msg.includes('Incomplete')) {
|
|
304
|
+
// 记录解析错误
|
|
305
|
+
console.warn('⚠️ Failed to parse CDP WebSocket message:', err.message, _buffers.length);
|
|
306
|
+
_buffers.length = 0;
|
|
307
|
+
Sentry.captureException(err, {
|
|
308
|
+
tags: { type: 'websocket_error', sandboxId }
|
|
309
|
+
});
|
|
310
|
+
logger.warn('Failed to parse CDP WebSocket message', {
|
|
311
|
+
error: err.message,
|
|
312
|
+
data: message,
|
|
313
|
+
sandboxId: process.env.SANDBOX_ID
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
socket.on('error', (err) => {
|
|
320
|
+
console.error('❌ CDP WebSocket error:', err);
|
|
321
|
+
logger.error('CDP WebSocket error', { error: err.message, sandboxId });
|
|
322
|
+
Sentry.captureException(err, {
|
|
323
|
+
tags: { type: 'websocket_error', sandboxId }
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
proxy.ws(req, socket, head);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
server.listen(cdpPort + 1, '0.0.0.0', () => {
|
|
331
|
+
console.log(`🎯 Proxy server listening on http://0.0.0.0:${cdpPort + 1} → http://127.0.0.1:${cdpPort}`);
|
|
332
|
+
logger.info('Proxy server started', {
|
|
333
|
+
port: cdpPort + 1,
|
|
334
|
+
target: cdpPort,
|
|
335
|
+
sandboxId,
|
|
336
|
+
settings: {
|
|
337
|
+
type: 'chromium',
|
|
338
|
+
args,
|
|
339
|
+
headless,
|
|
340
|
+
enableAdblock,
|
|
341
|
+
timeoutMS,
|
|
342
|
+
workspace,
|
|
343
|
+
sandboxId
|
|
344
|
+
},
|
|
345
|
+
});
|
|
346
|
+
Sentry.addBreadcrumb({
|
|
347
|
+
category: 'server',
|
|
348
|
+
message: 'Proxy server started',
|
|
349
|
+
level: 'info',
|
|
350
|
+
data: {
|
|
351
|
+
port: cdpPort + 1,
|
|
352
|
+
target: cdpPort,
|
|
353
|
+
sandboxId,
|
|
354
|
+
settings: {
|
|
355
|
+
type: 'chrome-stable',
|
|
356
|
+
args,
|
|
357
|
+
headless,
|
|
358
|
+
enableAdblock,
|
|
359
|
+
timeoutMS,
|
|
360
|
+
workspace,
|
|
361
|
+
sandboxId
|
|
362
|
+
},
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
});
|
|
366
|
+
} catch(ex) {
|
|
367
|
+
console.error('Failed to launch Browser:', ex);
|
|
368
|
+
logger.error('Failed to launch Chrome', {
|
|
369
|
+
error: ex.message,
|
|
370
|
+
args,
|
|
371
|
+
headless,
|
|
372
|
+
cdpPort,
|
|
373
|
+
enableAdblock,
|
|
374
|
+
sandboxId
|
|
375
|
+
});
|
|
376
|
+
Sentry.captureException(ex, {
|
|
377
|
+
tags: { type: 'launch_error', sandboxId },
|
|
378
|
+
extra: { args, headless, cdpPort, enableAdblock }
|
|
379
|
+
});
|
|
380
|
+
process.exit(1);
|
|
381
|
+
}
|