-
Notifications
You must be signed in to change notification settings - Fork 61
/
index.js
75 lines (65 loc) · 1.95 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
const path = require('path')
const exec = require('shelljs.exec')
const ping = require('ping')
// GITHUB
const core = require('@actions/core')
try {
// Get input defined in action metadata file
const pingURL = core.getInput('PING_URL')
const fileOVPN = core.getInput('FILE_OVPN')
const secret = core.getInput('SECRET')
const tlsKey = core.getInput('TLS_KEY')
if (process.env.CA_CRT == null) {
core.setFailed(`Can't get ca cert please add CA_CRT in secret`)
process.exit(1)
}
if (process.env.USER_CRT == null) {
core.setFailed(`Can't get user cert please add USER_CRT in secret`)
process.exit(1)
}
if (process.env.USER_KEY == null) {
core.setFailed(`Can't get user key please add USER_KEY in secret`)
process.exit(1)
}
const finalPath = path.resolve(process.cwd(), fileOVPN)
const createFile = (filename, data) => {
if (exec('echo ' + data + ' |base64 -d >> ' + filename).code !== 0) {
core.setFailed(`Can't create file ${filename}`)
process.exit(1)
} else {
if (exec('sudo chmod 600 ' + filename).code !== 0) {
core.setFailed(`Can't set permission file ${filename}`)
process.exit(1)
}
}
}
if (secret !== '') {
createFile('secret.txt', secret)
}
if (tlsKey !== '') {
createFile('tls.key', tlsKey)
}
createFile('ca.crt', process.env.CA_CRT)
createFile('user.crt', process.env.USER_CRT)
createFile('user.key', process.env.USER_KEY)
if (exec(`sudo openvpn --config ${finalPath} --daemon`).code !== 0) {
core.setFailed(`Can't setup config ${finalPath}`)
process.exit(1)
}
ping.promise
.probe(pingURL, {
timeout: 15,
min_reply: 15,
})
.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)
}
})
} catch (error) {
core.setFailed(error.message)
}