forked from zhiphe/Potatso-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProxy.swift
215 lines (188 loc) · 6.39 KB
/
Proxy.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//
// Proxy.swift
// Potatso
//
// Created by LEI on 4/6/16.
// Copyright © 2016 TouchingApp. All rights reserved.
//
import RealmSwift
public enum ProxyType: String {
case Shadowsocks = "SHADOWSOCKS"
case Https = "HTTPS"
case Socks5 = "SOCKS5"
case None = "NONE"
}
extension ProxyType: CustomStringConvertible {
public var description: String {
return rawValue
}
}
public enum ProxyError: ErrorType {
case InvalidType
case InvalidName
case InvalidHost
case InvalidPort
case InvalidAuthScheme
case NameAlreadyExists
case InvalidUri
case InvalidPassword
}
extension ProxyError: CustomStringConvertible {
public var description: String {
switch self {
case .InvalidType:
return "Invalid type"
case .InvalidName:
return "Invalid name"
case .InvalidHost:
return "Invalid host"
case .InvalidAuthScheme:
return "Invalid encryption"
case .InvalidUri:
return "Invalid uri"
case .NameAlreadyExists:
return "Name already exists"
case .InvalidPassword:
return "Invalid password"
case .InvalidPort:
return "Invalid port"
}
}
}
public class Proxy: BaseModel {
public dynamic var typeRaw = ProxyType.Shadowsocks.rawValue
public dynamic var name = ""
public dynamic var host = ""
public dynamic var port = 0
public dynamic var authscheme: String?
public dynamic var user: String?
public dynamic var password: String?
public dynamic var ota: Bool = false
public func validate(inRealm realm: Realm) throws {
guard let _ = ProxyType(rawValue: typeRaw)else {
throw ProxyError.InvalidType
}
guard name.characters.count > 0 else{
throw ProxyError.InvalidName
}
guard realm.objects(Proxy).filter("name = '\(name)'").first == nil else {
throw ProxyError.NameAlreadyExists
}
guard host.characters.count > 0 else {
throw ProxyError.InvalidHost
}
guard port > 0 && port <= Int(UINT16_MAX) else {
throw ProxyError.InvalidPort
}
switch type {
case .Shadowsocks:
guard let _ = authscheme else {
throw ProxyError.InvalidAuthScheme
}
default:
break
}
}
}
extension Proxy {
public var type: ProxyType {
get {
return ProxyType(rawValue: typeRaw) ?? .Shadowsocks
}
set(v) {
typeRaw = v.rawValue
}
}
public override static func indexedProperties() -> [String] {
return ["name"]
}
}
extension Proxy {
public convenience init(dictionary: [String: AnyObject], inRealm realm: Realm) throws {
self.init()
if let uriString = dictionary["uri"] as? String {
if uriString.lowercaseString.hasPrefix("ss://") {
// Shadowsocks
let proxyString = uriString.substringFromIndex(uriString.startIndex.advancedBy(5))
guard let pc1 = proxyString.rangeOfString(":")?.startIndex, pc2 = proxyString.rangeOfString(":", options: .BackwardsSearch)?.startIndex, pcm = proxyString.rangeOfString("@", options: .BackwardsSearch)?.startIndex else {
throw ProxyError.InvalidUri
}
if !(pc1 < pcm && pcm < pc2) {
throw ProxyError.InvalidUri
}
let fullAuthscheme = proxyString.substringWithRange(proxyString.startIndex..<pc1)
if let pOTA = proxyString.rangeOfString("-auth", options: .BackwardsSearch)?.startIndex {
self.authscheme = fullAuthscheme.substringToIndex(pOTA)
self.ota = true
}else {
self.authscheme = fullAuthscheme
}
self.password = proxyString.substringWithRange(pc1.successor()..<pcm)
self.host = proxyString.substringWithRange(pcm.successor()..<pc2)
guard let p = Int(proxyString.substringWithRange(pc2.successor()..<proxyString.endIndex)) else {
throw ProxyError.InvalidPort
}
self.port = p
self.type = .Shadowsocks
}else {
// Not supported yet
throw ProxyError.InvalidUri
}
guard let name = dictionary["name"] as? String else{
throw ProxyError.InvalidName
}
self.name = name
}else {
guard let name = dictionary["name"] as? String else{
throw ProxyError.InvalidName
}
guard let host = dictionary["host"] as? String else{
throw ProxyError.InvalidHost
}
guard let typeRaw = (dictionary["type"] as? String)?.uppercaseString, type = ProxyType(rawValue: typeRaw) else{
throw ProxyError.InvalidType
}
guard let portStr = (dictionary["port"] as? String), port = Int(portStr) else{
throw ProxyError.InvalidPort
}
guard let encryption = dictionary["encryption"] as? String else{
throw ProxyError.InvalidAuthScheme
}
guard let password = dictionary["password"] as? String else{
throw ProxyError.InvalidPassword
}
self.host = host
self.port = port
self.password = password
self.authscheme = encryption
self.name = name
self.type = type
}
if realm.objects(RuleSet).filter("name = '\(name)'").first != nil {
self.name = Proxy.dateFormatter.stringFromDate(NSDate())
}
try validate(inRealm: realm)
}
}
extension Proxy {
public var uri: String {
switch type {
case .Shadowsocks:
if let authscheme = authscheme, password = password {
return "ss://\(authscheme):\(password)@\(host):\(port)"
}
default:
break
}
return ""
}
}
extension Proxy: CustomStringConvertible {
public override var description: String {
return name
}
}
extension Proxy: Equatable {}
public func ==(lhs: Proxy, rhs: Proxy) -> Bool {
return lhs.uuid == rhs.uuid
}