-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
112 lines (92 loc) · 3.06 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
102
103
104
105
106
107
108
109
110
111
112
require("dotenv").config();
const grammy = require("grammy");
const resolver = require("dns").promises;
// The telegram bot
const bot = new grammy.Bot(process.env.BOT_TOKEN);
// User Sessions, Used for /generate command in group.
const sess = new Map();
// When required
resolver.setServers(["1.1.1.1"]);
async function generate(hostnames) {
let hosts = {};
let text = [];
for (i in hostnames) {
// Grab the Hostname, even in URL.
host = /(?:https?:\/\/)?([\w\d\.\-]+)/.exec(hostnames[i].toLowerCase())[1];
// Ignore onion address sinceit's unsupported here
if (host.endsWith(".onion"))
return ctx.reply(
`\`[i] I skipped ${host} because i'm not supporting .onion domain yet.\``,
{ parse_mode: "MarkdownV2" }
);
// Skip existing hostname
if (hosts[host]) continue;
// Look up IP address of {host} string.
// Returns Array with bunch of IP address of single hostname
hosts[host] = (await resolver.lookup(host, { all: true })).map(
(ip) => ip.address
);
}
// Parse hosts
for (host in hosts) {
let ips = hosts[host];
ips.forEach((ip) => {
text.push(`${ip}\t${host}`);
});
}
return text.join("\n");
}
// Handle /start command
bot.command("start", (ctx) =>
ctx
.reply(
"<b>🙋♂️Hi! I'm Static DNS file generator!</b>\n" +
"To generate a static DNS file, Send me a domains, Each splitted with space.",
{ reply_to_message_id: ctx.message.message_id, reply_markup: { force_reply: true, selective: true }, parse_mode: "HTML" }
)
.then(({ message_id }) => sess.set(ctx.message.from.id, message_id))
);
// Handle /generate command
bot.command("generate", (ctx) =>
ctx
.reply(
"What hostnames would you like me to generate? " +
"Each hostnames should be separated with space.",
{ reply_to_message_id: ctx.message.message_id, reply_markup: { force_reply: true, selective: true } }
)
.then(({ message_id }) => sess.set(ctx.message.from.id, message_id))
);
// Listen to text message event
bot.on("message:text", async (ctx) => {
if (
ctx.message.chat.type !== "private" &&
ctx.message.reply_to_message &&
sess.get(ctx.message.from.id) != ctx.message.reply_to_message.message_id
)
return;
try {
let hosts = await generate(ctx.message.text.split(" "));
let textMsg = `\`${hosts}\``;
// Sent as file if the string length is at the maximum length.
if (textMsg.length > 4096)
return ctx.replyWithDocument(
new grammy.InputFile(Buffer.from(hosts), "hosts"),
{ reply_to_message_id: ctx.message.message_id }
);
// Sent as text message whenever it's possible.
ctx.reply(textMsg, {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id,
});
} catch (error) {
ctx.reply(error.toString());
}
// Delete session when available.
sess.delete(ctx.message.from.id);
});
bot.catch(console.error);
bot.start();
bot.api
.getMe()
.then(({ first_name }) => console.log(`Logged as ${first_name}!`));
process.on("unhandledRejection", console.error);