Skip to content

Commit 67c474f

Browse files
committed
configure url session on ios
1 parent 16c6795 commit 67c474f

File tree

7 files changed

+99
-23
lines changed

7 files changed

+99
-23
lines changed

dart/lib/src/http_client/io_client_provider.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class IoClientProvider implements ClientProvider {
3535
return Client();
3636
}
3737
final pac = proxy.toPacString();
38-
if (proxy.type == ProxyType.socks) {
38+
if (proxy.type == SentryProxyType.socks) {
3939
options.logger(
4040
SentryLevel.warning,
4141
"Setting proxy '$pac' is not supported.",

dart/lib/src/protocol/sentry_proxy.dart

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class SentryProxy {
2-
final ProxyType type;
2+
final SentryProxyType type;
33
final String? host;
44
final int? port;
55
final String? user;
@@ -10,12 +10,12 @@ class SentryProxy {
1010
String toPacString() {
1111
String type = 'DIRECT';
1212
switch (this.type) {
13-
case ProxyType.direct:
13+
case SentryProxyType.direct:
1414
return 'DIRECT';
15-
case ProxyType.http:
15+
case SentryProxyType.http:
1616
type = 'PROXY';
1717
break;
18-
case ProxyType.socks:
18+
case SentryProxyType.socks:
1919
type = 'SOCKS';
2020
break;
2121
}
@@ -42,7 +42,7 @@ class SentryProxy {
4242
SentryProxy copyWith({
4343
String? host,
4444
int? port,
45-
ProxyType? type,
45+
SentryProxyType? type,
4646
String? user,
4747
String? pass,
4848
}) =>
@@ -55,7 +55,7 @@ class SentryProxy {
5555
);
5656
}
5757

58-
enum ProxyType {
58+
enum SentryProxyType {
5959
direct,
6060
http,
6161
socks;

dart/test/http_client/io_client_provider_test.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void main() {
1919

2020
test('http proxy should call findProxyResult', () async {
2121
fixture.options.proxy = SentryProxy(
22-
type: ProxyType.http,
22+
type: SentryProxyType.http,
2323
host: 'localhost',
2424
port: 8080,
2525
);
@@ -32,7 +32,7 @@ void main() {
3232
});
3333

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

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

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

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

5454
test('authenticated proxy http should call addProxyCredentials', () async {
5555
fixture.options.proxy = SentryProxy(
56-
type: ProxyType.http,
56+
type: SentryProxyType.http,
5757
host: 'localhost',
5858
port: 8080,
5959
user: 'admin',

dart/test/protocol/sentry_proxy_test.dart

+10-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ void main() {
66
final proxy = SentryProxy(
77
host: 'localhost',
88
port: 8080,
9-
type: ProxyType.http,
9+
type: SentryProxyType.http,
1010
user: 'admin',
1111
pass: '0000',
1212
);
@@ -21,40 +21,40 @@ void main() {
2121

2222
group('toPacString', () {
2323
test('returns "DIRECT" for ProxyType.direct', () {
24-
SentryProxy proxy = SentryProxy(type: ProxyType.direct);
24+
SentryProxy proxy = SentryProxy(type: SentryProxyType.direct);
2525
expect(proxy.toPacString(), equals('DIRECT'));
2626
});
2727

2828
test('returns "PROXY host:port" for ProxyType.http with host and port', () {
2929
SentryProxy proxy =
30-
SentryProxy(type: ProxyType.http, host: 'localhost', port: 8080);
30+
SentryProxy(type: SentryProxyType.http, host: 'localhost', port: 8080);
3131
expect(proxy.toPacString(), equals('PROXY localhost:8080'));
3232
});
3333

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

3939
test('returns "SOCKS host:port" for ProxyType.socks with host and port',
4040
() {
4141
SentryProxy proxy =
42-
SentryProxy(type: ProxyType.socks, host: 'localhost', port: 8080);
42+
SentryProxy(type: SentryProxyType.socks, host: 'localhost', port: 8080);
4343
expect(proxy.toPacString(), equals('SOCKS localhost:8080'));
4444
});
4545

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

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

5656
test('falls back to "DIRECT" if socks is missing host', () {
57-
SentryProxy proxy = SentryProxy(type: ProxyType.socks);
57+
SentryProxy proxy = SentryProxy(type: SentryProxyType.socks);
5858
expect(proxy.toPacString(), equals('DIRECT'));
5959
});
6060
});
@@ -85,14 +85,14 @@ void main() {
8585
final copy = data.copyWith(
8686
host: 'localhost-2',
8787
port: 9001,
88-
type: ProxyType.socks,
88+
type: SentryProxyType.socks,
8989
user: 'user',
9090
pass: '1234',
9191
);
9292

9393
expect('localhost-2', copy.host);
9494
expect(9001, copy.port);
95-
expect(ProxyType.socks, copy.type);
95+
expect(SentryProxyType.socks, copy.type);
9696
expect('user', copy.user);
9797
expect('1234', copy.pass);
9898
});

flutter/example/ios/RunnerTests/SentryFlutterTests.swift

+43-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@ final class SentryFlutterTests: XCTestCase {
4343
"maxAttachmentSize": NSNumber(value: 9004),
4444
"captureFailedRequests": false,
4545
"enableAppHangTracking": false,
46-
"appHangTimeoutIntervalMillis": NSNumber(value: 10000)
46+
"appHangTimeoutIntervalMillis": NSNumber(value: 10000),
47+
"proxy": [
48+
"host": "localhost",
49+
"port": NSNumber(value: 8080),
50+
"type": "hTtP", // mixed case to check enum mapping
51+
"user": "admin",
52+
"pass": "0000",
53+
]
4754
]
4855
)
4956

@@ -68,6 +75,41 @@ final class SentryFlutterTests: XCTestCase {
6875
XCTAssertEqual(false, fixture.options.enableCaptureFailedRequests)
6976
XCTAssertEqual(false, fixture.options.enableAppHangTracking)
7077
XCTAssertEqual(10, fixture.options.appHangTimeoutInterval)
78+
79+
XCTAssertNotNil(fixture.options.urlSession)
80+
XCTAssertEqual(true, fixture.options.urlSession?.configuration.connectionProxyDictionary?[kCFNetworkProxiesHTTPEnable as String] as? Bool)
81+
XCTAssertEqual("localhost", fixture.options.urlSession?.configuration.connectionProxyDictionary?[kCFNetworkProxiesHTTPProxy as String] as? String)
82+
XCTAssertEqual(8080, fixture.options.urlSession?.configuration.connectionProxyDictionary?[kCFNetworkProxiesHTTPPort as String] as? Int)
83+
XCTAssertEqual("admin", fixture.options.urlSession?.configuration.connectionProxyDictionary?[kCFProxyUsernameKey as String] as? String)
84+
XCTAssertEqual("0000", fixture.options.urlSession?.configuration.connectionProxyDictionary?[kCFProxyPasswordKey as String] as? String)
85+
}
86+
87+
func testUpdateSocksProxy() {
88+
let sut = fixture.getSut()
89+
90+
sut.update(
91+
options: fixture.options,
92+
with: [
93+
"proxy": [
94+
"host": "localhost",
95+
"port": 8080,
96+
"type": "sOcKs", // mixed case to check enum mapping
97+
"user": "admin",
98+
"pass": "0000",
99+
]
100+
]
101+
)
102+
103+
#if os(macOS)
104+
XCTAssertNotNil(fixture.options.urlSession)
105+
XCTAssertEqual(true, fixture.options.urlSession?.configuration.connectionProxyDictionary?[kCFNetworkProxiesSOCKSEnable as String] as? Bool)
106+
XCTAssertEqual("localhost", fixture.options.urlSession?.configuration.connectionProxyDictionary?[kCFNetworkProxiesSOCKSProxy as String] as? String)
107+
XCTAssertEqual(8080, fixture.options.urlSession?.configuration.connectionProxyDictionary?[kCFNetworkProxiesSOCKSPort as String] as? Int)
108+
XCTAssertEqual("admin", fixture.options.urlSession?.configuration.connectionProxyDictionary?[kCFProxyUsernameKey as String] as? String)
109+
XCTAssertEqual("0000", fixture.options.urlSession?.configuration.connectionProxyDictionary?[kCFProxyPasswordKey as String] as? String)
110+
#else
111+
XCTAssertNil(fixture.options.urlSession)
112+
#endif
71113
}
72114
}
73115

flutter/ios/Classes/SentryFlutter.swift

+34
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,40 @@ public final class SentryFlutter {
7070
if let appHangTimeoutIntervalMillis = data["appHangTimeoutIntervalMillis"] as? NSNumber {
7171
options.appHangTimeoutInterval = appHangTimeoutIntervalMillis.doubleValue / 1000
7272
}
73+
if let proxy = data["proxy"] as? [String: Any] {
74+
guard let host = proxy["host"] as? String,
75+
let port = proxy["port"] as? Int,
76+
let type = proxy["type"] as? String else {
77+
return
78+
}
79+
80+
var connectionProxyDictionary: [String: Any] = [:]
81+
if type.lowercased() == "http" {
82+
connectionProxyDictionary[kCFNetworkProxiesHTTPEnable as String] = true
83+
connectionProxyDictionary[kCFNetworkProxiesHTTPProxy as String] = host
84+
connectionProxyDictionary[kCFNetworkProxiesHTTPPort as String] = port
85+
} else if type.lowercased() == "socks" {
86+
#if os(macOS)
87+
connectionProxyDictionary[kCFNetworkProxiesSOCKSEnable as String] = true
88+
connectionProxyDictionary[kCFNetworkProxiesSOCKSProxy as String] = host
89+
connectionProxyDictionary[kCFNetworkProxiesSOCKSPort as String] = port
90+
#else
91+
return
92+
#endif
93+
} else {
94+
return
95+
}
96+
97+
if let user = proxy["user"] as? String, let pass = proxy["pass"] {
98+
connectionProxyDictionary[kCFProxyUsernameKey as String] = user
99+
connectionProxyDictionary[kCFProxyPasswordKey as String] = pass
100+
}
101+
102+
let configuration = URLSessionConfiguration.default
103+
configuration.connectionProxyDictionary = connectionProxyDictionary
104+
105+
options.urlSession = URLSession(configuration: configuration)
106+
}
73107
}
74108

75109
private func logLevelFrom(diagnosticLevel: String) -> SentryLevel {

flutter/test/integrations/init_native_sdk_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void main() {
108108
..proxy = SentryProxy(
109109
host: "localhost",
110110
port: 8080,
111-
type: ProxyType.http,
111+
type: SentryProxyType.http,
112112
user: 'admin',
113113
pass: '0000',
114114
);

0 commit comments

Comments
 (0)