forked from jedsada-gh/action-connect-ovpn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (73 loc) · 2.28 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
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').trim()
? core.getInput('PING_URL').trim()
: '127.0.0.1'
const fileOVPN = core.getInput('FILE_OVPN').trim()
? core.getInput('FILE_OVPN').trim()
: './.github/vpn/config.ovpn'
const secret = core.getInput('SECRET').trim()
? core.getInput('SECRET').trim()
: process.env.SECRET_USERNAME_PASSWORD.trim()
const tlsKey = core.getInput('TLS_KEY').trim()
? core.getInput('TLS_KEY').trim()
: process.env.TLS_KEY.trim()
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.trim())
createFile('user.crt', process.env.USER_CRT.trim())
createFile('user.key', process.env.USER_KEY.trim())
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)
}