Skip to content

Let's use AI to Earn!

feat: 代理开发完成

gaoSir2e1cb6295a47
diff --git a/electron/plat/coomont.ts b/electron/plat/coomont.ts
index 938a86d0..457d3841 100644
--- a/electron/plat/coomont.ts
+++ b/electron/plat/coomont.ts
@@ -4,10 +4,11 @@ import requestNet from './requestNet';
export async function proxyCheck(proxy: string) {
try {
const res = await requestNet({
- url: 'https://www.baidu.com',
+ url: 'https://httpbin.org/ip',
method: 'GET',
proxy,
});
+ console.log(res);
if (res.status === 200 || res.status === 201 || res.data.length > 100000) {
return true;
}
diff --git a/electron/plat/requestNet.ts b/electron/plat/requestNet.ts
index 4ee191a2..af517cca 100644
--- a/electron/plat/requestNet.ts
+++ b/electron/plat/requestNet.ts
@@ -2,6 +2,7 @@ import { net, session, Session } from 'electron';
import FormData from 'form-data';
import { ipv4Regular } from '../../commont/regular';
import { parseProxyString } from '../../commont/utils';
+import { ProxyInfo } from '@@/utils.type';
export interface IRequestNetResult<T> {
status: number;
@@ -33,14 +34,14 @@ const requestNet = <T = any>({
isReqFile,
proxy,
}: IRequestNetParams): Promise<IRequestNetResult<T>> => {
+ let customSession: Session;
+ let proxyInfo: ProxyInfo | false;
return new Promise(async (resolve, reject) => {
try {
- let customSession: Session;
-
// 如果传入了代理配置,动态设置代理
if (proxy) {
// 解析代理信息
- const proxyInfo = parseProxyString(proxy);
+ proxyInfo = parseProxyString(proxy);
if (proxyInfo === false || !ipv4Regular.test(proxyInfo.ipAndPort))
throw new Error('代理地址不合法');
customSession = session.fromPartition(
@@ -57,7 +58,7 @@ const requestNet = <T = any>({
...(headers ? headers : {}),
...(proxy
? {
- 'x-forwarded-for': proxy,
+ 'x-forwarded-for': proxyInfo.ipAndPort,
}
: {}),
};
@@ -117,6 +118,15 @@ const requestNet = <T = any>({
});
});
+ if (proxyInfo && typeof proxyInfo === 'object') {
+ req.on('login', (authInfo, callback) => {
+ callback(
+ (proxyInfo as ProxyInfo).username,
+ (proxyInfo as ProxyInfo).password,
+ );
+ });
+ }
+
// 错误处理
req.on('error', (error) => {
console.log('error:', error);