forked from zhiphe/Potatso-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRule.swift
185 lines (151 loc) · 4.21 KB
/
Rule.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// Rule.swift
// Potatso
//
// Created by LEI on 4/6/16.
// Copyright © 2016 TouchingApp. All rights reserved.
//
import RealmSwift
import PotatsoBase
private let ruleValueKey = "value";
private let ruleActionKey = "action";
public enum RuleType: String {
case URLMatch = "URL-MATCH"
case URL = "URL"
case Domain = "DOMAIN"
case DomainMatch = "DOMAIN-MATCH"
case DomainSuffix = "DOMAIN-SUFFIX"
case GeoIP = "GEOIP"
case IPCIDR = "IP-CIDR"
}
extension RuleType {
public static func fromInt(intValue: Int) -> RuleType? {
switch intValue {
case 1:
return .Domain
case 2:
return .DomainSuffix
case 3:
return .DomainMatch
case 4:
return .URL
case 5:
return .URLMatch
case 6:
return .GeoIP
case 7:
return .IPCIDR
default:
return nil
}
}
}
extension RuleType: CustomStringConvertible {
public var description: String {
return rawValue
}
}
public enum RuleAction: String {
case Direct = "DIRECT"
case Reject = "REJECT"
case Proxy = "PROXY"
}
extension RuleAction {
public static func fromInt(intValue: Int) -> RuleAction? {
switch intValue {
case 1:
return .Direct
case 2:
return .Reject
case 3:
return .Proxy
default:
return nil
}
}
}
extension RuleAction: CustomStringConvertible {
public var description: String {
return rawValue
}
}
public enum RuleError: ErrorType {
case InvalidRule(String)
}
extension RuleError: CustomStringConvertible {
public var description: String {
switch self {
case .InvalidRule(let rule):
return "Invalid rule - \(rule)"
}
}
}
public final class Rule: BaseModel {
public dynamic var typeRaw = ""
public dynamic var content = ""
public dynamic var order = 0
public let rulesets = LinkingObjects(fromType: RuleSet.self, property: "rules")
}
extension Rule {
public var type : RuleType {
get {
return RuleType(rawValue: typeRaw) ?? .URL
}
set(v) {
typeRaw = v.rawValue
}
}
public var action : RuleAction {
let json = content.jsonDictionary()
if let raw = json?[ruleActionKey] as? String {
return RuleAction(rawValue: raw) ?? .Proxy
}
return .Proxy
}
public var value : String {
let json = content.jsonDictionary()
return json?[ruleValueKey] as? String ?? ""
}
public var pattern: String {
switch type {
case .DomainMatch:
return "*\(value)*"
case .DomainSuffix:
return ".\(value)/"
default:
return value
}
}
}
extension Rule {
public convenience init(str: String) throws {
self.init()
var ruleStr = str.stringByReplacingOccurrencesOfString("\t", withString: "")
ruleStr = ruleStr.stringByReplacingOccurrencesOfString(" ", withString: "")
let parts = ruleStr.componentsSeparatedByString(",")
guard parts.count >= 3 else {
throw RuleError.InvalidRule(str)
}
let actionStr = parts[2].uppercaseString
let typeStr = parts[0].uppercaseString
let value = parts[1]
guard let type = RuleType(rawValue: typeStr), action = RuleAction(rawValue: actionStr) where value.characters.count > 0 else {
throw RuleError.InvalidRule(str)
}
update(type, action: action, value: value)
}
public convenience init(type: RuleType, action: RuleAction, value: String) {
self.init()
update(type, action: action, value: value)
}
public func update(type: RuleType, action: RuleAction, value: String) {
self.type = type
self.content = [ruleActionKey: action.rawValue, ruleValueKey: value].jsonString() ?? ""
}
public override var description: String {
return "\(type), \(value), \(action)"
}
}
public func ==(lhs: Rule, rhs: Rule) -> Bool {
return lhs.uuid == rhs.uuid
}