-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
50 lines (40 loc) · 1.62 KB
/
main.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
import * as dotenv from "dotenv"
import readline from "readline";
import fs from "fs";
import axios from "axios";
import * as os from "os"
dotenv.config()
////////////////////////////////////////////////////////////
async function setup() {
await checkEnv()
const info = await getIP()
await sendToTelegram(JSON.stringify({[os.hostname]: info}, null, 2))
}
async function checkEnv() {
const lineReader = readline.createInterface({
input: fs.createReadStream('./default.env')
})
lineReader.on('line', function (line) {
if(line!=="") {
const variable = line.substring(0, line.indexOf("=") > 0 ? line.indexOf("=") : line.length)
if (!Object.keys(process.env).includes(variable)) {
console.error(`the ${variable} environment variable is not set`)
process.exit(101)
}
}
})
lineReader.on('close', function () {
console.debug("All required environment variables are in place")
})
}
setup()
async function sendToTelegram(message) {
await axios.post(`https://api.telegram.org/bot${process.env.BOT_TOKEN}/sendMessage`, `chat_id=${process.env.CHAT_ID}&text=${message}`)
}
async function getIP(){
const interfaces = os.networkInterfaces()
const {data:externalv4} = await axios.get('https://api.ipify.org?format=json')
const {data:externalv6} = await axios.get('https://api64.ipify.org?format=json')
interfaces.External = [{address:externalv4.ip, family: 'IPv4', internal: false}, {address:externalv6.ip===externalv4.ip?"n/a":externalv6.ip, family: 'IPv6', internal: false} ]
return interfaces
}