-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions.js
More file actions
154 lines (146 loc) · 3.61 KB
/
Functions.js
File metadata and controls
154 lines (146 loc) · 3.61 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const { default: axios } = require("axios");
const fs = require("fs");
async function fetchSite(domain) {
try {
let response = await axios.get(`https://${domain}`);
if (response.data) {
console.log(domain);
}
} catch (error) {
} finally {
return;
}
}
function sortDomains(domains, rootDomain) {
for (let i = 0; i < domains.subdomains.length; i++) {
console.log(domains.subdomains[i] + "." + rootDomain);
}
console.log("-=== finished Searching ====-");
}
async function getSubDomainData(domain, keys, index) {
if (index == keys.length)
return console.log("Failed to load data \n Try with new Api Keys ");
axios
.get(`https://api.securitytrails.com/v1/domain/${domain}/subdomains`, {
headers: {
APIKEY: keys[index],
},
})
.then((response) => {
const subdomains = response.data;
return sortDomains(subdomains, domain);
})
.catch(async (error) => {
console.log("Error happened try resting api key \n Trying with next api key");
let nextApi = index + 1;
return await getSubDomainData(domain, keys, nextApi);
});
}
async function ReadFile() {
try {
let data = fs.readFileSync(__dirname + "/names.json", "utf-8");
return JSON.parse(data);
} catch (error) {
console.log(error);
return [];
}
}
async function FindByNames(domain) {
let data = await ReadFile();
for (let i = 0; i < data.length; i++) {
let newDomain = data[i] + "." + domain;
await fetchSite(newDomain);
}
console.log("finished searching");
}
async function getStDomains(domain) {
let apiKeys, keys;
try {
apiKeys = fs.readFileSync(__dirname + "/apikeys.json", "utf-8");
apiKeys = JSON.parse(apiKeys);
keys = apiKeys.keys;
} catch (error) {
console.log(error);
}
console.log("Searching subdomains...");
getSubDomainData(domain, keys, 0);
}
function addApiKey(key) {
let apiKeys;
try {
apiKeys = fs.readFileSync(__dirname + "/apikeys.json", "utf-8");
apiKeys = JSON.parse(apiKeys);
} catch (error) {
console.log(error);
}
if (apiKeys) {
if (apiKeys.keys.includes(key))
return console.log("Api key already exists");
apiKeys.keys.push(key);
try {
fs.writeFileSync(
__dirname + "/apikeys.json",
JSON.stringify(apiKeys),
"utf-8"
);
console.log("Api key added");
} catch (error) {
console.log("error: ", error);
}
}
}
function removeApiKey(key) {
let apiKeys;
try {
apiKeys = fs.readFileSync(__dirname + "/apikeys.json", "utf-8");
apiKeys = JSON.parse(apiKeys);
} catch (error) {
console.log(error);
}
if (apiKeys) {
if (!apiKeys.keys.includes(key))
return console.log("Api key does not exists");
let filteredArray = apiKeys.keys.filter((ele) => ele != key);
apiKeys.keys = filteredArray;
try {
fs.writeFileSync(
__dirname + "/apikeys.json",
JSON.stringify(apiKeys),
"utf-8"
);
console.log("Api key Removed");
} catch (error) {
console.log("error: ", error);
}
}
}
function resetApiKey() {
let apiKeys = { keys: [] };
try {
fs.writeFileSync(
__dirname + "/apikeys.json",
JSON.stringify(apiKeys),
"utf-8"
);
console.log("Successfully reset api keys");
} catch (error) {
console.log("error: ", error);
}
}
function allApiKeys() {
let apiKeys;
try {
apiKeys = fs.readFileSync(__dirname + "/apikeys.json", "utf-8");
console.log(apiKeys);
} catch (error) {
console.log(error);
}
}
module.exports = {
getStDomains,
FindByNames,
addApiKey,
removeApiKey,
resetApiKey,
allApiKeys,
};