node-proxylens 0.0.2 → 1.1.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 +27 -82
- package/bin/proxylens.js +91 -0
- package/index.js +289 -0
- package/lib/ProxyLens.js +289 -0
- package/lib/api.js +51 -0
- package/lib/commands/login.js +24 -0
- package/lib/commands/logout.js +32 -0
- package/lib/commands/whoami.js +30 -0
- package/lib/config.js +30 -0
- package/lib/constants.js +15 -0
- package/lib/modes/local.js +86 -0
- package/lib/modes/tunnel.js +197 -0
- package/lib/ui.js +32 -0
- package/package.json +8 -14
- package/LICENSE +0 -7
- package/out/dist-obf/proxylens.js +0 -61
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
const WebSocket = require('ws');
|
|
2
|
+
const readline = require('readline');
|
|
3
|
+
const { DEFAULT_SERVER } = require('../constants');
|
|
4
|
+
const { getToken } = require('../config');
|
|
5
|
+
const { verifyDomain } = require('../api');
|
|
6
|
+
const { printBanner, logError, logInfo, logWarning, logSuccess } = require('../ui');
|
|
7
|
+
|
|
8
|
+
async function verifyAndRunTunnel(target, listenPort, uiServer, flags, lens) {
|
|
9
|
+
// Determine Root Domain (Default or Custom)
|
|
10
|
+
const serverUrl = flags.serverUrl || DEFAULT_SERVER;
|
|
11
|
+
let rootDomain = flags.rootDomain;
|
|
12
|
+
|
|
13
|
+
if (!rootDomain) {
|
|
14
|
+
// Smart Default: If connecting to connect.proxylens.dev, default tunnels to proxylens.dev
|
|
15
|
+
try {
|
|
16
|
+
const hostname = new URL(serverUrl.replace(/^ws/, 'http')).hostname;
|
|
17
|
+
if (hostname === 'connect.proxylens.dev') {
|
|
18
|
+
rootDomain = 'proxylens.dev';
|
|
19
|
+
} else {
|
|
20
|
+
rootDomain = hostname === 'localhost' ? 'localhost' : hostname;
|
|
21
|
+
}
|
|
22
|
+
} catch (e) {
|
|
23
|
+
rootDomain = 'proxylens.dev';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const token = getToken();
|
|
28
|
+
|
|
29
|
+
// Strict Auth for Custom Subdomains
|
|
30
|
+
if (!token && flags.subdomain) {
|
|
31
|
+
logError(`⛔ Authentication Required`);
|
|
32
|
+
console.error(`To use a custom subdomain, you must be logged in.`);
|
|
33
|
+
console.error(`Run: \x1b[33mproxylens login <your_token>\x1b[0m\n`);
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Verify Domain Ownership BEFORE connecting
|
|
38
|
+
logInfo(`Verifying ownership of ${flags.subdomain}${rootDomain === 'proxylens.dev' ? '.proxylens.dev' : ' on ' + rootDomain}...`);
|
|
39
|
+
const domainCheck = await verifyDomain(token, flags.subdomain, rootDomain);
|
|
40
|
+
|
|
41
|
+
if (!domainCheck.valid) {
|
|
42
|
+
logError(`⛔ Access Denied: ${domainCheck.error}`);
|
|
43
|
+
console.error(`Please register this subdomain in your dashboard first.\nhttps://proxylens.dev/dashboard`);
|
|
44
|
+
process.exit(1);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
runTunnelMode(target, listenPort, flags.subdomain, rootDomain, serverUrl, token, false, lens);
|
|
48
|
+
|
|
49
|
+
// In Tunnel Mode, handle root request to show status
|
|
50
|
+
uiServer.listen(listenPort);
|
|
51
|
+
uiServer.on('request', (req, res) => {
|
|
52
|
+
if (!req.url.startsWith('/_proxylens')) {
|
|
53
|
+
res.writeHead(200);
|
|
54
|
+
res.end('ProxyLens Tunnel Running. Go to /_proxylens to view traffic.');
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function runTunnelMode(target, listenPort, subdomain, rootDomain, serverUrl, token, force, lens) {
|
|
60
|
+
if (!serverUrl.startsWith('ws')) serverUrl = `ws://${serverUrl}`;
|
|
61
|
+
|
|
62
|
+
// Construct URL with extra params
|
|
63
|
+
let tunnelUrl = `${serverUrl}/register?subdomain=${subdomain}&domain=${rootDomain}`;
|
|
64
|
+
if (force) tunnelUrl += '&force=true';
|
|
65
|
+
if (token) tunnelUrl += `&token=${token}`;
|
|
66
|
+
|
|
67
|
+
logInfo(`\n🔭 ProxyLens Tunnel Connecting to ${rootDomain}...`);
|
|
68
|
+
|
|
69
|
+
const ws = new WebSocket(tunnelUrl);
|
|
70
|
+
let pingInterval;
|
|
71
|
+
|
|
72
|
+
ws.on('open', () => {
|
|
73
|
+
logSuccess(`✔ Connected! Public: https://${subdomain}.${rootDomain}`);
|
|
74
|
+
printBanner(target, `https://${subdomain}.${rootDomain}`, listenPort, true);
|
|
75
|
+
|
|
76
|
+
// Keep Alive
|
|
77
|
+
pingInterval = setInterval(() => ws.ping(), 30000);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
ws.on('message', async (data) => {
|
|
81
|
+
try {
|
|
82
|
+
const msg = JSON.parse(data);
|
|
83
|
+
if (msg.type === 'REQUEST') {
|
|
84
|
+
handleTunnelRequest(msg, ws, target, lens);
|
|
85
|
+
}
|
|
86
|
+
} catch (e) { }
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
ws.on('error', (e) => {
|
|
90
|
+
if (e.message.includes('409')) {
|
|
91
|
+
logWarning(`\n⚠ The subdomain '${subdomain}' is already in use.`);
|
|
92
|
+
askUserForceConnect(target, listenPort, subdomain, rootDomain, serverUrl, lens);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (e.message.includes('403')) {
|
|
96
|
+
logError(`\n⛔ Access Denied: Subdomain not allowed or Token invalid.`);
|
|
97
|
+
process.exit(1);
|
|
98
|
+
}
|
|
99
|
+
logError('Tunnel Error: ' + e.message);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
ws.on('close', (code, reason) => {
|
|
103
|
+
clearInterval(pingInterval);
|
|
104
|
+
const reasonStr = reason.toString();
|
|
105
|
+
|
|
106
|
+
if (reasonStr === "Session Replaced") {
|
|
107
|
+
logError('\n⛔ Disconnected: Another client claimed this subdomain.');
|
|
108
|
+
process.exit(0);
|
|
109
|
+
} else if (!code || code === 1006) {
|
|
110
|
+
logError('Connection lost. Retrying in 3s...');
|
|
111
|
+
setTimeout(() => runTunnelMode(target, listenPort, subdomain, rootDomain, serverUrl, token, force, lens), 3000);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function askUserForceConnect(target, listenPort, subdomain, rootDomain, serverUrl, lens) {
|
|
117
|
+
const rl = readline.createInterface({
|
|
118
|
+
input: process.stdin,
|
|
119
|
+
output: process.stdout
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
rl.question('Do you want to close the remote connection and take over? (y/N) ', (answer) => {
|
|
123
|
+
rl.close();
|
|
124
|
+
if (answer.toLowerCase() === 'y') {
|
|
125
|
+
runTunnelMode(target, listenPort, subdomain, rootDomain, serverUrl, getToken(), true, lens); // Force = true
|
|
126
|
+
} else {
|
|
127
|
+
console.log("Aborted.");
|
|
128
|
+
process.exit(0);
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function handleTunnelRequest(msg, ws, targetHost, lens) {
|
|
134
|
+
const { id, method, path, headers, body } = msg.payload;
|
|
135
|
+
const start = Date.now();
|
|
136
|
+
|
|
137
|
+
const baseUrl = targetHost.replace(/\/$/, '');
|
|
138
|
+
const reqPath = path.startsWith('/') ? path : '/' + path;
|
|
139
|
+
const targetUrl = `${baseUrl}${reqPath}`;
|
|
140
|
+
|
|
141
|
+
lens.broadcast({
|
|
142
|
+
id, timestamp: start, clientIp: 'Tunnel User',
|
|
143
|
+
request: { method, path, headers, body: body || null, queryParams: {} },
|
|
144
|
+
pending: true
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const cleanHeaders = {};
|
|
148
|
+
const forbiddenHeaders = ['connection', 'host', 'upgrade', 'keep-alive', 'transfer-encoding', 'content-length'];
|
|
149
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
150
|
+
if (!forbiddenHeaders.includes(key.toLowerCase())) cleanHeaders[key] = value;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const reqOptions = { method, headers: cleanHeaders };
|
|
154
|
+
if (body) reqOptions.body = body;
|
|
155
|
+
|
|
156
|
+
try {
|
|
157
|
+
const fetch = global.fetch || require('node-fetch');
|
|
158
|
+
const res = await fetch(targetUrl, reqOptions);
|
|
159
|
+
const resBuffer = await res.arrayBuffer();
|
|
160
|
+
const nodeBuffer = Buffer.from(resBuffer);
|
|
161
|
+
const resHeaders = {};
|
|
162
|
+
res.headers.forEach((v, k) => resHeaders[k] = v);
|
|
163
|
+
|
|
164
|
+
const logEntry = {
|
|
165
|
+
id, timestamp: start, duration: Date.now() - start,
|
|
166
|
+
clientIp: 'Tunnel User',
|
|
167
|
+
request: { method, path, headers, body },
|
|
168
|
+
response: {
|
|
169
|
+
status: res.status, statusText: res.statusText, headers: resHeaders, body: nodeBuffer.toString('utf8')
|
|
170
|
+
},
|
|
171
|
+
pending: false
|
|
172
|
+
};
|
|
173
|
+
lens.logTransaction(logEntry);
|
|
174
|
+
|
|
175
|
+
ws.send(JSON.stringify({
|
|
176
|
+
type: 'RESPONSE', reqId: id,
|
|
177
|
+
payload: { status: res.status, headers: resHeaders, body: nodeBuffer.toString('base64') }
|
|
178
|
+
}));
|
|
179
|
+
|
|
180
|
+
} catch (err) {
|
|
181
|
+
const logEntry = {
|
|
182
|
+
id, timestamp: start, duration: Date.now() - start,
|
|
183
|
+
clientIp: 'Tunnel User',
|
|
184
|
+
request: { method, path, headers, body },
|
|
185
|
+
response: { status: 502, statusText: 'Bad Gateway', headers: {}, body: `Error connecting to ${targetHost}: ${err.message}` },
|
|
186
|
+
pending: false
|
|
187
|
+
};
|
|
188
|
+
lens.logTransaction(logEntry);
|
|
189
|
+
|
|
190
|
+
ws.send(JSON.stringify({
|
|
191
|
+
type: 'RESPONSE', reqId: id,
|
|
192
|
+
payload: { status: 502, headers: {}, body: Buffer.from('Target connection failed').toString('base64') }
|
|
193
|
+
}));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
module.exports = { verifyAndRunTunnel };
|
package/lib/ui.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
function logError(message) {
|
|
2
|
+
console.error(`\n\x1b[31m${message}\x1b[0m\n`);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
function logSuccess(message) {
|
|
6
|
+
console.log(`\x1b[32m${message}\x1b[0m`);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function logInfo(message) {
|
|
10
|
+
console.log(`\x1b[36m${message}\x1b[0m`);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function logWarning(message) {
|
|
14
|
+
console.log(`\x1b[33m${message}\x1b[0m`);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function printBanner(target, proxyUrl, uiPort, isTunnel) {
|
|
18
|
+
console.log(`\n\x1b[36m🔭 ProxyLens AI Inspector\x1b[0m`);
|
|
19
|
+
console.log(`----------------------------------------`);
|
|
20
|
+
console.log(`Target: \x1b[33m${target}\x1b[0m`);
|
|
21
|
+
console.log(`${isTunnel ? 'Public URL:' : 'Proxy URL:'} \x1b[32m${proxyUrl}\x1b[0m`);
|
|
22
|
+
console.log(`Inspector: \x1b[35mhttp://localhost:${uiPort}/_proxylens\x1b[0m`);
|
|
23
|
+
console.log(`----------------------------------------\n`);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = {
|
|
27
|
+
logError,
|
|
28
|
+
logSuccess,
|
|
29
|
+
logInfo,
|
|
30
|
+
logWarning,
|
|
31
|
+
printBanner
|
|
32
|
+
};
|
package/package.json
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-proxylens",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "CLI for ProxyLens AI Traffic Inspector",
|
|
5
|
-
"main": "lib/
|
|
3
|
+
"version": "1.1.1",
|
|
4
|
+
"description": "Node.js SDK and CLI for ProxyLens AI Traffic Inspector",
|
|
5
|
+
"main": "lib/ProxyLens.js",
|
|
6
6
|
"bin": {
|
|
7
|
-
"proxylens": "
|
|
7
|
+
"proxylens": "./bin/proxylens.js"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
|
-
"
|
|
11
|
-
"obfuscate": "javascript-obfuscator out/dist/proxylens.js --output out/dist-obf/proxylens.js",
|
|
12
|
-
"build": "npm run build-dist && npm run obfuscate",
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
13
11
|
"publish": "npm publish"
|
|
14
12
|
},
|
|
15
13
|
"keywords": [
|
|
@@ -21,14 +19,10 @@
|
|
|
21
19
|
"cli"
|
|
22
20
|
],
|
|
23
21
|
"author": "ProxyLens AI",
|
|
24
|
-
"license": "
|
|
22
|
+
"license": "MIT",
|
|
25
23
|
"dependencies": {
|
|
26
|
-
"
|
|
24
|
+
"ws": "^8.16.0",
|
|
27
25
|
"uuid": "^9.0.1",
|
|
28
|
-
"
|
|
29
|
-
},
|
|
30
|
-
"devDependencies": {
|
|
31
|
-
"javascript-obfuscator": "^5.0.1",
|
|
32
|
-
"esbuild": "^0.19.0"
|
|
26
|
+
"http-proxy": "^1.18.1"
|
|
33
27
|
}
|
|
34
28
|
}
|