cm-tunnel 1.3.3 → 1.7.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 +775 -106
- package/dist/StaticServer.d.ts +51 -0
- package/dist/StaticServer.js +480 -41
- package/dist/Tunnel.d.ts +2 -0
- package/dist/Tunnel.js +10 -3
- package/dist/cli-utils.d.ts +148 -0
- package/dist/cli-utils.js +400 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +423 -0
- package/dist/config.d.ts +7 -0
- package/dist/config.js +95 -0
- package/dist/dashboard.d.ts +23 -0
- package/dist/dashboard.js +163 -0
- package/dist/doctor.d.ts +2 -0
- package/dist/doctor.js +152 -0
- package/dist/net-utils.d.ts +6 -0
- package/dist/net-utils.js +40 -0
- package/dist/providers/cloudflare.js +3 -1
- package/package.json +13 -7
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { STRINGS } from './cli-utils.js';
|
|
2
|
+
const MAX_LOG_LINES = 10;
|
|
3
|
+
const RESET = '\x1b[0m';
|
|
4
|
+
const BOLD = '\x1b[1m';
|
|
5
|
+
const DIM = '\x1b[2m';
|
|
6
|
+
const GREEN = '\x1b[32m';
|
|
7
|
+
const YELLOW = '\x1b[33m';
|
|
8
|
+
const RED = '\x1b[31m';
|
|
9
|
+
const HIDE_CURSOR = '\x1b[?25l';
|
|
10
|
+
const SHOW_CURSOR = '\x1b[?25h';
|
|
11
|
+
const CLEAR = '\x1b[2J\x1b[H';
|
|
12
|
+
function stripAnsi(text) {
|
|
13
|
+
return text
|
|
14
|
+
.replace(/\x1b\[[0-9;?]*[A-Za-z]/g, '')
|
|
15
|
+
.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f]/g, '');
|
|
16
|
+
}
|
|
17
|
+
function truncate(text, width) {
|
|
18
|
+
if (text.length <= width)
|
|
19
|
+
return text;
|
|
20
|
+
return `${text.slice(0, Math.max(0, width - 1))}…`;
|
|
21
|
+
}
|
|
22
|
+
function modeLabel(plan, s) {
|
|
23
|
+
if (plan.target)
|
|
24
|
+
return s.dashboardModeTarget(plan.target);
|
|
25
|
+
if (plan.mode === 'site')
|
|
26
|
+
return s.dashboardModeSite;
|
|
27
|
+
if (plan.mode === 'api') {
|
|
28
|
+
return plan.apiMode === 'server'
|
|
29
|
+
? s.dashboardModeApiServer
|
|
30
|
+
: s.dashboardModeApi;
|
|
31
|
+
}
|
|
32
|
+
return plan.apiMode === 'server'
|
|
33
|
+
? s.dashboardModeBothServer
|
|
34
|
+
: s.dashboardModeBoth;
|
|
35
|
+
}
|
|
36
|
+
function publishLabel(plan, s) {
|
|
37
|
+
if (plan.publish === 'quick')
|
|
38
|
+
return s.dashboardPublishQuick;
|
|
39
|
+
return s.dashboardPublishDomain(plan.domain ?? '?');
|
|
40
|
+
}
|
|
41
|
+
export class Dashboard {
|
|
42
|
+
lang;
|
|
43
|
+
plan;
|
|
44
|
+
logs = [];
|
|
45
|
+
partial = '';
|
|
46
|
+
url = '';
|
|
47
|
+
localPort;
|
|
48
|
+
status = 'starting';
|
|
49
|
+
renderTimer = null;
|
|
50
|
+
enabled;
|
|
51
|
+
color;
|
|
52
|
+
constructor(lang, plan) {
|
|
53
|
+
this.lang = lang;
|
|
54
|
+
this.plan = plan;
|
|
55
|
+
this.enabled = !!process.stdout.isTTY;
|
|
56
|
+
this.color = this.enabled && !process.env.NO_COLOR;
|
|
57
|
+
}
|
|
58
|
+
get isEnabled() {
|
|
59
|
+
return this.enabled;
|
|
60
|
+
}
|
|
61
|
+
push(chunk) {
|
|
62
|
+
const text = stripAnsi(chunk).replace(/\r/g, '');
|
|
63
|
+
this.partial += text;
|
|
64
|
+
const lines = this.partial.split('\n');
|
|
65
|
+
this.partial = lines.pop() ?? '';
|
|
66
|
+
for (const line of lines) {
|
|
67
|
+
if (line.trim())
|
|
68
|
+
this.logs.push(line);
|
|
69
|
+
}
|
|
70
|
+
if (this.logs.length > MAX_LOG_LINES) {
|
|
71
|
+
this.logs = this.logs.slice(-MAX_LOG_LINES);
|
|
72
|
+
}
|
|
73
|
+
if (this.enabled)
|
|
74
|
+
this.scheduleRender();
|
|
75
|
+
}
|
|
76
|
+
scheduleRender() {
|
|
77
|
+
if (this.renderTimer)
|
|
78
|
+
return;
|
|
79
|
+
this.renderTimer = setTimeout(() => {
|
|
80
|
+
this.renderTimer = null;
|
|
81
|
+
this.render();
|
|
82
|
+
}, 100);
|
|
83
|
+
}
|
|
84
|
+
setUrl(url) {
|
|
85
|
+
this.url = url;
|
|
86
|
+
this.status = 'online';
|
|
87
|
+
if (this.enabled)
|
|
88
|
+
this.render();
|
|
89
|
+
}
|
|
90
|
+
setLocalPort(port) {
|
|
91
|
+
this.localPort = port;
|
|
92
|
+
if (this.enabled)
|
|
93
|
+
this.scheduleRender();
|
|
94
|
+
}
|
|
95
|
+
start() {
|
|
96
|
+
if (!this.enabled)
|
|
97
|
+
return;
|
|
98
|
+
process.stdout.write(HIDE_CURSOR);
|
|
99
|
+
this.render();
|
|
100
|
+
}
|
|
101
|
+
stop() {
|
|
102
|
+
if (this.renderTimer) {
|
|
103
|
+
clearTimeout(this.renderTimer);
|
|
104
|
+
this.renderTimer = null;
|
|
105
|
+
}
|
|
106
|
+
if (!this.enabled)
|
|
107
|
+
return;
|
|
108
|
+
this.status = 'stopped';
|
|
109
|
+
this.render();
|
|
110
|
+
process.stdout.write(`${SHOW_CURSOR}\n`);
|
|
111
|
+
}
|
|
112
|
+
paint(text, code) {
|
|
113
|
+
return this.color ? `${code}${text}${RESET}` : text;
|
|
114
|
+
}
|
|
115
|
+
render() {
|
|
116
|
+
if (!this.enabled)
|
|
117
|
+
return;
|
|
118
|
+
const s = STRINGS[this.lang];
|
|
119
|
+
const width = Math.max(40, Math.min(process.stdout.columns || 100, 120));
|
|
120
|
+
const line = (text) => truncate(text, width);
|
|
121
|
+
const statusText = this.status === 'online'
|
|
122
|
+
? s.dashboardOnline
|
|
123
|
+
: this.status === 'stopped'
|
|
124
|
+
? s.dashboardStopped
|
|
125
|
+
: s.dashboardStarting;
|
|
126
|
+
const statusColor = this.status === 'online'
|
|
127
|
+
? GREEN
|
|
128
|
+
: this.status === 'stopped'
|
|
129
|
+
? RED
|
|
130
|
+
: YELLOW;
|
|
131
|
+
const output = [
|
|
132
|
+
`${this.paint(s.dashboardTitle, BOLD)} ${this.paint(`● ${statusText}`, statusColor)}`,
|
|
133
|
+
'',
|
|
134
|
+
];
|
|
135
|
+
if (this.url) {
|
|
136
|
+
output.push(line(this.paint(s.dashboardUrl(this.url), BOLD)));
|
|
137
|
+
}
|
|
138
|
+
output.push(line(`Modo: ${modeLabel(this.plan, s)}`));
|
|
139
|
+
output.push(line(`Pub: ${publishLabel(this.plan, s)}`));
|
|
140
|
+
if (this.plan.target) {
|
|
141
|
+
output.push(line(s.dashboardLocal(`http://localhost:${this.plan.target}`)));
|
|
142
|
+
}
|
|
143
|
+
else if (this.localPort) {
|
|
144
|
+
output.push(line(s.dashboardLocal(`http://localhost:${this.localPort}`)));
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
output.push(line(s.dashboardAwaitingLocal));
|
|
148
|
+
}
|
|
149
|
+
output.push('');
|
|
150
|
+
output.push(this.paint(s.dashboardLogs, DIM));
|
|
151
|
+
if (this.logs.length === 0) {
|
|
152
|
+
output.push(this.paint(` ${s.dashboardWaitingLogs}`, DIM));
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
for (const log of this.logs) {
|
|
156
|
+
output.push(this.paint(` ${log}`, DIM));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
output.push('');
|
|
160
|
+
output.push(this.paint(s.dashboardCtrlC, DIM));
|
|
161
|
+
process.stdout.write(`${CLEAR}${output.join('\n')}\n`);
|
|
162
|
+
}
|
|
163
|
+
}
|
package/dist/doctor.d.ts
ADDED
package/dist/doctor.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { promises as dns } from 'node:dns';
|
|
3
|
+
import { existsSync, statSync } from 'node:fs';
|
|
4
|
+
import { join, resolve } from 'node:path';
|
|
5
|
+
import { CONFIG_FILE, loadConfig, loadDotEnv } from './config.js';
|
|
6
|
+
import { STRINGS } from './cli-utils.js';
|
|
7
|
+
import { isPortFree, isPortInUse } from './net-utils.js';
|
|
8
|
+
function describePlan(plan) {
|
|
9
|
+
if (plan.target) {
|
|
10
|
+
return `server=${plan.target}, publish=${plan.publish}`;
|
|
11
|
+
}
|
|
12
|
+
const parts = [`mode=${plan.mode}`, `publish=${plan.publish}`];
|
|
13
|
+
if (plan.publish === 'domain') {
|
|
14
|
+
parts.push(`domain=${plan.domain ?? '?'}`, `port=${plan.port ?? '?'}`);
|
|
15
|
+
}
|
|
16
|
+
return parts.join(', ');
|
|
17
|
+
}
|
|
18
|
+
export async function runDoctor(lang) {
|
|
19
|
+
const s = STRINGS[lang];
|
|
20
|
+
let ok = 0;
|
|
21
|
+
let warn = 0;
|
|
22
|
+
let fail = 0;
|
|
23
|
+
const okLine = (message) => {
|
|
24
|
+
ok++;
|
|
25
|
+
console.log(`✔ ${message}`);
|
|
26
|
+
};
|
|
27
|
+
const warnLine = (message) => {
|
|
28
|
+
warn++;
|
|
29
|
+
console.log(`! ${message}`);
|
|
30
|
+
};
|
|
31
|
+
const failLine = (message) => {
|
|
32
|
+
fail++;
|
|
33
|
+
console.log(`✖ ${message}`);
|
|
34
|
+
};
|
|
35
|
+
console.log(`\n${s.doctorTitle}\n`);
|
|
36
|
+
const [major, minor] = process.versions.node.split('.').map(Number);
|
|
37
|
+
if (major > 18 || (major === 18 && minor >= 3)) {
|
|
38
|
+
okLine(s.doctorNodeOk(process.versions.node));
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
failLine(s.doctorNodeFail(process.versions.node));
|
|
42
|
+
}
|
|
43
|
+
const cloudflared = spawnSync('cloudflared', ['--version'], {
|
|
44
|
+
encoding: 'utf8',
|
|
45
|
+
timeout: 10000,
|
|
46
|
+
});
|
|
47
|
+
if (!cloudflared.error && cloudflared.status === 0) {
|
|
48
|
+
const version = (cloudflared.stdout || cloudflared.stderr || '').split(/\r?\n/)[0].trim() ||
|
|
49
|
+
'ok';
|
|
50
|
+
okLine(s.doctorCloudflaredOk(version));
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
failLine(s.doctorCloudflaredFail);
|
|
54
|
+
}
|
|
55
|
+
loadDotEnv();
|
|
56
|
+
const plan = loadConfig();
|
|
57
|
+
if (!plan) {
|
|
58
|
+
if (existsSync(resolve(CONFIG_FILE))) {
|
|
59
|
+
failLine(s.doctorConfigInvalid);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
warnLine(s.doctorConfigMissing);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
okLine(s.doctorConfigOk(describePlan(plan)));
|
|
67
|
+
if (plan.publish === 'domain') {
|
|
68
|
+
const token = process.env.CLOUDFLARE_TOKEN;
|
|
69
|
+
if (token) {
|
|
70
|
+
okLine(s.doctorTokenOk(token.length));
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
failLine(s.doctorTokenMissing);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (!plan.target) {
|
|
77
|
+
const checkFolder = (dir) => {
|
|
78
|
+
if (!dir) {
|
|
79
|
+
failLine(s.doctorFolderMissing('?'));
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const abs = resolve(dir);
|
|
83
|
+
if (existsSync(abs) && statSync(abs).isDirectory()) {
|
|
84
|
+
okLine(s.doctorFolderOk(dir));
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
failLine(s.doctorFolderMissing(dir));
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
if (plan.mode !== 'api')
|
|
91
|
+
checkFolder(plan.public);
|
|
92
|
+
if (plan.mode !== 'site') {
|
|
93
|
+
if (plan.apiMode === 'server') {
|
|
94
|
+
if (plan.api)
|
|
95
|
+
checkFolder(plan.api);
|
|
96
|
+
const entry = plan.apiEntry ?? (plan.api ? join(plan.api, 'index.js') : undefined);
|
|
97
|
+
if (entry) {
|
|
98
|
+
const abs = resolve(entry);
|
|
99
|
+
if (existsSync(abs) && statSync(abs).isFile()) {
|
|
100
|
+
okLine(s.doctorApiEntryOk(entry));
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
failLine(s.doctorApiEntryMissing(entry));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
failLine(s.doctorApiEntryMissing('?'));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
checkFolder(plan.api);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (plan.target) {
|
|
116
|
+
if (await isPortInUse(plan.target)) {
|
|
117
|
+
okLine(s.doctorTargetUp(plan.target));
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
failLine(s.doctorTargetDown(plan.target));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
else if (plan.publish === 'domain' && plan.port) {
|
|
124
|
+
if (await isPortFree(plan.port)) {
|
|
125
|
+
okLine(s.doctorPortFree(plan.port));
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
failLine(s.doctorPortBusy(plan.port));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (plan.publish === 'domain' && plan.domain) {
|
|
132
|
+
try {
|
|
133
|
+
const address = await dns.lookup(plan.domain);
|
|
134
|
+
okLine(s.doctorDnsOk(plan.domain, address.address));
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
failLine(s.doctorDnsFail(plan.domain));
|
|
138
|
+
}
|
|
139
|
+
const localPort = plan.target ?? plan.port ?? 3000;
|
|
140
|
+
warnLine(s.doctorHostnameInfo(plan.domain, localPort));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
console.log(`\n${s.doctorSummary(ok, warn, fail)}`);
|
|
144
|
+
if (fail > 0) {
|
|
145
|
+
console.log(s.doctorHasErrors);
|
|
146
|
+
return 1;
|
|
147
|
+
}
|
|
148
|
+
if (plan) {
|
|
149
|
+
console.log(s.doctorAllGood);
|
|
150
|
+
}
|
|
151
|
+
return 0;
|
|
152
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Retorna uma porta livre em 127.0.0.1. */
|
|
2
|
+
export declare function findFreePort(): Promise<number>;
|
|
3
|
+
/** Retorna true se a porta está livre para um novo servidor. */
|
|
4
|
+
export declare function isPortFree(port: number): Promise<boolean>;
|
|
5
|
+
/** Retorna true se há algo escutando na porta. */
|
|
6
|
+
export declare function isPortInUse(port: number): Promise<boolean>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { createConnection, createServer } from 'node:net';
|
|
2
|
+
/** Retorna uma porta livre em 127.0.0.1. */
|
|
3
|
+
export function findFreePort() {
|
|
4
|
+
return new Promise((resolvePort, reject) => {
|
|
5
|
+
const server = createServer();
|
|
6
|
+
server.once('error', reject);
|
|
7
|
+
server.listen(0, '127.0.0.1', () => {
|
|
8
|
+
const address = server.address();
|
|
9
|
+
if (address && typeof address !== 'string') {
|
|
10
|
+
const port = address.port;
|
|
11
|
+
server.close(() => resolvePort(port));
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
server.close();
|
|
15
|
+
reject(new Error('Não foi possível obter uma porta livre.'));
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
/** Retorna true se a porta está livre para um novo servidor. */
|
|
20
|
+
export function isPortFree(port) {
|
|
21
|
+
return new Promise((resolvePort) => {
|
|
22
|
+
const server = createServer();
|
|
23
|
+
server.once('error', () => resolvePort(false));
|
|
24
|
+
server.once('listening', () => {
|
|
25
|
+
server.close(() => resolvePort(true));
|
|
26
|
+
});
|
|
27
|
+
server.listen(port, '127.0.0.1');
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/** Retorna true se há algo escutando na porta. */
|
|
31
|
+
export function isPortInUse(port) {
|
|
32
|
+
return new Promise((resolvePort) => {
|
|
33
|
+
const socket = createConnection({ port, host: '127.0.0.1' });
|
|
34
|
+
socket.once('connect', () => {
|
|
35
|
+
socket.destroy();
|
|
36
|
+
resolvePort(true);
|
|
37
|
+
});
|
|
38
|
+
socket.once('error', () => resolvePort(false));
|
|
39
|
+
});
|
|
40
|
+
}
|
|
@@ -219,7 +219,9 @@ export default class CloudflareProvider extends Provider {
|
|
|
219
219
|
const lowerText = text.toLowerCase();
|
|
220
220
|
// Guarda as últimas saídas para enriquecer a mensagem de erro caso
|
|
221
221
|
// o processo encerre sem que nenhum padrão conhecido seja detectado.
|
|
222
|
-
|
|
222
|
+
const sanitized = sanitizeOutput(text, token);
|
|
223
|
+
this.options.onLog?.(sanitized);
|
|
224
|
+
this.lastOutput = (this.lastOutput + sanitized).slice(-4000);
|
|
223
225
|
// ---- Detecção de sucesso ----
|
|
224
226
|
if (this.mode === "quick") {
|
|
225
227
|
const match = text.match(this.urlRegex);
|
package/package.json
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cm-tunnel",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.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",
|
|
@@ -34,14 +37,14 @@
|
|
|
34
37
|
"license": "MIT",
|
|
35
38
|
"repository": {
|
|
36
39
|
"type": "git",
|
|
37
|
-
"url": "git+https://github.com/
|
|
40
|
+
"url": "git+https://github.com/cmstudioapps/cm-tunnel.git"
|
|
38
41
|
},
|
|
39
42
|
"bugs": {
|
|
40
|
-
"url": "https://github.com/
|
|
43
|
+
"url": "https://github.com/cmstudioapps/cm-tunnel/issues"
|
|
41
44
|
},
|
|
42
|
-
"homepage": "https://github.com/
|
|
45
|
+
"homepage": "https://github.com/cmstudioapps/cm-tunnel#readme",
|
|
43
46
|
"engines": {
|
|
44
|
-
"node": ">=18.
|
|
47
|
+
"node": ">=18.3.0"
|
|
45
48
|
},
|
|
46
49
|
"files": [
|
|
47
50
|
"dist",
|
|
@@ -51,13 +54,16 @@
|
|
|
51
54
|
"dependencies": {
|
|
52
55
|
"@ngrok/ngrok": "^1.7.0",
|
|
53
56
|
"bancoz": "^2.2.8",
|
|
57
|
+
"cm-tunnel": "^1.5.0",
|
|
54
58
|
"http-proxy": "^1.18.1",
|
|
55
|
-
"localtunnel": "^2.0.2"
|
|
59
|
+
"localtunnel": "^2.0.2",
|
|
60
|
+
"prompts": "^2.4.2"
|
|
56
61
|
},
|
|
57
62
|
"devDependencies": {
|
|
58
63
|
"@types/http-proxy": "^1.17.17",
|
|
59
64
|
"@types/localtunnel": "^2.0.4",
|
|
60
65
|
"@types/node": "^20.12.7",
|
|
66
|
+
"@types/prompts": "^2.4.9",
|
|
61
67
|
"eslint": "^9.1.1",
|
|
62
68
|
"prettier": "^3.2.5",
|
|
63
69
|
"tsup": "^8.0.2",
|