Skip to content

Commit d4d94b1

Browse files
committed
feat: updates for firefox
1 parent 361fd10 commit d4d94b1

6 files changed

Lines changed: 81 additions & 85 deletions

File tree

src/entrypoints/background.ts

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { browser } from 'wxt/browser'
22
import { handleUserToken } from './utils/handleUserToken'
3-
import { clearProxySettings } from './popup/proxy.service'
3+
import { clearProxySettings, updateProxySettings } from './popup/proxy.service'
44

55
const FOUR_DAYS_IN_MS = 4 * 24 * 60 * 60 * 1000
66
let interval: NodeJS.Timeout | null = null
77

88
function startInterval() {
99
if (interval) clearInterval(interval)
1010
interval = setInterval(() => {
11-
console.log('Refresh token')
1211
handleUserToken()
1312
}, FOUR_DAYS_IN_MS)
1413
}
@@ -31,14 +30,21 @@ export default defineBackground(() => {
3130
.then((items) => {
3231
const { ip, city, region, country } = items
3332
const locationText = `${city}, ${region}, ${country}`
34-
3533
sendResponse({
3634
location: locationText,
3735
ip,
3836
})
3937
})
4038
.catch(() => {
41-
// NO OP
39+
sendResponse(null)
40+
})
41+
} else if (message === 'SET_PROXY') {
42+
updateProxySettings()
43+
.then(() => {
44+
sendResponse({})
45+
})
46+
.catch(() => {
47+
sendResponse({})
4248
})
4349
} else if (message === 'RESET_PROXY') {
4450
clearProxySettings()
@@ -59,12 +65,13 @@ export default defineBackground(() => {
5965
}
6066

6167
async function initializeLocalCache() {
62-
const result = await browser.storage.local.get(['userToken', 'connection'])
68+
const result = await browser.storage.local.get(['userToken', 'connection', 'vpnEnabled'])
6369
const userToken = result.userToken as { token: string } | undefined
6470
const connection = result.connection as string | undefined
6571
console.log('INITIAL LOCAL STORAGE: ', userToken)
6672
localCache.token = userToken?.token ?? null
6773
localCache.connection = connection ?? null
74+
localCache.vpnEnabled = (result.vpnEnabled as boolean) ?? false
6875
}
6976

7077
startInterval()
@@ -79,29 +86,47 @@ export default defineBackground(() => {
7986
if (changes.connection?.newValue) {
8087
localCache.connection = (changes.connection.newValue as string) ?? null
8188
}
89+
if ('vpnEnabled' in changes) {
90+
localCache.vpnEnabled = (changes.vpnEnabled.newValue as boolean) ?? false
91+
}
8292
}
8393
})
8494

85-
browser.webRequest.onAuthRequired.addListener(
86-
function (details) {
87-
if (details.isProxy) {
88-
return {
89-
authCredentials: {
90-
username: localCache.connection ?? 'FR',
91-
password: localCache.token ?? '',
92-
},
93-
}
94-
}
95-
return {}
96-
},
97-
{ urls: ['<all_urls>'] },
98-
['blocking']
99-
)
95+
if (import.meta.env.BROWSER === 'firefox') {
96+
const VPN_HOST = import.meta.env.VITE_VPN_SERVER_ADDRESS
97+
const VPN_PORT = Number(import.meta.env.VITE_VPN_SERVER_PORT)
98+
;(browser as any).proxy.onRequest.addListener(
99+
(details: any) => {
100+
if (details.tabId === -1 || details.originUrl?.startsWith('moz-extension://')) return { type: 'direct' }
101+
if (!localCache.vpnEnabled) return { type: 'direct' }
102+
return { type: 'http', host: VPN_HOST, port: VPN_PORT, username: localCache.connection ?? 'FR', password: localCache.token ?? '' }
103+
},
104+
{ urls: ['<all_urls>'] },
105+
)
100106

101-
browser.webRequest.onErrorOccurred.addListener(
102-
(error) => {
103-
console.log('[AN ERROR OCURRED]:', error)
104-
},
105-
{ urls: ['<all_urls>'] }
106-
)
107+
browser.webRequest.onAuthRequired.addListener(
108+
function (details) {
109+
if (!details.isProxy) return {}
110+
return { authCredentials: { username: localCache.connection ?? 'FR', password: localCache.token ?? '' } }
111+
},
112+
{ urls: ['<all_urls>'] },
113+
['blocking'],
114+
)
115+
} else {
116+
browser.webRequest.onAuthRequired.addListener(
117+
function (details) {
118+
if (details.isProxy) {
119+
return {
120+
authCredentials: {
121+
username: localCache.connection ?? 'FR',
122+
password: localCache.token ?? '',
123+
},
124+
}
125+
}
126+
return {}
127+
},
128+
{ urls: ['<all_urls>'] },
129+
['blocking'],
130+
)
131+
}
107132
})

src/entrypoints/content/index.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,10 @@ export default defineContentScript({
5252
if (eventMessage === MESSAGES.USER_TOKEN) {
5353
const token = event.data.payload.token
5454

55-
browser.storage.local
56-
.set({
57-
userToken: {
58-
token,
59-
type: 'user',
60-
},
61-
})
62-
.then(() => {
63-
console.log(
64-
'The user has been authenticated in the VPN extension',
65-
)
66-
})
55+
browser.storage.local.set({ userToken: { token, type: 'user' } })
6756
} else if (eventMessage === MESSAGES.USER_LOG_OUT) {
6857
browser.storage.local.clear().then(async () => {
6958
await browser.runtime.sendMessage('RESET_PROXY')
70-
console.log('The user has been logged out from the VPN extension')
7159
})
7260
}
7361
}

src/entrypoints/popup/App.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useEffect, useState } from 'react'
22
import { browser } from 'wxt/browser'
33

4-
import { clearProxySettings, updateProxySettings } from './proxy.service'
4+
import { clearProxySettings } from './proxy.service'
55
import { ConnectionDetails } from '../components/ConnectionDetails'
66
import { VpnStatus } from '../components/VpnStatus'
77
import { Footer } from '../components/Footer'
@@ -82,9 +82,7 @@ export const App = () => {
8282

8383
setSelectedLocation(location)
8484
} catch (error) {
85-
console.error(`ERROR WHILE INITIALIZING APP STATE: ${error}`)
8685
if (error instanceof UnauthorizedError) {
87-
console.warn('Authorization error detected:', error.message)
8886
await onLogOut()
8987
}
9088
}
@@ -100,11 +98,11 @@ export const App = () => {
10098
}
10199

102100
const onConnectVpn = async () => {
103-
await updateProxySettings()
101+
await browser.runtime.sendMessage('SET_PROXY')
104102
const userData = await browser.runtime.sendMessage('GET_DATA')
105-
setUserData(userData)
106-
await storageService.saveVpnStatus('ON', userData)
107-
103+
const resolvedUserData = userData ?? defaultUserDataInfo
104+
setUserData(resolvedUserData)
105+
await storageService.saveVpnStatus('ON', resolvedUserData)
108106
setStatus('ON')
109107
}
110108

@@ -125,9 +123,7 @@ export const App = () => {
125123
}
126124
} catch (err) {
127125
await onDisconnectVpn()
128-
} finally {
129-
const newStatus = status === 'OFF' ? 'ON' : 'OFF'
130-
setStatus(newStatus)
126+
setStatus('OFF')
131127
}
132128
}
133129

src/entrypoints/popup/proxy.service.ts

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@ const VPN_CONFIG = {
55
PORT: Number(import.meta.env.VITE_VPN_SERVER_PORT),
66
}
77

8+
const IS_FIREFOX = import.meta.env.BROWSER === 'firefox'
9+
810
async function clearProxyCache() {
9-
const options: Record<string, any> = {}
10-
const rootDomain = VPN_CONFIG.HOST
11-
options.origins = []
12-
options.origins.push('http://' + rootDomain)
13-
options.origins.push('https://' + rootDomain)
14-
15-
const types = { cookies: true }
16-
browser.browsingData.remove(options, types).then(() => {
17-
console.log('PROXY CACHE REMOVED')
18-
})
11+
browser.browsingData.remove({}, { cookies: true })
1912
}
2013

2114
export async function updateProxySettings() {
15+
if (IS_FIREFOX) {
16+
await browser.storage.local.set({ vpnEnabled: true })
17+
return
18+
}
19+
2220
const proxyConfig = {
2321
mode: 'fixed_servers' as const,
2422
rules: {
@@ -31,30 +29,21 @@ export async function updateProxySettings() {
3129
},
3230
}
3331

34-
browser.proxy.settings
35-
.set({ value: proxyConfig, scope: 'regular' })
36-
.then(() => {
37-
console.log('CONNECTED')
38-
})
39-
.catch((err) => {
40-
console.log('ERROR WHILE CONNECTING TO THE PROXY: ', err)
41-
})
32+
browser.proxy.settings.set({ value: proxyConfig, scope: 'regular' })
4233
}
4334

4435
export async function clearProxySettings() {
36+
if (IS_FIREFOX) {
37+
await browser.storage.local.set({ vpnEnabled: false })
38+
clearProxyCache()
39+
return
40+
}
41+
4542
const proxyConfig = {
4643
mode: 'system' as const,
4744
}
4845

49-
browser.proxy.settings
50-
.set({ value: proxyConfig, scope: 'regular' })
51-
.then(() => {
52-
if (browser.runtime.lastError) {
53-
console.error(
54-
'ERROR ADDING THE DEFAULT PROXY CONFIG: ',
55-
browser.runtime.lastError,
56-
)
57-
}
58-
clearProxyCache()
59-
})
46+
browser.proxy.settings.set({ value: proxyConfig, scope: 'regular' }).then(() => {
47+
clearProxyCache()
48+
})
6049
}

src/entrypoints/utils/handleUserToken.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ import storageService, { getUserToken } from '../services/storage.service'
77

88
const refreshExistentUserToken = async (userToken: string) => {
99
const refreshedToken = await refreshUserToken(userToken)
10-
console.log(`User token refreshed`)
11-
await storageService.saveUserToken('user', refreshedToken)
10+
await storageService.saveUserToken('user', refreshedToken)
1211
}
1312

1413
const refreshAnonymousToken = async () => {
1514
const anonymousToken = await getAnonymousToken()
16-
console.log(`Anonymous token refreshed`)
17-
await storageService.saveUserToken('anonymous', anonymousToken.token)
15+
await storageService.saveUserToken('anonymous', anonymousToken.token)
1816
}
1917

2018
export const handleUserToken = async () => {

wxt.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default defineConfig({
77
}),
88
modules: ['@wxt-dev/i18n/module'],
99
srcDir: 'src',
10-
manifest: {
10+
manifest: ({ browser }) => ({
1111
name: 'Internxt VPN - Free, Encrypted & Unlimited VPN',
1212
short_name: 'Internxt VPN',
1313
default_locale: 'en',
@@ -25,7 +25,7 @@ export default defineConfig({
2525
'storage',
2626
'proxy',
2727
'webRequest',
28-
'webRequestAuthProvider',
28+
...(browser === 'firefox' ? ['webRequestBlocking'] : ['webRequestAuthProvider']),
2929
'browsingData',
3030
],
3131
web_accessible_resources: [
@@ -38,5 +38,5 @@ export default defineConfig({
3838
action: {
3939
default_popup: 'index.html',
4040
},
41-
},
41+
}),
4242
})

0 commit comments

Comments
 (0)