This repository has been archived by the owner on Jul 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (50 loc) · 1.62 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
const core = require('@actions/core');
const ping = require('ping')
const path = require('path')
function execShellCommand(cmd) {
const exec = require('child_process').exec;
return new Promise((resolve) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
throw error
}
resolve(stdout ? stdout : stderr);
});
});
}
(async() => {
try {
const ovpnFile = core.getInput('ovpn');
const caCrtBase64 = core.getInput('ca-crt');
const userCrtBase64 = core.getInput('user-crt');
const userKeyBase64 = core.getInput('user-key');
const pingURL = core.getInput('ping-url');
const profileAbsolutePath = path.resolve(process.cwd(), ovpnFile)
console.log(`\n\tUse profile: ${profileAbsolutePath}`)
execShellCommand(`echo '${caCrtBase64}' | base64 -d >> ca.crt`)
execShellCommand(`echo '${userCrtBase64}' | base64 -d >> user.crt`)
execShellCommand(`echo '${userKeyBase64}' | base64 -d >> user.key`)
execShellCommand(`sudo openvpn --config ${profileAbsolutePath} --daemon`)
if (pingURL) {
console.log(`Starting to ping ${pingURL} to verify the connect status`)
ping.promise
.probe(pingURL, {
timeout: 10,
min_reply: 10,
})
.then(function (res) {
if (res.alive) {
core.info('Connect vpn passed')
core.setOutput('STATUS', true)
} else {
core.setFailed('Connect vpn failed')
core.setOutput('STATUS', false)
}
})
} else {
core.setOutput('STATUS', true)
}
} catch (error) {
core.setFailed(error.message);
}
})();