11import { browser } from 'wxt/browser'
22import { handleUserToken } from './utils/handleUserToken'
3- import { clearProxySettings } from './popup/proxy.service'
3+ import { clearProxySettings , updateProxySettings } from './popup/proxy.service'
44
55const FOUR_DAYS_IN_MS = 4 * 24 * 60 * 60 * 1000
66let interval : NodeJS . Timeout | null = null
77
88function 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} )
0 commit comments