-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
134 lines (98 loc) · 3.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const core = require('@actions/core');
const exec = require('@actions/exec');
const tc = require('@actions/tool-cache');
const io = require('@actions/io');
const fs = require("fs");
const path = require("path");
const os = require("os");
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
async function download() {
NGROK_MAC = "https://github.com/cloudflare/cloudflared/releases/download/2022.10.3/cloudflared-darwin-amd64.tgz"
NGROK_Linux = "https://github.com/cloudflare/cloudflared/releases/download/2022.10.3/cloudflared-linux-amd64"
NGROK_Win = "https://github.com/cloudflare/cloudflared/releases/download/2022.10.3/cloudflared-windows-amd64.exe"
let link = NGROK_Win;
let ext = "";
if (os.platform() == "darwin") {
link = NGROK_MAC;
ext = "tgz";
} else if (os.platform() == "linux") {
link = NGROK_Linux;
}
let workingDir = __dirname;
{
core.info("Downloading: " + link);
let img = await tc.downloadTool(link);
core.info("Downloaded file: " + img);
if (os.platform() == "darwin") {
await io.mv(img, path.join(workingDir, "./cf." + ext));
await exec.exec("tar -xzf " + path.join(workingDir, "./cf." + ext));
await io.mv("cloudflared", path.join(workingDir, "cloudflared"));
} else if (os.platform() == "linux") {
await io.mv(img, path.join(workingDir, "./cloudflared"));
await exec.exec("sh", [], { input: "chmod +x " + path.join(workingDir, "./cloudflared")});
} else {
await io.mv(img, path.join(workingDir, "./cloudflared.exe"));
}
}
// if (ext === "tgz") {
// await exec.exec("tar -xzf " + path.join(workingDir, "./cf." + ext));
// await io.mv("cloudflared", path.join(workingDir, "cloudflared"));
// } else if (link === NGROK_MAC) {
// await exec.exec("7za e -y " + path.join(workingDir, "./cf." + ext) + " -o" + workingDir);
// } else {
// await exec.exec("unzip " + path.join(workingDir, "./cf." + ext) + " -d " + workingDir);
// }
}
async function run(protocol, port) {
let workingDir = __dirname;
let cfd = path.join(workingDir, "./cloudflared");
let log = path.join(workingDir, "./cf.log");
if (os.platform() == "win32") {
cfd += ".exe";
cfd = cfd.replace(/^(\w):|\\+/g,'/$1');
log = log.replace(/^(\w):|\\+/g,'/$1');
}
await exec.exec("sh", [], { input: `if ! ${cfd} update; then echo ok;fi` });
await exec.exec("sh", [], { input: `${cfd} tunnel --url ${protocol}://localhost:${port} >${log} 2>&1 &` });
for (let i = 0; i < 10; i++) {
await sleep(5000);
let output = "";
await exec.exec("sh", [], {
input: `cat "${log}" | grep https:// | grep trycloudflare.com | head -1 | cut -d '|' -f 2 | tr -d ' ' | cut -d '/' -f 3`,
listeners: {
stdout: (s) => {
output += s;
core.info(s);
}
}
});
let server = output;//lines[lines.length - 1];
if (!server) {
continue;
}
core.info("server: " + server);
await exec.exec("sh", [], { input: `echo "server=${server}" >> $GITHUB_OUTPUT` });
break;
}
}
async function main() {
let protocol = core.getInput("protocol");
core.info("protocol: " + protocol);
if (!protocol) {
protocol = "tcp";
}
let port = core.getInput("port");
core.info("port: " + port);
if (!port) {
core.setFailed("No port !");
return;
}
await download();
await run(protocol, port);
process.exit();
}
main().catch(ex => {
core.setFailed(ex.message);
});