-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBotProtector.ts
More file actions
234 lines (193 loc) · 7.9 KB
/
BotProtector.ts
File metadata and controls
234 lines (193 loc) · 7.9 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { google } from 'googleapis';
class BotProtector {
private requestLog: Map<string, number[]> = new Map();
private blockedIPs: Set<string> = new Set();
private readonly RATE_LIMIT = 100; // requests per minute
private readonly BLOCK_DURATION = 3600000; // 1 hour in milliseconds
private youtube: any;
private viewPatterns: Map<string, any> = new Map();
private readonly VIEW_THRESHOLD = 30; // suspicious views per minute
private readonly VIEW_PATTERN_WINDOW = 300000; // 5 minutes
constructor(apiKey: string) {
this.youtube = google.youtube({
version: 'v3',
auth: apiKey
});
this.startViewMonitoring();
}
public checkRequest(ip: string): boolean {
if (this.blockedIPs.has(ip)) {
return false;
}
const now = Date.now();
const userRequests = this.requestLog.get(ip) || [];
// Remove old requests
const recentRequests = userRequests.filter(time => now - time < 60000);
if (recentRequests.length >= this.RATE_LIMIT) {
this.blockIP(ip);
return false;
}
recentRequests.push(now);
this.requestLog.set(ip, recentRequests);
return true;
}
private blockIP(ip: string): void {
this.blockedIPs.add(ip);
setTimeout(() => {
this.blockedIPs.delete(ip);
}, this.BLOCK_DURATION);
}
public isPatternSuspicious(behavior: string[]): boolean {
// Check for repeated identical actions
const uniqueActions = new Set(behavior);
if (uniqueActions.size === 1 && behavior.length > 10) {
return true;
}
// Check for rapid succession of actions
const timeThreshold = behavior.length > 5 &&
(parseInt(behavior[behavior.length - 1]) - parseInt(behavior[0])) < 1000;
return timeThreshold;
}
public async checkYouTubeComment(comment: string): Promise<boolean> {
const spamPatterns = [
/check.+my.+channel/i,
/subscribe.+back/i,
/sub4sub/i,
/follow.+me/i,
/want.+free.+subscribers/i
];
// Check for spam patterns
if (spamPatterns.some(pattern => pattern.test(comment))) {
return false;
}
// Check for excessive links
const linkCount = (comment.match(/http[s]?:\/\//g) || []).length;
if (linkCount > 2) {
return false;
}
return true;
}
public async monitorChannel(channelId: string): Promise<void> {
try {
setInterval(async () => {
const response = await this.youtube.activities.list({
part: 'snippet',
channelId: channelId,
maxResults: 50
});
for (const activity of response.data.items) {
if (this.isSpamActivity(activity)) {
await this.handleSpamActivity(activity);
}
}
}, 60000); // Check every minute
} catch (error) {
console.error('Error monitoring channel:', error);
}
}
private isSpamActivity(activity: any): boolean {
// Add specific checks for suspicious activity
const suspiciousPatterns = [
activity.snippet.publishedAt < Date.now() - 1000, // Too rapid actions
activity.snippet.type === 'comment' && this.isPatternSuspicious([activity.snippet.description])
];
return suspiciousPatterns.some(pattern => pattern);
}
private async handleSpamActivity(activity: any): Promise<void> {
try {
if (activity.snippet.type === 'comment') {
await this.youtube.comments.setModerationStatus({
id: activity.id,
moderationStatus: 'rejected'
});
}
console.log(`Blocked suspicious activity: ${activity.id}`);
} catch (error) {
console.error('Error handling spam activity:', error);
}
}
private startViewMonitoring(): void {
setInterval(() => this.cleanupViewPatterns(), 300000); // Cleanup every 5 minutes
}
private async detectViewBot(videoId: string, viewerData: any): Promise<boolean> {
const now = Date.now();
const key = `${videoId}_${viewerData.ip}`;
if (!this.viewPatterns.has(key)) {
this.viewPatterns.set(key, {
timestamps: [],
watchDuration: [],
sessionCount: 0
});
}
const pattern = this.viewPatterns.get(key);
pattern.timestamps.push(now);
pattern.sessionCount++;
// Check for suspicious patterns
const isBot = this.analyzeViewPattern(pattern);
// Clean old timestamps
pattern.timestamps = pattern.timestamps.filter(t => now - t < this.VIEW_PATTERN_WINDOW);
return isBot;
}
private analyzeViewPattern(pattern: any): boolean {
const recentTimestamps = pattern.timestamps;
// Check for rapid view switching
if (recentTimestamps.length > this.VIEW_THRESHOLD) {
return true;
}
// Check for unnaturally consistent intervals
if (recentTimestamps.length > 5) {
const intervals = [];
for (let i = 1; i < recentTimestamps.length; i++) {
intervals.push(recentTimestamps[i] - recentTimestamps[i-1]);
}
// If intervals are too consistent (bot-like behavior)
const avgInterval = intervals.reduce((a, b) => a + b) / intervals.length;
const allSimilar = intervals.every(interval =>
Math.abs(interval - avgInterval) < 100 // 100ms variance threshold
);
if (allSimilar) return true;
}
return false;
}
public async monitorVideoViews(videoId: string): Promise<void> {
try {
const response = await this.youtube.videos.list({
part: 'statistics',
id: videoId
});
const video = response.data.items[0];
const currentViews = parseInt(video.statistics.viewCount);
setInterval(async () => {
const newResponse = await this.youtube.videos.list({
part: 'statistics',
id: videoId
});
const newVideo = newResponse.data.items[0];
const newViews = parseInt(newVideo.statistics.viewCount);
// Check for suspicious view spike
if (newViews - currentViews > this.VIEW_THRESHOLD) {
console.log(`Suspicious view activity detected on video ${videoId}`);
await this.reportSuspiciousViews(videoId);
}
}, 60000); // Check every minute
} catch (error) {
console.error('Error monitoring video views:', error);
}
}
private async reportSuspiciousViews(videoId: string): Promise<void> {
try {
await this.youtube.videos.reportAbuse({
videoId: videoId,
requestBody: {
reasonId: 'botting',
secondaryReasonId: 'artificial_traffic_spam',
comments: 'Suspicious view bot activity detected'
}
});
console.log(`Reported suspicious views for video ${videoId}`);
} catch (error) {
console.error('Error reporting suspicious views:', error);
}
}
}
export default BotProtector;