-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
357 lines (285 loc) · 8.56 KB
/
index.mjs
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import https from 'node:https'
import {
TG_DOMAIN,
EXCEPTIONAL_BOT_NAMES,
ATTRIBUTES,
ERROR_NOT_TELEGRAM_LINK,
ERROR_USER_DOES_NOT_EXIST,
ERROR_LINK_EXPIRED,
TYPE_USER,
TYPE_BOT,
TYPE_PUBLIC_GROUP,
TYPE_PRIVATE_GROUP,
TYPE_PUBLIC_CHANNEL,
TYPE_PRIVATE_CHANNEL,
REGEX_USERNAME,
REGEX_INVITECODE,
} from './constants.mjs'
const botExceptions = new Set(EXCEPTIONAL_BOT_NAMES)
const objectAttrsOrder = Object.fromEntries(ATTRIBUTES.map((a, i) => [a, i]))
const cleanUnicode = (text) => {
return text
.replaceAll('!', '!')
.replaceAll(''', '\'')
.replaceAll('$', '$')
.replaceAll('&', '&')
.replaceAll('"', '"')
}
const sortValues = (object, order) => {
return Object.fromEntries(
Object
.entries(object)
.sort(([a], [b]) => order[a] - order[b])
)
}
const pickValues = (object, pick) => {
return Object.fromEntries(
Object
.entries(object)
.filter(([key]) => pick.includes(key) || key === 'error')
)
}
const request = async (url) => {
return new Promise((resolve, reject) => {
const req = https.get(url, (res) => {
res.on('data', (data) => {
resolve(data.toString())
})
})
req.on('error', reject)
req.end()
})
}
const isValidUsername = (rawUsername) => {
if (!rawUsername || rawUsername.startsWith('+')) return false
const username = rawUsername.startsWith('@') ? rawUsername.slice(1) : rawUsername
if (EXCEPTIONAL_BOT_NAMES.includes(username)) return true
return REGEX_USERNAME.test(username)
}
const isValidInviteCode = (rawCode) => {
if (!rawCode || rawCode.startsWith('@')) return false
const code = rawCode.startsWith('+') ? rawCode.slice(1) : rawCode
return REGEX_INVITECODE.test(code)
}
const getSlugFromInput = (input) => {
let slug
try {
let url = new URL(input)
const { protocol, host, pathname, searchParams } = url
if (protocol === 'tg:') {
if (host === 'resolve') {
slug = searchParams.get('domain')
} else if (host === 'join') {
const invite = searchParams.get('invite')
if (invite) slug = `+${invite}`
}
} else if (
(protocol === 'http:' || protocol === 'https:') &&
(host === 't.me' || host === 'telegram.me' || host === 'telegram.dog')
) {
if (pathname.startsWith('/joinchat/')) {
slug = `+${pathname.slice(10)}`
} else if (pathname.startsWith('/s/')) {
slug = cutBetween(pathname.slice(2), '/', '?')
} else {
slug = cutBetween(pathname, '/', '?')
}
}
} catch (e) {
slug = input
}
if (isValidUsername(slug)) {
slug = slug.startsWith('@') ? slug.slice(1) : slug
} else if (isValidInviteCode(slug)) {
slug = slug.startsWith('+') ? slug : `+${slug}`
} else {
slug = null
}
if (!slug) {
return null
}
return slug
}
const slugToTelegramWebUrl = (slug) => {
if (!isValidUsername(slug) && !isValidInviteCode(slug)) return null
return `https://t.me/${slug}`
}
const slugToTelegramURL = (slug) => {
if (!isValidUsername(slug) && !isValidInviteCode(slug)) return null
if (slug.startsWith('+')) {
return `tg://join?invite=${slug.slice(1)}`
} else {
return `tg://resolve?domain=${slug}`
}
}
const cutBetween = (string, start, end) => {
let foundStartIndex = 0
let foundEndIndex = 0
let isStartHasFound = false
let output = ''
for (let i = 0, l = string.length; i < l; i++) {
const char = string[i]
if (isStartHasFound) {
output += char
if (end[foundEndIndex] === char) {
foundEndIndex++
if (foundEndIndex === end.length) {
output = output.slice(0, end.length * -1)
break
}
} else {
foundEndIndex = 0
}
} else {
if (start[foundStartIndex] === char) {
foundStartIndex++
if (foundStartIndex === start.length) {
isStartHasFound = true
}
} else {
foundStartIndex = 0
}
}
}
return output
}
const getAttrsFromHTML = (html) => {
const values = {}
const lines = html.split('\n')
for (const line of lines) {
if (!values.title && line.startsWith('<meta property="og:title"')) {
console.log(line)
values.title = cleanUnicode(cutBetween(line, 'content="', '">'))
if (values.title.startsWith('Telegram: Contact')) {
return { error: ERROR_USER_DOES_NOT_EXIST }
}
if (values.title === 'Join group chat on Telegram') {
return { error: ERROR_LINK_EXPIRED }
}
continue
}
if (!values.image && line.startsWith('<meta property="og:image"')) {
values.image = cutBetween(line, 'content="', '">')
if (values.image === 'https://telegram.org/img/t_logo.png') {
values.image = undefined
}
continue
}
if (!values.info && line.startsWith('<meta property="og:description"')) {
values.info = cleanUnicode(cutBetween(line, 'content="', '">'))
.replaceAll('\t', '\n')
.trim()
if (values.info === `You can contact @${values.username} right away.`) {
delete values.info
} else if (values.info === `You can view and join @${values.username} right away.`) {
delete values.info
}
continue
}
if (!values.verified && line.includes('<i class="verified-icon">')) {
values.verified = true
continue
}
if (!values.type) {
if (line.includes('">Join Channel</a>')) {
values.type = TYPE_PRIVATE_CHANNEL
continue
}
if (line.includes('">Preview channel</a>')) {
values.type = TYPE_PUBLIC_CHANNEL
values.previewUrl = `${TG_DOMAIN}/s/${values.username}`
continue
}
if (line.includes('">Join Group</a>')) {
values.type = TYPE_PRIVATE_GROUP
continue
}
if (line.includes('">View in Telegram</a>')) {
values.type = TYPE_PUBLIC_GROUP
continue
}
if (line.includes('">Send Message</a>')) {
const lcUsername = values.username.toLowerCase()
if (lcUsername.endsWith('bot') || botExceptions.has(lcUsername)) {
values.type = TYPE_BOT
} else {
values.type = TYPE_USER
}
continue
}
}
if (!values.username && line.includes('<title>Telegram: Contact ')) {
values.username = cutBetween(line, 'Contact @', '<')
}
if (line.startsWith('<div class="tgme_page_extra">')) {
const string = cutBetween(line, '">', '<')
if (!string) continue
string.split(', ')
.forEach(part => {
const value = parseInt(part.replace(/\D/g, ''))
if (part.includes('subscriber')) {
values.subscribers = value
} else if (part.includes('online')) {
values.online = value
} else if (part.includes('member')) {
values.members = value
}
})
}
}
values.verified = !!values.verified
return values
}
const tginfo = async (input, attrs = [], throwOnError = false) => {
const _attrs = attrs.filter(attr => ATTRIBUTES.includes(attr))
let values = {}
const slug = getSlugFromInput(input)
if (slug) {
values.webUrl = slugToTelegramWebUrl(slug)
values.tgUrl = slugToTelegramURL(slug)
const hasTgUrl = _attrs.includes('tgUrl')
const hasWebUrl = _attrs.includes('webUrl')
const hasType = _attrs.includes('type')
const hasWrongName = EXCEPTIONAL_BOT_NAMES.includes(slug)
if (_attrs.length === 1) {
if (hasWebUrl) {
return { webUrl: values.webUrl }
}
if (hasTgUrl) {
return { tgUrl: values.tgUrl }
}
if (hasType && hasWrongName) {
return { type: TYPE_BOT }
}
} else if (_attrs.length === 2) {
if (hasTgUrl && hasWebUrl) {
return { webUrl: values.webUrl, tgUrl: values.tgUrl }
}
if (hasType && hasWebUrl && hasWrongName) {
return { type: TYPE_BOT, webUrl: values.webUrl }
}
if (hasType && hasTgUrl && hasWrongName) {
return { type: TYPE_BOT, tgUrl: values.tgUrl }
}
} else if (_attrs.length === 3) {
if (hasType && hasWebUrl && hasTgUrl && hasWrongName) {
return { type: TYPE_BOT, webUrl: values.webUrl, tgUrl: values.tgUrl }
}
}
const url = values.webUrl
values = {
...values,
...getAttrsFromHTML(await request(url)),
}
} else {
values.error = ERROR_NOT_TELEGRAM_LINK
}
if (values.error && throwOnError) {
throw new Error(values.error)
}
if (_attrs.length === 0) {
return sortValues(values, objectAttrsOrder)
}
return pickValues(sortValues(values, objectAttrsOrder), _attrs)
}
export default tginfo