tor ip leak protection

This commit is contained in:
Fabian Freund
2025-09-26 15:36:36 +02:00
parent 7da05d0780
commit 9ec56ac62a
3 changed files with 75 additions and 65 deletions
@@ -38,11 +38,9 @@ class ProxySettingsReplication extends _$ProxySettingsReplication {
fireImmediately: true, fireImmediately: true,
torProxyServiceProvider.select((data) => data.value), torProxyServiceProvider.select((data) => data.value),
(previous, next) async { (previous, next) async {
if (next != null) {
await ref await ref
.read(torProxyRepositoryProvider.notifier) .read(torProxyRepositoryProvider.notifier)
.setProxyPort(next); .setProxyPort(next ?? -1);
}
}, },
onError: (error, stackTrace) { onError: (error, stackTrace) {
logger.e( logger.e(
@@ -31,6 +31,7 @@ export default class BackgroundMain {
this.store = store this.store = store
} }
/*
initializeAuthListener(cookieStoreId: string, proxy: HttpProxySettings | HttpsProxySettings): void { initializeAuthListener(cookieStoreId: string, proxy: HttpProxySettings | HttpsProxySettings): void {
const listener: (details: _OnAuthRequiredDetails) => BlockingResponse = (details) => { const listener: (details: _OnAuthRequiredDetails) => BlockingResponse = (details) => {
if (!details.isProxy) return {} if (!details.isProxy) return {}
@@ -55,39 +56,46 @@ export default class BackgroundMain {
['blocking'] ['blocking']
) )
} }
*/
async onRequest(requestDetails: Pick<_OnRequestDetails, 'cookieStoreId' | 'url' | 'tabId'>): Promise<DoNotProxy | ProxyInfo[]> { async onRequest(requestDetails: Pick<_OnRequestDetails, 'cookieStoreId' | 'url' | 'tabId'>): Promise<DoNotProxy | ProxyInfo[]> {
if (requestDetails.tabId > -1) { const tab = (requestDetails.tabId > -1) ? (await browser.tabs.get(requestDetails.tabId)) : null
const tab = (await browser.tabs.get(requestDetails.tabId))
if (this.store.hasGeneralRelation() || if (this.store.hasGeneralRelation() ||
tab === null ||
tab.cookieStoreId?.startsWith(containerIdentifier) === true || tab.cookieStoreId?.startsWith(containerIdentifier) === true ||
tab.cookieStoreId === privateIdentifier tab.cookieStoreId === privateIdentifier
) { ) {
try { try {
let cookieStoreId: string let cookieStoreId: string
if (tab.cookieStoreId?.startsWith(containerIdentifier) === true) { if (tab?.cookieStoreId?.startsWith(containerIdentifier) === true) {
cookieStoreId = tab.cookieStoreId.substring(containerIdentifier.length) cookieStoreId = tab.cookieStoreId.substring(containerIdentifier.length)
} else if (tab.cookieStoreId === privateIdentifier) { } else if (tab?.cookieStoreId === privateIdentifier) {
// Handle private tabs - use 'private' as identifier // Handle private tabs - use 'private' as identifier
cookieStoreId = 'private' cookieStoreId = 'private'
} else { } else {
cookieStoreId = 'general' cookieStoreId = 'general'
} }
const proxies = this.store.getProxiesForContainer(cookieStoreId) let proxies = this.store.getProxiesForContainer(cookieStoreId)
if ((proxies?.length ?? 0) == 0 && tab === null) {
//When no tab is specified and general relation doesnt exist, we get all proxies to avoid leakage
proxies = this.store.getAllProxies()
if (proxies === null) { if (proxies.length == 0) {
return doNotProxy
}
} else if (proxies === null) {
return doNotProxy return doNotProxy
} }
if (proxies.length > 0) { if (proxies.length > 0) {
proxies.forEach(p => { // proxies.forEach(p => {
if (p.type === ProxyType.Http || p.type === ProxyType.Https) { // if (p.type === ProxyType.Http || p.type === ProxyType.Https) {
this.initializeAuthListener(cookieStoreId, p) // this.initializeAuthListener(cookieStoreId, p)
} // }
}) // })
const result: ProxyInfo[] = proxies.filter((p: ProxySettings) => { const result: ProxyInfo[] = proxies.filter((p: ProxySettings) => {
try { try {
@@ -115,7 +123,6 @@ export default class BackgroundMain {
return [emergencyBreak] return [emergencyBreak]
} }
} }
}
return doNotProxy return doNotProxy
} }
@@ -17,6 +17,7 @@ port.onMessage.addListener((raw: unknown): void => {
const message = raw as Message; const message = raw as Message;
switch (message.action) { switch (message.action) {
case "setProxyPort": case "setProxyPort":
if (message.args > -1) {
store.putProxy(new Socks5ProxySettings({ store.putProxy(new Socks5ProxySettings({
id: 'tor', id: 'tor',
type: 'socks', type: 'socks',
@@ -27,6 +28,10 @@ port.onMessage.addListener((raw: unknown): void => {
proxyDNS: true, proxyDNS: true,
})) }))
console.log('put tor port ' + message.args) console.log('put tor port ' + message.args)
} else {
store.deleteProxyById('tor')
console.log('remove tor port')
}
break break
case "addContainerProxy": case "addContainerProxy":