UNPKG

mcbbsmis

Version:

Minecraft Connect Bind BiliBili Sync Message Interaction Service

109 lines (108 loc) 4.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const node_path_1 = require("node:path"); const node_fs_1 = require("node:fs"); const axios_1 = tslib_1.__importStar(require("axios")); const qrcode_terminal_1 = tslib_1.__importDefault(require("qrcode-terminal")); const config_1 = tslib_1.__importDefault(require("../config")); class Cookie { path; constructor(path = './config/cookies/') { this.path = (0, node_path_1.join)((0, node_path_1.resolve)(), path); if (!(0, node_fs_1.existsSync)(this.path)) (0, node_fs_1.mkdirSync)(this.path, { recursive: true }); } has(username) { return (0, node_fs_1.existsSync)((0, node_path_1.join)(this.path, `./${username}.txt`)); } get(username) { return (0, node_fs_1.readFileSync)((0, node_path_1.join)(this.path, `./${username}.txt`), { encoding: 'utf-8', flag: 'r' }); } set(username, cookie) { (0, node_fs_1.writeFileSync)((0, node_path_1.join)(this.path, `./${username}.txt`), cookie, { encoding: 'utf-8', flag: 'w' }); } delete(username) { (0, node_fs_1.rmSync)((0, node_path_1.join)(this.path, `./${username}.txt`), { force: true, retryDelay: 1000 }); } } class AuthUnit extends Cookie { static QRCODE_URL = 'https://passport.bilibili.com/x/passport-login/web/qrcode/generate?source=main-fe-header'; static AuthUnit; headers = new axios_1.AxiosHeaders({ 'User-Agent': 'PostmanRuntime/7.43.0', Origin: 'https://www.bilibili.com', Referer: 'https://www.bilibili.com' }); constructor(username) { super(); this.getUser(username); } static create(username) { if (!AuthUnit.AuthUnit) { AuthUnit.AuthUnit = new AuthUnit(username); } return AuthUnit.AuthUnit; } async getUser(username) { if (!super.has(username)) { config_1.default.LOGGER.warn(config_1.default.LANGUAGE.get('#9')); try { const { data: { data: { url, qrcode_key } } } = await (0, axios_1.default)({ url: AuthUnit.QRCODE_URL, method: 'GET', headers: this.headers }); config_1.default.LOGGER.info(config_1.default.LANGUAGE.get('#10')); qrcode_terminal_1.default.generate(url, qrcode_key); const checkLogin = setInterval(async () => { const data = await (0, axios_1.default)({ url: `https://passport.bilibili.com/x/passport-login/web/qrcode/poll?qrcode_key=${qrcode_key}&source=main-fe-header`, method: 'GET', headers: this.headers }); const { data: { data: { code } } } = data; switch (code) { case 0: { config_1.default.LOGGER.info(config_1.default.LANGUAGE.get('#13')); super.set(username, Reflect.get(data.headers, 'set-cookie')?.join('; ')); clearInterval(checkLogin); } break; case 86090: { config_1.default.LOGGER.info(config_1.default.LANGUAGE.get('#12')); } break; case 86101: { config_1.default.LOGGER.info(config_1.default.LANGUAGE.get('#11')); } break; default: { config_1.default.LOGGER.error(`${config_1.default.LANGUAGE.get('#14')} ${code}`); super.delete(username); process.exit(0); } } }, 2000); } catch (err) { config_1.default.LOGGER.error(config_1.default.LANGUAGE.get('#21')); super.delete(username); process.exit(0); } } } } exports.default = AuthUnit;