Skip to content

Commit

Permalink
When the Profile synchronized by Gist contains ruleListProfile/pacScr…
Browse files Browse the repository at this point in the history
…ipt, update the corresponding URL content in time #110
  • Loading branch information
zero-top committed Feb 24, 2025
1 parent 7c47b1f commit b0ad75d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ class ProxyImpl
)
profilePac = profilePacCache.get(profile)
profilePacKey = referenced_profiles.map(
(_profile) -> _profile.name + '_' + (_profile.revision or 1)
(_profile) ->
revision = _profile.revision or 1
# remote pacScript and rule list use sha256 to ensue uniqueId
if OmegaPac.Profiles.updateUrl(_profile) and _profile.sha256
revision = _profile.sha256
_profile.name + '_' + revision
).join(',')
if profilePac?[profilePacKey]
return profilePac[profilePacKey]
Expand Down
47 changes: 34 additions & 13 deletions omega-target/src/options.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,25 @@ OmegaPac = require 'omega-pac'
jsondiffpatch = require 'jsondiffpatch'


generateSHA256 = (text) ->
return new Promise((resolve, reject) ->
encoder = new TextEncoder()
data = encoder.encode(text)
crypto.subtle.digest("SHA-256", data).then((hashBuffer) ->
# 将 ArrayBuffer 转换为十六进制字符串
hashArray = Array.from(new Uint8Array(hashBuffer))
hashHex = hashArray.map((byte) ->
byte.toString(16).padStart(2, '0')).join('')
# 输出 SHA-256 哈希值
resolve(hashHex)
).catch((e) ->
console.log('eee', e)
reject(e)
)
)

PROFILETEMPPACKEY = '__tempZeroRuleListPac'
transformValueKeys = ['lastUpdate', 'ruleList', 'pacScript', 'sha256']

generateProfileTempPac = (profile) ->
tempProfile = OmegaPac.Profiles.create(PROFILETEMPPACKEY, profile.profileType)
Expand Down Expand Up @@ -76,7 +93,7 @@ class Options
if OmegaPac.Profiles.updateUrl(value)
profile = {}
for k, v of value
continue if k == 'lastUpdate' || k == 'ruleList' || k == 'pacScript'
continue if transformValueKeys.indexOf(k) >= 0
profile[k] = v
value = profile
return value
Expand Down Expand Up @@ -136,7 +153,8 @@ class Options
@sync.init({gistId, gistToken}).catch((e) ->
console.log('sync init fail::', e)
)
@_syncWatchStop = @sync.watchAndPull(@_storage)
@_syncWatchStop =
@sync.watchAndPull(@_storage, @updateProfile.bind(this))
@sync.copyTo(@_storage).catch(Storage.StorageUnavailableError, =>
console.error('Warning: Sync storage is not available in this ' +
'browser! Disabling options sync.')
Expand Down Expand Up @@ -745,16 +763,18 @@ class Options
# rejected by fetchUrl and will not end up here.
# So empty data indicates success without any update (e.g. 304).
return profile unless data
profile = OmegaPac.Profiles.byKey(key, @_options)
profile.lastUpdate = new Date().toISOString()
if OmegaPac.Profiles.update(profile, data)
OmegaPac.Profiles.dropCache(profile)
OmegaPac.Profiles.updateRevision(profile)
changes = {}
changes[key] = profile
@_setOptions(changes).return(profile)
else
return profile
generateSHA256(data).then((dataSHA256) =>
profile = OmegaPac.Profiles.byKey(key, @_options)
profile.lastUpdate = new Date().toISOString()
if OmegaPac.Profiles.update(profile, data) or not profile.sha256
profile.sha256 = dataSHA256
OmegaPac.Profiles.dropCache(profile)
changes = {}
changes[key] = profile
@_setOptions(changes).return(profile)
else
return profile
)
).catch (reason) ->
if reason instanceof Error then reason else new Error(reason)

Expand Down Expand Up @@ -1157,7 +1177,8 @@ class Options
@sync.enabled = true
@_syncWatchStop?()
@sync.requestPush(@_options)
@_syncWatchStop = @sync.watchAndPull(@_storage)
@_syncWatchStop =
@sync.watchAndPull(@_storage, @updateProfile.bind(this))
if args.useBuiltInSync
@sync.toggleBuiltInSync(true)
else
Expand Down
13 changes: 10 additions & 3 deletions omega-target/src/options_sync.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Promise = require 'bluebird'
Storage = require './storage'
Log = require './log'
{Revision} = require 'omega-pac'
{Revision, Profiles} = require 'omega-pac'
jsondiffpatch = require 'jsondiffpatch'
TokenBucket = require('limiter').TokenBucket

Expand Down Expand Up @@ -197,7 +197,7 @@ class OptionsSync
# @param {Storage} local The local storage to be written to
# @returns {function} Calling the returned function will stop watching.
###
watchAndPull: (local) ->
watchAndPull: (local, updateProfile) ->
pullScheduled = null
pull = {}
doPull = =>
Expand All @@ -208,7 +208,14 @@ class OptionsSync
Storage.operationsForChanges(changes, base: base, merge: @merge)
).then (operations) =>
@_logOperations('OptionsSync::pull', operations)
local.apply(operations)
local.apply(operations).then( ->
updateProfileNames = []
Object.values(operations.set).forEach((profile) ->
if typeof profile is 'object' and Profiles.updateUrl(profile)
updateProfileNames.push(profile.name)
)
updateProfile?(updateProfileNames)
).return(operations)

@storage.watch null, (changes, opts = {}) =>
for own key, value of changes
Expand Down

0 comments on commit b0ad75d

Please sign in to comment.