cm-tunnel 1.1.0 → 1.3.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 +28 -3
- package/dist/Provider.d.ts +1 -0
- package/dist/StaticServer.d.ts +23 -0
- package/dist/StaticServer.js +93 -0
- package/dist/Tunnel.d.ts +4 -1
- package/dist/Tunnel.js +14 -4
- package/dist/index.d.ts +8 -1
- package/dist/index.js +6 -3
- package/dist/providers/ngrok.d.ts +8 -0
- package/dist/providers/ngrok.js +44 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# cm-tunnel
|
|
2
2
|
|
|
3
|
-
Exponha seu servidor HTTP local para a internet através de uma API extremamente simples. Suporta **Cloudflare** e **
|
|
3
|
+
Exponha seu servidor HTTP local para a internet através de uma API extremamente simples. Suporta **Cloudflare**, **LocalTunnel** e **Ngrok**.
|
|
4
4
|
|
|
5
5
|
## Instalação
|
|
6
6
|
|
|
@@ -23,9 +23,14 @@ import tunnel from 'cm-tunnel';
|
|
|
23
23
|
|
|
24
24
|
try {
|
|
25
25
|
const t = await tunnel({
|
|
26
|
-
provider: 'cloudflare', //
|
|
26
|
+
provider: 'cloudflare', // 'cloudflare', 'localtunnel' ou 'ngrok'
|
|
27
27
|
port: 3000,
|
|
28
|
-
subdomain: 'meu-app-unico', // Opcional (
|
|
28
|
+
subdomain: 'meu-app-unico', // Opcional (para localtunnel e ngrok)
|
|
29
|
+
authtoken: 'token_aqui', // Opcional (recomendado para ngrok)
|
|
30
|
+
serve: { // Opcional: Servidor Estático Interno
|
|
31
|
+
html: '<h1>Online!</h1>', // String HTML para servir
|
|
32
|
+
// directory: './public' // Alternativa: Serve uma pasta inteira
|
|
33
|
+
},
|
|
29
34
|
rateLimit: { // Opcional: Bloqueia IPs por excesso de requisições
|
|
30
35
|
maxRequestsPerMinute: 60,
|
|
31
36
|
routes: ['/api/'] // Se vazio, aplica globalmente
|
|
@@ -53,6 +58,26 @@ const t1 = await tunnel.cloudflare({ port: 3000 });
|
|
|
53
58
|
|
|
54
59
|
// Usando localtunnel
|
|
55
60
|
const t2 = await tunnel.localtunnel({ port: 3000 });
|
|
61
|
+
|
|
62
|
+
// Usando ngrok
|
|
63
|
+
const t3 = await tunnel.ngrok({ port: 3000, authtoken: 'seu_token' });
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Servidor HTML/Estático Standalone (Sem Túnel)
|
|
67
|
+
|
|
68
|
+
Você também pode usar a nossa engine de servidor embutida apenas para testar páginas locais rapidamente, sem expô-las à internet (útil para desenvolvimento local):
|
|
69
|
+
|
|
70
|
+
```javascript
|
|
71
|
+
import tunnel from 'cm-tunnel';
|
|
72
|
+
|
|
73
|
+
const localServer = await tunnel.serve({
|
|
74
|
+
html: '<h1>Teste Local</h1>',
|
|
75
|
+
// directory: './public',
|
|
76
|
+
port: 8080 // Opcional
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
console.log('Rodando localmente em:', localServer.url);
|
|
80
|
+
// localServer.stop() para encerrar
|
|
56
81
|
```
|
|
57
82
|
|
|
58
83
|
## Objeto de Retorno
|
package/dist/Provider.d.ts
CHANGED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface ServeOptions {
|
|
2
|
+
html?: string;
|
|
3
|
+
directory?: string;
|
|
4
|
+
port?: number;
|
|
5
|
+
}
|
|
6
|
+
export declare class StaticServer {
|
|
7
|
+
private options;
|
|
8
|
+
private server;
|
|
9
|
+
port: number;
|
|
10
|
+
url: string;
|
|
11
|
+
constructor(options: ServeOptions);
|
|
12
|
+
start(): Promise<{
|
|
13
|
+
port: number;
|
|
14
|
+
url: string;
|
|
15
|
+
stop: () => void;
|
|
16
|
+
}>;
|
|
17
|
+
stop(): void;
|
|
18
|
+
}
|
|
19
|
+
export declare function serve(options: ServeOptions): Promise<{
|
|
20
|
+
port: number;
|
|
21
|
+
url: string;
|
|
22
|
+
stop: () => void;
|
|
23
|
+
}>;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import http from 'http';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
const MIME_TYPES = {
|
|
5
|
+
'.html': 'text/html',
|
|
6
|
+
'.css': 'text/css',
|
|
7
|
+
'.js': 'text/javascript',
|
|
8
|
+
'.json': 'application/json',
|
|
9
|
+
'.png': 'image/png',
|
|
10
|
+
'.jpg': 'image/jpeg',
|
|
11
|
+
'.jpeg': 'image/jpeg',
|
|
12
|
+
'.gif': 'image/gif',
|
|
13
|
+
'.svg': 'image/svg+xml',
|
|
14
|
+
'.txt': 'text/plain',
|
|
15
|
+
'.ico': 'image/x-icon',
|
|
16
|
+
};
|
|
17
|
+
export class StaticServer {
|
|
18
|
+
options;
|
|
19
|
+
server = null;
|
|
20
|
+
port = 0;
|
|
21
|
+
url = '';
|
|
22
|
+
constructor(options) {
|
|
23
|
+
this.options = options;
|
|
24
|
+
}
|
|
25
|
+
start() {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
this.server = http.createServer((req, res) => {
|
|
28
|
+
if (this.options.html) {
|
|
29
|
+
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
30
|
+
res.end(this.options.html);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (this.options.directory) {
|
|
34
|
+
let filePath = path.join(this.options.directory, req.url === '/' ? 'index.html' : (req.url || ''));
|
|
35
|
+
// Basic security check to prevent directory traversal
|
|
36
|
+
if (!filePath.startsWith(path.resolve(this.options.directory))) {
|
|
37
|
+
res.writeHead(403);
|
|
38
|
+
return res.end('Forbidden');
|
|
39
|
+
}
|
|
40
|
+
fs.stat(filePath, (err, stats) => {
|
|
41
|
+
if (err) {
|
|
42
|
+
res.writeHead(404);
|
|
43
|
+
return res.end('Not Found');
|
|
44
|
+
}
|
|
45
|
+
if (stats.isDirectory()) {
|
|
46
|
+
filePath = path.join(filePath, 'index.html');
|
|
47
|
+
}
|
|
48
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
49
|
+
const contentType = MIME_TYPES[ext] || 'application/octet-stream';
|
|
50
|
+
fs.readFile(filePath, (err, content) => {
|
|
51
|
+
if (err) {
|
|
52
|
+
res.writeHead(500);
|
|
53
|
+
return res.end('Internal Server Error');
|
|
54
|
+
}
|
|
55
|
+
res.writeHead(200, { 'Content-Type': contentType });
|
|
56
|
+
res.end(content);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
res.writeHead(400);
|
|
62
|
+
res.end('No serve options provided (html or directory).');
|
|
63
|
+
});
|
|
64
|
+
this.server.on('error', reject);
|
|
65
|
+
const targetPort = this.options.port || 0;
|
|
66
|
+
this.server.listen(targetPort, '127.0.0.1', () => {
|
|
67
|
+
const address = this.server?.address();
|
|
68
|
+
if (address && typeof address !== 'string') {
|
|
69
|
+
this.port = address.port;
|
|
70
|
+
this.url = `http://127.0.0.1:${this.port}`;
|
|
71
|
+
resolve({
|
|
72
|
+
port: this.port,
|
|
73
|
+
url: this.url,
|
|
74
|
+
stop: this.stop.bind(this)
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
reject(new Error('Failed to get server address'));
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
stop() {
|
|
84
|
+
if (this.server) {
|
|
85
|
+
this.server.close();
|
|
86
|
+
this.server = null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
export async function serve(options) {
|
|
91
|
+
const server = new StaticServer(options);
|
|
92
|
+
return await server.start();
|
|
93
|
+
}
|
package/dist/Tunnel.d.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { EventEmitter } from "events";
|
|
2
2
|
import { TunnelResult } from "./Provider.js";
|
|
3
3
|
import { RateLimitOptions } from "./RateLimiter.js";
|
|
4
|
+
import { ServeOptions } from "./StaticServer.js";
|
|
4
5
|
export interface TunnelOptions {
|
|
5
6
|
provider: string;
|
|
6
|
-
port
|
|
7
|
+
port?: number;
|
|
7
8
|
subdomain?: string;
|
|
9
|
+
authtoken?: string;
|
|
8
10
|
rateLimit?: RateLimitOptions | boolean;
|
|
11
|
+
serve?: ServeOptions;
|
|
9
12
|
}
|
|
10
13
|
export interface TunnelInstance extends TunnelResult, EventEmitter {
|
|
11
14
|
on(event: "ready", listener: (url: string) => void): this;
|
package/dist/Tunnel.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { EventEmitter } from "events";
|
|
2
2
|
import { RateLimiter } from "./RateLimiter.js";
|
|
3
|
+
import { StaticServer } from "./StaticServer.js";
|
|
3
4
|
export async function createTunnel(options) {
|
|
4
5
|
if (!options.port || isNaN(options.port)) {
|
|
5
6
|
throw new Error("Porta inválida.");
|
|
@@ -23,10 +24,17 @@ export async function createTunnel(options) {
|
|
|
23
24
|
throw err;
|
|
24
25
|
}
|
|
25
26
|
let rateLimiterInstance = null;
|
|
26
|
-
let
|
|
27
|
+
let staticServerInstance = null;
|
|
28
|
+
// A porta do usuário (ou gerada pelo StaticServer) que o túnel/proxy vai mirar
|
|
29
|
+
let targetPort = options.port || 3000;
|
|
30
|
+
if (options.serve) {
|
|
31
|
+
staticServerInstance = new StaticServer(options.serve);
|
|
32
|
+
const serveResult = await staticServerInstance.start();
|
|
33
|
+
targetPort = serveResult.port; // O túnel ou proxy apontará para o StaticServer
|
|
34
|
+
}
|
|
27
35
|
if (options.rateLimit) {
|
|
28
|
-
rateLimiterInstance = new RateLimiter(
|
|
29
|
-
targetPort = await rateLimiterInstance.start();
|
|
36
|
+
rateLimiterInstance = new RateLimiter(targetPort, options.rateLimit);
|
|
37
|
+
targetPort = await rateLimiterInstance.start(); // O túnel apontará para o proxy
|
|
30
38
|
}
|
|
31
39
|
const providerInstance = new ProviderClass({ ...options, port: targetPort });
|
|
32
40
|
// We wrap the result in an EventEmitter to match the required API
|
|
@@ -34,6 +42,7 @@ export async function createTunnel(options) {
|
|
|
34
42
|
providerInstance.on("ready", (url) => emitter.emit("ready", url));
|
|
35
43
|
providerInstance.on("close", () => {
|
|
36
44
|
rateLimiterInstance?.stop();
|
|
45
|
+
staticServerInstance?.stop();
|
|
37
46
|
emitter.emit("close");
|
|
38
47
|
});
|
|
39
48
|
providerInstance.on("error", (err) => emitter.emit("error", err));
|
|
@@ -43,9 +52,10 @@ export async function createTunnel(options) {
|
|
|
43
52
|
Object.assign(emitter, {
|
|
44
53
|
provider: result.provider,
|
|
45
54
|
url: result.url,
|
|
46
|
-
port: options.port, // Return original user port
|
|
55
|
+
port: options.port || targetPort, // Return original user port or dynamic port
|
|
47
56
|
stop: () => {
|
|
48
57
|
rateLimiterInstance?.stop();
|
|
58
|
+
staticServerInstance?.stop();
|
|
49
59
|
result.stop();
|
|
50
60
|
},
|
|
51
61
|
process: result.process,
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
import { TunnelOptions, TunnelInstance } from
|
|
1
|
+
import { TunnelOptions, TunnelInstance } from './Tunnel.js';
|
|
2
|
+
import { ServeOptions } from './StaticServer.js';
|
|
2
3
|
type OmitProvider<T> = Omit<T, "provider">;
|
|
3
4
|
interface TunnelAPI {
|
|
4
5
|
(options: TunnelOptions): Promise<TunnelInstance>;
|
|
5
6
|
cloudflare: (options: OmitProvider<TunnelOptions>) => Promise<TunnelInstance>;
|
|
6
7
|
localtunnel: (options: OmitProvider<TunnelOptions>) => Promise<TunnelInstance>;
|
|
8
|
+
ngrok: (options: OmitProvider<TunnelOptions>) => Promise<TunnelInstance>;
|
|
9
|
+
serve: (options: ServeOptions) => Promise<{
|
|
10
|
+
port: number;
|
|
11
|
+
url: string;
|
|
12
|
+
stop: () => void;
|
|
13
|
+
}>;
|
|
7
14
|
}
|
|
8
15
|
declare const tunnel: TunnelAPI;
|
|
9
16
|
export default tunnel;
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import { createTunnel } from
|
|
1
|
+
import { createTunnel } from './Tunnel.js';
|
|
2
|
+
import { serve } from './StaticServer.js';
|
|
2
3
|
const tunnel = createTunnel;
|
|
3
|
-
tunnel.cloudflare = (options) => createTunnel({ ...options, provider:
|
|
4
|
-
tunnel.localtunnel = (options) => createTunnel({ ...options, provider:
|
|
4
|
+
tunnel.cloudflare = (options) => createTunnel({ ...options, provider: 'cloudflare' });
|
|
5
|
+
tunnel.localtunnel = (options) => createTunnel({ ...options, provider: 'localtunnel' });
|
|
6
|
+
tunnel.ngrok = (options) => createTunnel({ ...options, provider: 'ngrok' });
|
|
7
|
+
tunnel.serve = serve;
|
|
5
8
|
export default tunnel;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import ngrok from '@ngrok/ngrok';
|
|
2
|
+
import { Provider } from '../Provider.js';
|
|
3
|
+
export default class NgrokProvider extends Provider {
|
|
4
|
+
url = null;
|
|
5
|
+
listener = null;
|
|
6
|
+
constructor(options) {
|
|
7
|
+
super(options);
|
|
8
|
+
}
|
|
9
|
+
start() {
|
|
10
|
+
return new Promise(async (resolve, reject) => {
|
|
11
|
+
try {
|
|
12
|
+
if (this.options.authtoken) {
|
|
13
|
+
ngrok.authtoken(this.options.authtoken);
|
|
14
|
+
}
|
|
15
|
+
this.listener = await ngrok.forward({ addr: this.port, authtoken: this.options.authtoken, domain: this.options.subdomain });
|
|
16
|
+
this.url = this.listener.url() || null;
|
|
17
|
+
if (!this.url) {
|
|
18
|
+
return reject(new Error('Falha ao obter URL do Ngrok.'));
|
|
19
|
+
}
|
|
20
|
+
this.emit('ready', this.url);
|
|
21
|
+
resolve({
|
|
22
|
+
provider: 'ngrok',
|
|
23
|
+
url: this.url,
|
|
24
|
+
port: this.port,
|
|
25
|
+
stop: this.stop.bind(this),
|
|
26
|
+
process: this.listener,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
reject(new Error(`Falha ao iniciar o túnel Ngrok: ${err.message}`));
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
stop() {
|
|
35
|
+
if (this.listener) {
|
|
36
|
+
this.listener.close().then(() => {
|
|
37
|
+
this.emit('close');
|
|
38
|
+
}).catch((err) => {
|
|
39
|
+
this.emit('error', err);
|
|
40
|
+
});
|
|
41
|
+
this.listener = null;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cm-tunnel",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.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",
|
|
@@ -49,6 +49,7 @@
|
|
|
49
49
|
"LICENSE"
|
|
50
50
|
],
|
|
51
51
|
"dependencies": {
|
|
52
|
+
"@ngrok/ngrok": "^1.7.0",
|
|
52
53
|
"bancoz": "^2.2.8",
|
|
53
54
|
"http-proxy": "^1.18.1",
|
|
54
55
|
"localtunnel": "^2.0.2"
|