This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
133 lines (116 loc) · 4.75 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 axios = require('axios');
function getConcatenatedLocations(obj) {
const concatenatedKeysAndValues = {};
function traverse(current, path) {
for (const key in current) {
const value = current[key];
const newPath = path ? `${path}.${key.split(':')[0]}` : key.split(':')[0];
if (Array.isArray(value)) {
value.forEach((item) => {
const [key, val] = item.split(':');
concatenatedKeysAndValues[`${newPath}.${key}`] = val;
});
} else if (typeof value === 'object') {
traverse(value, newPath);
} else {
const [key, val] = value.split(':');
concatenatedKeysAndValues[`${newPath}.${key}`] = val;
}
}
}
traverse(obj);
return concatenatedKeysAndValues;
}
// Run on every change: npm run prepare
async function run() {
const failIfText = core.getInput('failIf').trim(); // any>100 or NA.US.MA.BO>80 or NA.*>90
const target = core.getInput('target') // https://example.com
const locationsInput = JSON.parse(core.getInput('locations')) // ["*"]
const apiKey = core.getInput('api_key')
const failIfArr = failIfText.split(">")
const failIfLocation = failIfArr[0].trim()
const failIfLatency = parseInt(failIfArr[1].trim())
let failedLocations = []
response = {}
if (failIfText !== "") {
core.info(`\u001b[33mFail if: ${failIfText} \x1b[0m`)
}
core.info(`\u001b[33mTarget: ${target} \x1b[0m`)
core.info(`\u001b[33mLocations: ${locationsInput} \x1b[0m`)
try {
const responseLocations = await axios.get(
'https://api.ddosify.com/v1/latency/locations/',
{
headers: {
"X-API-KEY": apiKey,
"Content-Type": "application/json",
"Accept-Encoding": "gzip,deflate,compress"
}
}
);
const responseTest = await axios.post(
'https://api.ddosify.com/v1/latency/test/',
{
"target": target,
"locations": locationsInput
},
{
headers: {
"X-API-KEY": apiKey,
"Content-Type": "application/json",
"Accept-Encoding": "gzip,deflate,compress"
}
}
);
const locations = getConcatenatedLocations(responseLocations.data);
console.log(`┌────────────────────────────────────────────────────────────────────────────────────┐`);
console.log(`| ${"Location".padEnd(20)} | ${"Location Code".padEnd(15)} | ${"Status Code".padEnd(8)} | ${"Latency (ms)".padEnd(10)} | ${"Fail If (ms)".padEnd(8)} |`);
console.log(`├────────────────────────────────────────────────────────────────────────────────────┤`);
for (let locKey in responseTest.data) {
logText = ""
response[locKey + 1] = { ...{ "location": locations[locKey] }, ...{ "location_code": locKey }, ...responseTest.data[locKey] }
latency = responseTest.data[locKey]["latency"];
statusCode = responseTest.data[locKey]["status_code"]
if (statusCode === undefined) {
statusCode = "Fail"
}
latency = responseTest.data[locKey]["latency"]
if (latency === undefined) {
latency = "Fail"
}
logText = `| ${locations[locKey].padEnd(20)} | ${locKey.padEnd(15)} | ${statusCode.toString().padEnd(8)} | ${latency.toString().padEnd(8)} |`;
if (failIfText !== "" && ((failIfLocation === "any") || (locKey.startsWith(failIfLocation.replaceAll("*", ""))))) {
logText += ` >${failIfLatency.toString().padEnd(8)} |`
if (latency !== "Fail" && latency > failIfLatency) {
failedLocations.push(`${latency},${locations[locKey]},${failIfLatency}`)
logText = "\u001b[38;2;255;0;0m" + logText + "\x1b[0m"
}
}
else {
logText += `${"".padEnd(13)} |`
}
core.info(logText);
}
core.setOutput("result", response);
// console.table(response);
for (let i in failedLocations) {
const fLoc = failedLocations[i].split(",")
core.error(`${fLoc[1]} latency: ${fLoc[0]}ms > ${fLoc[2]}ms.`);
}
if (failedLocations.length > 0) {
core.setFailed("")
}
} catch (error) {
if (error.response && error.response.status === 401) {
core.setFailed("api_key is not set. You can get is from https://app.ddosify.com");
}
else if (error.response && error.response.status === 403) {
core.setFailed("api_key is not valid. You can get is from https://app.ddosify.com");
}
else {
core.setFailed(error.message);
}
}
}
run();