Skip to content

Commit

Permalink
Fixed: Optional proxy permission
Browse files Browse the repository at this point in the history
  • Loading branch information
mbnuqw committed Mar 26, 2021
1 parent 5ca9b3a commit 417928a
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 2 deletions.
8 changes: 8 additions & 0 deletions addon/actions/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ async function loadPermissions() {
this.permWebRequestBlocking = await browser.permissions.contains({
permissions: ['webRequest', 'webRequestBlocking'],
})
this.permProxy = await browser.permissions.contains({ permissions: ['proxy'] })

if (!this.permAllUrls) {
for (let c of Object.values(this.containers)) {
Expand All @@ -34,6 +35,13 @@ async function loadPermissions() {
if (c.userAgentActive) c.userAgentActive = false
}
}

if (!this.permProxy) {
for (let c of Object.values(this.containers)) {
if (c.proxified) c.proxified = false
if (c.proxy) c.proxy.type = 'direct'
}
}
}

function onMenuHidden() {
Expand Down
2 changes: 2 additions & 0 deletions addon/actions/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,14 @@ function updateReqHandlerDebounced() {
}

function turnOnReqHandler() {
if (!browser.proxy) return
if (!browser.proxy.onRequest.hasListener(Actions.proxyReqHandler)) {
browser.proxy.onRequest.addListener(Actions.proxyReqHandler, { urls: ['<all_urls>'] })
}
}

function turnOffReqHandler() {
if (!browser.proxy) return
if (browser.proxy.onRequest.hasListener(Actions.proxyReqHandler)) {
browser.proxy.onRequest.removeListener(Actions.proxyReqHandler)
}
Expand Down
4 changes: 4 additions & 0 deletions addon/locales/en.settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ export default {
'settings.web_request_blocking_info': {
message: 'Required for:\n- Changing the User-Agent per container',
},
'settings.proxy_label': { message: 'Proxy:' },
'settings.proxy_info': {
message: 'Required for:\n- Per-container proxy',
},

// --- Storage
'settings.storage_title': { message: 'Storage' },
Expand Down
4 changes: 4 additions & 0 deletions addon/locales/ru.settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,10 @@ export default {
'settings.web_request_blocking_info': {
message: 'Необходимо для:\n- Настройки User-Agent контейнеров',
},
'settings.proxy_label': { message: 'Прокси' },
'settings.proxy_info': {
message: 'Необходимо для:\n- Настройки прокси контейнеров',
},

// --- Storage
'settings.storage_title': { message: 'Данные' },
Expand Down
2 changes: 1 addition & 1 deletion addon/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"contextualIdentities",
"cookies",
"storage",
"proxy",
"bookmarks",
"sessions",
"menus",
Expand All @@ -38,6 +37,7 @@
"optional_permissions": [
"<all_urls>",
"tabHide",
"proxy",
"clipboardWrite",
"webRequest",
"webRequestBlocking"
Expand Down
10 changes: 10 additions & 0 deletions src/page.settings/actions/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ async function loadPermissions(init) {
this.state.permWebRequestBlocking = await browser.permissions.contains({
permissions: ['webRequest', 'webRequestBlocking'],
})
this.state.permProxy = await browser.permissions.contains({ permissions: ['proxy'] })

if (!this.state.permAllUrls) {
for (let c of Object.values(this.state.containers)) {
Expand All @@ -62,6 +63,14 @@ async function loadPermissions(init) {
}
if (!init) this.actions.saveContainersDebounced()
}

if (!this.state.permProxy) {
for (let c of Object.values(this.state.containers)) {
if (c.proxified) c.proxified = false
if (c.proxy) c.proxy.type = 'direct'
}
if (!init) this.actions.saveContainersDebounced()
}
}

function goToPerm(permId) {
Expand Down Expand Up @@ -100,6 +109,7 @@ async function updateActiveView() {
if (hash === 'tab-hide') return this.actions.goToPerm('tab_hide')
if (hash === 'clipboard-write') return this.actions.goToPerm('clipboard_write')
if (hash === 'web-request-blocking') return this.actions.goToPerm('web_request_blocking')
if (hash === 'proxy') return this.actions.goToPerm('proxy')

if (hash.startsWith('menu_editor')) {
setTimeout(
Expand Down
5 changes: 5 additions & 0 deletions src/page.settings/components/container-config.vue
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ export default {
this.switchProxy('direct')
State.selectedContainer = null
return
} else if (!State.permProxy) {
window.location.hash = 'proxy'
this.switchProxy('direct')
State.selectedContainer = null
return
}
}
Expand Down
17 changes: 16 additions & 1 deletion src/page.settings/components/import-config.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export default {
permWebData: false,
permTabHide: false,
permWebReqBlock: false,
permProxy: false,
}
},
Expand Down Expand Up @@ -176,14 +177,19 @@ export default {
let webData = false
let webReqBlock = false
let tabsHide = false
let proxy = false
this.permWebReqBlock = false
this.permWebData = false
this.permTabHide = false
this.permProxy = false
this.permNeeded = false
if (this.containers && State.importConfig.containers_v4) {
for (let ctr of Object.values(State.importConfig.containers_v4)) {
if (ctr.proxified) webData = true
if (ctr.proxified) {
webData = true
proxy = true
}
if (ctr.includeHostsActive) webData = true
if (ctr.excludeHostsActive) webData = true
if (ctr.userAgentActive) {
Expand Down Expand Up @@ -211,13 +217,18 @@ export default {
this.permTabHide = true
this.permNeeded = true
}
if (proxy && !State.permProxy) {
this.permProxy = true
this.permNeeded = true
}
},
requestPermissions() {
const request = { origins: [], permissions: [] }
if (this.permWebData) request.origins.push('<all_urls>')
if (this.permWebReqBlock) request.permissions.push('webRequest', 'webRequestBlocking')
if (this.permTabHide) request.permissions.push('tabHide')
if (this.permProxy) request.permissions.push('proxy')
browser.permissions.request(request).then(allowed => {
browser.runtime.sendMessage({ action: 'loadPermissions' })
if (this.permWebData) {
Expand All @@ -232,6 +243,10 @@ export default {
State.permTabHide = allowed
this.permTabHide = !allowed
}
if (this.permProxy) {
State.permProxy = allowed
this.permProxy = !allowed
}
this.permNeeded = !allowed
})
},
Expand Down
24 changes: 24 additions & 0 deletions src/page.settings/components/settings.permissions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ section
:value="$store.state.permWebRequestBlocking"
:note="t('settings.web_request_blocking_info')"
@input="togglePermWebRequestBlocking")
.permission(
ref="proxy"
:data-highlight="$store.state.highlightedField === 'proxy'"
@click="onHighlighClick('proxy')")
ToggleField(
label="settings.proxy_label"
:value="$store.state.permProxy"
:note="t('settings.proxy_info')"
@input="togglePermProxy")
</template>

<script>
Expand Down Expand Up @@ -128,6 +137,21 @@ export default {
})
}
},
async togglePermProxy() {
if (State.permProxy) {
await browser.permissions.remove({ permissions: ['proxy'] })
browser.runtime.sendMessage({ action: 'loadPermissions' })
Actions.loadPermissions()
} else {
const request = { origins: ['<all_urls>'], permissions: ['proxy'] }
browser.permissions.request(request).then(allowed => {
browser.runtime.sendMessage({ action: 'loadPermissions' })
State.permProxy = allowed
State.permAllUrls = allowed
})
}
},
},
}
</script>
1 change: 1 addition & 0 deletions src/page.settings/store/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default {
permTabHide: false,
permClipboardWrite: false,
permWebRequestBlocking: false,
permProxy: false,

dbgDetails: '',
}

0 comments on commit 417928a

Please sign in to comment.