Skip to content

Commit

Permalink
configure url session on ios
Browse files Browse the repository at this point in the history
  • Loading branch information
denrase committed Jul 29, 2024
1 parent 16c6795 commit 67c474f
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 23 deletions.
2 changes: 1 addition & 1 deletion dart/lib/src/http_client/io_client_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class IoClientProvider implements ClientProvider {
return Client();
}
final pac = proxy.toPacString();
if (proxy.type == ProxyType.socks) {
if (proxy.type == SentryProxyType.socks) {
options.logger(
SentryLevel.warning,
"Setting proxy '$pac' is not supported.",
Expand Down
12 changes: 6 additions & 6 deletions dart/lib/src/protocol/sentry_proxy.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class SentryProxy {
final ProxyType type;
final SentryProxyType type;
final String? host;
final int? port;
final String? user;
Expand All @@ -10,12 +10,12 @@ class SentryProxy {
String toPacString() {
String type = 'DIRECT';
switch (this.type) {
case ProxyType.direct:
case SentryProxyType.direct:
return 'DIRECT';
case ProxyType.http:
case SentryProxyType.http:
type = 'PROXY';
break;
case ProxyType.socks:
case SentryProxyType.socks:
type = 'SOCKS';
break;
}
Expand All @@ -42,7 +42,7 @@ class SentryProxy {
SentryProxy copyWith({
String? host,
int? port,
ProxyType? type,
SentryProxyType? type,
String? user,
String? pass,
}) =>
Expand All @@ -55,7 +55,7 @@ class SentryProxy {
);
}

enum ProxyType {
enum SentryProxyType {
direct,
http,
socks;
Expand Down
8 changes: 4 additions & 4 deletions dart/test/http_client/io_client_provider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void main() {

test('http proxy should call findProxyResult', () async {
fixture.options.proxy = SentryProxy(
type: ProxyType.http,
type: SentryProxyType.http,
host: 'localhost',
port: 8080,
);
Expand All @@ -32,7 +32,7 @@ void main() {
});

test('direct proxy should call findProxyResult', () async {
fixture.options.proxy = SentryProxy(type: ProxyType.direct);
fixture.options.proxy = SentryProxy(type: SentryProxyType.direct);

final sut = fixture.getSut();
sut.getClient(fixture.options);
Expand All @@ -43,7 +43,7 @@ void main() {

test('socks proxy should not call findProxyResult', () async {
fixture.options.proxy =
SentryProxy(type: ProxyType.socks, host: 'localhost', port: 8080);
SentryProxy(type: SentryProxyType.socks, host: 'localhost', port: 8080);

final sut = fixture.getSut();
sut.getClient(fixture.options);
Expand All @@ -53,7 +53,7 @@ void main() {

test('authenticated proxy http should call addProxyCredentials', () async {
fixture.options.proxy = SentryProxy(
type: ProxyType.http,
type: SentryProxyType.http,
host: 'localhost',
port: 8080,
user: 'admin',
Expand Down
20 changes: 10 additions & 10 deletions dart/test/protocol/sentry_proxy_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ void main() {
final proxy = SentryProxy(
host: 'localhost',
port: 8080,
type: ProxyType.http,
type: SentryProxyType.http,
user: 'admin',
pass: '0000',
);
Expand All @@ -21,40 +21,40 @@ void main() {

group('toPacString', () {
test('returns "DIRECT" for ProxyType.direct', () {
SentryProxy proxy = SentryProxy(type: ProxyType.direct);
SentryProxy proxy = SentryProxy(type: SentryProxyType.direct);
expect(proxy.toPacString(), equals('DIRECT'));
});

test('returns "PROXY host:port" for ProxyType.http with host and port', () {
SentryProxy proxy =
SentryProxy(type: ProxyType.http, host: 'localhost', port: 8080);
SentryProxy(type: SentryProxyType.http, host: 'localhost', port: 8080);
expect(proxy.toPacString(), equals('PROXY localhost:8080'));
});

test('returns "PROXY host" for ProxyType.http with host only', () {
SentryProxy proxy = SentryProxy(type: ProxyType.http, host: 'localhost');
SentryProxy proxy = SentryProxy(type: SentryProxyType.http, host: 'localhost');
expect(proxy.toPacString(), equals('PROXY localhost'));
});

test('returns "SOCKS host:port" for ProxyType.socks with host and port',
() {
SentryProxy proxy =
SentryProxy(type: ProxyType.socks, host: 'localhost', port: 8080);
SentryProxy(type: SentryProxyType.socks, host: 'localhost', port: 8080);
expect(proxy.toPacString(), equals('SOCKS localhost:8080'));
});

test('returns "SOCKS host" for ProxyType.socks with host only', () {
SentryProxy proxy = SentryProxy(type: ProxyType.socks, host: 'localhost');
SentryProxy proxy = SentryProxy(type: SentryProxyType.socks, host: 'localhost');
expect(proxy.toPacString(), equals('SOCKS localhost'));
});

test('falls back to "DIRECT" if http is missing host', () {
SentryProxy proxy = SentryProxy(type: ProxyType.http);
SentryProxy proxy = SentryProxy(type: SentryProxyType.http);
expect(proxy.toPacString(), equals('DIRECT'));
});

test('falls back to "DIRECT" if socks is missing host', () {
SentryProxy proxy = SentryProxy(type: ProxyType.socks);
SentryProxy proxy = SentryProxy(type: SentryProxyType.socks);
expect(proxy.toPacString(), equals('DIRECT'));
});
});
Expand Down Expand Up @@ -85,14 +85,14 @@ void main() {
final copy = data.copyWith(
host: 'localhost-2',
port: 9001,
type: ProxyType.socks,
type: SentryProxyType.socks,
user: 'user',
pass: '1234',
);

expect('localhost-2', copy.host);
expect(9001, copy.port);
expect(ProxyType.socks, copy.type);
expect(SentryProxyType.socks, copy.type);
expect('user', copy.user);
expect('1234', copy.pass);
});
Expand Down
44 changes: 43 additions & 1 deletion flutter/example/ios/RunnerTests/SentryFlutterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ final class SentryFlutterTests: XCTestCase {
"maxAttachmentSize": NSNumber(value: 9004),
"captureFailedRequests": false,
"enableAppHangTracking": false,
"appHangTimeoutIntervalMillis": NSNumber(value: 10000)
"appHangTimeoutIntervalMillis": NSNumber(value: 10000),
"proxy": [
"host": "localhost",
"port": NSNumber(value: 8080),
"type": "hTtP", // mixed case to check enum mapping
"user": "admin",
"pass": "0000",
]
]
)

Expand All @@ -68,6 +75,41 @@ final class SentryFlutterTests: XCTestCase {
XCTAssertEqual(false, fixture.options.enableCaptureFailedRequests)
XCTAssertEqual(false, fixture.options.enableAppHangTracking)
XCTAssertEqual(10, fixture.options.appHangTimeoutInterval)

XCTAssertNotNil(fixture.options.urlSession)
XCTAssertEqual(true, fixture.options.urlSession?.configuration.connectionProxyDictionary?[kCFNetworkProxiesHTTPEnable as String] as? Bool)

Check failure on line 80 in flutter/example/ios/RunnerTests/SentryFlutterTests.swift

View workflow job for this annotation

GitHub Actions / swift-lint

Line Length Violation: Line should be 120 characters or less; currently it has 146 characters (line_length)
XCTAssertEqual("localhost", fixture.options.urlSession?.configuration.connectionProxyDictionary?[kCFNetworkProxiesHTTPProxy as String] as? String)

Check failure on line 81 in flutter/example/ios/RunnerTests/SentryFlutterTests.swift

View workflow job for this annotation

GitHub Actions / swift-lint

Line Length Violation: Line should be 120 characters or less; currently it has 154 characters (line_length)
XCTAssertEqual(8080, fixture.options.urlSession?.configuration.connectionProxyDictionary?[kCFNetworkProxiesHTTPPort as String] as? Int)

Check failure on line 82 in flutter/example/ios/RunnerTests/SentryFlutterTests.swift

View workflow job for this annotation

GitHub Actions / swift-lint

Line Length Violation: Line should be 120 characters or less; currently it has 143 characters (line_length)
XCTAssertEqual("admin", fixture.options.urlSession?.configuration.connectionProxyDictionary?[kCFProxyUsernameKey as String] as? String)

Check failure on line 83 in flutter/example/ios/RunnerTests/SentryFlutterTests.swift

View workflow job for this annotation

GitHub Actions / swift-lint

Line Length Violation: Line should be 120 characters or less; currently it has 143 characters (line_length)
XCTAssertEqual("0000", fixture.options.urlSession?.configuration.connectionProxyDictionary?[kCFProxyPasswordKey as String] as? String)

Check failure on line 84 in flutter/example/ios/RunnerTests/SentryFlutterTests.swift

View workflow job for this annotation

GitHub Actions / swift-lint

Line Length Violation: Line should be 120 characters or less; currently it has 142 characters (line_length)
}

func testUpdateSocksProxy() {
let sut = fixture.getSut()

sut.update(
options: fixture.options,
with: [
"proxy": [
"host": "localhost",
"port": 8080,
"type": "sOcKs", // mixed case to check enum mapping
"user": "admin",
"pass": "0000",
]
]
)

#if os(macOS)
XCTAssertNotNil(fixture.options.urlSession)
XCTAssertEqual(true, fixture.options.urlSession?.configuration.connectionProxyDictionary?[kCFNetworkProxiesSOCKSEnable as String] as? Bool)

Check failure on line 105 in flutter/example/ios/RunnerTests/SentryFlutterTests.swift

View workflow job for this annotation

GitHub Actions / swift-lint

Line Length Violation: Line should be 120 characters or less; currently it has 147 characters (line_length)
XCTAssertEqual("localhost", fixture.options.urlSession?.configuration.connectionProxyDictionary?[kCFNetworkProxiesSOCKSProxy as String] as? String)

Check failure on line 106 in flutter/example/ios/RunnerTests/SentryFlutterTests.swift

View workflow job for this annotation

GitHub Actions / swift-lint

Line Length Violation: Line should be 120 characters or less; currently it has 155 characters (line_length)
XCTAssertEqual(8080, fixture.options.urlSession?.configuration.connectionProxyDictionary?[kCFNetworkProxiesSOCKSPort as String] as? Int)

Check failure on line 107 in flutter/example/ios/RunnerTests/SentryFlutterTests.swift

View workflow job for this annotation

GitHub Actions / swift-lint

Line Length Violation: Line should be 120 characters or less; currently it has 144 characters (line_length)
XCTAssertEqual("admin", fixture.options.urlSession?.configuration.connectionProxyDictionary?[kCFProxyUsernameKey as String] as? String)

Check failure on line 108 in flutter/example/ios/RunnerTests/SentryFlutterTests.swift

View workflow job for this annotation

GitHub Actions / swift-lint

Line Length Violation: Line should be 120 characters or less; currently it has 143 characters (line_length)
XCTAssertEqual("0000", fixture.options.urlSession?.configuration.connectionProxyDictionary?[kCFProxyPasswordKey as String] as? String)
#else
XCTAssertNil(fixture.options.urlSession)
#endif
}
}

Expand Down
34 changes: 34 additions & 0 deletions flutter/ios/Classes/SentryFlutter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,40 @@ public final class SentryFlutter {
if let appHangTimeoutIntervalMillis = data["appHangTimeoutIntervalMillis"] as? NSNumber {
options.appHangTimeoutInterval = appHangTimeoutIntervalMillis.doubleValue / 1000
}
if let proxy = data["proxy"] as? [String: Any] {
guard let host = proxy["host"] as? String,
let port = proxy["port"] as? Int,
let type = proxy["type"] as? String else {
return
}

var connectionProxyDictionary: [String: Any] = [:]
if type.lowercased() == "http" {
connectionProxyDictionary[kCFNetworkProxiesHTTPEnable as String] = true
connectionProxyDictionary[kCFNetworkProxiesHTTPProxy as String] = host
connectionProxyDictionary[kCFNetworkProxiesHTTPPort as String] = port
} else if type.lowercased() == "socks" {
#if os(macOS)
connectionProxyDictionary[kCFNetworkProxiesSOCKSEnable as String] = true
connectionProxyDictionary[kCFNetworkProxiesSOCKSProxy as String] = host
connectionProxyDictionary[kCFNetworkProxiesSOCKSPort as String] = port
#else
return
#endif
} else {
return
}

if let user = proxy["user"] as? String, let pass = proxy["pass"] {
connectionProxyDictionary[kCFProxyUsernameKey as String] = user
connectionProxyDictionary[kCFProxyPasswordKey as String] = pass
}

let configuration = URLSessionConfiguration.default
configuration.connectionProxyDictionary = connectionProxyDictionary

options.urlSession = URLSession(configuration: configuration)
}
}

private func logLevelFrom(diagnosticLevel: String) -> SentryLevel {
Expand Down
2 changes: 1 addition & 1 deletion flutter/test/integrations/init_native_sdk_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void main() {
..proxy = SentryProxy(
host: "localhost",
port: 8080,
type: ProxyType.http,
type: SentryProxyType.http,
user: 'admin',
pass: '0000',
);
Expand Down

0 comments on commit 67c474f

Please sign in to comment.