cm-tunnel 1.2.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 +21 -0
- package/dist/StaticServer.d.ts +23 -0
- package/dist/StaticServer.js +93 -0
- package/dist/Tunnel.d.ts +3 -1
- package/dist/Tunnel.js +14 -4
- package/dist/index.d.ts +7 -1
- package/dist/index.js +3 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,6 +27,10 @@ try {
|
|
|
27
27
|
port: 3000,
|
|
28
28
|
subdomain: 'meu-app-unico', // Opcional (para localtunnel e ngrok)
|
|
29
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
|
+
},
|
|
30
34
|
rateLimit: { // Opcional: Bloqueia IPs por excesso de requisições
|
|
31
35
|
maxRequestsPerMinute: 60,
|
|
32
36
|
routes: ['/api/'] // Se vazio, aplica globalmente
|
|
@@ -59,6 +63,23 @@ const t2 = await tunnel.localtunnel({ port: 3000 });
|
|
|
59
63
|
const t3 = await tunnel.ngrok({ port: 3000, authtoken: 'seu_token' });
|
|
60
64
|
```
|
|
61
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
|
|
81
|
+
```
|
|
82
|
+
|
|
62
83
|
## Objeto de Retorno
|
|
63
84
|
|
|
64
85
|
A função retorna uma Promise que resolve em um objeto `TunnelInstance` contendo:
|
|
@@ -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,12 +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;
|
|
8
9
|
authtoken?: string;
|
|
9
10
|
rateLimit?: RateLimitOptions | boolean;
|
|
11
|
+
serve?: ServeOptions;
|
|
10
12
|
}
|
|
11
13
|
export interface TunnelInstance extends TunnelResult, EventEmitter {
|
|
12
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,10 +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>;
|
|
7
8
|
ngrok: (options: OmitProvider<TunnelOptions>) => Promise<TunnelInstance>;
|
|
9
|
+
serve: (options: ServeOptions) => Promise<{
|
|
10
|
+
port: number;
|
|
11
|
+
url: string;
|
|
12
|
+
stop: () => void;
|
|
13
|
+
}>;
|
|
8
14
|
}
|
|
9
15
|
declare const tunnel: TunnelAPI;
|
|
10
16
|
export default tunnel;
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { createTunnel } from
|
|
1
|
+
import { createTunnel } from './Tunnel.js';
|
|
2
|
+
import { serve } from './StaticServer.js';
|
|
2
3
|
const tunnel = createTunnel;
|
|
3
4
|
tunnel.cloudflare = (options) => createTunnel({ ...options, provider: 'cloudflare' });
|
|
4
5
|
tunnel.localtunnel = (options) => createTunnel({ ...options, provider: 'localtunnel' });
|
|
5
6
|
tunnel.ngrok = (options) => createTunnel({ ...options, provider: 'ngrok' });
|
|
7
|
+
tunnel.serve = serve;
|
|
6
8
|
export default tunnel;
|
package/package.json
CHANGED