mirror of
https://github.com/whyour/qinglong.git
synced 2026-07-01 04:40:38 +08:00
重构Cookie管理逻辑和交互
This commit is contained in:
+62
-1
@@ -46,7 +46,9 @@ export default (app: Router) => {
|
||||
const logger: Logger = Container.get('logger');
|
||||
try {
|
||||
const cookieService = Container.get(CookieService);
|
||||
const data = await cookieService.addCookie(req.query.cookie as string);
|
||||
const data = await cookieService.addQrCookie(
|
||||
req.query.cookie as string,
|
||||
);
|
||||
return res.send({ code: 200, data });
|
||||
} catch (e) {
|
||||
logger.error('🔥 error: %o', e);
|
||||
@@ -55,6 +57,65 @@ export default (app: Router) => {
|
||||
},
|
||||
);
|
||||
|
||||
route.post(
|
||||
'/cookie',
|
||||
async (req: Request, res: Response, next: NextFunction) => {
|
||||
const logger: Logger = Container.get('logger');
|
||||
try {
|
||||
const cookieService = Container.get(CookieService);
|
||||
const data = await cookieService.addCookie(req.body.cookies);
|
||||
if (data) {
|
||||
return res.send({ code: 500, data });
|
||||
} else {
|
||||
return res.send({ code: 200, data: '新建成功' });
|
||||
}
|
||||
} catch (e) {
|
||||
logger.error('🔥 error: %o', e);
|
||||
return next(e);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
route.put(
|
||||
'/cookie',
|
||||
async (req: Request, res: Response, next: NextFunction) => {
|
||||
const logger: Logger = Container.get('logger');
|
||||
try {
|
||||
const cookieService = Container.get(CookieService);
|
||||
const data = await cookieService.updateCookie(req.body);
|
||||
if (data) {
|
||||
return res.send({ code: 500, data });
|
||||
} else {
|
||||
return res.send({ code: 200, data: '新建成功' });
|
||||
}
|
||||
} catch (e) {
|
||||
logger.error('🔥 error: %o', e);
|
||||
return next(e);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
route.delete(
|
||||
'/cookie',
|
||||
async (req: Request, res: Response, next: NextFunction) => {
|
||||
const logger: Logger = Container.get('logger');
|
||||
try {
|
||||
const cookieService = Container.get(CookieService);
|
||||
const data = await cookieService.deleteCookie(
|
||||
req.body.cookie as string,
|
||||
);
|
||||
if (data) {
|
||||
return res.send({ code: 500, data });
|
||||
} else {
|
||||
return res.send({ code: 200, data: '新建成功' });
|
||||
}
|
||||
} catch (e) {
|
||||
logger.error('🔥 error: %o', e);
|
||||
return next(e);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
route.post(
|
||||
'/cookie/refresh',
|
||||
async (req: Request, res: Response, next: NextFunction) => {
|
||||
|
||||
@@ -5,6 +5,7 @@ process.env.NODE_ENV = process.env.NODE_ENV || 'development';
|
||||
|
||||
const envFound = dotenv.config();
|
||||
const rootPath = path.resolve(__dirname, '../../');
|
||||
const cookieFile = path.join(rootPath, 'config/cookie.sh');
|
||||
const confFile = path.join(rootPath, 'config/config.sh');
|
||||
const sampleFile = path.join(rootPath, 'sample/config.sh.sample');
|
||||
const crontabFile = path.join(rootPath, 'config/crontab.list');
|
||||
@@ -41,6 +42,7 @@ export default {
|
||||
crontabFile,
|
||||
sampleFile,
|
||||
confFile,
|
||||
cookieFile,
|
||||
fileMap: {
|
||||
'config.sh': confFile,
|
||||
'crontab.list': crontabFile,
|
||||
|
||||
+38
-10
@@ -155,14 +155,14 @@ export default class CookieService {
|
||||
return userCookie;
|
||||
}
|
||||
|
||||
public async addCookie(oldCookie: string) {
|
||||
public async addQrCookie(cookie: string) {
|
||||
const res: any = await this.checkLogin();
|
||||
if (res.body.errcode === 0) {
|
||||
let ucookie = this.getCookie(res);
|
||||
let content = getFileContentByName(config.confFile);
|
||||
const regx = /.*Cookie[0-9]{1}\=\"(.+?)\"/g;
|
||||
if (content.match(regx)) {
|
||||
const lastCookie = oldCookie || (content.match(regx) as any[]).pop();
|
||||
const lastCookie = cookie || (content.match(regx) as any[]).pop();
|
||||
const cookieRegx = /Cookie([0-9]+)\=.+?/.exec(lastCookie);
|
||||
if (cookieRegx) {
|
||||
const num = parseInt(cookieRegx[1]) + 1;
|
||||
@@ -181,6 +181,40 @@ export default class CookieService {
|
||||
}
|
||||
}
|
||||
|
||||
public async addCookie(cookies: string[]) {
|
||||
let content = getFileContentByName(config.cookieFile);
|
||||
const originCookies = content.split('\n').filter((x) => !!x);
|
||||
const result = originCookies.concat(cookies);
|
||||
fs.writeFileSync(config.cookieFile, result.join('\n'));
|
||||
return '';
|
||||
}
|
||||
|
||||
public async updateCookie({ cookie, oldCookie }) {
|
||||
let content = getFileContentByName(config.cookieFile);
|
||||
const cookies = content.split('\n');
|
||||
const index = cookies.findIndex((x) => x === oldCookie);
|
||||
if (index !== -1) {
|
||||
cookies[index] = cookie;
|
||||
fs.writeFileSync(config.cookieFile, cookies.join('\n'));
|
||||
return '';
|
||||
} else {
|
||||
return '未找到要原有Cookie';
|
||||
}
|
||||
}
|
||||
|
||||
public async deleteCookie(cookie: string) {
|
||||
let content = getFileContentByName(config.cookieFile);
|
||||
const cookies = content.split('\n');
|
||||
const index = cookies.findIndex((x) => x === cookie);
|
||||
if (index !== -1) {
|
||||
cookies.splice(index, 1);
|
||||
fs.writeFileSync(config.cookieFile, cookies.join('\n'));
|
||||
return '';
|
||||
} else {
|
||||
return '未找到要删除的Cookie';
|
||||
}
|
||||
}
|
||||
|
||||
private async checkLogin() {
|
||||
try {
|
||||
if (this.cookies == '') {
|
||||
@@ -224,14 +258,8 @@ export default class CookieService {
|
||||
}
|
||||
|
||||
public async getCookies() {
|
||||
const content = getFileContentByName(config.confFile);
|
||||
const regx = /Cookie[0-9]{1}\=\"(.+?)\"/g;
|
||||
let m,
|
||||
data = [];
|
||||
while ((m = regx.exec(content)) !== null) {
|
||||
data.push(m[1]);
|
||||
}
|
||||
return this.formatCookie(data);
|
||||
const content = getFileContentByName(config.cookieFile);
|
||||
return this.formatCookie(content.split('\n').filter((x) => !!x));
|
||||
}
|
||||
|
||||
private async formatCookie(data: any[]) {
|
||||
|
||||
Reference in New Issue
Block a user