Skip to content

Commit

Permalink
support config proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
ovnrain committed Aug 29, 2023
1 parent 3ba91bd commit c4a28af
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 15 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@
"express-validator": "^7.0.1",
"got": "^13.0.0",
"http-errors": "^2.0.0",
"https-proxy-agent": "^7.0.1",
"node-html-parser": "^6.1.6",
"probe-image-size": "^7.2.3",
"qs": "^6.11.2"
"qs": "^6.11.2",
"socks-proxy-agent": "^8.0.1"
},
"type": "module"
}
55 changes: 53 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ declare namespace NodeJS {
PORT?: string;
SSL_CERT?: string;
SSL_KEY?: string;
HTTP_PROXY?: string;
HTTPS_PROXY?: string;
}
}
34 changes: 34 additions & 0 deletions src/router/v1/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Agent as HttpsAgent } from 'https';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { SocksProxyAgent } from 'socks-proxy-agent';
import got, { type ExtendOptions } from 'got';
import { JAVBUS_TIMEOUT, USER_AGENT } from './constants.js';

const PROXY_URL = process.env.HTTP_PROXY || process.env.HTTPS_PROXY;

export let agent: HttpsAgent | undefined = undefined;

if (PROXY_URL) {
if (/^https?:\/\//.test(PROXY_URL)) {
agent = new HttpsProxyAgent(PROXY_URL);
} else if (/^socks/.test(PROXY_URL)) {
agent = new SocksProxyAgent(PROXY_URL);
}
}

const extendOptions: ExtendOptions = {
headers: {
'User-Agent': USER_AGENT,
},
timeout: {
request: JAVBUS_TIMEOUT,
},
};

if (agent) {
extendOptions.agent = { http: agent, https: agent };
}

const client = got.extend(extendOptions);

export default client;
15 changes: 3 additions & 12 deletions src/router/v1/javbusParser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import got from 'got';
import bytes from 'bytes';
import { parse } from 'node-html-parser';
import type { HTMLElement } from 'node-html-parser';
import probe from 'probe-image-size';
import { JAVBUS_TIMEOUT, JAVBUS, USER_AGENT } from './constants.js';
import client, { agent } from './client.js';
import { JAVBUS } from './constants.js';
import type {
FilterType,
ImageSize,
Expand All @@ -24,15 +24,6 @@ type StarInfoRequiredKey = 'avatar' | 'id' | 'name';

type StarInfoOptionalKey = Exclude<keyof StarInfo, StarInfoRequiredKey>;

const client = got.extend({
headers: {
'User-Agent': USER_AGENT,
},
timeout: {
request: JAVBUS_TIMEOUT,
},
});

const starInfoMap: Record<StarInfoOptionalKey, string> = {
birthday: '生日: ',
age: '年齡: ',
Expand Down Expand Up @@ -291,7 +282,7 @@ export async function getMovieDetail(id: string): Promise<MovieDetail> {
let imageSize: ImageSize | null = null;

if (img) {
const { width, height } = await probe(img);
const { width, height } = await probe(img, { agent });
imageSize = { width, height };
}

Expand Down

0 comments on commit c4a28af

Please sign in to comment.