Saed / authme.js
Saedmh's picture
Upload 37 files
a08f3c3 verified
Raw History Blame Contribute Delete
7.17 kB
'use strict';
/**
* AuthMe Handler — التعامل مع AuthMeReloaded
* يكتشف لو السيرفر محتاج تسجيل، ويسجل دخول تلقائي
*/
const { EventEmitter } = require('events');
const sleep = ms => new Promise(r => setTimeout(r, Math.max(0, ms)));
const rand = (a, b) => Math.floor(Math.random() * (b - a + 1)) + a;
// ═══ رسائل AuthMe ═══
const AUTHME_MESSAGES = {
// تسجيل جديد
register: [
'Please register with',
'سجل كلمة المرور',
'To register type',
'اكتب كلمة المرور',
'register <password> <password>',
'التسجيل:',
],
// تسجيل دخول
login: [
'Please login with',
'سجل الدخول',
'To login type',
'اكتب كلمة المرور للدخول',
'login <password>',
'الدخول:',
],
// نجاح
success: [
'Successfully registered',
'تم التسجيل بنجاح',
'Successfully logged in',
'تم تسجيل الدخول',
'Du wurdest erfolgreich',
'Registration complete',
],
// خطأ
error: [
'Wrong password',
'كلمة المرور خاطئة',
'Invalid password',
'كلمة المرور غير صحيحة',
'already logged in',
'محصل دخول بالفعل',
'You are already authenticated',
],
// تحقق
needVerify: [
'verify',
'تحقق',
'confirm',
'أكد',
],
};
class AuthMeHandler extends EventEmitter {
constructor(bot, config = {}) {
super();
this.bot = bot;
this.config = {
password: config.password || '123456',
loginCommand: config.loginCommand || '/login',
registerCommand: config.registerCommand || '/register',
authDelay: config.authDelay || rand(1500, 3500),
maxRetries: config.maxRetries || 3,
autoLogin: config.autoLogin !== false,
autoRegister: config.autoRegister !== false,
};
this._state = 'unknown'; // unknown, needRegister, needLogin, logged, error
this._retries = 0;
this._authenticated = false;
this._authTimer = null;
this._chatHandler = null;
this._log('INIT', `AuthMe Handler جاهز — كلمة السر: ${this.config.password}`);
}
_log(tag, msg) {
const ts = new Date().toISOString().replace('T', ' ').slice(0, 19);
console.log(`[${ts}] [AUTHME] ${tag} ${msg}`);
}
// ═══ بدء الاستماع ═══
start() {
if (!this.bot) return;
// استمع لرسائل الشات
this._chatHandler = (msg) => this._onMessage(msg);
this.bot.on('messagestr', this._chatHandler);
// بعد الاتصال، استنى شوية وابدأ التحقق
this._authTimer = setTimeout(() => {
this._checkAuthState();
}, this.config.authDelay);
this._log('START', 'بدء مراقبة AuthMe');
}
// ═══ إيقاف ═══
stop() {
if (this._authTimer) { clearTimeout(this._authTimer); this._authTimer = null; }
if (this.bot && this._chatHandler) {
this.bot.removeListener('messagestr', this._chatHandler);
}
this._authenticated = false;
this._state = 'unknown';
this._log('STOP', 'إيقاف AuthMe Handler');
}
// ═══ استقبال رسالة ═══
_onMessage(msg) {
if (!msg || this._authenticated) return;
const text = msg.toString().replace(/<[^>]+>/g, '').trim().toLowerCase();
// فحص لو رسالة AuthMe
if (this._isAuthMessage(text, AUTHME_MESSAGES.register)) {
this._log('📝', 'محتاج تسجيل جديد');
this._state = 'needRegister';
this._handleRegister();
} else if (this._isAuthMessage(text, AUTHME_MESSAGES.login)) {
this._log('🔐', 'محتاج تسجيل دخول');
this._state = 'needLogin';
this._handleLogin();
} else if (this._isAuthMessage(text, AUTHME_MESSAGES.success)) {
this._log('✅', 'تم التسجيل/الدخول بنجاح!');
this._state = 'logged';
this._authenticated = true;
this._retries = 0;
this.emit('authenticated');
} else if (this._isAuthMessage(text, AUTHME_MESSAGES.error)) {
this._log('❌', `خطأ في كلمة المرور (${this._retries + 1}/${this.config.maxRetries})`);
this._retries++;
if (this._retries < this.config.maxRetries) {
setTimeout(() => {
if (this._state === 'needLogin') this._handleLogin();
else if (this._state === 'needRegister') this._handleRegister();
}, rand(2000, 4000));
} else {
this._log('💀', 'فشل التسجيل عدة مرات!');
this._state = 'error';
this.emit('authFailed');
}
}
}
// ═══ فحص رسالة AuthMe ═══
_isAuthMessage(text, patterns) {
return patterns.some(p => text.includes(p.toLowerCase()));
}
// ═══ فحص حالة التحقق ═══
_checkAuthState() {
if (this._authenticated || !this.bot) return;
// جرّب تسجيل الدخول مباشرة
if (this.config.autoLogin) {
this._log('🔄', 'محاولة تسجيل دخول تلقائي...');
this._handleLogin();
}
}
// ═══ تسجيل دخول ═══
async _handleLogin() {
if (!this.bot || this._authenticated) return;
const cmd = `${this.config.loginCommand} ${this.config.password}`;
this._log('🔐', `يكتب: ${cmd}`);
await sleep(rand(500, 1500));
try {
this.bot.chat(cmd);
this.emit('loginAttempt', this.config.password);
} catch(e) {
this._log('❌', `خطأ في الإرسال: ${e.message}`);
}
}
// ═══ تسجيل جديد ═══
async _handleRegister() {
if (!this.bot || this._authenticated || !this.config.autoRegister) return;
const cmd = `${this.config.registerCommand} ${this.config.password} ${this.config.password}`;
this._log('📝', `يكتب: ${cmd}`);
await sleep(rand(500, 1500));
try {
this.bot.chat(cmd);
this.emit('registerAttempt', this.config.password);
} catch(e) {
this._log('❌', `خطأ في الإرسال: ${e.message}`);
}
}
// ═══ تغيير كلمة المرور ═══
async changePassword(oldPass, newPass) {
if (!this.bot) return;
try {
this.bot.chat(`/changepassword ${oldPass} ${newPass}`);
this.config.password = newPass;
this._log('🔑', 'تم تغيير كلمة المرور');
this.emit('passwordChanged', newPass);
} catch(_) {}
}
// ═══ فحص لو مسجل دخول ═══
isAuthenticated() {
return this._authenticated;
}
// ═══ حالة AuthMe ═══
getState() {
return {
state: this._state,
authenticated: this._authenticated,
retries: this._retries,
password: this.config.password,
};
}
// ═══ إعادة المحاولة ═══
retry() {
this._retries = 0;
this._authenticated = false;
this._state = 'unknown';
this._checkAuthState();
}
destroy() {
this.stop();
this.removeAllListeners();
}
}
module.exports = { AuthMeHandler, AUTHME_MESSAGES };