diff --git a/ShadowsocksX-NG/HTTPUserProxy.swift b/ShadowsocksX-NG/HTTPUserProxy.swift index cfc4c15b..67330baf 100644 --- a/ShadowsocksX-NG/HTTPUserProxy.swift +++ b/ShadowsocksX-NG/HTTPUserProxy.swift @@ -12,7 +12,7 @@ import GCDWebServer class HTTPUserProxy{ static let shard = HTTPUserProxy() - let adapter = APIAdapter() + let app = AppFacade() let server = GCDWebServer() let api_port:UInt = 9528 @@ -55,7 +55,7 @@ class HTTPUserProxy{ func addHandler_getStatus() { server.addHandler(forMethod: "GET", path: "/status", request: GCDWebServerRequest.self, processBlock: {request in - return GCDWebServerDataResponse(jsonObject: ["Enable":self.adapter.getStatus()], contentType: "json") + return GCDWebServerDataResponse(jsonObject: ["Enable":self.app.getStatus()], contentType: "json") }) } @@ -63,12 +63,12 @@ class HTTPUserProxy{ server.addHandler(forMethod: "PUT", path: "/status", request: GCDWebServerURLEncodedFormRequest.self, processBlock: {request in if let targetStatus_str = (request as? GCDWebServerURLEncodedFormRequest)?.arguments["Enable"] as? String{ if let targetStatus = Bool(targetStatus_str) { - self.adapter.setStatus(status: targetStatus) + self.app.setStatus(status: targetStatus) return GCDWebServerResponse() } } else { - self.adapter.toggleStatus() + self.app.toggleStatus() return GCDWebServerResponse() } return GCDWebServerResponse(statusCode: 400) @@ -77,14 +77,14 @@ class HTTPUserProxy{ func addHandler_getServerList() { server.addHandler(forMethod: "GET", path: "/servers", request: GCDWebServerRequest.self, processBlock: {request in - return GCDWebServerDataResponse(jsonObject: self.adapter.getServerList(), contentType: "json") + return GCDWebServerDataResponse(jsonObject: self.app.getServerList(), contentType: "json") }) } func addHandler_getCurrentServer() { server.addHandler(forMethod: "GET", path: "/current", request: GCDWebServerRequest.self, processBlock: {request in - if let activeId = self.adapter.getCurrentServerId() { - return GCDWebServerDataResponse(jsonObject: self.adapter.getServer(uuid: activeId)!, contentType: "json") + if let activeId = self.app.getCurrentServerId() { + return GCDWebServerDataResponse(jsonObject: self.app.getServer(uuid: activeId)!, contentType: "json") } else { return GCDWebServerResponse(statusCode: 404); @@ -96,8 +96,8 @@ class HTTPUserProxy{ server.addHandler(forMethod: "PUT", path: "/current", request: GCDWebServerURLEncodedFormRequest.self, processBlock: {request in if let targetId = (request as? GCDWebServerURLEncodedFormRequest)?.arguments["Id"] as? String{ - if self.adapter.getServer(uuid: targetId) != nil { - self.adapter.setCurrentServer(uuid: targetId); + if self.app.getServer(uuid: targetId) != nil { + self.app.setCurrentServer(uuid: targetId); return GCDWebServerResponse() } } @@ -111,7 +111,7 @@ class HTTPUserProxy{ if (server["ServerPort"] != nil) { server["ServerPort"] = UInt16(server["ServerPort"] as! String) if (Validator.profile(server)) { // validate - self.adapter.addServer(server: server) + self.app.addServer(server: server) return GCDWebServerResponse(); } } @@ -127,10 +127,10 @@ class HTTPUserProxy{ if (server["ServerPort"] != nil) { server["ServerPort"] = UInt16(server["ServerPort"] as! String) } - if (self.adapter.getServer(uuid: id) != nil) { + if (self.app.getServer(uuid: id) != nil) { if (Validator.existAttributes(server)) { - if (self.adapter.getCurrentServerId() != id) { - self.adapter.modifyServer(uuid: id, server: server) + if (self.app.getCurrentServerId() != id) { + self.app.modifyServer(uuid: id, server: server) return GCDWebServerResponse() } else { @@ -149,9 +149,9 @@ class HTTPUserProxy{ server.addHandler(forMethod: "DELETE", pathRegex: "/servers/"+self.UUID_REGEX, request: GCDWebServerRequest.self , processBlock: {request in let id = String(request.path.dropFirst("/servers/".count)) - if((self.adapter.getServer(uuid: id)) != nil) { - if (self.adapter.getCurrentServerId() != id) { - self.adapter.deleteServer(uuid: id) + if((self.app.getServer(uuid: id)) != nil) { + if (self.app.getCurrentServerId() != id) { + self.app.deleteServer(uuid: id) return GCDWebServerResponse() } else { return GCDWebServerResponse(statusCode: 400) @@ -165,7 +165,7 @@ class HTTPUserProxy{ func addHandler_getMode() { server.addHandler(forMethod: "GET", path: "/mode", request: GCDWebServerRequest.self, processBlock: {request in - return GCDWebServerDataResponse(jsonObject: ["Mode":self.adapter.getMode().rawValue], contentType: "json") + return GCDWebServerDataResponse(jsonObject: ["Mode":self.app.getMode().rawValue], contentType: "json") }) } @@ -173,7 +173,7 @@ class HTTPUserProxy{ server.addHandler(forMethod: "PUT", path: "/mode", request: GCDWebServerURLEncodedFormRequest.self, processBlock: {request in if let mode_str = (request as? GCDWebServerURLEncodedFormRequest)?.arguments["Mode"] as? String{ if let mode = ProxyType(rawValue: mode_str) { - self.adapter.setMode(mode: mode); + self.app.setMode(mode: mode); return GCDWebServerResponse() } @@ -183,10 +183,10 @@ class HTTPUserProxy{ } } -class APIAdapter { - let SerMgr = ServerProfileManager.instance - let defaults = UserDefaults.standard - let appdeleget = NSApplication.shared.delegate as! AppDelegate +class AppFacade { + private let SerMgr = ServerProfileManager.instance + private let defaults = UserDefaults.standard + private let appdeleget = NSApplication.shared.delegate as! AppDelegate func getStatus()->Bool { return self.defaults.bool(forKey: "ShadowsocksOn");