adspower-browser 2.1.6 → 2.1.7-beta.1
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 +34 -0
- package/cli/authWorker.js +215 -0
- package/cli/index.js +218 -83
- package/cwd/lib/main.min.js +2 -2
- package/package.json +1 -1
package/README.MD
CHANGED
|
@@ -228,3 +228,37 @@ docker-compose -f ./docker-compose.yml up -d
|
|
|
228
228
|
|
|
229
229
|
docker-compose exec adspower-cli /bin/bash
|
|
230
230
|
```
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
## WorkBuddy client authorization
|
|
234
|
+
|
|
235
|
+
Set `IS_WORKBUDDY=1` for every CLI invocation in this mode (the Connector injects it):
|
|
236
|
+
|
|
237
|
+
```bash
|
|
238
|
+
export IS_WORKBUDDY=1
|
|
239
|
+
ads login
|
|
240
|
+
ads logstatus
|
|
241
|
+
ads get-browser-list '{"page":1,"limit":10}'
|
|
242
|
+
ads logout
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
With the variable present, login/logout/logstatus replace start/stop/restart/status.
|
|
246
|
+
Unset it for normal runtime commands. Presence is the switch, including an empty value or `0`.
|
|
247
|
+
`login --loginhost https://app-wx.loginkits.com` selects an alternate authorization site;
|
|
248
|
+
the default is https://app.adspower.com. Login no longer accepts --base-url or starts a runtime.
|
|
249
|
+
The website opens `adsPowerGlobal://auth` with source=workbuddy and the one-time callback challenge.
|
|
250
|
+
The logged-in desktop client returns apiKey, apiPort (Local API), rpaPort (RPA Plus), and userId.
|
|
251
|
+
Both actual services must be ready. The callback worker survives foreground process termination,
|
|
252
|
+
waits five minutes, uses a random callback port and serializes login workers on port 59753.
|
|
253
|
+
|
|
254
|
+
Credentials persist in `~/.adspowerCli/<md5(userId)>` with mode 0600 on POSIX;
|
|
255
|
+
`client-user.json` selects the current user without containing the key.
|
|
256
|
+
Business commands use this saved connection, independently of normal runtime PID files.
|
|
257
|
+
`logstatus` prints JSON and exits 0 only when all four saved fields are valid; it does not probe
|
|
258
|
+
client liveness or validate the key remotely. Reauthorize after client ports or credentials change.
|
|
259
|
+
`logout` cancels pending login and removes the current user's connection and index;
|
|
260
|
+
it does not stop the desktop client or revoke the server key. Other user files remain until explicitly logged out.
|
|
261
|
+
|
|
262
|
+
Deploy the matching frontend to both the authorization site and the desktop renderer site.
|
|
263
|
+
The native client must forward the complete protocol URL through renderer-scheme.
|
|
264
|
+
The connector's unAuth maps to ads logout. Build/package locally before testing; local changes do not update npm.
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (let key of __getOwnPropNames(from))
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
12
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
+
}
|
|
14
|
+
return to;
|
|
15
|
+
};
|
|
16
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
17
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
18
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
19
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
20
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
|
|
25
|
+
// src/authWorker.ts
|
|
26
|
+
var import_node_fs2 = require("fs");
|
|
27
|
+
var import_node_path3 = __toESM(require("path"));
|
|
28
|
+
var import_node_crypto3 = require("crypto");
|
|
29
|
+
var import_node_net2 = require("net");
|
|
30
|
+
|
|
31
|
+
// src/auth.ts
|
|
32
|
+
var import_node_child_process = require("child_process");
|
|
33
|
+
var import_node_fs = require("fs");
|
|
34
|
+
var import_node_os = __toESM(require("os"));
|
|
35
|
+
var import_node_path = __toESM(require("path"));
|
|
36
|
+
var import_node_net = require("net");
|
|
37
|
+
var pendingPath = import_node_path.default.join(import_node_os.default.homedir(), ".adspowerCli", "workbuddy-login.json");
|
|
38
|
+
|
|
39
|
+
// src/clientSession.ts
|
|
40
|
+
var fs3 = __toESM(require("fs"));
|
|
41
|
+
var import_node_path2 = __toESM(require("path"));
|
|
42
|
+
var import_node_crypto = require("crypto");
|
|
43
|
+
|
|
44
|
+
// src/tools/paths.ts
|
|
45
|
+
var os2 = __toESM(require("os"));
|
|
46
|
+
var path2 = __toESM(require("path"));
|
|
47
|
+
var fs2 = __toESM(require("fs"));
|
|
48
|
+
var import_fs_extra2 = require("fs-extra2");
|
|
49
|
+
var getHomedir = () => {
|
|
50
|
+
return (typeof os2.homedir == "function" ? os2.homedir() : process.env[process.platform == "win32" ? "USERPROFILE" : "HOME"]) || "~";
|
|
51
|
+
};
|
|
52
|
+
var getPidFileDir = (create = true) => {
|
|
53
|
+
const dir = path2.join(getHomedir(), ".adspowerCli");
|
|
54
|
+
if (create && !fs2.existsSync(dir)) {
|
|
55
|
+
(0, import_fs_extra2.ensureDirSync)(dir);
|
|
56
|
+
}
|
|
57
|
+
return dir;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// src/clientSession.ts
|
|
61
|
+
var userFile = (userId) => import_node_path2.default.join(getPidFileDir(false), (0, import_node_crypto.createHash)("md5").update(userId).digest("hex"));
|
|
62
|
+
var indexFile = () => import_node_path2.default.join(getPidFileDir(false), "client-user.json");
|
|
63
|
+
var validPort = (value) => Number.isInteger(value) && Number(value) > 0 && Number(value) <= 65535;
|
|
64
|
+
function isClientSession(value) {
|
|
65
|
+
if (!value || typeof value !== "object") return false;
|
|
66
|
+
const session = value;
|
|
67
|
+
return typeof session.apiKey === "string" && !!session.apiKey.trim() && session.apiKey.length <= 4096 && !/[\r\n]/.test(session.apiKey) && typeof session.userId === "string" && !!session.userId.trim() && session.userId.length <= 256 && session.userId !== "1.0.0" && validPort(session.apiPort) && validPort(session.rpaPort);
|
|
68
|
+
}
|
|
69
|
+
function writePrivateJson(file, value) {
|
|
70
|
+
const temporary = `${file}.${(0, import_node_crypto.randomUUID)()}.tmp`;
|
|
71
|
+
try {
|
|
72
|
+
fs3.writeFileSync(temporary, JSON.stringify(value), { mode: 384, flag: "wx" });
|
|
73
|
+
fs3.renameSync(temporary, file);
|
|
74
|
+
} finally {
|
|
75
|
+
fs3.rmSync(temporary, { force: true });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function saveClientSession(session) {
|
|
79
|
+
if (!isClientSession(session)) throw new Error("Incomplete client connection");
|
|
80
|
+
getPidFileDir();
|
|
81
|
+
const { apiKey, userId, apiPort, rpaPort } = session;
|
|
82
|
+
writePrivateJson(userFile(userId), { apiKey, userId, apiPort, rpaPort });
|
|
83
|
+
writePrivateJson(indexFile(), { userId });
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// src/loginReceiver.ts
|
|
87
|
+
var import_node_http = require("http");
|
|
88
|
+
var import_node_crypto2 = require("crypto");
|
|
89
|
+
async function createLoginReceiver(origin, onKey, onAccepted) {
|
|
90
|
+
const allowedOrigins = /* @__PURE__ */ new Set([origin, "https://app.adspower.com", "https://app.adspower.net"]);
|
|
91
|
+
const state = (0, import_node_crypto2.randomBytes)(32).toString("base64url");
|
|
92
|
+
let consumed = false;
|
|
93
|
+
const server = (0, import_node_http.createServer)(async (req, res) => {
|
|
94
|
+
const address2 = server.address();
|
|
95
|
+
if (!address2 || typeof address2 === "string" || req.headers.host !== `127.0.0.1:${address2.port}` || !allowedOrigins.has(req.headers.origin || "") || req.url !== "/callback") {
|
|
96
|
+
res.writeHead(403).end();
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
res.setHeader("Access-Control-Allow-Origin", req.headers.origin);
|
|
100
|
+
res.setHeader("Vary", "Origin");
|
|
101
|
+
res.setHeader("Cache-Control", "no-store");
|
|
102
|
+
if (req.method === "OPTIONS") {
|
|
103
|
+
res.setHeader("Access-Control-Allow-Methods", "POST");
|
|
104
|
+
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
|
105
|
+
res.setHeader("Access-Control-Allow-Private-Network", "true");
|
|
106
|
+
res.writeHead(204).end();
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (req.method !== "POST" || req.headers["content-type"]?.split(";")[0] !== "application/json") {
|
|
110
|
+
res.writeHead(405).end();
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
const body = await readBody(req);
|
|
115
|
+
if (typeof body.state !== "string" || Buffer.byteLength(body.state) !== Buffer.byteLength(state) || !(0, import_node_crypto2.timingSafeEqual)(Buffer.from(body.state), Buffer.from(state)) || consumed) {
|
|
116
|
+
res.writeHead(403).end();
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (!isClientSession(body)) {
|
|
120
|
+
res.writeHead(400).end();
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
consumed = true;
|
|
124
|
+
await onKey(body);
|
|
125
|
+
if (onAccepted) res.once("finish", onAccepted);
|
|
126
|
+
res.setHeader("Content-Type", "application/json");
|
|
127
|
+
res.end(JSON.stringify({ accepted: true }));
|
|
128
|
+
} catch {
|
|
129
|
+
res.writeHead(400).end();
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
server.requestTimeout = 1e4;
|
|
133
|
+
server.headersTimeout = 1e4;
|
|
134
|
+
await new Promise((resolve, reject) => {
|
|
135
|
+
server.once("error", reject);
|
|
136
|
+
server.listen(0, "127.0.0.1", resolve);
|
|
137
|
+
});
|
|
138
|
+
const address = server.address();
|
|
139
|
+
if (!address || typeof address === "string") throw new Error("Invalid receiver address");
|
|
140
|
+
const url = new URL("/login", origin);
|
|
141
|
+
url.searchParams.set("source", "workbuddy");
|
|
142
|
+
url.hash = new URLSearchParams({ workbuddy_port: String(address.port), workbuddy_state: state }).toString();
|
|
143
|
+
return { url: url.href, close: () => {
|
|
144
|
+
server.close();
|
|
145
|
+
server.closeAllConnections();
|
|
146
|
+
} };
|
|
147
|
+
}
|
|
148
|
+
async function readBody(req) {
|
|
149
|
+
let body = "";
|
|
150
|
+
for await (const chunk of req) {
|
|
151
|
+
body += chunk.toString();
|
|
152
|
+
if (Buffer.byteLength(body) > 8192) throw new Error("Body too large");
|
|
153
|
+
}
|
|
154
|
+
return JSON.parse(body);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// src/authWorker.ts
|
|
158
|
+
async function main() {
|
|
159
|
+
const origin = new URL(process.argv[2] || "https://app.adspower.com").origin;
|
|
160
|
+
const lock = (0, import_node_net2.createServer)((socket) => socket.destroy());
|
|
161
|
+
await new Promise((resolve, reject) => {
|
|
162
|
+
lock.once("error", reject);
|
|
163
|
+
lock.listen({ host: "127.0.0.1", port: 59753, exclusive: true }, resolve);
|
|
164
|
+
});
|
|
165
|
+
const id = (0, import_node_crypto3.randomUUID)();
|
|
166
|
+
const ownsSession = async () => {
|
|
167
|
+
try {
|
|
168
|
+
return JSON.parse(await import_node_fs2.promises.readFile(pendingPath, "utf8")).id === id;
|
|
169
|
+
} catch {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
let finishing = false;
|
|
174
|
+
let receiver;
|
|
175
|
+
let expiry;
|
|
176
|
+
let cancellation;
|
|
177
|
+
const finish = async () => {
|
|
178
|
+
if (finishing) return;
|
|
179
|
+
finishing = true;
|
|
180
|
+
clearTimeout(expiry);
|
|
181
|
+
clearInterval(cancellation);
|
|
182
|
+
receiver?.close();
|
|
183
|
+
try {
|
|
184
|
+
if (await ownsSession()) await import_node_fs2.promises.rm(pendingPath, { force: true });
|
|
185
|
+
} finally {
|
|
186
|
+
lock.close();
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
try {
|
|
190
|
+
await import_node_fs2.promises.mkdir(import_node_path3.default.dirname(pendingPath), { recursive: true, mode: 448 });
|
|
191
|
+
await import_node_fs2.promises.writeFile(pendingPath, JSON.stringify({ id, expiresAt: Date.now() + 3e5 }), { mode: 384 });
|
|
192
|
+
receiver = await createLoginReceiver(origin, async (session) => {
|
|
193
|
+
if (finishing || !await ownsSession()) throw new Error("Login cancelled");
|
|
194
|
+
saveClientSession(session);
|
|
195
|
+
}, () => {
|
|
196
|
+
void finish();
|
|
197
|
+
});
|
|
198
|
+
expiry = setTimeout(() => {
|
|
199
|
+
void finish();
|
|
200
|
+
}, 3e5);
|
|
201
|
+
cancellation = setInterval(() => {
|
|
202
|
+
void ownsSession().then((owns) => {
|
|
203
|
+
if (!owns) void finish();
|
|
204
|
+
});
|
|
205
|
+
}, 500);
|
|
206
|
+
process.send?.({ url: receiver.url });
|
|
207
|
+
} catch {
|
|
208
|
+
await finish();
|
|
209
|
+
throw new Error("Unable to initialize login receiver");
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
main().catch(() => {
|
|
213
|
+
process.send?.({ error: "Unable to start login. Check port 59753, or run ads logout to cancel a pending login." });
|
|
214
|
+
process.exitCode = 1;
|
|
215
|
+
});
|