Skip to content

Commit 7d2baa8

Browse files
authored
fix(routes): remove hard-coded UA and use header presets (#21559)
1 parent 795028c commit 7d2baa8

File tree

17 files changed

+29
-70
lines changed

17 files changed

+29
-70
lines changed

lib/routes/changba/user.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import type { Route } from '@/types';
55
import { ViewType } from '@/types';
66
import cache from '@/utils/cache';
77
import got from '@/utils/got';
8-
9-
const headers = { 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1' };
8+
import { PRESETS } from '@/utils/header-generator';
109

1110
export const route: Route = {
1211
path: '/:userid',
@@ -38,7 +37,7 @@ async function handler(ctx) {
3837
const response = await got({
3938
method: 'get',
4039
url,
41-
headers,
40+
headerGeneratorOptions: PRESETS.MODERN_IOS,
4241
});
4342
const data = response.data;
4443
const $ = load(data);
@@ -54,7 +53,7 @@ async function handler(ctx) {
5453
const result = await got({
5554
method: 'get',
5655
url: link,
57-
headers,
56+
headerGeneratorOptions: PRESETS.MODERN_IOS,
5857
});
5958

6059
const re = /workid: '\d+'/;

lib/routes/ddosi/category.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { load } from 'cheerio';
22

3-
import { config } from '@/config';
43
import type { Route } from '@/types';
54
import got from '@/utils/got';
5+
import { PRESETS } from '@/utils/header-generator';
66
import { parseDate } from '@/utils/parse-date';
77

88
export const route: Route = {
@@ -25,22 +25,21 @@ export const route: Route = {
2525
},
2626
],
2727
name: '分类',
28-
maintainers: [],
28+
maintainers: ['XinRoom'],
2929
handler,
3030
url: 'ddosi.org/',
3131
};
3232

3333
async function handler(ctx) {
3434
const url = 'https://www.ddosi.org/category';
35-
const userAgent = config.ua || 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1';
3635
const category = ctx.req.param('category');
3736
const response = await got({
3837
method: 'get',
3938
url: `${url}/${category}/`,
4039
headers: {
41-
'User-Agent': userAgent,
4240
Referer: url,
4341
},
42+
headerGeneratorOptions: PRESETS.MODERN_IOS,
4443
});
4544
const $ = load(response.data);
4645
const list = $('main>article').toArray();

lib/routes/ddosi/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
import { load } from 'cheerio';
22

3-
import { config } from '@/config';
43
import type { Route } from '@/types';
54
import got from '@/utils/got';
5+
import { PRESETS } from '@/utils/header-generator';
66
import { parseDate } from '@/utils/parse-date';
77

88
export const route: Route = {
99
path: '/',
10+
example: '/ddosi',
1011
radar: [
1112
{
1213
source: ['ddosi.org/'],
1314
target: '',
1415
},
1516
],
16-
name: 'Unknown',
17+
name: '首页',
1718
maintainers: ['XinRoom'],
1819
handler,
1920
url: 'ddosi.org/',
2021
};
2122

2223
async function handler() {
2324
const url = 'https://www.ddosi.org/';
24-
const userAgent = config.ua || 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1';
2525
const response = await got({
2626
method: 'get',
2727
url: String(url),
2828
headers: {
29-
'User-Agent': userAgent,
3029
Referer: url,
3130
},
31+
headerGeneratorOptions: PRESETS.MODERN_IOS,
3232
});
3333
const $ = load(response.data);
3434
const list = $('main>article').toArray();

lib/routes/dianping/user.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { config } from '@/config';
22
import type { Route } from '@/types';
3+
import { PRESETS } from '@/utils/header-generator';
34
import ofetch from '@/utils/ofetch';
45

56
export const route: Route = {
@@ -58,12 +59,10 @@ const starMap: Record<number, string> = {
5859
async function handler(ctx) {
5960
const id = ctx.req.param('id');
6061

61-
const userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1';
6262
const userPage = `https://m.dianping.com/userprofile/${id}`;
6363
const cookie = config.dianping.cookie;
6464

6565
const headers: Record<string, string> = {
66-
'User-Agent': userAgent,
6766
Referer: userPage,
6867
};
6968

@@ -73,13 +72,15 @@ async function handler(ctx) {
7372

7473
const pageResponse = await ofetch(userPage, {
7574
headers,
75+
headerGeneratorOptions: PRESETS.MODERN_IOS,
7676
});
7777

7878
const nickNameReg = /window\.nickName = "(.*?)"/g;
7979
const nickName = nickNameReg.exec(pageResponse as string)?.[1];
8080

8181
const response = await ofetch(`https://m.dianping.com/member/ajax/NobleUserFeeds?userId=${id}`, {
8282
headers,
83+
headerGeneratorOptions: PRESETS.MODERN_IOS,
8384
});
8485

8586
const data = response.data;

lib/routes/hyperdash/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export async function fetchTopTraders(): Promise<TraderData[]> {
2121
const traders: TraderData[] = await ofetch(apiUrl, {
2222
headers: {
2323
'x-api-key': 'hyperdash_public_7vN3mK8pQ4wX2cL9hF5tR1bY6gS0jD',
24-
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36',
2524
},
2625
});
2726

lib/routes/jandan/utils.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@ import type { DataItem } from '@/types';
44
import ofetch from '@/utils/ofetch';
55
import { parseDate } from '@/utils/parse-date';
66

7-
export const USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36';
8-
97
/**
108
* Extract page ID from script tags in HTML
119
*/
1210
export const extractPageId = async (url: string, referer: string): Promise<string> => {
1311
const response = await ofetch(url, {
1412
headers: {
15-
'User-Agent': USER_AGENT,
1613
Referer: referer,
1714
Accept: 'application/json, text/plain, */*',
1815
},
@@ -39,7 +36,6 @@ export const handleTopSection = async (rootUrl: string, type: string): Promise<{
3936
const apiUrl = `${rootUrl}/api/top/${type}`;
4037
const response = await ofetch(apiUrl, {
4138
headers: {
42-
'User-Agent': USER_AGENT,
4339
Referer: rootUrl,
4440
Accept: 'application/json, text/plain, */*',
4541
},
@@ -114,7 +110,6 @@ export const handleForumSection = async (rootUrl: string): Promise<{ title: stri
114110
const apiUrl = `${rootUrl}/api/forum/posts/${forumId}?page=1`;
115111
const forumData = await ofetch(apiUrl, {
116112
headers: {
117-
'User-Agent': USER_AGENT,
118113
Referer: currentUrl,
119114
Accept: 'application/json, text/plain, */*',
120115
},
@@ -174,7 +169,6 @@ export const handleCommentSection = async (rootUrl: string, category: string): P
174169

175170
const response = await ofetch(currentUrl, {
176171
headers: {
177-
'User-Agent': USER_AGENT,
178172
Referer: rootUrl,
179173
Accept: 'application/json, text/plain, */*',
180174
},
@@ -200,7 +194,6 @@ export const handleCommentSection = async (rootUrl: string, category: string): P
200194
const apiUrl = `${rootUrl}/api/comment/post/${pageId}?order=desc&page=1`;
201195
const commentsData = await ofetch(apiUrl, {
202196
headers: {
203-
'User-Agent': USER_AGENT,
204197
Referer: currentUrl,
205198
Accept: 'application/json, text/plain, */*',
206199
},

lib/routes/kuaidi100/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ export default {
230230
headers: {
231231
Referer: 'https://www.kuaidi100.com/',
232232
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,ja;q=0.7',
233-
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36',
234233
Cookie: `${cookie.globacsrftoken}; ${cookie.csrf}; ${cookie.wwwid}; ${cookie.dasddocHref}; ${cookie.dasddocReferrer}; ${
235234
cookie.dasddocTitl
236235
}; addcom=${number}; addnu=${id}; snt_query_meta=${queryMeta}; sortStatus=0; Hm_lpvt_22ea01af58ba2be0fec7c11b25e88e6c=${timestamp}; Hm_lvt_22ea01af58ba2be0fec7c11b25e88e6c=${timestamp - 1642}`,

lib/routes/qiche365/recall.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import timezone from '@/utils/timezone';
77

88
const baseUrl = 'https://www.qiche365.org.cn';
99

10-
const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
11-
1210
export const route: Route = {
1311
path: '/recall/:channel',
1412
name: '汽车召回',
@@ -34,7 +32,6 @@ async function handler(ctx): Promise<Data> {
3432
// second request with those cookies returns the actual JSON data.
3533
const initResponse = await ofetch.raw(targetUrl, {
3634
headers: {
37-
'User-Agent': userAgent,
3835
'Accept-Language': 'zh-CN,zh;q=0.9',
3936
},
4037
ignoreResponseError: true,
@@ -44,7 +41,6 @@ async function handler(ctx): Promise<Data> {
4441

4542
const { html } = await ofetch(targetUrl, {
4643
headers: {
47-
'User-Agent': userAgent,
4844
'Accept-Language': 'zh-CN,zh;q=0.9',
4945
Cookie: cookies,
5046
},

lib/routes/qidian/author.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ import { load } from 'cheerio';
22

33
import type { Route } from '@/types';
44
import got from '@/utils/got';
5-
6-
const headers = {
7-
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Mobile/15E148 Safari/604.1',
8-
};
5+
import { PRESETS } from '@/utils/header-generator';
96

107
export const route: Route = {
118
path: '/author/:id',
@@ -35,7 +32,7 @@ async function handler(ctx) {
3532
const currentUrl = `https://my.qidian.com/author/${id}/`;
3633

3734
// Reason: PC site (my.qidian.com) returns anti-bot JS challenge; mobile site has SSR data
38-
const response = await got(`https://m.qidian.com/author/${id}/`, { headers });
35+
const response = await got(`https://m.qidian.com/author/${id}/`, { headerGeneratorOptions: PRESETS.MODERN_IOS });
3936
const $ = load(response.data);
4037
const { pageContext } = JSON.parse($('#vite-plugin-ssr_pageContext').text());
4138
const pageData = pageContext.pageProps.pageData;

lib/routes/qidian/chapter.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { load } from 'cheerio';
33
import type { Route } from '@/types';
44
import { ViewType } from '@/types';
55
import got from '@/utils/got';
6+
import { PRESETS } from '@/utils/header-generator';
67
import { parseDate } from '@/utils/parse-date';
78

89
export const route: Route = {
@@ -32,18 +33,13 @@ export const route: Route = {
3233
async function handler(ctx) {
3334
const id = ctx.req.param('id');
3435

35-
const userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Mobile/15E148 Safari/604.1';
36-
const headers = {
37-
'User-Agent': userAgent,
38-
};
39-
40-
const response = await got(`https://m.qidian.com/book/${id}.html`, { headers });
36+
const response = await got(`https://m.qidian.com/book/${id}.html`, { headerGeneratorOptions: PRESETS.MODERN_IOS });
4137
const $ = load(response.data);
4238

4339
const name = $('meta[property="og:title"]').attr('content');
4440
const coverUrl = `https:${$('.detail__header-cover__img').attr('src')}`;
4541

46-
const { data: catalog } = await got(`https://m.qidian.com/book/${id}/catalog/`, { headers });
42+
const { data: catalog } = await got(`https://m.qidian.com/book/${id}/catalog/`, { headerGeneratorOptions: PRESETS.MODERN_IOS });
4743
const $c = load(catalog);
4844
const { pageContext } = JSON.parse($c('#vite-plugin-ssr_pageContext').text());
4945

0 commit comments

Comments
 (0)