Feature Request: Support for Custom NSURLSessionConfiguration in Native WebSocket Implementation #111
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Reason for Changes
We encountered an issue on iOS versions earlier than iOs 17. In these versions, system-wide proxy settings are not applied to new instances of URLSessionWebSocketTask. This means that if the application relies on system-level proxy configurations, the WebSocket connections will bypass them.
To enable proxying for iOS versions below 17, proxy settings must be explicitly provided when creating the URLSession. Without this capability, developers cannot ensure that WebSocket traffic respects proxy configurations, leading to potential security and compliance issues in controlled network environments.
This limitation makes it critical to allow developers to pass a custom NSURLSessionConfiguration to the NativeWebSocket implementation.
proxyman: Capture WS/WSS from iOS
Description
Currently, the NativeWebSocket implementation uses URLSessionConfiguration.default for its NSURLSessionConfiguration. While this works for standard use cases, it becomes a limitation for advanced configurations where custom settings (e.g., proxy settings) are required. Without a way to inject a custom NSURLSessionConfiguration, developers are forced to use method swizzling, which is not ideal because:
To address this, I have implemented changes that allow developers to pass a custom NSURLSessionConfiguration through the WebSocketTransport configuration. These changes are backward-compatible and provide a more flexible approach for advanced configurations.
Key Features Added:
• Developers can now pass a custom URLSessionConfiguration provider to the NativeWebSocket through the WebSocketTransport configuration.
• Example use case: Adding proxy settings to the WebSocket connection.
• The default behavior remains unchanged, using URLSessionConfiguration.default if no custom provider is specified.
Code Changes:
Extended Example, now user can specify proxy params for centrifuge client.
Risks or Considerations:
• Potential Misuse of Configuration Provider: Developers need to ensure the custom URLSessionConfiguration is well-defined, as incorrect configurations could cause unexpected behavior.
Screenshot Descriptions
Below are screenshots to assist with configuring Charles Proxy and testing proxying of WebSocket traffic for CentrifugeClient:
1. Charles Proxy Configuration:
• The Charles Proxy server is set up on the local IP address 192.168.1.80 and listens for traffic on port 8889.
• The screenshot shows the necessary settings in the Charles application to enable WebSocket proxying.
2. Captured Traffic in Charles:
• This screenshot demonstrates successful interception of WebSocket traffic between the CentrifugeClient and the Centrifugo server.
• The server hosting Centrifugo is running on 192.168.1.136 with port 8000.
3. Application Proxy Settings:
• The screenshots shows the proxy settings applied directly within the application using the urlSessionConfigurationProvider configuration.