Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to get to Starscream's underlying SOCKS proxy property. #1108

Merged
merged 1 commit into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions Sources/ApolloWebSocket/ApolloWebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,34 @@ public protocol ApolloWebSocketClient: WebSocketClient {
var callbackQueue: DispatchQueue { get set }
}

public protocol SOCKSProxyable {

/// Determines whether a SOCKS proxy is enabled on the underlying request.
/// Mostly useful for debugging with tools like Charles Proxy.
var enableSOCKSProxy: Bool { get set }
}

// MARK: - WebSocket

/// Included implementation of an `ApolloWebSocketClient`, based on `Starscream`'s `WebSocket`.
public class ApolloWebSocket: WebSocket, ApolloWebSocketClient {
public class ApolloWebSocket: WebSocket, ApolloWebSocketClient, SOCKSProxyable {

private var stream: FoundationStream!

public var enableSOCKSProxy: Bool {
get {
return self.stream.enableSOCKSProxy
}
set {
self.stream.enableSOCKSProxy = newValue
}
}

required public convenience init(request: URLRequest, protocols: [String]? = nil) {
let stream = FoundationStream()
self.init(request: request,
protocols: protocols,
stream: FoundationStream())
stream: stream)
self.stream = stream
}
}
22 changes: 22 additions & 0 deletions Sources/ApolloWebSocket/WebSocketTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ public class WebSocketTransport {
self.addApolloClientHeaders(to: &self.websocket.request)
}
}

/// Determines whether a SOCKS proxy is enabled on the underlying request.
/// Mostly useful for debugging with tools like Charles Proxy.
/// Note: Will return `false` from the getter and no-op the setter for implementations that do not conform to `SOCKSProxyable`.
public var enableSOCKSProxy: Bool {
get {
guard let socket = self.websocket as? SOCKSProxyable else {
// If it's not proxyable, then the proxy can't be enabled
return false
}

return socket.enableSOCKSProxy
}
set {
guard var socket = self.websocket as? SOCKSProxyable else {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has to be guard var as guard let wouldn't let me change enableSOCKSProxy since this protocol is not class-bound.

// If it's not proxyable, there's nothing to do here.
return
}

socket.enableSOCKSProxy = newValue
}
}

/// Designated initializer
///
Expand Down