1
1
import { RequestDetails , ProxyDetails } from './socksProxy.types' ;
2
+ import ipaddr from 'ipaddr.js' ;
2
3
3
4
const getGlobalProxyDetails = async ( ) : Promise < ProxyDetails > => {
4
5
const response = await browser . storage . local . get ( 'globalProxyDetails' ) ;
@@ -59,13 +60,7 @@ const handleProxyRequest = async (details: browser.proxy._OnRequestDetails) => {
59
60
const proxiedHosts = Object . keys ( hostProxiesParsed ) ;
60
61
const currentHost = getCurrentHost ( details ) ;
61
62
62
- if ( excludedHostsParsed . includes ( currentHost ) || currentHost . includes ( 'localhost' ) ) {
63
- // Disable logging for localhost
64
- if ( ! currentHost . includes ( 'localhost' ) ) {
65
- console . log ( 'excluded: ' , details . url ) ;
66
- console . log ( 'proxy used: direct' ) ;
67
- console . log ( '_____________________________' ) ;
68
- }
63
+ if ( excludedHostsParsed . includes ( currentHost ) || isLocalOrReservedIP ( currentHost ) ) {
69
64
return { type : 'direct' } ;
70
65
} else if (
71
66
proxiedHosts . includes ( currentHost ) &&
@@ -87,21 +82,38 @@ const getCurrentHost = (details: RequestDetails) => {
87
82
// the host is determined from its top parent frame (frameID === 0)
88
83
const frame = details . frameAncestors . find ( ( frame ) => frame . frameId === 0 ) ;
89
84
if ( frame ) {
90
- return new URL ( frame . url ) . host ;
85
+ return new URL ( frame . url ) . hostname ;
91
86
}
92
- } else if (
93
- new URL ( details . url ) . host . includes ( 'localhost' ) ||
94
- new URL ( details . url ) . host . includes ( '::1' ) ||
95
- new URL ( details . url ) . host . includes ( '127.0.0.1' )
96
- ) {
97
- // This is to make sure localhost traffic is not proxied
98
- return new URL ( details . url ) . host ;
87
+ } else if ( isLocalOrReservedIP ( new URL ( details . url ) . hostname ) ) {
88
+ // This is to handle localhost/reserved IP ranges
89
+ return new URL ( details . url ) . hostname ;
99
90
} else if ( details . documentUrl ) {
100
91
// when the request comes froms a a page(top level frame),
101
92
// then the host is determined from the document URL
102
- return new URL ( details . documentUrl ) . host ;
93
+ return new URL ( details . documentUrl ) . hostname ;
103
94
}
104
95
// When a request is initiated in the browser background,
105
96
// the host is derived from the request URL itself
106
- return new URL ( details . url ) . host ;
97
+ return new URL ( details . url ) . hostname ;
98
+ } ;
99
+
100
+ export const isLocalOrReservedIP = ( hostname : string ) => {
101
+ if ( hostname . includes ( 'localhost' ) ) return true ;
102
+ if ( ! ipaddr . isValid ( hostname ) ) return false ;
103
+
104
+ try {
105
+ const addr = ipaddr . parse ( hostname ) ;
106
+ const range = addr . range ( ) ;
107
+
108
+ return (
109
+ range === 'private' ||
110
+ range === 'multicast' ||
111
+ range === 'linkLocal' ||
112
+ range === 'loopback' ||
113
+ range === 'uniqueLocal'
114
+ ) ;
115
+ } catch ( e : unknown ) {
116
+ console . error ( 'Invalid IP address:' , e ) ;
117
+ return false ;
118
+ }
107
119
} ;
0 commit comments