-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
161 lines (148 loc) · 4.8 KB
/
script.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const express = require('express');
const Joi = require('joi'); //used for validation
const app = express();
const {
exec
} = require('child_process');
const {
MongoClient
} = require('mongodb');
app.use(express.json());
const getCircularReplacer = () => {
const seen = new WeakSet();
return (key, value) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
};
async function set_mongo(obj) {
/**
* Connection URI. Update <username>, <password>, and <your-cluster-url> to reflect your cluster.
* Example mongodb://<server address>:<server port>/<database name>/<collection name>
*/
const url_mongo = "mongodb://localhost:27017/identity/sites";
const client = new MongoClient(url_mongo, {
useUnifiedTopology: true
});
try {
// Connect to the MongoDB cluster
await client.connect();
await client.db('identity').collection('sites').insertOne(obj);
await client.close();
return search_mongo(obj.urls);
} catch (e) {
await client.close();
return e;
}
}
async function search_mongo(url) {
const url_mongo = "mongodb://localhost:27017/identity/sites";
const client = new MongoClient(url_mongo, {
useUnifiedTopology: true
});
try {
// Connect to the MongoDB cluster
await client.connect();
const queries = {
urls: url,
"technologies.categories.slug": 'cms'
};
const site = await client.db("identity").collection("sites").findOne(queries);
if (site == null) {
const exist_site = await client.db("identity").collection("sites").findOne({
urls: url
});
if (exist_site == null) {
return url
}
return false;
}
const count = 0
const result = 0;
while (true) {
if (site.technologies[count].categories[0].slug == 'cms') {
const name = site.technologies[count].name;
const version = site.technologies[count].version;
global.result = { name, version };
break;
}
count++;
}
// const rest = JSON.stringify(global.result, getCircularReplacer()); //برای نمایش خروجی کوئری در ترمینال
global.result["status"] = true;
await client.close();
return global.result;
} catch (e) {
await client.close();
return e;
}
}
app.get('/identity_site', async (req, res) => {
const result_search = await search_mongo(req.query.url)
if (result_search == req.query.url) {
exec(`node wappalyzer/src/drivers/npm/cli.js ${req.query.url}`, async (err, stdout, stderr) => {
if (err) {
console.error(err);
} else {
try {
// console.log(`stdout: ${stdout}`);
// console.log(`stderr: ${stderr}`);
const data = stdout;
const object = JSON.parse(data);
const urls = {
"urls": req.query.url
};
const technologies = {
"technologies": object.technologies
};
const obj = {
...urls,
...technologies
};
const result = await set_mongo(obj);
if (result) {
res.json(result);
return true;
} else {
res.json({ status: false });
return true;
}
} catch (err) {
res.send(err);
}
}
});
} else if (result_search) {
res.json(result_search);
return true;
} else {
res.json({ status: false });
return true;
}
});
app.get('/remove/identity_site', async (req, res) => {
const url_mongo = "mongodb://localhost:27017/identity/sites";
const client = new MongoClient(url_mongo, {
useUnifiedTopology: true
});
try {
// Connect to the MongoDB cluster
await client.connect();
const queries = {
urls: req.query.url
};
await client.db("identity").collection("sites").findOneAndDelete(queries);
await client.close();
res.json({ status: true });
} catch (e) {
await client.close();
res.json({ status: false });
}
});
const port = process.env.PORT || 8080;
app.listen(port, () => console.log(`Listening on port ${port}..`));