87 lines
3.4 KiB
TypeScript
87 lines
3.4 KiB
TypeScript
import { app } from 'electron'
|
|
import type { Session, WebContents } from 'electron'
|
|
import { readFileSync, renameSync, writeFileSync } from 'node:fs'
|
|
import { join } from 'node:path'
|
|
|
|
export const sitePermissions = ['camera', 'microphone', 'location', 'notifications', 'clipboard'] as const
|
|
type Permission = typeof sitePermissions[number]
|
|
type PermissionMap = Partial<Record<Permission, true>>
|
|
type Site = { origin: string; grants: PermissionMap }
|
|
|
|
let cache: Site[] | undefined
|
|
const path = () => join(app.getPath('userData'), 'browser-permissions.json')
|
|
|
|
function sites(): Site[] {
|
|
if (cache) return cache
|
|
try {
|
|
const saved: unknown = JSON.parse(readFileSync(path(), 'utf8'))
|
|
cache = Array.isArray(saved) ? saved.slice(0, 200).filter((site) =>
|
|
typeof site?.origin === 'string' && typeof site?.grants === 'object' && site.grants !== null) : []
|
|
} catch {
|
|
cache = []
|
|
}
|
|
return cache
|
|
}
|
|
|
|
export function grants(origin: string) {
|
|
return sites().find((site) => site.origin === origin)?.grants ?? {}
|
|
}
|
|
|
|
export function setGrant(origin: string, key: string, allowed: boolean) {
|
|
const permission = sitePermissions.find((item) => item === key)
|
|
if (!permission) throw new Error('未知网站权限')
|
|
const next = sites().filter((item) => item.origin !== origin)
|
|
const updated = { ...grants(origin) }
|
|
if (allowed) updated[permission] = true
|
|
else delete updated[permission]
|
|
if (sitePermissions.some((item) => updated[item] === true)) next.push({ origin, grants: updated })
|
|
if (next.length > 200) throw new Error('网站权限记录已达上限')
|
|
save(next)
|
|
}
|
|
|
|
export function clearGrants() {
|
|
save([])
|
|
}
|
|
|
|
function save(next: Site[]) {
|
|
const file = path()
|
|
writeFileSync(file + '.tmp', JSON.stringify(next), { mode: 0o600 })
|
|
renameSync(file + '.tmp', file)
|
|
cache = next
|
|
}
|
|
|
|
export function configurePermissions(session: Session, privateMode: boolean, visible: (contents: WebContents) => boolean) {
|
|
function approved(contents: WebContents | null, requester: string, mainFrame: boolean, permission: string, media: string[]) {
|
|
if (!contents || !mainFrame || !visible(contents)) return false
|
|
let origin: string
|
|
try {
|
|
const source = new URL(requester)
|
|
const page = new URL(contents.getURL())
|
|
if (source.origin !== page.origin || !['https:', 'http:'].includes(source.protocol)) return false
|
|
origin = source.origin
|
|
} catch {
|
|
return false
|
|
}
|
|
if (permission === 'fullscreen') return true
|
|
if (privateMode) return false
|
|
const current = grants(origin)
|
|
switch (permission) {
|
|
case 'media':
|
|
return media.length > 0 && media.every((type) => type === 'audio' && current.microphone === true ||
|
|
type === 'video' && current.camera === true)
|
|
case 'geolocation':
|
|
case 'geolocation-approximate': return current.location === true
|
|
case 'notifications': return current.notifications === true
|
|
case 'clipboard-read':
|
|
case 'clipboard-sanitized-write': return current.clipboard === true
|
|
default: return false
|
|
}
|
|
}
|
|
|
|
session.setPermissionCheckHandler((contents, permission, origin, details) =>
|
|
approved(contents, origin, details.isMainFrame, permission, details.mediaType ? [details.mediaType] : []))
|
|
session.setPermissionRequestHandler((contents, permission, callback, details) =>
|
|
callback(approved(contents, details.requestingUrl, details.isMainFrame, permission,
|
|
'mediaTypes' in details ? details.mediaTypes ?? [] : [])))
|
|
}
|