Skip to content

Commit fdb4766

Browse files
author
Stephen Mitchell
authored
Merge pull request #11 from scuml/SSL
Adds connectWithSSL parameter
2 parents 4083d02 + 3e3d094 commit fdb4766

File tree

2 files changed

+65
-60
lines changed

2 files changed

+65
-60
lines changed

Sources/SwiftRedis/Redis.swift

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ import Foundation
2121
/// The `Redis` class represents a handle for issueing commands to a Redis server.
2222
/// It provides a set of type safe functions for issueing those commands.
2323
public class Redis {
24-
24+
2525
/// Redis Serialization Protocol handle
2626
public var respHandle: RedisResp?
27-
27+
2828
/// Whether the client is connected or not.
2929
/// Does not reflect state changes in the event of a disconnect.
3030
public var connected: Bool {
3131
return respHandle?.status == .connected
3232
}
33-
33+
3434
/// Initializes a `Redis` instance
3535
public init () {}
36-
36+
3737
/// Connects to a redis server
3838
///
3939
/// - Parameter host: the server IP address.
@@ -52,14 +52,14 @@ public class Redis {
5252
public func disconnect(){
5353
respHandle?.disconnect()
5454
}
55-
55+
5656
/// Authenticate against the server
5757
///
5858
/// - Parameter pswd: String for the password.
5959
/// - Parameter callback: callback function that is called after authenticating,
6060
/// NSError will be nil if successful.
6161
public func auth(_ pswd: String, callback: (NSError?) -> Void) {
62-
62+
6363
issueCommand("AUTH", pswd) {(response: RedisResponse) in
6464
let (_, error) = self.redisOkResponseHandler(response, nilOk: false)
6565
callback(error)
@@ -83,20 +83,20 @@ public class Redis {
8383
/// - Parameter callback: callback function for after the database is selected,
8484
/// NSError will be nil if successful.
8585
public func select(_ db: Int, callback: (NSError?) -> Void) {
86-
86+
8787
issueCommand("SELECT", String(db)) {(response: RedisResponse) in
8888
let (_, error) = self.redisOkResponseHandler(response, nilOk: false)
8989
callback(error)
9090
}
9191
}
92-
92+
9393
/// Ping the server to test if a connection is still alive
9494
///
9595
/// - Parameter pingStr: String for the ping message.
9696
/// - Parameter callback: callback function for after the pong is received,
9797
/// NSError will be nil if successful.
9898
public func ping(_ pingStr: String?=nil, callback: (NSError?) -> Void) {
99-
99+
100100
var command = ["PING"]
101101
if let pingStr = pingStr {
102102
command.append(pingStr)
@@ -122,19 +122,19 @@ public class Redis {
122122
}
123123
}
124124
}
125-
125+
126126
/// Echos a message
127127
///
128128
/// - Parameter str: String for the message.
129129
/// - Parameter callback: callback function with the String echoed back,
130130
/// NSError will be nil if successful.
131131
public func echo(_ str: String, callback: (RedisString?, NSError?) -> Void) {
132-
132+
133133
issueCommand("ECHO", str) {(response: RedisResponse) in
134134
self.redisStringResponseHandler(response, callback: callback)
135135
}
136136
}
137-
137+
138138
/// Get information and statistics about the server
139139
///
140140
/// - Parameter callback: callback function with the response as a collection of text
@@ -144,7 +144,7 @@ public class Redis {
144144
self.redisStringResponseHandler(response, callback: callback)
145145
}
146146
}
147-
147+
148148
/// Get information and statistics about the server
149149
///
150150
/// - Parameter callback: callback function with the response as a struct
@@ -155,7 +155,7 @@ public class Redis {
155155
self.redisDictionaryResponseHandler(response, callback: callback)
156156
}
157157
}
158-
158+
159159
/// Delete all the keys of the currently selected DB. This command never fails.
160160
///
161161
/// - Parameter callback: a function returning the response,
@@ -166,30 +166,30 @@ public class Redis {
166166
callback(ok, _: error)
167167
}
168168
}
169-
170-
169+
170+
171171
//
172172
// MARK: Transaction support
173173
//
174-
174+
175175
/// Create a `RedisMulti` object in order to perform a Redis transaction
176176
public func multi() -> RedisMulti {
177177
return RedisMulti(redis: self)
178178
}
179-
180-
179+
180+
181181
//
182182
// MARK: Base API functions
183183
//
184-
184+
185185
/// Issue a Redis command
186186
///
187187
/// - Parameter stringArgs: A list of Strings making up the Redis command to issue
188188
/// - Parameter callback: a function returning the response in the form of a `RedisResponse`
189189
public func issueCommand(_ stringArgs: String..., callback: (RedisResponse) -> Void) {
190190
issueCommandInArray(stringArgs, callback: callback)
191191
}
192-
192+
193193
/// Issue a Redis command
194194
///
195195
/// - Parameter stringArgs: An array of Strings making up the Redis command to issue
@@ -199,23 +199,23 @@ public class Redis {
199199
callback(RedisResponse.Error("Not connected to Redis server"))
200200
return
201201
}
202-
202+
203203
guard stringArgs.count > 0 else {
204204
callback(RedisResponse.Error("Empty command"))
205205
return
206206
}
207-
207+
print("ISSUE ", stringArgs)
208208
respHandle.issueCommand(stringArgs, callback: callback)
209209
}
210-
210+
211211
/// Issue a Redis command
212212
///
213213
/// - Parameter stringArgs: A list of `RedisString` objects making up the Redis command to issue
214214
/// - Parameter callback: a function returning the response in the form of a `RedisResponse`
215215
public func issueCommand(_ stringArgs: RedisString..., callback: (RedisResponse) -> Void) {
216216
issueCommandInArray(stringArgs, callback: callback)
217217
}
218-
218+
219219
/// Issue a Redis command
220220
///
221221
/// - Parameter stringArgs: An array of `RedisString` objects making up the Redis command to issue
@@ -225,19 +225,19 @@ public class Redis {
225225
callback(RedisResponse.Error("Not connected to Redis server"))
226226
return
227227
}
228-
228+
229229
guard stringArgs.count > 0 else {
230230
callback(RedisResponse.Error("Empty command"))
231231
return
232232
}
233-
233+
234234
respHandle.issueCommand(stringArgs, callback: callback)
235235
}
236-
236+
237237
//
238238
// MARK: Helper functions
239239
//
240-
240+
241241
func redisBoolResponseHandler(_ response: RedisResponse, callback: (Bool, NSError?) -> Void) {
242242
switch(response) {
243243
case .IntegerValue(let num):
@@ -252,7 +252,7 @@ public class Redis {
252252
callback(false, _: createUnexpectedResponseError(response))
253253
}
254254
}
255-
255+
256256
func redisIntegerResponseHandler(_ response: RedisResponse, callback: (Int?, NSError?) -> Void) {
257257
switch(response) {
258258
case .IntegerValue(let num):
@@ -265,7 +265,7 @@ public class Redis {
265265
callback(nil, _: createUnexpectedResponseError(response))
266266
}
267267
}
268-
268+
269269
func redisOkResponseHandler(_ response: RedisResponse, nilOk: Bool=true) -> (Bool, NSError?) {
270270
switch(response) {
271271
case .Status(let str):
@@ -282,7 +282,7 @@ public class Redis {
282282
return (false, createUnexpectedResponseError(response))
283283
}
284284
}
285-
285+
286286
func redisSimpleStringResponseHandler(response: RedisResponse, callback: (String?, NSError?) -> Void) {
287287
switch(response) {
288288
case .Status(let str):
@@ -295,7 +295,7 @@ public class Redis {
295295
callback(nil, _: createUnexpectedResponseError(response))
296296
}
297297
}
298-
298+
299299
func redisStringResponseHandler(_ response: RedisResponse, callback: (RedisString?, NSError?) -> Void) {
300300
switch(response) {
301301
case .StringValue(let str):
@@ -308,11 +308,11 @@ public class Redis {
308308
callback(nil, _: createUnexpectedResponseError(response))
309309
}
310310
}
311-
311+
312312
func redisArrayResponseHandler(response: RedisResponse, callback: ([RedisResponse?]?, NSError?) -> Void) {
313313
var error: NSError? = nil
314314
var result: [RedisResponse?]?
315-
315+
316316
switch(response) {
317317
case .Array(let responses):
318318
result = responses
@@ -325,7 +325,7 @@ public class Redis {
325325
}
326326
callback(error == nil ? result : nil, _: error)
327327
}
328-
328+
329329
func getCoordinates(from responses: [RedisResponse?]?, callback: ([(Double, Double)?]?, NSError?) -> Void) {
330330
guard let responses = responses else {
331331
callback(nil, createError("Could not get coordinates from nil response.", code: 1))
@@ -347,11 +347,11 @@ public class Redis {
347347
}
348348
callback(result, nil)
349349
}
350-
350+
351351
func redisStringArrayResponseHandler(_ response: RedisResponse, callback: ([RedisString?]?, NSError?) -> Void) {
352352
var error: NSError? = nil
353353
var result: [RedisString?]?
354-
354+
355355
switch(response) {
356356
case .Array(let responses):
357357
var strings = [RedisString?]()
@@ -375,11 +375,11 @@ public class Redis {
375375
}
376376
callback(error == nil ? result : nil, _: error)
377377
}
378-
378+
379379
func redisStringArrayOrIntegerResponseHandler(_ response: RedisResponse, callback: ([RedisString?]?, NSError?) -> Void) {
380380
var error: NSError? = nil
381381
var result: [RedisString?]?
382-
382+
383383
switch(response) {
384384
case .Array(let responses):
385385
var strings = [RedisString?]()
@@ -405,12 +405,12 @@ public class Redis {
405405
}
406406
callback(error == nil ? result : nil, _: error)
407407
}
408-
408+
409409
func redisScanResponseHandler(_ response: RedisResponse, callback: (RedisString?, [RedisString?]?, NSError?) -> Void) {
410410
var error: NSError? = nil
411411
var cursor: RedisString?
412412
var result: [RedisString?]?
413-
413+
414414
switch(response) {
415415
case .Array(let responses):
416416
var strings = [RedisString?]()
@@ -441,14 +441,14 @@ public class Redis {
441441
default:
442442
error = self.createUnexpectedResponseError(response)
443443
}
444-
444+
445445
if(error == nil) {
446446
callback(cursor, result, nil)
447447
} else {
448448
callback(nil, nil, error)
449449
}
450450
}
451-
451+
452452
func redisDictionaryResponseHandler(_ response: RedisResponse, callback: (RedisInfo?, NSError?) -> Void) {
453453
switch(response) {
454454
case .StringValue(let str):
@@ -461,11 +461,11 @@ public class Redis {
461461
callback(nil, _: createUnexpectedResponseError(response))
462462
}
463463
}
464-
464+
465465
func createUnexpectedResponseError(_ response: RedisResponse) -> NSError {
466466
return createError("Unexpected result received from Redis \(response)", code: 2)
467467
}
468-
468+
469469
func createError(_ errorMessage: String, code: Int) -> NSError {
470470
#if os(Linux)
471471
let userInfo: [String: Any]
@@ -475,7 +475,7 @@ public class Redis {
475475
userInfo = [NSLocalizedDescriptionKey: errorMessage]
476476
return NSError(domain: "RedisDomain", code: code, userInfo: userInfo)
477477
}
478-
478+
479479
func createRedisError(_ redisError: String) -> NSError {
480480
return createError(redisError, code: 1)
481481
}

Sources/SwiftRedis/RedisSSL.swift

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ extension Redis {
2323
/// clientAllowsSelfSignedCertificates: true
2424
/// )
2525
/// - Parameter callback: callback function for on completion, NSError will be nil if successful.
26-
public func connect(host: String, port: Int32, sslConfig: SSLService.Configuration? = nil, sslSkipVerification: Bool = false, callback: (NSError?) -> Void) {
27-
respHandle = RedisResp(host: host, port: port, sslConfig: sslConfig, sslSkipVerification: sslSkipVerification)
26+
public func connect(host: String, port: Int32, connectWithSSL: Bool = false, sslConfig: SSLService.Configuration? = nil, sslSkipVerification: Bool = false, callback: (NSError?) -> Void) {
27+
respHandle = RedisResp(host: host, port: port, connectWithSSL: connectWithSSL, sslConfig: sslConfig, sslSkipVerification: sslSkipVerification)
2828

2929
if respHandle?.status == .notConnected {
3030
callback(createError("Failed to connect to Redis server", code: 2))
@@ -36,24 +36,29 @@ extension Redis {
3636

3737
extension RedisResp {
3838

39-
convenience init(host: String, port: Int32, sslConfig: SSLService.Configuration? = nil, sslSkipVerification: Bool = false) {
39+
convenience init(host: String, port: Int32, connectWithSSL: Bool = false, sslConfig: SSLService.Configuration? = nil, sslSkipVerification: Bool = false) {
4040
self.init()
4141
socket = try? Socket.create()
42-
if let sslConfig = sslConfig{
43-
/// SSL Configs can only use a password protected .p12 on Mac OS
44-
// let sslConfig = SSLService.Configuration(
45-
// withChainFilePath: "/keystore.p12",
46-
// withPassword: "pass",
47-
// usingSelfSignedCerts: true,
48-
// clientAllowsSelfSignedCertificates: true
49-
// )
42+
if connectWithSSL{
43+
var sslConfigToUse = SSLService.Configuration()
44+
if let sslConfig = sslConfig{
45+
/// SSL Configs can only use a password protected .p12 on Mac OS
46+
// let sslConfig = SSLService.Configuration(
47+
// withChainFilePath: "/keystore.p12",
48+
// withPassword: "pass",
49+
// usingSelfSignedCerts: true,
50+
// clientAllowsSelfSignedCertificates: true
51+
// )
52+
sslConfigToUse = sslConfig
53+
}
54+
5055
do{
51-
let sslService = try SSLService(usingConfiguration: sslConfig)
56+
let sslService = try SSLService(usingConfiguration: sslConfigToUse)
5257
sslService?.skipVerification = sslSkipVerification
58+
socket?.delegate = sslService
5359
}catch{
5460
print(error)
5561
}
56-
socket?.delegate = try? SSLService(usingConfiguration: sslConfig)
5762
}
5863
self.connect(host: host, port: port)
5964
}

0 commit comments

Comments
 (0)