fix(route): Fix 403 issue on 18comic route by using API calls instead of directly fetching the webpage. (#19485)

* fix(route): Fix 403 issue on 18comic route by using API calls instead of directly fetching the webpage.

* avoid using await in loop

* Replace the original search implementation with API-based search.

* using api in album route

* Fix

* Fix
This commit is contained in:
charles
2025-07-02 23:16:12 +08:00
committed by GitHub
parent 39912f4531
commit 6e202ac335
3 changed files with 182 additions and 69 deletions
+59 -64
View File
@@ -1,10 +1,8 @@
import { Route } from '@/types';
import { defaultDomain, getApiUrl, getRootUrl, processApiItems } from './utils';
import { art } from '@/utils/render';
import path from 'node:path';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import { defaultDomain, getRootUrl } from './utils';
export const route: Route = {
path: '/album/:id',
@@ -37,71 +35,68 @@ async function handler(ctx) {
const id = ctx.req.param('id');
const { domain = defaultDomain } = ctx.req.query();
const rootUrl = getRootUrl(domain);
const currentUrl = `${rootUrl}/album/${id}`;
const response = await got({
method: 'get',
url: currentUrl,
});
const apiUrl = `${getApiUrl()}/album?id=${id}`;
const $ = load(response.data);
const apiResult = await processApiItems(apiUrl);
const category = $('span[data-type="tags"]')
.first()
.find('a')
.toArray()
.map((c) => $(c).text());
const author = $('span[data-type="author"]')
.first()
.find('a')
.toArray()
.map((a) => $(a).text())
.join(', ');
let items = $('.btn-toolbar')
.first()
.find('a')
.toArray()
.map((item) => {
item = $(item);
return {
title: item.text(),
link: `${rootUrl}${item.attr('href')}`,
guid: `https://18comic.org${item.attr('href')}`,
pubDate: parseDate(item.find('.hidden-xs').text()),
};
const category = apiResult.tags;
const author = apiResult.author.join(', ');
const description = apiResult.description;
const addTime = apiResult.addtime;
let results: any[] = [];
if (apiResult.series.length === 0) {
results.push({
title: apiResult.name,
link: `${rootUrl}/photo/${id}`,
guid: `${rootUrl}/photo/${id}`,
updated: new Date(addTime * 1000),
pubDate: new Date(addTime * 1000),
category,
author,
description: art(path.join(__dirname, 'templates/description.art'), {
introduction: description,
// 不取图片,因为专辑的图片会被分割排序,所以只取封面图
images: [`https://cdn-msp3.${domain}/media/albums/${id}_3x4.jpg`],
cover: `https://cdn-msp3.${domain}/media/albums/${id}_3x4.jpg`,
category,
}),
});
items = await Promise.all(
items.map((item) =>
cache.tryGet(item.guid, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = load(detailResponse.data);
content('.tab-content').remove();
item.author = author;
item.category = category;
item.description = `<img src="${content('.thumb-overlay-albums img[data-original]')
.toArray()
.map((image) => content(image).attr('data-original'))
.join('"><img src="')}">`;
return item;
})
)
);
} else {
results = await Promise.all(
apiResult.series.map((item, index) =>
cache.tryGet(`18comic:album:${item.id}`, async () => {
const chapterApiUrl = `${getApiUrl()}/chapter?id=${item.id}`;
const chapterResult = await processApiItems(chapterApiUrl);
const result = {};
const chapterNum = index + 1;
result.title = `第${String(chapterNum)}話 ${item.name === '' ? `${String(chapterNum)}` : item.name}`;
result.link = `${rootUrl}/photo/${item.id}`;
result.guid = `${rootUrl}/photo/${item.id}`;
result.updated = new Date(chapterResult.addtime * 1000);
result.pubDate = addTime;
result.category = category;
result.author = author;
result.description = art(path.join(__dirname, 'templates/description.art'), {
introduction: description,
// 不取图片,因为专辑的图片会被分割排序,所以只取封面图
images: [`https://cdn-msp3.${domain}/media/albums/${item.id}_3x4.jpg`],
cover: `https://cdn-msp3.${domain}/media/albums/${item.id}_3x4.jpg`,
category,
});
return result;
})
)
);
results = results.reverse();
}
return {
title: $('title').text(),
link: currentUrl,
item: items,
description: $('meta[property="og:description"]').attr('content'),
title: `${apiResult.name} - 禁漫天堂`,
link: currentUrl.replace(/\?$/, ''),
item: results,
allowEmpty: true,
description,
};
}
+53 -4
View File
@@ -1,5 +1,9 @@
import { Route } from '@/types';
import { defaultDomain, getRootUrl, ProcessItems } from './utils';
import { apiMapCategory, defaultDomain, getApiUrl, getRootUrl, processApiItems } from './utils';
import { parseDate } from '@/utils/parse-date';
import { art } from '@/utils/render';
import path from 'node:path';
import cache from '@/utils/cache';
export const route: Route = {
path: '/search/:option?/:category?/:keyword?/:time?/:order?',
@@ -40,11 +44,56 @@ async function handler(ctx) {
const category = ctx.req.param('category') ?? 'all';
const keyword = ctx.req.param('keyword') ?? '';
const time = ctx.req.param('time') ?? 'a';
const order = ctx.req.param('order') ?? 'mr';
const { domain = defaultDomain } = ctx.req.query();
const rootUrl = getRootUrl(domain);
let order = ctx.req.param('order') ?? 'mr';
const currentUrl = `${rootUrl}/search/${option}${category === 'all' ? '' : `/${category}`}${keyword ? `?search_query=${keyword}` : '?'}${time === 'a' ? '' : `&t=${time}`}${order === 'mr' ? '' : `&o=${order}`}`;
const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit')) : 20;
return await ProcessItems(ctx, currentUrl, rootUrl);
let apiUrl = getApiUrl();
order = time === 'a' ? order : `${order}_${time}`;
apiUrl = `${apiUrl}/search?search_query=${keyword}&o=${order}`;
let apiResult = await processApiItems(apiUrl);
let filteredItemsByCategory = apiResult.content;
// Filter items by category if not 'all'
if (category !== 'all') {
filteredItemsByCategory = apiResult.content.filter((item) => item.category.title === apiMapCategory(category));
}
filteredItemsByCategory = filteredItemsByCategory.slice(0, limit);
const results = await Promise.all(
filteredItemsByCategory.map((item) =>
cache.tryGet(`18comic:search:${item.id}`, async () => {
const result = {};
result.title = item.name;
result.link = `${rootUrl}/album/${item.id}`;
result.guid = `18comic:/album/${item.id}`;
result.updated = parseDate(item.update_at);
apiUrl = `${getApiUrl()}/album?id=${item.id}`;
apiResult = await processApiItems(apiUrl);
result.pubDate = new Date(apiResult.addtime * 1000);
result.category = apiResult.tags.map((tag) => tag);
result.author = apiResult.author.map((a) => a).join(', ');
result.description = art(path.join(__dirname, 'templates/description.art'), {
introduction: apiResult.description,
images: [
`https://cdn-msp3.${domain}/media/albums/${item.id}_3x4.jpg`,
// 取得的预览图片会被分割排序,所以先只取封面图
// `https://cdn-msp3.${domain}/media/photos/${item.id}/00001.webp`,
// `https://cdn-msp3.${domain}/media/photos/${item.id}/00002.webp`,
// `https://cdn-msp3.${domain}/media/photos/${item.id}/00003.webp`,
],
cover: `https://cdn-msp3.${domain}/media/albums/${item.id}_3x4.jpg`,
category: result.category,
});
return result;
})
)
);
return {
title: `Search Results For '${keyword}' - 禁漫天堂`,
link: currentUrl.replace(/\?$/, ''),
item: results,
allowEmpty: true,
};
}
+70 -1
View File
@@ -6,11 +6,15 @@ import { art } from '@/utils/render';
import path from 'node:path';
import { config } from '@/config';
import ConfigNotFoundError from '@/errors/types/config-not-found';
import md5 from '@/utils/md5';
import CryptoJS from 'crypto-js';
const defaultDomain = 'jmcomic1.me';
// list of address: https://jmcomic2.bet
const allowDomain = new Set(['18comic.vip', '18comic.org', 'jmcomic.me', 'jmcomic1.me', 'jm-comic3.art', 'jm-comic.club', 'jm-comic2.ark']);
const apiDomain = 'www.cdnblackmyth.club';
const getRootUrl = (domain) => {
if (!config.feature.allow_user_supply_unsafe_domain && !allowDomain.has(domain)) {
throw new ConfigNotFoundError(`This RSS is disabled unless 'ALLOW_USER_SUPPLY_UNSAFE_DOMAIN' is set to 'true'.`);
@@ -19,6 +23,71 @@ const getRootUrl = (domain) => {
return `https://${domain}`;
};
const apiMapCategory = (category) => {
switch (category) {
case 'another':
return '其他漫畫';
case 'doujin':
return '同人';
case 'hanman':
return '韓漫';
case 'meiman':
return '美漫';
case 'short':
return '短篇';
case 'single':
return '單本';
default:
return null;
}
};
const getApiUrl = () => `https://${apiDomain}`;
// using api to fetch data
const processApiItems = async (apiUrl: string) => {
apiUrl = apiUrl.replace(/\?$/, '');
// get timestamp using javascript native api
const ts = Date.now();
const tokenParam = `${ts},1.7.5`;
// md5 from {token ts + "18comicAPP"
let token = `${ts}18comicAPP`;
token = md5(token);
const response = await got(apiUrl, {
headers: {
token,
tokenparam: tokenParam,
},
});
// decode base64
const encryptedWordArray = CryptoJS.enc.Base64.parse(response.data.data);
// to md5 hex string , this string must be 32 bytes , because it is used as key for AES-256
const md5HexStr = CryptoJS.MD5(ts + '185Hcomic3PAPP7R').toString(); // hex 字串
// convert string to WordArray that can be used as key for AES
const key = CryptoJS.enc.Utf8.parse(md5HexStr); // 32 bytes => AES-256
// create a CipherParams object from the encrypted WordArray
const cipherParams = CryptoJS.lib.CipherParams.create({
ciphertext: encryptedWordArray,
});
// decrypt the CipherParams object using AES in ECB mode with PKCS7 padding
const decrypted = CryptoJS.AES.decrypt(cipherParams, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
});
// convert the decrypted WordArray to a UTF-8 string , the result is a JSON string
const resultJson = decrypted.toString(CryptoJS.enc.Utf8);
const result = JSON.parse(resultJson);
return result;
};
const ProcessItems = async (ctx, currentUrl, rootUrl) => {
currentUrl = currentUrl.replace(/\?$/, '');
@@ -82,4 +151,4 @@ const ProcessItems = async (ctx, currentUrl, rootUrl) => {
};
};
export { defaultDomain, getRootUrl, ProcessItems };
export { defaultDomain, getRootUrl, ProcessItems, getApiUrl, processApiItems, apiMapCategory };