From e2d9293e5afcd1a15ed87f1f482f533777f9ac96 Mon Sep 17 00:00:00 2001 From: FelisCatus Date: Sun, 26 Aug 2018 20:22:38 -0700 Subject: [PATCH] Fix externalProfile watcher after refactoring. Fix #1607. --- .../src/coffee/background.coffee | 6 +- .../src/module/options.coffee | 3 - .../src/module/parse_external_profile.coffee | 110 ------------------ .../src/module/proxy/proxy_impl.coffee | 1 + .../module/proxy/proxy_impl_settings.coffee | 110 +++++++++++++++++- omega-web/src/popup.jade | 2 +- 6 files changed, 115 insertions(+), 117 deletions(-) delete mode 100644 omega-target-chromium-extension/src/module/parse_external_profile.coffee diff --git a/omega-target-chromium-extension/src/coffee/background.coffee b/omega-target-chromium-extension/src/coffee/background.coffee index 669706b..ac4514a 100644 --- a/omega-target-chromium-extension/src/coffee/background.coffee +++ b/omega-target-chromium-extension/src/coffee/background.coffee @@ -253,10 +253,12 @@ proxyImpl.watchProxyChange (details) -> clearTimeout(timeout) if timeout? parsed = null timeout = setTimeout (-> - options.setExternalProfile(parsed, {noRevert: noRevert, internal: internal}) + if parsed + options.setExternalProfile(parsed, + {noRevert: noRevert, internal: internal}) ), 500 - parsed = options.parseExternalProfile(details) + parsed = proxyImpl.parseExternalProfile(details, options._options) return external = false diff --git a/omega-target-chromium-extension/src/module/options.coffee b/omega-target-chromium-extension/src/module/options.coffee index efc4d77..09ac9f2 100644 --- a/omega-target-chromium-extension/src/module/options.coffee +++ b/omega-target-chromium-extension/src/module/options.coffee @@ -2,7 +2,6 @@ OmegaTarget = require('omega-target') OmegaPac = OmegaTarget.OmegaPac Promise = OmegaTarget.Promise querystring = require('querystring') -parseExternalProfile = require('./parse_external_profile') WebRequestMonitor = require('./web_request_monitor') ChromePort = require('./chrome_port') fetchUrl = require('./fetch_url') @@ -10,8 +9,6 @@ Url = require('url') class ChromeOptions extends OmegaTarget.Options _inspect: null - parseExternalProfile: (details) -> - parseExternalProfile(details, @_options, @_fixedProfileConfig.bind(this)) fetchUrl: fetchUrl diff --git a/omega-target-chromium-extension/src/module/parse_external_profile.coffee b/omega-target-chromium-extension/src/module/parse_external_profile.coffee deleted file mode 100644 index 731d673..0000000 --- a/omega-target-chromium-extension/src/module/parse_external_profile.coffee +++ /dev/null @@ -1,110 +0,0 @@ -OmegaTarget = require('omega-target') -OmegaPac = OmegaTarget.OmegaPac - -module.exports = (details, options, fixedProfileConfig) -> - if details.name - details - else - switch details.value.mode - when 'system' - OmegaPac.Profiles.byName('system') - when 'direct' - OmegaPac.Profiles.byName('direct') - when 'auto_detect' - OmegaPac.Profiles.create({ - profileType: 'PacProfile' - name: '' - pacUrl: 'http://wpad/wpad.dat' - }) - when 'pac_script' - url = details.value.pacScript.url - if url - profile = null - OmegaPac.Profiles.each options, (key, p) -> - if p.profileType == 'PacProfile' and p.pacUrl == url - profile = p - profile ? OmegaPac.Profiles.create({ - profileType: 'PacProfile' - name: '' - pacUrl: url - }) - else do -> - profile = null - script = details.value.pacScript.data - OmegaPac.Profiles.each options, (key, p) -> - if p.profileType == 'PacProfile' and p.pacScript == script - profile = p - return profile if profile - # Try to parse the prefix used by this class. - script = script.trim() - magic = '/*OmegaProfile*' - if script.substr(0, magic.length) == magic - end = script.indexOf('*/') - if end > 0 - i = magic.length - tokens = script.substring(magic.length, end).split('*') - [profileName, revision] = tokens - try - profileName = JSON.parse(profileName) - catch _ - profileName = null - if profileName and revision - profile = OmegaPac.Profiles.byName(profileName, options) - if OmegaPac.Revision.compare(profile.revision, revision) == 0 - return profile - return OmegaPac.Profiles.create({ - profileType: 'PacProfile' - name: '' - pacScript: script - }) - when 'fixed_servers' - props = ['proxyForHttp', 'proxyForHttps', 'proxyForFtp', - 'fallbackProxy', 'singleProxy'] - proxies = {} - for prop in props - result = OmegaPac.Profiles.pacResult(details.value.rules[prop]) - if prop == 'singleProxy' and details.value.rules[prop]? - proxies['fallbackProxy'] = result - else - proxies[prop] = result - bypassSet = {} - bypassCount = 0 - if details.value.rules.bypassList - for pattern in details.value.rules.bypassList - bypassSet[pattern] = true - bypassCount++ - if bypassSet[''] - for host in OmegaPac.Conditions.localHosts when bypassSet[host] - delete bypassSet[host] - bypassCount-- - profile = null - OmegaPac.Profiles.each options, (key, p) -> - return if p.profileType != 'FixedProfile' - return if p.bypassList.length != bypassCount - for condition in p.bypassList - return unless bypassSet[condition.pattern] - rules = fixedProfileConfig(p).rules - if rules['singleProxy'] - rules['fallbackProxy'] = rules['singleProxy'] - delete rules['singleProxy'] - return unless rules? - for prop in props when rules[prop] or proxies[prop] - if OmegaPac.Profiles.pacResult(rules[prop]) != proxies[prop] - return - profile = p - if profile - profile - else - profile = OmegaPac.Profiles.create({ - profileType: 'FixedProfile' - name: '' - }) - for prop in props when details.value.rules[prop] - if prop == 'singleProxy' - profile['fallbackProxy'] = details.value.rules[prop] - else - profile[prop] = details.value.rules[prop] - profile.bypassList = - for own pattern of bypassSet - {conditionType: 'BypassCondition', pattern: pattern} - profile diff --git a/omega-target-chromium-extension/src/module/proxy/proxy_impl.coffee b/omega-target-chromium-extension/src/module/proxy/proxy_impl.coffee index b43ae99..a5ffda3 100644 --- a/omega-target-chromium-extension/src/module/proxy/proxy_impl.coffee +++ b/omega-target-chromium-extension/src/module/proxy/proxy_impl.coffee @@ -8,6 +8,7 @@ class ProxyImpl @isSupported: -> false applyProfile: (profile, meta) -> Promise.reject() watchProxyChange: (callback) -> null + parseExternalProfile: (details, options) -> null _profileNotFound: (name) -> @log.error("Profile #{name} not found! Things may go very, very wrong.") return OmegaPac.Profiles.create({ diff --git a/omega-target-chromium-extension/src/module/proxy/proxy_impl_settings.coffee b/omega-target-chromium-extension/src/module/proxy/proxy_impl_settings.coffee index 8c6c791..47b5a8b 100644 --- a/omega-target-chromium-extension/src/module/proxy/proxy_impl_settings.coffee +++ b/omega-target-chromium-extension/src/module/proxy/proxy_impl_settings.coffee @@ -1,4 +1,5 @@ OmegaTarget = require('omega-target') +OmegaPac = OmegaTarget.OmegaPac Promise = OmegaTarget.Promise chromeApiPromisify = require('../chrome_api').chromeApiPromisify ProxyImpl = require('./proxy_impl') @@ -83,8 +84,115 @@ class SettingsProxyImpl extends ProxyImpl if not @_proxyChangeWatchers? @_proxyChangeWatchers = [] if chrome?.proxy?.settings?.onChange? - chrome.proxy.settings.onChange.addListener @_proxyChangeListener + chrome.proxy.settings.onChange.addListener( + @_proxyChangeListener.bind(this)) @_proxyChangeWatchers.push(callback) return + parseExternalProfile: (details, options) -> + if details.name + return details + switch details.value.mode + when 'system' + OmegaPac.Profiles.byName('system') + when 'direct' + OmegaPac.Profiles.byName('direct') + when 'auto_detect' + OmegaPac.Profiles.create({ + profileType: 'PacProfile' + name: '' + pacUrl: 'http://wpad/wpad.dat' + }) + when 'pac_script' + url = details.value.pacScript.url + if url + profile = null + OmegaPac.Profiles.each options, (key, p) -> + if p.profileType == 'PacProfile' and p.pacUrl == url + profile = p + profile ? OmegaPac.Profiles.create({ + profileType: 'PacProfile' + name: '' + pacUrl: url + }) + else do -> + profile = null + script = details.value.pacScript.data + OmegaPac.Profiles.each options, (key, p) -> + if p.profileType == 'PacProfile' and p.pacScript == script + profile = p + return profile if profile + # Try to parse the prefix used by this class. + script = script.trim() + magic = '/*OmegaProfile*' + if script.substr(0, magic.length) == magic + end = script.indexOf('*/') + if end > 0 + i = magic.length + tokens = script.substring(magic.length, end).split('*') + [profileName, revision] = tokens + try + profileName = JSON.parse(profileName) + catch _ + profileName = null + if profileName and revision + profile = OmegaPac.Profiles.byName(profileName, options) + if OmegaPac.Revision.compare(profile.revision, revision) == 0 + return profile + return OmegaPac.Profiles.create({ + profileType: 'PacProfile' + name: '' + pacScript: script + }) + when 'fixed_servers' + props = ['proxyForHttp', 'proxyForHttps', 'proxyForFtp', + 'fallbackProxy', 'singleProxy'] + proxies = {} + for prop in props + result = OmegaPac.Profiles.pacResult(details.value.rules[prop]) + if prop == 'singleProxy' and details.value.rules[prop]? + proxies['fallbackProxy'] = result + else + proxies[prop] = result + bypassSet = {} + bypassCount = 0 + if details.value.rules.bypassList + for pattern in details.value.rules.bypassList + bypassSet[pattern] = true + bypassCount++ + if bypassSet[''] + for host in OmegaPac.Conditions.localHosts when bypassSet[host] + delete bypassSet[host] + bypassCount-- + profile = null + OmegaPac.Profiles.each options, (key, p) => + return if p.profileType != 'FixedProfile' + return if p.bypassList.length != bypassCount + for condition in p.bypassList + return unless bypassSet[condition.pattern] + rules = @_fixedProfileConfig(p).rules + if rules['singleProxy'] + rules['fallbackProxy'] = rules['singleProxy'] + delete rules['singleProxy'] + return unless rules? + for prop in props when rules[prop] or proxies[prop] + if OmegaPac.Profiles.pacResult(rules[prop]) != proxies[prop] + return + profile = p + if profile + profile + else + profile = OmegaPac.Profiles.create({ + profileType: 'FixedProfile' + name: '' + }) + for prop in props when details.value.rules[prop] + if prop == 'singleProxy' + profile['fallbackProxy'] = details.value.rules[prop] + else + profile[prop] = details.value.rules[prop] + profile.bypassList = + for own pattern of bypassSet + {conditionType: 'BypassCondition', pattern: pattern} + profile module.exports = SettingsProxyImpl diff --git a/omega-web/src/popup.jade b/omega-web/src/popup.jade index aab0aff..302d68a 100644 --- a/omega-web/src/popup.jade +++ b/omega-web/src/popup.jade @@ -36,7 +36,7 @@ html(lang='en' ng-app='omegaPopup' ng-controller='PopupCtrl' ng-csp) span(omega-profile-icon='externalProfile' icon='getIcon(externalProfile, "normal")' options='availableProfiles' disp-name='dispNameFilter') = ' ' span(ng-show='!nameExternal.open') {{'popup_externalProfile' | tr}} - input.form-control(ng-show='!!nameExternal.open' ng-model='externalProfile.name' ng-blur='nameExternalForm.submit()' + input.form-control(ng-show='!!nameExternal.open' ng-model='externalProfile.name' ng-blur='nameExternalForm.$valid && saveExternal()' placeholder='{{"popup_externalProfileName" | tr}}' ui-validate='validateProfileName' autofocus) li.request-info(ng-show='!!requestInfoProvided' class='bg-warning') a(href='#' role='button' ng-click='showRequestInfo = true' data-shortcut='requestInfo')