ZeroOmega/omega-target-chromium-extension/src/upgrade.coffee

151 lines
5.3 KiB
CoffeeScript
Raw Normal View History

2014-09-20 11:49:04 -04:00
OmegaTarget = require('omega-target')
OmegaPac = OmegaTarget.OmegaPac
2014-10-04 03:55:03 -04:00
module.exports = (oldOptions, i18n) ->
2014-09-20 11:49:04 -04:00
config = try JSON.parse(oldOptions['config'])
if config
options = changes ? {}
options['schemaVersion'] = 2
boolItems =
'-confirmDeletion': 'confirmDeletion'
'-refreshOnProfileChange': 'refreshTab'
'-enableQuickSwitch': 'quickSwitch'
'-revertProxyChanges': 'preventProxyChanges'
for own key, oldKey of boolItems
options[key] = !!config[oldKey]
options['-downloadInterval'] =
parseInt(config['ruleListReload']) || 15
2014-10-04 03:55:03 -04:00
auto = OmegaPac.Profiles.create(
2014-09-20 11:49:04 -04:00
profileType: 'SwitchProfile'
2014-10-04 03:55:03 -04:00
name: i18n.upgrade_profile_auto
2014-09-20 11:49:04 -04:00
color: '#55bb55'
2014-10-04 03:55:03 -04:00
defaultProfileName: 'direct' # We will set this to rulelist.name soon.
2014-09-20 11:49:04 -04:00
)
2014-10-04 03:55:03 -04:00
OmegaPac.Profiles.updateRevision(auto)
options[OmegaPac.Profiles.nameAsKey(auto.name)] = auto
2014-09-20 11:49:04 -04:00
2014-10-04 03:55:03 -04:00
rulelist = OmegaPac.Profiles.create(
2014-09-20 11:49:04 -04:00
profileType: 'RuleListProfile'
2014-10-04 03:55:03 -04:00
name: '__ruleListOf_' + auto.name
2014-09-20 11:49:04 -04:00
color: '#dd6633'
format:
if config['ruleListAutoProxy'] then 'AutoProxy' else 'Switchy'
defaultProfileName: 'direct'
sourceUrl: config['ruleListUrl'] || ''
)
2014-10-04 03:55:03 -04:00
options[OmegaPac.Profiles.nameAsKey(rulelist.name)] = rulelist
2014-09-20 11:49:04 -04:00
2014-10-04 03:55:03 -04:00
auto.defaultProfileName = rulelist.name
nameMap = {'auto': auto.name, 'direct': 'direct'}
2014-09-20 11:49:04 -04:00
oldProfiles = (try JSON.parse(oldOptions['profiles'])) || {}
colorTranslations =
'blue': '#99ccee'
'green': '#99dd99'
'red': '#ffaa88'
'yellow': '#ffee99'
'purple': '#d497ee'
'': '#99ccee'
for own _, oldProfile of oldProfiles
profile = null
switch oldProfile['proxyMode']
when 'auto'
profile = OmegaPac.Profiles.create(
profileType: 'PacProfile'
)
url = oldProfile['proxyConfigUrl']
if url.substr(0, 5) == 'data:'
text = url.substr(url.indexOf(',') + 1)
Buffer = require('buffer').Buffer
text = new Buffer(text, 'base64').toString('utf8')
profile.pacScript = text
else
profile.pacUrl = url
when 'manual'
profile = OmegaPac.Profiles.create(
profileType: 'FixedProfile'
)
if !!oldProfile['useSameProxy']
profile.fallbackProxy = OmegaPac.Profiles.parseHostPort(
oldProfile['proxyHttp'], 'http')
else if oldProfile['proxySocks']
protocol =
if oldProfile['socksVersion'] == 5
'socks5'
else
'socks4'
profile.fallbackProxy = OmegaPac.Profiles.parseHostPort(
oldProfile['proxySocks'],
protocol
)
else
profile.proxyForHttp = OmegaPac.Profiles.parseHostPort(
oldProfile['proxyHttp'], 'http')
profile.proxyForHttps = OmegaPac.Profiles.parseHostPort(
oldProfile['proxyHttps'], 'http')
profile.proxyForFtp = OmegaPac.Profiles.parseHostPort(
oldProfile['proxyFtp'], 'http')
if oldProfile['proxyExceptions']?
haslocalPattern = false
profile.bypassList = []
oldProfile['proxyExceptions'].split(';').forEach (line) ->
line = line.trim()
return unless line
haslocalPattern = true if line == '<local>'
profile.bypassList.push(
conditionType: 'BypassCondition'
pattern: line
)
if haslocalPattern
profile.bypassList = profile.bypassList.filter (cond) ->
OmegaPac.Conditions.localHosts.indexOf(cond.pattern) < 0
if profile
color = oldProfile['color']
profile.color = colorTranslations[color] ? colorTranslations['']
name = oldProfile['name'] ? oldProfile['id']
if name[0] == '_'
name = 'p' + name
2014-09-20 11:49:04 -04:00
profile.name = name
num = 1
while OmegaPac.Profiles.byName(profile.name, options)
profile.name = name + num
num++
nameMap[oldProfile['id']] = profile.name
OmegaPac.Profiles.updateRevision(profile)
options[OmegaPac.Profiles.nameAsKey(profile.name)] = profile
startupId = config['startupProfileId']
options['-startupProfileName'] = nameMap[startupId] || ''
quickSwitch = try JSON.parse(oldOptions['quickSwitchProfiles'])
options['-quickSwitchProfiles'] = if not quickSwitch? then [] else
quickSwitch.map (p) -> nameMap[p]
if config['ruleListProfileId']
2014-10-04 03:55:03 -04:00
rulelist.matchProfileName =
2014-09-20 11:49:04 -04:00
nameMap[config['ruleListProfileId']] || 'direct'
defaultRule = try JSON.parse(oldOptions['defaultRule'])
if defaultRule
rulelist.defaultProfileName =
2014-09-20 11:49:04 -04:00
nameMap[defaultRule.profileId] || 'direct'
if not config.ruleListEnabled
auto.defaultProfileName = rulelist.defaultProfileName
2014-09-20 11:49:04 -04:00
rules = try JSON.parse(oldOptions['rules'])
if rules
2014-10-04 03:55:03 -04:00
auto.rules = for own _, rule of rules
2014-09-20 11:49:04 -04:00
profileName: nameMap[rule['profileId']] || 'direct'
condition:
conditionType:
if rule['patternType'] == 'wildcard'
# TODO(catus): Recognize HostWildcardCondition.
'UrlWildcardCondition'
else
'UrlRegexCondition'
pattern: rule['urlPattern']
return options
return