@@ -12,8 +12,7 @@ import GCDWebServer
12
12
class HTTPUserProxy {
13
13
static let shard = HTTPUserProxy ( )
14
14
15
- let adapter = APIAdapter ( )
16
- let v = Validator ( )
15
+ let app = AppFacade ( )
17
16
18
17
let server = GCDWebServer ( )
19
18
let api_port : UInt = 9528
@@ -56,20 +55,20 @@ class HTTPUserProxy{
56
55
57
56
func addHandler_getStatus( ) {
58
57
server. addHandler ( forMethod: " GET " , path: " /status " , request: GCDWebServerRequest . self, processBlock: { request in
59
- return GCDWebServerDataResponse ( jsonObject: [ " Enable " : self . adapter . getStatus ( ) ] , contentType: " json " )
58
+ return GCDWebServerDataResponse ( jsonObject: [ " Enable " : self . app . getStatus ( ) ] , contentType: " json " )
60
59
} )
61
60
}
62
61
63
62
func addHandler_setStatus( ) {
64
63
server. addHandler ( forMethod: " PUT " , path: " /status " , request: GCDWebServerURLEncodedFormRequest . self, processBlock: { request in
65
64
if let targetStatus_str = ( request as? GCDWebServerURLEncodedFormRequest ) ? . arguments [ " Enable " ] as? String {
66
65
if let targetStatus = Bool ( targetStatus_str) {
67
- self . adapter . setStatus ( status: targetStatus)
66
+ self . app . setStatus ( status: targetStatus)
68
67
return GCDWebServerResponse ( )
69
68
}
70
69
}
71
70
else {
72
- self . adapter . toggleStatus ( )
71
+ self . app . toggleStatus ( )
73
72
return GCDWebServerResponse ( )
74
73
}
75
74
return GCDWebServerResponse ( statusCode: 400 )
@@ -78,14 +77,14 @@ class HTTPUserProxy{
78
77
79
78
func addHandler_getServerList( ) {
80
79
server. addHandler ( forMethod: " GET " , path: " /servers " , request: GCDWebServerRequest . self, processBlock: { request in
81
- return GCDWebServerDataResponse ( jsonObject: self . adapter . getServerList ( ) , contentType: " json " )
80
+ return GCDWebServerDataResponse ( jsonObject: self . app . getServerList ( ) , contentType: " json " )
82
81
} )
83
82
}
84
83
85
84
func addHandler_getCurrentServer( ) {
86
85
server. addHandler ( forMethod: " GET " , path: " /current " , request: GCDWebServerRequest . self, processBlock: { request in
87
- if let activeId = self . adapter . getCurrentServerId ( ) {
88
- return GCDWebServerDataResponse ( jsonObject: self . adapter . getServer ( uuid: activeId) !, contentType: " json " )
86
+ if let activeId = self . app . getCurrentServerId ( ) {
87
+ return GCDWebServerDataResponse ( jsonObject: self . app . getServer ( uuid: activeId) !, contentType: " json " )
89
88
}
90
89
else {
91
90
return GCDWebServerResponse ( statusCode: 404 ) ;
@@ -97,8 +96,8 @@ class HTTPUserProxy{
97
96
server. addHandler ( forMethod: " PUT " , path: " /current " , request: GCDWebServerURLEncodedFormRequest . self, processBlock: { request in
98
97
99
98
if let targetId = ( request as? GCDWebServerURLEncodedFormRequest ) ? . arguments [ " Id " ] as? String {
100
- if self . adapter . getServer ( uuid: targetId) != nil {
101
- self . adapter . setCurrentServer ( uuid: targetId) ;
99
+ if self . app . getServer ( uuid: targetId) != nil {
100
+ self . app . setCurrentServer ( uuid: targetId) ;
102
101
return GCDWebServerResponse ( )
103
102
}
104
103
}
@@ -111,8 +110,8 @@ class HTTPUserProxy{
111
110
if var server = ( ( request as? GCDWebServerURLEncodedFormRequest ) ? . arguments) as? [ String : Any ] {
112
111
if ( server [ " ServerPort " ] != nil ) {
113
112
server [ " ServerPort " ] = UInt16 ( server [ " ServerPort " ] as! String )
114
- if ( Validator . integrity ( server ) && Validator . existAttributes ( server) ) { // validate
115
- self . adapter . addServer ( server: server)
113
+ if ( Validator . profile ( server) ) { // validate
114
+ self . app . addServer ( server: server)
116
115
return GCDWebServerResponse ( ) ;
117
116
}
118
117
}
@@ -128,10 +127,10 @@ class HTTPUserProxy{
128
127
if ( server [ " ServerPort " ] != nil ) {
129
128
server [ " ServerPort " ] = UInt16 ( server [ " ServerPort " ] as! String )
130
129
}
131
- if ( self . adapter . getServer ( uuid: id) != nil ) {
130
+ if ( self . app . getServer ( uuid: id) != nil ) {
132
131
if ( Validator . existAttributes ( server) ) {
133
- if ( self . adapter . getCurrentServerId ( ) != id) {
134
- self . adapter . modifyServer ( uuid: id, server: server)
132
+ if ( self . app . getCurrentServerId ( ) != id) {
133
+ self . app . modifyServer ( uuid: id, server: server)
135
134
return GCDWebServerResponse ( )
136
135
}
137
136
else {
@@ -150,9 +149,9 @@ class HTTPUserProxy{
150
149
server. addHandler ( forMethod: " DELETE " , pathRegex: " /servers/ " + self . UUID_REGEX, request: GCDWebServerRequest . self
151
150
, processBlock: { request in
152
151
let id = String ( request. path. dropFirst ( " /servers/ " . count) )
153
- if ( ( self . adapter . getServer ( uuid: id) ) != nil ) {
154
- if ( self . adapter . getCurrentServerId ( ) != id) {
155
- self . adapter . deleteServer ( uuid: id)
152
+ if ( ( self . app . getServer ( uuid: id) ) != nil ) {
153
+ if ( self . app . getCurrentServerId ( ) != id) {
154
+ self . app . deleteServer ( uuid: id)
156
155
return GCDWebServerResponse ( )
157
156
} else {
158
157
return GCDWebServerResponse ( statusCode: 400 )
@@ -166,15 +165,15 @@ class HTTPUserProxy{
166
165
167
166
func addHandler_getMode( ) {
168
167
server. addHandler ( forMethod: " GET " , path: " /mode " , request: GCDWebServerRequest . self, processBlock: { request in
169
- return GCDWebServerDataResponse ( jsonObject: [ " Mode " : self . adapter . getMode ( ) . rawValue] , contentType: " json " )
168
+ return GCDWebServerDataResponse ( jsonObject: [ " Mode " : self . app . getMode ( ) . rawValue] , contentType: " json " )
170
169
} )
171
170
}
172
171
173
172
func addHandler_setMode( ) {
174
173
server. addHandler ( forMethod: " PUT " , path: " /mode " , request: GCDWebServerURLEncodedFormRequest . self, processBlock: { request in
175
174
if let mode_str = ( request as? GCDWebServerURLEncodedFormRequest ) ? . arguments [ " Mode " ] as? String {
176
- if let mode = APIAdapter . Mode ( rawValue: mode_str) {
177
- self . adapter . setMode ( mode: mode) ;
175
+ if let mode = ProxyType ( rawValue: mode_str) {
176
+ self . app . setMode ( mode: mode) ;
178
177
179
178
return GCDWebServerResponse ( )
180
179
}
@@ -184,12 +183,10 @@ class HTTPUserProxy{
184
183
}
185
184
}
186
185
187
- class APIAdapter {
188
- enum Mode : String { case auto= " auto " , global= " global " , manual= " manual " } ;
189
-
190
- let SerMgr = ServerProfileManager . instance
191
- let defaults = UserDefaults . standard
192
- let appdeleget = NSApplication . shared. delegate as! AppDelegate
186
+ class AppFacade {
187
+ private let SerMgr = ServerProfileManager . instance
188
+ private let defaults = UserDefaults . standard
189
+ private let appdeleget = NSApplication . shared. delegate as! AppDelegate
193
190
194
191
func getStatus( ) -> Bool {
195
192
return self . defaults. bool ( forKey: " ShadowsocksOn " ) ;
@@ -281,37 +278,40 @@ class APIAdapter {
281
278
self . appdeleget. updateServersMenu ( )
282
279
}
283
280
284
- func getMode( ) -> Mode {
281
+ func getMode( ) -> ProxyType {
285
282
let mode_str = self . defaults. string ( forKey: " ShadowsocksRunningMode " ) ;
286
283
switch mode_str {
287
- case " auto " : return . auto
284
+ case " auto " : return . pac
288
285
case " global " : return . global;
289
286
case " manual " : return . manual
290
287
default : fatalError ( )
291
288
}
292
289
}
293
290
294
- func setMode( mode: Mode ) {
291
+ func setMode( mode: ProxyType ) {
295
292
let defaults = UserDefaults . standard
296
293
297
294
switch mode{
298
- case . auto : defaults. setValue ( " auto " , forKey: " ShadowsocksRunningMode " )
295
+ case . pac : defaults. setValue ( " auto " , forKey: " ShadowsocksRunningMode " )
299
296
case . global: defaults. setValue ( " global " , forKey: " ShadowsocksRunningMode " )
300
297
case . manual: defaults. setValue ( " manual " , forKey: " ShadowsocksRunningMode " )
301
298
}
302
299
300
+ Globals . proxyType = mode
301
+
303
302
self . appdeleget. updateRunningModeMenu ( )
304
303
self . appdeleget. applyConfig ( )
305
304
}
306
305
}
307
306
308
307
class Validator {
309
- static func integrity( _ data: Dictionary < String , Any > ) -> Bool {
308
+ // Check if a ServerProfile can be constructed from input dictionary
309
+ static func profile( _ data: Dictionary < String , Any > ) -> Bool {
310
310
if ( data [ " ServerHost " ] == nil || data [ " ServerPort " ] as? NSNumber == nil
311
311
|| data [ " Method " ] == nil || data [ " Password " ] == nil ) {
312
312
return false ;
313
313
}
314
- return true ;
314
+ return existAttributes ( data ) ;
315
315
}
316
316
317
317
static func existAttributes( _ server: Dictionary < String , Any > ) -> Bool {
@@ -352,7 +352,6 @@ class Validator {
352
352
353
353
static func method( _ str: String ) -> Bool {
354
354
// Copy from PreferencesWindowController.swift
355
- // Better to make valid methods enumeration type.
356
355
return [
357
356
" aes-128-gcm " ,
358
357
" aes-192-gcm " ,
@@ -373,11 +372,11 @@ class Validator {
373
372
" chacha20 " ,
374
373
" chacha20-ietf " ,
375
374
" rc4-md5 " ,
376
- ] . contains ( str) ;
375
+ ] . contains ( str)
377
376
}
378
377
379
378
static func password( _ str: String ) -> Bool {
380
- return true ;
379
+ return !str . isEmpty ;
381
380
}
382
381
383
382
static func remark( _ str: String ) -> Bool {
0 commit comments