From a338a76dcc287edd90cdc63bc31b6141f0afdb37 Mon Sep 17 00:00:00 2001 From: Mark Siu Date: Wed, 9 May 2018 15:47:42 +0800 Subject: [PATCH 1/2] Allow to use Charles proxy to capture the websocket transactions --- Source/WebSocket.swift | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Source/WebSocket.swift b/Source/WebSocket.swift index 6c22da2..3b6f5b7 100644 --- a/Source/WebSocket.swift +++ b/Source/WebSocket.swift @@ -534,6 +534,7 @@ private class InnerWebSocket: Hashable { var _subProtocol = "" var _compression = WebSocketCompression() var _allowSelfSignedSSL = false + var _allowProxyInSimulator = false var _services = WebSocketService.None var _event = WebSocketEvents() var _eventDelegate: WebSocketDelegate? @@ -560,6 +561,10 @@ private class InnerWebSocket: Hashable { get { lock(); defer { unlock() }; return _allowSelfSignedSSL } set { lock(); defer { unlock() }; _allowSelfSignedSSL = newValue } } + var allowProxyInSimulator : Bool { + get { lock(); defer { unlock() }; return _allowProxyInSimulator } + set { lock(); defer { unlock() }; _allowProxyInSimulator = newValue } + } var services : WebSocketService { get { lock(); defer { unlock() }; return _services } set { lock(); defer { unlock() }; _services = newValue } @@ -593,6 +598,7 @@ private class InnerWebSocket: Hashable { ws.eclose = eclose ws.compression = compression ws.allowSelfSignedSSL = allowSelfSignedSSL + ws.allowProxyInSimulator = allowProxyInSimulator ws.services = services ws.event = event ws.eventQueue = eventQueue @@ -1085,6 +1091,12 @@ private class InnerWebSocket: Hashable { rd.setProperty(prop, forKey: Stream.PropertyKey(rawValue: kCFStreamPropertySSLSettings as String as String)) wr.setProperty(prop, forKey: Stream.PropertyKey(rawValue: kCFStreamPropertySSLSettings as String as String)) } + if allowProxyInSimulator { + let proxyDict = CFNetworkCopySystemProxySettings() + let prop = CFDictionaryCreateMutableCopy(nil, 0, proxyDict!.takeRetainedValue()) + rd.setProperty(prop, forKey: Stream.PropertyKey(rawValue: kCFStreamPropertySOCKSProxy as String as String)) + wr.setProperty(prop, forKey: Stream.PropertyKey(rawValue: kCFStreamPropertySOCKSProxy as String as String)) + } rd.delegate = delegate wr.delegate = delegate rd.schedule(in: RunLoop.main, forMode: RunLoopMode.defaultRunLoopMode) @@ -1702,6 +1714,11 @@ open class WebSocket: NSObject { get { return ws.allowSelfSignedSSL } set { ws.allowSelfSignedSSL = newValue } } + /// Allow to use Proxy to capture the websocket transfer. Default is false. + open var allowProxyInSimulator : Bool{ + get { return ws.allowProxyInSimulator } + set { ws.allowProxyInSimulator = newValue } + } /// The services of the WebSocket. open var services : WebSocketService{ get { return ws.services } From 2ad55c715efe05c056f092f48a2be1a863303e81 Mon Sep 17 00:00:00 2001 From: Mark Siu Date: Wed, 9 May 2018 15:58:18 +0800 Subject: [PATCH 2/2] Updated README.md about how to capture socket transactions in iOS simulator with proxy --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 1fae301..720c4d9 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,13 @@ let ws = WebSocket("ws://url") ws.allowSelfSignedSSL = true ``` +## Show Socket Transaction in iOS Simulator with Proxy + +```swift +let ws = WebSocket("ws://url") +ws.allowProxyInSimulator = true +``` + ## Network Services (VoIP, Video, Background, Voice) ```swift