|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:http/http.dart'; |
| 4 | +import 'package:http/io_client.dart'; |
| 5 | +import 'package:meta/meta.dart'; |
| 6 | + |
| 7 | +import '../protocol.dart'; |
| 8 | +import '../protocol/sentry_proxy.dart'; |
| 9 | +import '../sentry_options.dart'; |
| 10 | +import 'client_provider.dart'; |
| 11 | + |
| 12 | +@internal |
| 13 | +ClientProvider getClientProvider() { |
| 14 | + return IoClientProvider( |
| 15 | + () { |
| 16 | + return HttpClient(); |
| 17 | + }, |
| 18 | + (user, pass) { |
| 19 | + return HttpClientBasicCredentials(user, pass); |
| 20 | + }, |
| 21 | + ); |
| 22 | +} |
| 23 | + |
| 24 | +@internal |
| 25 | +class IoClientProvider implements ClientProvider { |
| 26 | + final HttpClient Function() _httpClient; |
| 27 | + final HttpClientCredentials Function(String, String) _httpClientCredentials; |
| 28 | + |
| 29 | + IoClientProvider(this._httpClient, this._httpClientCredentials); |
| 30 | + |
| 31 | + @override |
| 32 | + Client getClient(SentryOptions options) { |
| 33 | + final proxy = options.proxy; |
| 34 | + if (proxy == null) { |
| 35 | + return Client(); |
| 36 | + } |
| 37 | + final pac = proxy.toPacString(); |
| 38 | + if (proxy.type == SentryProxyType.socks) { |
| 39 | + options.logger( |
| 40 | + SentryLevel.warning, |
| 41 | + "Setting proxy '$pac' is not supported.", |
| 42 | + ); |
| 43 | + return Client(); |
| 44 | + } |
| 45 | + options.logger( |
| 46 | + SentryLevel.info, |
| 47 | + "Setting proxy '$pac'", |
| 48 | + ); |
| 49 | + final httpClient = _httpClient(); |
| 50 | + httpClient.findProxy = (url) => pac; |
| 51 | + |
| 52 | + final host = proxy.host; |
| 53 | + final port = proxy.port; |
| 54 | + final user = proxy.user; |
| 55 | + final pass = proxy.pass; |
| 56 | + |
| 57 | + if (host != null && port != null && user != null && pass != null) { |
| 58 | + httpClient.addProxyCredentials( |
| 59 | + host, |
| 60 | + port, |
| 61 | + '', |
| 62 | + _httpClientCredentials(user, pass), |
| 63 | + ); |
| 64 | + } |
| 65 | + return IOClient(httpClient); |
| 66 | + } |
| 67 | +} |
0 commit comments