File size: 12,684 Bytes
8af9fb7 1e0c95a 8af9fb7 1e0c95a | 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 | 'use strict';
const express = require('express');
const { MongoClient } = require('mongodb');
const Busboy = require('busboy');
const path = require('path');
const crypto = require('crypto');
const { TelegramClient, Api } = require('telegram');
const { StringSession } = require('telegram/sessions');
const { PassThrough } = require('stream');
// βββββββββββββββββββββββββββββββββββββββββββββ
// CONFIG
// βββββββββββββββββββββββββββββββββββββββββββββ
const PORT = process.env.PORT || 7860;
const MONGO_URI = process.env.MONGO_URI || 'mongodb://127.0.0.1:27017/tgstore';
const CHANNEL_ID = BigInt(process.env.CHANNEL_ID || '0');
const CHUNK_SIZE = 1.9 * 1024 * 1024 * 1024; // 1.9 GB in bytes
const DL_WORKERS = parseInt(process.env.DL_WORKERS) || 4;
const BASE_URL = process.env.BASE_URL || `http://localhost:${PORT}`;
// βββββββββββββββββββββββββββββββββββββββββββββ
// MONGODB
// βββββββββββββββββββββββββββββββββββββββββββββ
let Sessions, Files;
async function connectMongo() {
const client = new MongoClient(MONGO_URI, {
maxPoolSize: 20,
tls: true,
tlsAllowInvalidCertificates: false,
serverSelectionTimeoutMS: 10000,
});
await client.connect();
const db = client.db();
Sessions = db.collection('sessions');
Files = db.collection('files');
await Files.createIndex({ fileId: 1 }, { unique: true });
console.log('[MongoDB] Connected');
}
// βββββββββββββββββββββββββββββββββββββββββββββ
// SESSION POOL (round-robin)
// βββββββββββββββββββββββββββββββββββββββββββββ
const clientPool = [];
let poolIndex = 0;
async function buildClient(apiId, apiHash, sessionString = '') {
const session = new StringSession(sessionString);
const client = new TelegramClient(session, Number(apiId), apiHash, {
connectionRetries: 5,
retryDelay: 1000,
autoReconnect: true,
maxConcurrentDownloads: DL_WORKERS,
});
await client.connect();
return client;
}
async function loadSessions() {
const docs = await Sessions.find({ active: true }).toArray();
for (const doc of docs) {
try {
const client = await buildClient(doc.apiId, doc.apiHash, doc.session);
clientPool.push(client);
console.log(`[Pool] Loaded session for ${doc.phone}`);
} catch (e) {
console.warn(`[Pool] Failed session ${doc._id}: ${e.message}`);
}
}
console.log(`[Pool] ${clientPool.length} session(s) active`);
}
function getClient() {
if (!clientPool.length)
throw new Error('No active Telegram sessions. Visit /strings to add one.');
const client = clientPool[poolIndex % clientPool.length];
poolIndex++;
return client;
}
// βββββββββββββββββββββββββββββββββββββββββββββ
// PENDING AUTH MAP phone β { client, hash }
// βββββββββββββββββββββββββββββββββββββββββββββ
const authMap = new Map();
// βββββββββββββββββββββββββββββββββββββββββββββ
// EXPRESS
// βββββββββββββββββββββββββββββββββββββββββββββ
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
// ββ GET /system ββββββββββββββββββββββββββββββ
app.get('/system', (_req, res) => {
const m = process.memoryUsage();
res.json({
status: 'ok',
uptime: process.uptime(),
memory: {
rss: m.rss,
heapUsed: m.heapUsed,
heapTotal: m.heapTotal,
external: m.external,
},
sessions: clientPool.length,
node: process.version,
ts: new Date().toISOString(),
});
});
// ββ GET /strings βββββββββββββββββββββββββββββ
app.get('/strings', (_req, res) => {
res.sendFile(path.join(__dirname, 'public', 'strings.html'));
});
// ββ POST /strings/send-code ββββββββββββββββββ
app.post('/strings/send-code', async (req, res) => {
const { apiId, apiHash, phone } = req.body;
if (!apiId || !apiHash || !phone)
return res.status(400).json({ error: 'apiId, apiHash and phone are required.' });
try {
const client = await buildClient(apiId, apiHash, '');
const result = await client.sendCode({ apiId: Number(apiId), apiHash }, phone);
authMap.set(phone, { client, apiId, apiHash, phoneCodeHash: result.phoneCodeHash });
res.json({ ok: true, message: 'OTP sent to your Telegram app.' });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
// ββ POST /strings/verify βββββββββββββββββββββ
app.post('/strings/verify', async (req, res) => {
const { phone, code, password } = req.body;
const entry = authMap.get(phone);
if (!entry)
return res.status(400).json({ error: 'No pending auth for this phone. Send code first.' });
const { client, apiId, apiHash, phoneCodeHash } = entry;
try {
await client.invoke(new Api.auth.SignIn({ phoneNumber: phone, phoneCodeHash, phoneCode: code }));
} catch (e) {
if (e.errorMessage === 'SESSION_PASSWORD_NEEDED') {
if (!password)
return res.status(400).json({ error: '2FA password required.', twoFA: true });
try {
const pwdInfo = await client.invoke(new Api.account.GetPassword());
const check = await require('telegram/utils/Password').computeCheck(pwdInfo, password);
await client.invoke(new Api.auth.CheckPassword({ password: check }));
} catch (e2) {
authMap.delete(phone);
return res.status(400).json({ error: e2.message });
}
} else {
authMap.delete(phone);
return res.status(400).json({ error: e.message });
}
}
const sessionString = client.session.save();
await Sessions.insertOne({
apiId, apiHash, phone,
session: sessionString,
active: true,
createdAt: new Date(),
});
clientPool.push(client);
authMap.delete(phone);
res.json({ ok: true, session: sessionString, message: 'Session saved and added to pool!' });
});
// ββ POST /upload βββββββββββββββββββββββββββββ
app.post('/upload', (req, res) => {
let client;
try { client = getClient(); }
catch (e) { return res.status(503).json({ error: e.message }); }
const busboy = Busboy({ headers: req.headers, limits: { files: 1, fileSize: Infinity } });
let responded = false;
const done = (code, body) => { if (!responded) { responded = true; res.status(code).json(body); } };
busboy.on('file', async (_field, fileStream, info) => {
const { filename, mimeType } = info;
const fileId = crypto.randomBytes(16).toString('hex');
const chunks = []; // { chunkIndex, messageId, size }
let chunkIndex = 0;
let totalSize = 0;
let chunkBufs = [];
let chunkBytes = 0;
let uploadError = null;
const flushChunk = async () => {
if (!chunkBufs.length) return;
const buf = Buffer.concat(chunkBufs);
chunkBufs = [];
chunkBytes = 0;
const idx = chunkIndex++;
const msgId = await uploadBuffer(client, buf, `${fileId}_${idx}`);
chunks.push({ chunkIndex: idx, messageId: msgId, size: buf.length });
};
// Backpressure-aware data handler
fileStream.on('data', async (data) => {
if (uploadError) return;
fileStream.pause();
totalSize += data.length;
chunkBufs.push(data);
chunkBytes += data.length;
try {
if (chunkBytes >= CHUNK_SIZE) await flushChunk();
} catch (e) {
uploadError = e;
fileStream.destroy();
}
fileStream.resume();
});
fileStream.on('end', async () => {
if (uploadError) return done(500, { error: uploadError.message });
try {
await flushChunk(); // flush remainder
await Files.insertOne({ fileId, filename, mimeType, totalSize, chunks, uploadedAt: new Date() });
done(200, {
ok: true, fileId, filename,
size: totalSize,
chunks: chunks.length,
downloadUrl: `${BASE_URL}/download/${fileId}`,
});
} catch (e) {
done(500, { error: e.message });
}
});
fileStream.on('error', (e) => done(500, { error: e.message }));
});
busboy.on('error', (e) => done(500, { error: e.message }));
req.pipe(busboy);
});
// ββ GET /download/:fileId ββββββββββββββββββββ
app.get('/download/:fileId', async (req, res) => {
const doc = await Files.findOne({ fileId: req.params.fileId });
if (!doc) return res.status(404).json({ error: 'File not found.' });
let client;
try { client = getClient(); }
catch (e) { return res.status(503).json({ error: e.message }); }
res.setHeader('Content-Disposition', `attachment; filename="${encodeURIComponent(doc.filename)}"`);
res.setHeader('Content-Type', doc.mimeType || 'application/octet-stream');
if (doc.totalSize) res.setHeader('Content-Length', String(doc.totalSize));
const sorted = [...doc.chunks].sort((a, b) => a.chunkIndex - b.chunkIndex);
for (const chunk of sorted) {
try {
await streamChunk(client, chunk.messageId, res);
} catch (e) {
console.error('[Download] chunk error:', e.message);
if (!res.headersSent) res.status(500).json({ error: e.message });
else res.destroy();
return;
}
}
res.end();
});
// βββββββββββββββββββββββββββββββββββββββββββββ
// TELEGRAM HELPERS
// βββββββββββββββββββββββββββββββββββββββββββββ
// Upload a Buffer to the Telegram channel and return the message ID
async function uploadBuffer(client, buffer, caption) {
const file = await client.uploadFile({
file: new BufferFile(caption, buffer),
workers: DL_WORKERS,
});
const msg = await client.sendFile(CHANNEL_ID, {
file,
caption,
forceDocument: true,
workers: DL_WORKERS,
});
return msg.id;
}
// Stream a Telegram message's media directly to the HTTP response
async function streamChunk(client, messageId, res) {
const [msg] = await client.getMessages(CHANNEL_ID, { ids: [messageId] });
if (!msg?.media) throw new Error(`No media in message ${messageId}`);
const pass = new PassThrough();
pass.on('data', (chunk) => {
const ok = res.write(chunk);
if (!ok) {
pass.pause();
res.once('drain', () => pass.resume()); // handle backpressure
}
});
await new Promise((resolve, reject) => {
pass.on('end', resolve);
pass.on('error', reject);
client.downloadMedia(msg, { outputFile: pass, workers: DL_WORKERS })
.then(() => pass.end())
.catch(reject);
});
}
// GramJS-compatible file object that reads from a Buffer
class BufferFile {
constructor(name, buffer) {
this.name = name;
this.size = buffer.length;
this.path = name;
this._buf = buffer;
}
async *[Symbol.asyncIterator]() {
const PART = 512 * 1024;
for (let i = 0; i < this._buf.length; i += PART)
yield this._buf.slice(i, i + PART);
}
}
// βββββββββββββββββββββββββββββββββββββββββββββ
// BOOT
// βββββββββββββββββββββββββββββββββββββββββββββ
(async () => {
await connectMongo();
await loadSessions();
app.listen(PORT, '0.0.0.0', () => {
console.log(`\nπ Server ready at http://0.0.0.0:${PORT}`);
console.log(`π Base URL : ${BASE_URL}`);
console.log(`π¦ Sessions : ${clientPool.length}`);
console.log(`ποΈ MongoDB : ${MONGO_URI}\n`);
});
})(); |