This repository was archived by the owner on Aug 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
133 lines (118 loc) · 4.07 KB
/
Copy pathserver.js
File metadata and controls
133 lines (118 loc) · 4.07 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
#!/usr/bin/env node
const { Server } = require('@modelcontextprotocol/sdk/server/index.js');
const { StdioServerTransport } = require('@modelcontextprotocol/sdk/server/stdio.js');
const https = require('https');
const fetch = (url) => {
return new Promise((resolve, reject) => {
const urlObj = new URL(url);
https.get({
hostname: urlObj.hostname,
path: urlObj.pathname + urlObj.search,
headers: { 'User-Agent': 'Node.js' }
}, (res) => {
if (res.statusCode === 301 || res.statusCode === 302) {
return fetch(res.headers.location).then(resolve).catch(reject);
}
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => resolve({ ok: res.statusCode === 200, data }));
}).on('error', reject);
});
};
const cleanEmail = (email) => email.replace(/['"]/g, '').trim().toLowerCase();
const extractEmails = (patch) => {
const matches = patch.match(/^From: .+ <(.+)>/gm) || [];
return matches.map(m => cleanEmail(m.match(/<(.+)>/)?.[1] || '')).filter(Boolean);
};
const delay = (ms) => new Promise(r => setTimeout(r, ms));
const findEmails = async (username) => {
const emails = new Set();
const seen = new Set();
// Scan events
for (let p = 1; p <= 10; p++) {
const res = await fetch(`https://api.github.com/users/${username}/events/public?per_page=100&page=${p}`);
if (!res.ok) break;
const events = JSON.parse(res.data);
if (!events.length) break;
for (const e of events) {
if (e.type !== 'PushEvent') continue;
for (const c of e.payload?.commits || []) {
if (seen.has(c.sha)) continue;
seen.add(c.sha);
try {
const patch = await fetch(`https://github.com/${e.repo?.name}/commit/${c.sha}.patch`);
if (patch.ok) extractEmails(patch.data).forEach(x => emails.add(x));
} catch {}
await delay(50);
}
}
}
// Scan repos
const repos = await fetch(`https://api.github.com/users/${username}/repos?per_page=100`);
if (repos.ok) {
for (const repo of JSON.parse(repos.data).filter(r => !r.fork)) {
for (let p = 1; p <= 5; p++) {
const res = await fetch(`https://api.github.com/repos/${username}/${repo.name}/commits?per_page=100&page=${p}`);
if (!res.ok) break;
const commits = JSON.parse(res.data);
if (!commits.length) break;
for (const c of commits) {
if (seen.has(c.sha)) continue;
seen.add(c.sha);
try {
const patch = await fetch(`https://github.com/${username}/${repo.name}/commit/${c.sha}.patch`);
if (patch.ok) extractEmails(patch.data).forEach(x => emails.add(x));
} catch {}
await delay(50);
}
}
}
}
return { emails: [...emails].sort(), commitCount: seen.size };
};
const server = new Server(
{ name: 'github-email-finder', version: '1.0.0' },
{ capabilities: { tools: {} } }
);
server.setRequestHandler('tools/list', async () => ({
tools: [{
name: 'find_github_emails',
description: 'Find email addresses from a GitHub user by scanning their commit patches',
inputSchema: {
type: 'object',
properties: {
username: {
type: 'string',
description: 'GitHub username to scan'
}
},
required: ['username']
}
}]
}));
server.setRequestHandler('tools/call', async (request) => {
if (request.params.name === 'find_github_emails') {
const { username } = request.params.arguments;
try {
const result = await findEmails(username);
return {
content: [{
type: 'text',
text: result.emails.length
? `Found ${result.emails.length} email(s) from ${result.commitCount} commits:\n\n${result.emails.join('\n')}`
: `No emails found for ${username}`
}]
};
} catch (error) {
return {
content: [{ type: 'text', text: `Error: ${error.message}` }],
isError: true
};
}
}
});
const main = async () => {
const transport = new StdioServerTransport();
await server.connect(transport);
};
main().catch(console.error);