locadot 1.3.1 → 1.4.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/dist/index.js +0 -0
- package/dist/lib/cloudflared.js +95 -0
- package/dist/server.js +33 -1
- package/package.json +46 -46
package/dist/index.js
CHANGED
|
File without changes
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.CloudflaredTunnel = void 0;
|
|
7
|
+
const child_process_1 = __importDefault(require("child_process"));
|
|
8
|
+
const process_1 = __importDefault(require("process"));
|
|
9
|
+
const command_exists_1 = require("command-exists");
|
|
10
|
+
class CloudflaredTunnel {
|
|
11
|
+
constructor(cloudflaredPath = "cloudflared") {
|
|
12
|
+
this.childProcess = null;
|
|
13
|
+
this.running = false;
|
|
14
|
+
this.cloudflaredPath = cloudflaredPath;
|
|
15
|
+
this.url = "http://localhost:80";
|
|
16
|
+
this.hostname = "";
|
|
17
|
+
}
|
|
18
|
+
get token() {
|
|
19
|
+
return this._token;
|
|
20
|
+
}
|
|
21
|
+
set token(token) {
|
|
22
|
+
if (token && typeof token === "string") {
|
|
23
|
+
token = token.trim();
|
|
24
|
+
// try to strip out "cloudflared.exe service install"
|
|
25
|
+
const array = token.split(" ");
|
|
26
|
+
if (array.length > 1) {
|
|
27
|
+
for (let i = 0; i < array.length - 1; i++) {
|
|
28
|
+
if (array[i] === "install") {
|
|
29
|
+
token = array[i + 1];
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
this._token = token;
|
|
35
|
+
}
|
|
36
|
+
checkInstalled() {
|
|
37
|
+
return (0, command_exists_1.sync)(this.cloudflaredPath);
|
|
38
|
+
}
|
|
39
|
+
emitChange(msg, code) {
|
|
40
|
+
if (this.change) {
|
|
41
|
+
this.change(this.running, msg, code);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
emitError(msg) {
|
|
45
|
+
if (this.error) {
|
|
46
|
+
this.error(msg);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
start() {
|
|
50
|
+
if (this.childProcess) {
|
|
51
|
+
this.emitError("Already started");
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (!this.checkInstalled()) {
|
|
55
|
+
this.emitError(`Cloudflared error: ${this.cloudflaredPath} is not found`);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (!this.token) {
|
|
59
|
+
this.emitError("Cloudflared error: Token is not set");
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const args = ["tunnel", "--no-autoupdate", "run", "--token", this.token];
|
|
63
|
+
this.running = true;
|
|
64
|
+
this.emitChange("Starting cloudflared");
|
|
65
|
+
this.childProcess = child_process_1.default.spawn(this.cloudflaredPath, args);
|
|
66
|
+
this.childProcess.stdout.pipe(process_1.default.stdout);
|
|
67
|
+
this.childProcess.stderr.pipe(process_1.default.stderr);
|
|
68
|
+
this.childProcess.on("close", (code) => {
|
|
69
|
+
this.running = false;
|
|
70
|
+
this.childProcess = null;
|
|
71
|
+
this.emitChange("Stopped cloudflared", code);
|
|
72
|
+
});
|
|
73
|
+
this.childProcess.on("error", (err) => {
|
|
74
|
+
if (err.code === "ENOENT") {
|
|
75
|
+
this.emitError(`Cloudflared error: ${this.cloudflaredPath} is not found`);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
this.emitError(err);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
this.childProcess.stderr.on("data", (data) => {
|
|
82
|
+
this.emitError(data.toString());
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
stop() {
|
|
86
|
+
this.emitChange("Stopping cloudflared");
|
|
87
|
+
if (this.childProcess) {
|
|
88
|
+
this.childProcess.kill("SIGINT");
|
|
89
|
+
this.childProcess = null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
exports.CloudflaredTunnel = CloudflaredTunnel;
|
|
94
|
+
const cloudflared = new CloudflaredTunnel();
|
|
95
|
+
exports.default = cloudflared;
|
package/dist/server.js
CHANGED
|
@@ -37,20 +37,52 @@ class ProxyHandler {
|
|
|
37
37
|
res.writeHead(502, { "Content-Type": "text/html" });
|
|
38
38
|
res.end((0, utils_1.proxyNotFound)(host));
|
|
39
39
|
}
|
|
40
|
-
proxy.web(req, res, {
|
|
40
|
+
proxy.web(req, res, {
|
|
41
|
+
target: `http://localhost:${targetPort}`,
|
|
42
|
+
changeOrigin: true,
|
|
43
|
+
headers: {
|
|
44
|
+
"X-Original-Host": host || "localhost",
|
|
45
|
+
},
|
|
46
|
+
}, (err) => {
|
|
41
47
|
locadot_file_1.default.updateLogs(host || "", "warn", `${err.name}=> ${err.message}`);
|
|
42
48
|
res.writeHead(502);
|
|
43
49
|
res.end("Connection failed. Host not found.");
|
|
44
50
|
});
|
|
45
51
|
};
|
|
52
|
+
const requestUpgrade = (req, socket, head) => {
|
|
53
|
+
const host = req.headers.host?.split(":")[0];
|
|
54
|
+
const targetPort = domainMap[host];
|
|
55
|
+
if (!targetPort) {
|
|
56
|
+
socket.end();
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
// locadotFile.updateLogs(
|
|
60
|
+
// host || "",
|
|
61
|
+
// "log",
|
|
62
|
+
// `WebSocket upgrade request for ${host} → localhost:${targetPort}`
|
|
63
|
+
// );
|
|
64
|
+
proxy.ws(req, socket, head, {
|
|
65
|
+
target: `ws://localhost:${targetPort}`,
|
|
66
|
+
ws: true,
|
|
67
|
+
changeOrigin: true,
|
|
68
|
+
headers: {
|
|
69
|
+
host: `localhost:${targetPort}`,
|
|
70
|
+
},
|
|
71
|
+
}, (err) => {
|
|
72
|
+
locadot_file_1.default.updateLogs(host || "", "warn", `WebSocket Error: ${err.name}=> ${err.message}`);
|
|
73
|
+
socket.end();
|
|
74
|
+
});
|
|
75
|
+
};
|
|
46
76
|
const httpsServer = https_1.default.createServer(defaultCert, requestHandler);
|
|
47
77
|
const httpServer = http_1.default.createServer(requestHandler);
|
|
48
78
|
httpsServer.listen(443, () => {
|
|
49
79
|
locadot_file_1.default.updateLogs("🛜 HTTPS proxy running on port 443");
|
|
50
80
|
});
|
|
81
|
+
httpsServer.on("upgrade", requestUpgrade);
|
|
51
82
|
httpServer.listen(80, () => {
|
|
52
83
|
locadot_file_1.default.updateLogs("🛜 HTTP proxy running on port 80");
|
|
53
84
|
});
|
|
85
|
+
httpServer.on("upgrade", requestUpgrade);
|
|
54
86
|
}
|
|
55
87
|
async startProxy() {
|
|
56
88
|
if (await locadot_file_1.default.getProcessId()) {
|
package/package.json
CHANGED
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "locadot",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Secure your local development environment with HTTPS and custom domains like dev.localhost.",
|
|
5
|
-
"homepage": "https://www.npmjs.com/package/locadot",
|
|
6
|
-
"main": "dist/index.js",
|
|
7
|
-
"bin": {
|
|
8
|
-
"locadot": "./dist/index.js"
|
|
9
|
-
},
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
},
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"https"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
"https"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
},
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
}
|
|
46
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "locadot",
|
|
3
|
+
"version": "1.4.1",
|
|
4
|
+
"description": "Secure your local development environment with HTTPS and custom domains like dev.localhost.",
|
|
5
|
+
"homepage": "https://www.npmjs.com/package/locadot",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"bin": {
|
|
8
|
+
"locadot": "./dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"dev": "tsx --watch src/index.ts",
|
|
12
|
+
"build": "tsc"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/avinashid/locadot.git"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"locadot",
|
|
20
|
+
"https",
|
|
21
|
+
"ssl",
|
|
22
|
+
"https-proxy",
|
|
23
|
+
"localhost",
|
|
24
|
+
"https"
|
|
25
|
+
],
|
|
26
|
+
"author": "avinashid",
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/avinashid/locadot/issues"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@types/http-proxy": "^1.17.16",
|
|
33
|
+
"@types/yargs": "^17.0.33",
|
|
34
|
+
"appdata-path": "^1.0.0",
|
|
35
|
+
"date-fns": "^4.1.0",
|
|
36
|
+
"detect-port": "^2.1.0",
|
|
37
|
+
"http-proxy": "^1.18.1",
|
|
38
|
+
"https": "^1.0.0",
|
|
39
|
+
"mkcert": "^3.2.0",
|
|
40
|
+
"yargs": "^17.7.2"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"tsx": "^4.19.3",
|
|
44
|
+
"typescript": "^5.8.3"
|
|
45
|
+
}
|
|
46
|
+
}
|