Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

Commit

Permalink
Correctly handle empty line in list files.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuhaow committed Oct 7, 2016
1 parent 8a655cd commit f2deaf8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
I will do my best to guarantee that this project adheres to [Semantic Versioning](http://semver.org/) after 1.0.0, but please do read change log before updating.

## [0.7.1]
### Fixed
- Correctly handle empty line in list files.

## [0.7.0]
### Added
- `DomainRuleList` can match domain based on prefix, suffix and keyword.
Expand Down
62 changes: 31 additions & 31 deletions src/Config/RuleParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ struct RuleParser {
guard let ruleConfigs = config.array else {
throw ConfigurationParserError.NoRuleDefined
}

var rules: [Rule] = []

for ruleConfig in ruleConfigs {
rules.append(try parseRule(ruleConfig, adapterFactoryManager: adapterFactoryManager))
}
return RuleManager(fromRules: rules, appendDirect: true)
}

static func parseRule(config: Yaml, adapterFactoryManager: AdapterFactoryManager) throws -> Rule {
guard let type = config["type"].string?.lowercaseString else {
throw ConfigurationParserError.RuleTypeMissing
}

switch type {
case "country":
return try parseCountryRule(config, adapterFactoryManager: adapterFactoryManager)
Expand All @@ -35,107 +35,107 @@ struct RuleParser {
throw ConfigurationParserError.UnknownRuleType
}
}

static func parseCountryRule(config: Yaml, adapterFactoryManager: AdapterFactoryManager) throws -> CountryRule {
guard let country = config["country"].string else {
throw ConfigurationParserError.RuleParsingError(errorInfo: "Country code (country) is required for country rule.")
}

guard let adapter_id = config["adapter"].stringOrIntString else {
throw ConfigurationParserError.RuleParsingError(errorInfo: "An adapter id (adapter_id) is required.")
}

guard let adapter = adapterFactoryManager[adapter_id] else {
throw ConfigurationParserError.RuleParsingError(errorInfo: "Unknown adapter id.")
}

guard let match = config["match"].bool else {
throw ConfigurationParserError.RuleParsingError(errorInfo: "You have to specify whether to apply this rule to ip match the given country or not with \"match\".")
}

return CountryRule(countryCode: country, match: match, adapterFactory: adapter)
}

static func parseAllRule(config: Yaml, adapterFactoryManager: AdapterFactoryManager) throws -> AllRule {
guard let adapter_id = config["adapter"].stringOrIntString else {
throw ConfigurationParserError.RuleParsingError(errorInfo: "An adapter id (adapter_id) is required.")
}

guard let adapter = adapterFactoryManager[adapter_id] else {
throw ConfigurationParserError.RuleParsingError(errorInfo: "Unknown adapter id.")
}

return AllRule(adapterFactory: adapter)
}

static func parseDomainListRule(config: Yaml, adapterFactoryManager: AdapterFactoryManager) throws -> DomainListRule {
guard let adapter_id = config["adapter"].stringOrIntString else {
throw ConfigurationParserError.RuleParsingError(errorInfo: "An adapter id (adapter_id) is required.")
}

guard let adapter = adapterFactoryManager[adapter_id] else {
throw ConfigurationParserError.RuleParsingError(errorInfo: "Unknown adapter id.")
}

guard var filepath = config["file"].stringOrIntString else {
throw ConfigurationParserError.RuleParsingError(errorInfo: "Must provide a file (file) containing domain rules in list.")
}

filepath = (filepath as NSString).stringByExpandingTildeInPath

do {
let content = try String(contentsOfFile: filepath)
let regexs = content.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())
var criteria: [DomainListRule.MatchCriterion] = []
for regex in regexs {
let re = try NSRegularExpression(pattern: regex, options: .CaseInsensitive)
criteria.append(DomainListRule.MatchCriterion.Regex(re))
if regex != "" {
let re = try NSRegularExpression(pattern: regex, options: .CaseInsensitive)
criteria.append(DomainListRule.MatchCriterion.Regex(re))
}
}

return DomainListRule(adapterFactory: adapter, criteria: criteria)
} catch let error {
throw ConfigurationParserError.RuleParsingError(errorInfo: "Encounter error when parse rule list file. \(error)")
}
}

static func parseIPRangeListRule(config: Yaml, adapterFactoryManager: AdapterFactoryManager) throws -> IPRangeListRule {
guard let adapter_id = config["adapter"].stringOrIntString else {
throw ConfigurationParserError.RuleParsingError(errorInfo: "An adapter id (adapter_id) is required.")
}

guard let adapter = adapterFactoryManager[adapter_id] else {
throw ConfigurationParserError.RuleParsingError(errorInfo: "Unknown adapter id.")
}

guard var filepath = config["file"].stringOrIntString else {
throw ConfigurationParserError.RuleParsingError(errorInfo: "Must provide a file (file) containing IP range rules in list.")
}

filepath = (filepath as NSString).stringByExpandingTildeInPath

do {
let content = try String(contentsOfFile: filepath)
var ranges = content.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())
if let range = ranges.last {
if range == "" {
ranges.removeLast()
}
ranges = ranges.filter {
$0 != ""
}
return try IPRangeListRule(adapterFactory: adapter, ranges: ranges)
} catch let error {
throw ConfigurationParserError.RuleParsingError(errorInfo: "Encounter error when parse IP range rule list file. \(error)")
}
}

static func parseDNSFailRule(config: Yaml, adapterFactoryManager: AdapterFactoryManager) throws -> DNSFailRule {
guard let adapter_id = config["adapter"].stringOrIntString else {
throw ConfigurationParserError.RuleParsingError(errorInfo: "An adapter id (adapter_id) is required.")
}

guard let adapter = adapterFactoryManager[adapter_id] else {
throw ConfigurationParserError.RuleParsingError(errorInfo: "Unknown adapter id.")
}

return DNSFailRule(adapterFactory: adapter)
}
}

0 comments on commit f2deaf8

Please sign in to comment.