-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
143 lines (114 loc) · 3.54 KB
/
Copy pathindex.js
File metadata and controls
143 lines (114 loc) · 3.54 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
const ping = require("ping");
const iohook = require("@tkomde/iohook");
const { exec } = require("child_process");
const axios = require("axios");
let token = "";
let chat_id = "";
try {
const keys = require("./keys");
token = keys.token;
chat_id = keys.chat_id;
} catch (err) {
console.error("Warning: keys.js not found or invalid. Please create keys.js using keys.example.js as a guide.");
}
const TOKEN = token;
const CHAT_ID = chat_id;
async function sendMessage(msg) {
if (!TOKEN || TOKEN === "YOUR_TELEGRAM_BOT_TOKEN") {
console.log("Telegram notification skipped (token not set):", msg);
return;
}
try {
await axios.post(`https://api.telegram.org/bot${TOKEN}/sendMessage`, {
chat_id: CHAT_ID,
text: msg
});
} catch (err) {
console.log("Telegram error:", err.message);
}
}
console.log("Script starting");
sendMessage("Script Started");
const phoneip = "10.23.1.39";
let failcount = 0;
let locked = false;
let isIdle = false;
let lastActivityTime = Date.now();
function updateActivity() {
const now = Date.now();
// prevent spam/jitter updates
if (now - lastActivityTime > 200) {
lastActivityTime = now;
}
}
iohook.on("mousemove", updateActivity);
iohook.on("keydown", updateActivity);
iohook.on("keyup", updateActivity);
iohook.on("mousedown", updateActivity);
iohook.start();
setInterval(() => {
const idleTime = Date.now() - lastActivityTime;
console.log("Idle time:", idleTime);
if (idleTime > 15000) {
isIdle = true;
console.log("User inactive");
} else {
isIdle = false;
}
}, 3000);
setInterval(async () => {
const res = await ping.promise.probe(phoneip);
console.log("ping:", res.alive);
if (!res.alive) {
failcount++;
console.log(`Phone not detected: ${failcount}`);
} else {
failcount = 0;
if (!isIdle) {
locked = false; // reset lock only when user is active again
}
}
if (failcount === 3 && isIdle && !locked) {
sendMessage("Laptop will lock soon");
}
if (failcount >= 4 && isIdle && !locked) {
locked = true;
console.log("Locking system...");
exec("rundll32.exe user32.dll,LockWorkStation");
sendMessage("laptop Locked");
}
}, 2000);
let lastUpdateId = 0;
async function pollTelegramUpdates() {
if (TOKEN && TOKEN !== "YOUR_TELEGRAM_BOT_TOKEN") {
try {
const res = await axios.get(
`https://api.telegram.org/bot${TOKEN}/getUpdates?offset=${lastUpdateId + 1}`
);
const updates = res.data.result || [];
for (let update of updates) {
lastUpdateId = update.update_id;
const message = update.message?.text;
if (!message) continue;
console.log("Received:", message);
// LOCK COMMAND
if (message === "Lock") {
exec("rundll32.exe user32.dll,LockWorkStation");
sendMessage("Laptop Locked");
}
// STATUS COMMAND
if (message === "status") {
if (locked) {
sendMessage("Locked");
} else {
sendMessage("Not locked");
}
}
}
} catch (err) {
console.log("Telegram polling error:", err.message);
}
}
setTimeout(pollTelegramUpdates, 2000);
}
pollTelegramUpdates();