Skip to content

Commit

Permalink
Merge branch 'main' into update-deps
Browse files Browse the repository at this point in the history
  • Loading branch information
ruihildt authored Apr 18, 2024
2 parents 5581ade + f8a61a3 commit 966aa73
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 17 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"dependencies": {
"axios": "^1.6.8",
"naive-ui": "^2.38.1",
"ipaddr.js": "^2.1.0",
"uuid": "^9.0.1",
"vue-query": "^1.26.0",
"vue-router": "^4.3.1"
Expand Down
37 changes: 37 additions & 0 deletions src/helpers/socksProxy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, expect, it } from '@jest/globals';
import { isLocalOrReservedIP } from './socksProxy';

describe('isLocalOrReservedIP', () => {
it('should return true for localhost', () => {
expect(isLocalOrReservedIP('localhost:8080')).toBeTruthy();
});

it('should return true for private IP', () => {
expect(isLocalOrReservedIP('192.168.1.1')).toBeTruthy();
});

it('should return true for loopback IP', () => {
expect(isLocalOrReservedIP('127.0.0.1')).toBeTruthy();
expect(isLocalOrReservedIP('::1')).toBeTruthy();
});

it('should return false for public IP', () => {
expect(isLocalOrReservedIP('8.8.8.8')).toBeFalsy();
});

it('should return false for invalid IP', () => {
expect(isLocalOrReservedIP('invalid.ip')).toBeFalsy();
});

it('should return true for unique local addresses', () => {
expect(isLocalOrReservedIP('fc00::')).toBeTruthy();
});

it('should return true for multicast addresses', () => {
expect(isLocalOrReservedIP('ff00::')).toBeTruthy();
});

it('should return false when IP address is not provided', () => {
expect(isLocalOrReservedIP('')).toBeFalsy();
});
});
46 changes: 29 additions & 17 deletions src/helpers/socksProxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RequestDetails, ProxyDetails } from './socksProxy.types';
import ipaddr from 'ipaddr.js';

const getGlobalProxyDetails = async (): Promise<ProxyDetails> => {
const response = await browser.storage.local.get('globalProxyDetails');
Expand Down Expand Up @@ -59,13 +60,7 @@ const handleProxyRequest = async (details: browser.proxy._OnRequestDetails) => {
const proxiedHosts = Object.keys(hostProxiesParsed);
const currentHost = getCurrentHost(details);

if (excludedHostsParsed.includes(currentHost) || currentHost.includes('localhost')) {
// Disable logging for localhost
if (!currentHost.includes('localhost')) {
console.log('excluded: ', details.url);
console.log('proxy used: direct');
console.log('_____________________________');
}
if (excludedHostsParsed.includes(currentHost) || isLocalOrReservedIP(currentHost)) {
return { type: 'direct' };
} else if (
proxiedHosts.includes(currentHost) &&
Expand All @@ -87,21 +82,38 @@ const getCurrentHost = (details: RequestDetails) => {
// the host is determined from its top parent frame (frameID === 0)
const frame = details.frameAncestors.find((frame) => frame.frameId === 0);
if (frame) {
return new URL(frame.url).host;
return new URL(frame.url).hostname;
}
} else if (
new URL(details.url).host.includes('localhost') ||
new URL(details.url).host.includes('::1') ||
new URL(details.url).host.includes('127.0.0.1')
) {
// This is to make sure localhost traffic is not proxied
return new URL(details.url).host;
} else if (isLocalOrReservedIP(new URL(details.url).hostname)) {
// This is to handle localhost/reserved IP ranges
return new URL(details.url).hostname;
} else if (details.documentUrl) {
// when the request comes froms a a page(top level frame),
// then the host is determined from the document URL
return new URL(details.documentUrl).host;
return new URL(details.documentUrl).hostname;
}
// When a request is initiated in the browser background,
// the host is derived from the request URL itself
return new URL(details.url).host;
return new URL(details.url).hostname;
};

export const isLocalOrReservedIP = (hostname: string) => {
if (hostname.includes('localhost')) return true;
if (!ipaddr.isValid(hostname)) return false;

try {
const addr = ipaddr.parse(hostname);
const range = addr.range();

return (
range === 'private' ||
range === 'multicast' ||
range === 'linkLocal' ||
range === 'loopback' ||
range === 'uniqueLocal'
);
} catch (e: unknown) {
console.error('Invalid IP address:', e);
return false;
}
};

0 comments on commit 966aa73

Please sign in to comment.