cm-tunnel 1.3.2 → 1.5.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 +605 -106
- package/dist/StaticServer.d.ts +25 -0
- package/dist/StaticServer.js +336 -41
- package/dist/Tunnel.js +34 -22
- package/dist/cli-utils.d.ts +89 -0
- package/dist/cli-utils.js +267 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +426 -0
- package/dist/providers/cloudflare.d.ts +10 -0
- package/dist/providers/cloudflare.js +44 -7
- package/package.json +9 -4
|
@@ -23,11 +23,21 @@ export interface CloudflareOptions {
|
|
|
23
23
|
interface CloudflareProviderOptions extends ProviderOptions {
|
|
24
24
|
cloudflare?: CloudflareOptions;
|
|
25
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Valida o arquivo de credenciais do túnel Cloudflare.
|
|
28
|
+
*
|
|
29
|
+
* O cloudflared exige o JSON gerado por `cloudflared tunnel create`.
|
|
30
|
+
* O cert.pem gerado por `cloudflared tunnel login` serve apenas para
|
|
31
|
+
* gerenciar túneis (criar, listar, apagar) e não pode ser usado no `run`.
|
|
32
|
+
*/
|
|
33
|
+
export declare function validateCredentialsFile(file: string): void;
|
|
26
34
|
export default class CloudflareProvider extends Provider {
|
|
27
35
|
private cp;
|
|
28
36
|
private urlRegex;
|
|
29
37
|
private cloudflareOpts;
|
|
30
38
|
private mode;
|
|
39
|
+
/** Últimas linhas emitidas pelo cloudflared, usadas para enriquecer erros. */
|
|
40
|
+
private lastOutput;
|
|
31
41
|
constructor(options: CloudflareProviderOptions);
|
|
32
42
|
/**
|
|
33
43
|
* Determina o modo efetivo com base nas opções fornecidas.
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { spawn } from "child_process";
|
|
2
2
|
import { existsSync } from "fs";
|
|
3
|
+
import { basename, extname } from "path";
|
|
3
4
|
import { Provider } from "../Provider.js";
|
|
4
5
|
/**
|
|
5
6
|
* Remove tokens e segredos de uma string de log/erro.
|
|
@@ -16,11 +17,32 @@ function sanitizeOutput(text, token) {
|
|
|
16
17
|
function escapeRegex(str) {
|
|
17
18
|
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
18
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Valida o arquivo de credenciais do túnel Cloudflare.
|
|
22
|
+
*
|
|
23
|
+
* O cloudflared exige o JSON gerado por `cloudflared tunnel create`.
|
|
24
|
+
* O cert.pem gerado por `cloudflared tunnel login` serve apenas para
|
|
25
|
+
* gerenciar túneis (criar, listar, apagar) e não pode ser usado no `run`.
|
|
26
|
+
*/
|
|
27
|
+
export function validateCredentialsFile(file) {
|
|
28
|
+
if (extname(file).toLowerCase() !== ".json") {
|
|
29
|
+
throw new Error(`credentialsFile inválido: "${file}". O cloudflared exige o arquivo .json de credenciais do túnel ` +
|
|
30
|
+
`(ex: ~/.cloudflared/<tunnel-id>.json), mas foi informado "${basename(file)}". ` +
|
|
31
|
+
'O cert.pem gerado por "cloudflared tunnel login" não serve para executar o túnel. ' +
|
|
32
|
+
'Gere as credenciais com "cloudflared tunnel create" ou use autenticação por token.');
|
|
33
|
+
}
|
|
34
|
+
if (!existsSync(file)) {
|
|
35
|
+
throw new Error(`Arquivo de credenciais não encontrado: "${file}". ` +
|
|
36
|
+
"Verifique o caminho ou use autenticação por token.");
|
|
37
|
+
}
|
|
38
|
+
}
|
|
19
39
|
export default class CloudflareProvider extends Provider {
|
|
20
40
|
cp = null;
|
|
21
|
-
urlRegex = /(https:\/\/[a-zA-Z0-9-]
|
|
41
|
+
urlRegex = /(https:\/\/([a-zA-Z0-9-]+)\.trycloudflare\.com)/;
|
|
22
42
|
cloudflareOpts;
|
|
23
43
|
mode;
|
|
44
|
+
/** Últimas linhas emitidas pelo cloudflared, usadas para enriquecer erros. */
|
|
45
|
+
lastOutput = "";
|
|
24
46
|
constructor(options) {
|
|
25
47
|
super(options);
|
|
26
48
|
this.cloudflareOpts = options.cloudflare || {};
|
|
@@ -69,10 +91,9 @@ export default class CloudflareProvider extends Provider {
|
|
|
69
91
|
"Forneça o caminho do arquivo JSON de credenciais ou use autenticação por token.");
|
|
70
92
|
}
|
|
71
93
|
}
|
|
72
|
-
// Validar
|
|
73
|
-
if (hasCredentials
|
|
74
|
-
|
|
75
|
-
"Verifique o caminho ou use autenticação por token.");
|
|
94
|
+
// Validar existência e formato do arquivo de credenciais quando informado
|
|
95
|
+
if (hasCredentials) {
|
|
96
|
+
validateCredentialsFile(this.cloudflareOpts.credentialsFile);
|
|
76
97
|
}
|
|
77
98
|
// Token e credenciais não devem ser usados simultaneamente
|
|
78
99
|
if (hasToken && hasCredentials) {
|
|
@@ -196,10 +217,16 @@ export default class CloudflareProvider extends Provider {
|
|
|
196
217
|
const handleOutput = (data) => {
|
|
197
218
|
const text = data.toString();
|
|
198
219
|
const lowerText = text.toLowerCase();
|
|
220
|
+
// Guarda as últimas saídas para enriquecer a mensagem de erro caso
|
|
221
|
+
// o processo encerre sem que nenhum padrão conhecido seja detectado.
|
|
222
|
+
this.lastOutput = (this.lastOutput + sanitizeOutput(text, token)).slice(-4000);
|
|
199
223
|
// ---- Detecção de sucesso ----
|
|
200
224
|
if (this.mode === "quick") {
|
|
201
225
|
const match = text.match(this.urlRegex);
|
|
202
|
-
|
|
226
|
+
// "api.trycloudflare.com" é o endpoint da API da Cloudflare e aparece
|
|
227
|
+
// justamente em mensagens de erro (ex: falha ao criar o quick tunnel).
|
|
228
|
+
// Só a URL com subdomínio aleatório é a URL pública do túnel.
|
|
229
|
+
if (match && match[1] && match[2].toLowerCase() !== "api" && !isResolved) {
|
|
203
230
|
isResolved = true;
|
|
204
231
|
cleanup();
|
|
205
232
|
const url = match[1];
|
|
@@ -227,6 +254,13 @@ export default class CloudflareProvider extends Provider {
|
|
|
227
254
|
finish(new Error(`A porta ${this.port} não possui nenhum servidor ativo. Inicie seu servidor antes de criar o túnel.`));
|
|
228
255
|
return;
|
|
229
256
|
}
|
|
257
|
+
if (lowerText.includes("should be .json") ||
|
|
258
|
+
(lowerText.includes("credentials file") && lowerText.includes(".pem")) ||
|
|
259
|
+
lowerText.includes("cert.pem")) {
|
|
260
|
+
finish(new Error("credentialsFile inválido: o cloudflared exige o arquivo .json de credenciais do túnel, não o cert.pem. " +
|
|
261
|
+
'Gere as credenciais com "cloudflared tunnel create" ou use autenticação por token.'));
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
230
264
|
if (lowerText.includes("invalid token") ||
|
|
231
265
|
lowerText.includes("token is expired") ||
|
|
232
266
|
lowerText.includes("failed to parse token") ||
|
|
@@ -274,8 +308,11 @@ export default class CloudflareProvider extends Provider {
|
|
|
274
308
|
cleanup();
|
|
275
309
|
if (!isResolved && code !== 0) {
|
|
276
310
|
isResolved = true;
|
|
311
|
+
const details = this.lastOutput.trim();
|
|
277
312
|
reject(new Error(`Cloudflared encerrou inesperadamente (código ${code}). ` +
|
|
278
|
-
|
|
313
|
+
(details
|
|
314
|
+
? `Saída do cloudflared:\n${details}`
|
|
315
|
+
: "Verifique se o cloudflared está instalado e configurado corretamente.")));
|
|
279
316
|
}
|
|
280
317
|
this.emit("close");
|
|
281
318
|
});
|
package/package.json
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cm-tunnel",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Exponha seu servidor HTTP local para a internet através do Cloudflare ou LocalTunnel de forma simples",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"module": "./dist/index.js",
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
|
+
"bin": {
|
|
10
|
+
"cm-tunnel": "./dist/cli.js"
|
|
11
|
+
},
|
|
9
12
|
"exports": {
|
|
10
13
|
".": {
|
|
11
14
|
"types": "./dist/index.d.ts",
|
|
@@ -14,7 +17,7 @@
|
|
|
14
17
|
}
|
|
15
18
|
},
|
|
16
19
|
"scripts": {
|
|
17
|
-
"build": "rm -rf dist && tsc",
|
|
20
|
+
"build": "rm -rf dist && tsc && node -e \"require('fs').chmodSync('dist/cli.js', 0o755)\"",
|
|
18
21
|
"test": "vitest run",
|
|
19
22
|
"test:watch": "vitest",
|
|
20
23
|
"lint": "tsc --noEmit",
|
|
@@ -41,7 +44,7 @@
|
|
|
41
44
|
},
|
|
42
45
|
"homepage": "https://github.com/your-username/cm-tunnel#readme",
|
|
43
46
|
"engines": {
|
|
44
|
-
"node": ">=18.
|
|
47
|
+
"node": ">=18.3.0"
|
|
45
48
|
},
|
|
46
49
|
"files": [
|
|
47
50
|
"dist",
|
|
@@ -52,12 +55,14 @@
|
|
|
52
55
|
"@ngrok/ngrok": "^1.7.0",
|
|
53
56
|
"bancoz": "^2.2.8",
|
|
54
57
|
"http-proxy": "^1.18.1",
|
|
55
|
-
"localtunnel": "^2.0.2"
|
|
58
|
+
"localtunnel": "^2.0.2",
|
|
59
|
+
"prompts": "^2.4.2"
|
|
56
60
|
},
|
|
57
61
|
"devDependencies": {
|
|
58
62
|
"@types/http-proxy": "^1.17.17",
|
|
59
63
|
"@types/localtunnel": "^2.0.4",
|
|
60
64
|
"@types/node": "^20.12.7",
|
|
65
|
+
"@types/prompts": "^2.4.9",
|
|
61
66
|
"eslint": "^9.1.1",
|
|
62
67
|
"prettier": "^3.2.5",
|
|
63
68
|
"tsup": "^8.0.2",
|