Skip to content

Commit

Permalink
处理 socks 代理不可用,本地代理无法添加问题
Browse files Browse the repository at this point in the history
  • Loading branch information
niuniuland committed Mar 25, 2024
1 parent cab6b32 commit 9f54468
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "chrome-power",
"description": "The first open source fingerprint browser.",
"version": "1.1.0",
"version": "1.1.1",
"private": true,
"author": {
"email": "[email protected]",
Expand Down
26 changes: 22 additions & 4 deletions packages/main/src/fingerprint/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ import type {AxiosProxyConfig} from 'axios';
const logger = createLogger(API_LOGGER_LABEL);

const getRealIP = async (proxy: DB.Proxy) => {
const requestProxy = getRequestProxy(proxy.proxy!, proxy.proxy_type!);
let agent: SocksProxyAgent | HttpProxyAgent<`http://${string}:${string}`> | HttpsProxyAgent<`http://${string}:${string}`> | undefined = undefined;
let requestProxy: AxiosProxyConfig | undefined = undefined;
if (proxy.proxy_type?.toLowerCase() === 'socks5') {
const agentInfo = getAgent(proxy);
agent = agentInfo.agent;
} else {
requestProxy = getRequestProxy(proxy.proxy!, proxy.proxy_type!);
}

const makeRequest = async (url: string, proxy: AxiosProxyConfig | undefined) => {
try {
const {data} = await axios.get(url, {
proxy: proxy,
proxy: agent ? false : proxy,
timeout: 5_000,
httpAgent: agent,
httpsAgent: agent,
});
return url.includes('ip-api.com') ? data.query : data.ip;
} catch (error) {
Expand Down Expand Up @@ -114,7 +123,14 @@ export async function testProxy(proxy: DB.Proxy) {
connectivity: {name: string; elapsedTime: number; status: string; reason?: string}[];
} = {connectivity: []};

const requestProxy = getRequestProxy(proxy.proxy!, proxy.proxy_type!);
let agent: SocksProxyAgent | HttpProxyAgent<`http://${string}:${string}`> | HttpsProxyAgent<`http://${string}:${string}`> | undefined = undefined;
let requestProxy: AxiosProxyConfig | undefined = undefined;
if (proxy.proxy_type?.toLowerCase() === 'socks5') {
const agentInfo = getAgent(proxy);
agent = agentInfo.agent;
} else {
requestProxy = getRequestProxy(proxy.proxy!, proxy.proxy_type!);
}
try {
const ipInfo = await getProxyInfo(proxy);
result.ipInfo = ipInfo || {};
Expand All @@ -125,8 +141,10 @@ export async function testProxy(proxy: DB.Proxy) {
const startTime = Date.now();
try {
const response = await axios.get(pin.url, {
proxy: proxy.proxy ? requestProxy : undefined,
proxy: proxy.proxy && proxy.proxy_type?.toLocaleLowerCase() !== 'socks5' ? requestProxy : undefined,
timeout: 5_000,
httpAgent: agent,
httpsAgent: agent,
});
const endTime = Date.now();
const elapsedTime = endTime - startTime; // Calculate the time taken for the request
Expand Down
37 changes: 16 additions & 21 deletions packages/renderer/src/pages/proxy/import/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,49 +162,44 @@ const ProxyImport = () => {
}, []);

const parseProxy = (proxy: string, index: number) => {
// Default type is HTTP
let type = 'HTTP';
let type = 'HTTP'; // 默认类型
let host = '',
port = 0,
username = '',
password = '',
remark = '';

// Convert the start of the proxy string to lowercase for comparison
const proxyLower = proxy.toLowerCase();

// Check if the type is specified and it's SOCKS5 or HTTP
if (proxyLower.startsWith('socks5://')) {
type = 'SOCKS5';
proxy = proxy.substring(9); // Remove the 'socks5://' (9 characters)
proxy = proxy.substring(9);
} else if (proxyLower.startsWith('http://')) {
type = 'HTTP';
proxy = proxy.substring(7); // Remove the 'http://' (7 characters)
proxy = proxy.substring(7);
}

// Extract the remark if present
// 如果存在,提取备注
const remarkIndex = proxy.indexOf('{');
if (remarkIndex !== -1) {
remark = proxy.substring(remarkIndex + 1, proxy.length - 1);
proxy = proxy.substring(0, remarkIndex);
proxy = proxy.substring(0, remarkIndex).trim();
}

// Validate the complete proxy format
const proxyRegex = /^([a-zA-Z0-9.-]+):(\d{1,5}):([a-zA-Z0-9-]+):([a-zA-Z0-9]+)$/;
// 调整正则表达式以使用户名和密码可选
const proxyRegex = /^([a-zA-Z0-9.-]+):(\d{1,5})(?::([a-zA-Z0-9-]*):([a-zA-Z0-9]*))?$/;

if (!proxyRegex.test(proxy)) {
throw new Error('Invalid proxy format');
throw new Error('无效的代理格式');
}

// Split the remaining proxy string by ':'
const parts = proxy.split(':');
const parts = proxy.match(proxyRegex);

host = parts?.[1] || '';
port = Number(parts?.[2] || '');

// Assign values based on the number of parts we got
host = parts[0];
port = Number(parts[1]);
if (parts.length >= 4) {
// Only assign username and password if they exist in the split parts
username = parts[2];
password = parts[3];
if (parts?.[3] && parts[4]) {
username = parts[3];
password = parts[4];
}

return {
Expand Down
5 changes: 2 additions & 3 deletions packages/shared/utils/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ export const getRequestProxy = (
): AxiosProxyConfig | undefined => {
if (!proxy) return;
const [host, port, username, password] = proxy.split(':');

return {
protocol: proxy_type.toLocaleLowerCase(),
host,
port: +port,
auth: {
auth: username ? {
username,
password,
},
} : undefined,
};
};

0 comments on commit 9f54468

Please sign in to comment.