Skip to content

Commit

Permalink
Merge pull request #90 from ProxymanApp/feat/multiple-connection
Browse files Browse the repository at this point in the history
Support multiple connection to Proxyman apps
  • Loading branch information
NghiaTranUIT authored Nov 22, 2021
2 parents f213b33 + fd343ef commit 395658f
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 103 deletions.
33 changes: 15 additions & 18 deletions Objc/AtlantisHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,26 @@
@implementation AtlantisHelper

+(id _Nullable) swizzleWebSocketReceiveMessageWithCompleteHandler:(id)handler responseHandler:(void (^_Nullable)(NSString* _Nullable str, NSData* _Nullable data, NSError* _Nullable error)) responseHandler {
if (@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)) {
typedef void (^WebSocketHandler) (NSURLSessionWebSocketMessage *message, NSError *error);
typedef void (^WebSocketHandler) (NSURLSessionWebSocketMessage *message, NSError *error);

// We need handle in Objc Helper because Xcode doesn't allow to compile with NSURLSessionWebSocketMessage class
// We get the data/string from NSURLSessionWebSocketMessage and pass back to Atlantis-Swift
// In objc, it's easer to implement
WebSocketHandler wrapperHandler = ^(NSURLSessionWebSocketMessage *message, NSError *error) {
// We need to handle in Objc Helper because Xcode doesn't allow to compile with NSURLSessionWebSocketMessage class
// We get the data/string from NSURLSessionWebSocketMessage and pass back to Atlantis-Swift
// In objc, it's easer to implement
WebSocketHandler wrapperHandler = ^(NSURLSessionWebSocketMessage *message, NSError *error) {

// Pass data to Atlantis Swift
if (responseHandler) {
responseHandler([message string], [message data], error);
}
// Pass data to Atlantis Swift
if (responseHandler) {
responseHandler([message string], [message data], error);
}

// Cast
WebSocketHandler originalHandler = (WebSocketHandler) handler;
// Cast
WebSocketHandler originalHandler = (WebSocketHandler) handler;

// Call the original
originalHandler(message, error);
};
// Call the original
originalHandler(message, error);
};

return wrapperHandler;
}
return nil;
return wrapperHandler;
}

@end
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import PackageDescription

let package = Package(
name: "Atlantis",
platforms: [.macOS(.v10_12),
.iOS(.v11),
.tvOS(.v11),],
platforms: [.macOS(.v10_15),
.iOS(.v13),
.tvOS(.v13)],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
Expand Down
6 changes: 0 additions & 6 deletions Sources/Atlantis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,23 +288,19 @@ extension Atlantis: InjectorDelegate {

extension Atlantis {

@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
func injectorSessionWebSocketDidSendPingPong(task: URLSessionTask) {
let message = URLSessionWebSocketTask.Message.string("ping")
sendWebSocketMessage(task: task, messageType: .pingPong, message: message)
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
func injectorSessionWebSocketDidReceive(task: URLSessionTask, message: URLSessionWebSocketTask.Message) {
sendWebSocketMessage(task: task, messageType: .receive, message: message)
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
func injectorSessionWebSocketDidSendMessage(task: URLSessionTask, message: URLSessionWebSocketTask.Message) {
sendWebSocketMessage(task: task, messageType: .send, message: message)
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
private func sendWebSocketMessage(task: URLSessionTask, messageType: WebsocketMessagePackage.MessageType, message: URLSessionWebSocketTask.Message) {
queue.sync {
// Since it's not possible to revert the Method Swizzling change
Expand All @@ -319,7 +315,6 @@ extension Atlantis {
}
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
func injectorSessionWebSocketDidSendCancelWithReason(task: URLSessionTask, closeCode: URLSessionWebSocketTask.CloseCode, reason: Data?) {
queue.sync {
// Since it's not possible to revert the Method Swizzling change
Expand All @@ -335,7 +330,6 @@ extension Atlantis {
}
}

@available(iOS 13.0, macOS 10.15, *)
private func prepareAndSendWSMessage(task: URLSessionTask, wsPackageBuilder: (String) -> WebsocketMessagePackage?) {
// Get the ID
let id = PackageIdentifier.getID(taskOrConnection: task)
Expand Down
7 changes: 0 additions & 7 deletions Sources/NetworkInjector+URLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ extension NetworkInjector {

extension NetworkInjector {

@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
func _swizzleURLSessionWebsocketSelector() {
guard let websocketClass = NSClassFromString("__NSURLSessionWebSocketTask") else {
print("[Atlantis][ERROR] Could not inject __NSURLSessionWebSocketTask!!")
Expand All @@ -344,7 +343,6 @@ extension NetworkInjector {
_swizzleURLSessionWebSocketCancelWithCloseCodeReasonSelector(websocketClass)
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
private func _swizzleURLSessionWebSocketSendMessageSelector(_ baseClass: AnyClass) {

// Prepare
Expand Down Expand Up @@ -379,7 +377,6 @@ extension NetworkInjector {
method_setImplementation(method, imp_implementationWithBlock(block))
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
private func _swizzleURLSessionWebSocketReceiveMessageSelector(_ baseClass: AnyClass) {

// Prepare
Expand Down Expand Up @@ -419,7 +416,6 @@ extension NetworkInjector {
method_setImplementation(method, imp_implementationWithBlock(block))
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
private func _swizzleURLSessionWebSocketSendPingPongSelector(_ baseClass: AnyClass) {

// Prepare
Expand Down Expand Up @@ -450,7 +446,6 @@ extension NetworkInjector {
method_setImplementation(method, imp_implementationWithBlock(block))
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
private func _swizzleURLSessionWebSocketCancelWithCloseCodeReasonSelector(_ baseClass: AnyClass) {

// Prepare
Expand Down Expand Up @@ -483,7 +478,6 @@ extension NetworkInjector {
method_setImplementation(method, imp_implementationWithBlock(block))
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
private func wrapWebSocketMessage(object: AnyObject) -> URLSessionWebSocketTask.Message? {
if let strValue = object.value(forKey: "string") as? String {
return URLSessionWebSocketTask.Message.string(strValue)
Expand All @@ -493,7 +487,6 @@ extension NetworkInjector {
return nil
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
private func wrapWebSocketMessage(strValue: String?, dataValue: Data?) -> URLSessionWebSocketTask.Message? {
if let strValue = strValue {
return URLSessionWebSocketTask.Message.string(strValue)
Expand Down
9 changes: 1 addition & 8 deletions Sources/NetworkInjector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@ protocol InjectorDelegate: AnyObject {
func injectorSessionDidUpload(task: URLSessionTask, request: NSURLRequest, data: Data?)

// Websocket
@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
func injectorSessionWebSocketDidSendMessage(task: URLSessionTask, message: URLSessionWebSocketTask.Message)
@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
func injectorSessionWebSocketDidReceive(task: URLSessionTask, message: URLSessionWebSocketTask.Message)
@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
func injectorSessionWebSocketDidSendPingPong(task: URLSessionTask)
@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
func injectorSessionWebSocketDidSendCancelWithReason(task: URLSessionTask, closeCode: URLSessionWebSocketTask.CloseCode, reason: Data?)

// For URLConnection
Expand Down Expand Up @@ -77,9 +73,7 @@ extension NetworkInjector {
injectURLSessionUploadTasks()

// Websocket
if #available(iOS 13.0, macOS 10.15, tvOS 13.0, *) {
injectURLSessionWebsocketTasks()
}
injectURLSessionWebsocketTasks()
}

private func injectAllURLConnection() {
Expand Down Expand Up @@ -138,7 +132,6 @@ extension NetworkInjector {
_swizzleURLSessionUploadSelector(baseClass: URLSession.self)
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
private func injectURLSessionWebsocketTasks() {
_swizzleURLSessionWebsocketSelector()
}
Expand Down
10 changes: 3 additions & 7 deletions Sources/Packages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,9 @@ public final class TrafficPackage: Codable, CustomDebugStringConvertible, Serial
let request = Request(currentRequest) else { return nil }

// Check if it's a websocket
if #available(iOS 13.0, *) {
if let websocketClass = NSClassFromString("__NSURLSessionWebSocketTask"),
sessionTask.isKind(of: websocketClass) {
return TrafficPackage(id: id, request: request, packageType: .websocket)
}
if let websocketClass = NSClassFromString("__NSURLSessionWebSocketTask"),
sessionTask.isKind(of: websocketClass) {
return TrafficPackage(id: id, request: request, packageType: .websocket)
}

// Or normal websocket
Expand Down Expand Up @@ -197,7 +195,6 @@ public final class TrafficPackage: Codable, CustomDebugStringConvertible, Serial
return "Package: id=\(id), request=\(String(describing: request)), response=\(String(describing: response))"
}

@available(iOS 13.0, *)
func setWebsocketMessagePackage(package: WebsocketMessagePackage) {
self.websocketMessagePackage = package
}
Expand Down Expand Up @@ -341,7 +338,6 @@ struct WebsocketMessagePackage: Codable, Serializable {
case data(Data)
case string(String)

@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
init?(message: URLSessionWebSocketTask.Message) {
switch message {
case .data(let data):
Expand Down
Loading

0 comments on commit 395658f

Please sign in to comment.