Skip to content

Use ipaddr.js to exclude local and reserved IP from proxying #209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -88,6 +88,7 @@
},
"dependencies": {
"axios": "^1.6.7",
"ipaddr.js": "^2.1.0",
"naive-ui": "^2.37.3",
"uuid": "^9.0.1",
"vue-query": "^1.26.0",
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;
}
};
Loading