Skip to content

Commit 93510a6

Browse files
committed
Now text data sent to HTTP API are validated.
1 parent b617e83 commit 93510a6

File tree

2 files changed

+39
-39
lines changed

2 files changed

+39
-39
lines changed

ShadowsocksX-NG/HTTPUserProxy.swift

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import GCDWebServer
1212
class HTTPUserProxy{
1313
static let shard = HTTPUserProxy()
1414

15-
let adapter = APIAdapter()
16-
let v = Validator()
15+
let app = AppFacade()
1716

1817
let server = GCDWebServer()
1918
let api_port:UInt = 9528
@@ -56,20 +55,20 @@ class HTTPUserProxy{
5655

5756
func addHandler_getStatus() {
5857
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")
6059
})
6160
}
6261

6362
func addHandler_setStatus() {
6463
server.addHandler(forMethod: "PUT", path: "/status", request: GCDWebServerURLEncodedFormRequest.self, processBlock: {request in
6564
if let targetStatus_str = (request as? GCDWebServerURLEncodedFormRequest)?.arguments["Enable"] as? String{
6665
if let targetStatus = Bool(targetStatus_str) {
67-
self.adapter.setStatus(status: targetStatus)
66+
self.app.setStatus(status: targetStatus)
6867
return GCDWebServerResponse()
6968
}
7069
}
7170
else {
72-
self.adapter.toggleStatus()
71+
self.app.toggleStatus()
7372
return GCDWebServerResponse()
7473
}
7574
return GCDWebServerResponse(statusCode: 400)
@@ -78,14 +77,14 @@ class HTTPUserProxy{
7877

7978
func addHandler_getServerList() {
8079
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")
8281
})
8382
}
8483

8584
func addHandler_getCurrentServer() {
8685
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")
8988
}
9089
else {
9190
return GCDWebServerResponse(statusCode: 404);
@@ -97,8 +96,8 @@ class HTTPUserProxy{
9796
server.addHandler(forMethod: "PUT", path: "/current", request: GCDWebServerURLEncodedFormRequest.self, processBlock: {request in
9897

9998
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);
102101
return GCDWebServerResponse()
103102
}
104103
}
@@ -111,8 +110,8 @@ class HTTPUserProxy{
111110
if var server = ((request as? GCDWebServerURLEncodedFormRequest)?.arguments) as? [String: Any] {
112111
if (server["ServerPort"] != nil) {
113112
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)
116115
return GCDWebServerResponse();
117116
}
118117
}
@@ -128,10 +127,10 @@ class HTTPUserProxy{
128127
if (server["ServerPort"] != nil) {
129128
server["ServerPort"] = UInt16(server["ServerPort"] as! String)
130129
}
131-
if (self.adapter.getServer(uuid: id) != nil) {
130+
if (self.app.getServer(uuid: id) != nil) {
132131
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)
135134
return GCDWebServerResponse()
136135
}
137136
else {
@@ -150,9 +149,9 @@ class HTTPUserProxy{
150149
server.addHandler(forMethod: "DELETE", pathRegex: "/servers/"+self.UUID_REGEX, request: GCDWebServerRequest.self
151150
, processBlock: {request in
152151
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)
156155
return GCDWebServerResponse()
157156
} else {
158157
return GCDWebServerResponse(statusCode: 400)
@@ -166,15 +165,15 @@ class HTTPUserProxy{
166165

167166
func addHandler_getMode() {
168167
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")
170169
})
171170
}
172171

173172
func addHandler_setMode() {
174173
server.addHandler(forMethod: "PUT", path: "/mode", request: GCDWebServerURLEncodedFormRequest.self, processBlock: {request in
175174
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);
178177

179178
return GCDWebServerResponse()
180179
}
@@ -184,12 +183,10 @@ class HTTPUserProxy{
184183
}
185184
}
186185

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
193190

194191
func getStatus()->Bool {
195192
return self.defaults.bool(forKey: "ShadowsocksOn");
@@ -281,37 +278,40 @@ class APIAdapter {
281278
self.appdeleget.updateServersMenu()
282279
}
283280

284-
func getMode()->Mode {
281+
func getMode()->ProxyType {
285282
let mode_str = self.defaults.string(forKey: "ShadowsocksRunningMode");
286283
switch mode_str {
287-
case "auto": return .auto
284+
case "auto": return .pac
288285
case "global": return .global;
289286
case "manual": return .manual
290287
default:fatalError()
291288
}
292289
}
293290

294-
func setMode(mode:Mode) {
291+
func setMode(mode:ProxyType) {
295292
let defaults = UserDefaults.standard
296293

297294
switch mode{
298-
case .auto:defaults.setValue("auto", forKey: "ShadowsocksRunningMode")
295+
case .pac:defaults.setValue("auto", forKey: "ShadowsocksRunningMode")
299296
case .global:defaults.setValue("global", forKey: "ShadowsocksRunningMode")
300297
case .manual:defaults.setValue("manual", forKey: "ShadowsocksRunningMode")
301298
}
302299

300+
Globals.proxyType = mode
301+
303302
self.appdeleget.updateRunningModeMenu()
304303
self.appdeleget.applyConfig()
305304
}
306305
}
307306

308307
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 {
310310
if (data["ServerHost"] == nil || data["ServerPort"] as? NSNumber == nil
311311
|| data["Method"] == nil || data["Password"] == nil) {
312312
return false;
313313
}
314-
return true;
314+
return existAttributes(data);
315315
}
316316

317317
static func existAttributes(_ server:Dictionary<String, Any>) -> Bool {
@@ -352,7 +352,6 @@ class Validator {
352352

353353
static func method(_ str:String) -> Bool {
354354
// Copy from PreferencesWindowController.swift
355-
// Better to make valid methods enumeration type.
356355
return [
357356
"aes-128-gcm",
358357
"aes-192-gcm",
@@ -373,11 +372,11 @@ class Validator {
373372
"chacha20",
374373
"chacha20-ietf",
375374
"rc4-md5",
376-
].contains(str);
375+
].contains(str)
377376
}
378377

379378
static func password(_ str:String) -> Bool {
380-
return true;
379+
return !str.isEmpty;
381380
}
382381

383382
static func remark(_ str:String) -> Bool {

ShadowsocksX-NG/Utils.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ extension Data {
2424
}
2525
}
2626

27-
enum ProxyType {
28-
case pac
29-
case global
27+
enum ProxyType:String {
28+
case pac = "auto"
29+
case global = "global"
30+
case manual = "manual"
3031
}
3132

3233
struct Globals {

0 commit comments

Comments
 (0)