Skip to content

Commit

Permalink
Merge pull request #1108 from apollographql/add/websocket-property
Browse files Browse the repository at this point in the history
Add ability to get to Starscream's underlying SOCKS proxy property.
  • Loading branch information
designatednerd authored Mar 31, 2020
2 parents 2e0d56e + 00c4725 commit 9a1ecc0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
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 {
// If it's not proxyable, there's nothing to do here.
return
}

socket.enableSOCKSProxy = newValue
}
}

/// Designated initializer
///
Expand Down

0 comments on commit 9a1ecc0

Please sign in to comment.