Skip to content

Commit 2085f48

Browse files
committed
chore: final implementation of sending the request through proxy server
1 parent 107a17c commit 2085f48

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

lib/providers/collection_providers.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,11 @@ class CollectionStateNotifier
295295
substitutedHttpRequestModel,
296296
defaultUriScheme: defaultUriScheme,
297297
noSSL: noSSL,
298+
isProxyEnabled: ref.read(settingsProvider).isProxyEnabled,
299+
proxyHost: ref.read(settingsProvider).proxyHost,
300+
proxyPort: ref.read(settingsProvider).proxyPort,
301+
proxyUsername: ref.read(settingsProvider).proxyUsername,
302+
proxyPassword: ref.read(settingsProvider).proxyPassword,
298303
);
299304

300305
late final RequestModel newRequestModel;

lib/providers/settings_providers.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class ThemeStateNotifier extends StateNotifier<SettingsModel> {
3838
String? proxyPort,
3939
String? proxyUsername,
4040
String? proxyPassword,
41+
bool? useSystemProxy,
42+
String? proxyBypassRules,
4143
}) async {
4244
state = SettingsModel(
4345
isDark: isDark ?? state.isDark,

packages/apidash_core/lib/services/http_client_manager.dart

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,36 @@ http.Client createHttpClientWithNoSSL() {
1111
return IOClient(ioClient);
1212
}
1313

14+
http.Client createHttpClientWithProxy(
15+
String proxyHost,
16+
String proxyPort,
17+
{String? proxyUsername,
18+
String? proxyPassword,
19+
bool noSSL = false}
20+
) {
21+
var ioClient = HttpClient();
22+
23+
// Configure proxy settings
24+
ioClient.findProxy = (uri) {
25+
return 'PROXY $proxyHost:$proxyPort';
26+
};
27+
28+
// Configure proxy authentication if credentials are provided
29+
if (proxyUsername != null && proxyPassword != null) {
30+
ioClient.authenticate = (Uri url, String scheme, String? realm) async {
31+
return true;
32+
};
33+
}
34+
35+
// Disable SSL verification if required
36+
if (noSSL) {
37+
ioClient.badCertificateCallback =
38+
(X509Certificate cert, String host, int port) => true;
39+
}
40+
41+
return IOClient(ioClient);
42+
}
43+
1444
class HttpClientManager {
1545
static final HttpClientManager _instance = HttpClientManager._internal();
1646
static const int _maxCancelledRequests = 100;
@@ -26,9 +56,23 @@ class HttpClientManager {
2656
http.Client createClient(
2757
String requestId, {
2858
bool noSSL = false,
59+
String? proxyHost,
60+
String? proxyPort,
61+
String? proxyUsername,
62+
String? proxyPassword,
2963
}) {
30-
final client =
31-
(noSSL && !kIsWeb) ? createHttpClientWithNoSSL() : http.Client();
64+
final client = proxyHost != null && proxyPort != null
65+
? createHttpClientWithProxy(
66+
proxyHost,
67+
proxyPort,
68+
proxyUsername: proxyUsername,
69+
proxyPassword: proxyPassword,
70+
noSSL: noSSL
71+
)
72+
: (noSSL && !kIsWeb)
73+
? createHttpClientWithNoSSL()
74+
: http.Client();
75+
3276
_clients[requestId] = client;
3377
return client;
3478
}

packages/apidash_core/lib/services/http_service.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,21 @@ Future<(HttpResponse?, Duration?, String?)> sendHttpRequest(
1818
HttpRequestModel requestModel, {
1919
SupportedUriSchemes defaultUriScheme = kDefaultUriScheme,
2020
bool noSSL = false,
21+
bool? isProxyEnabled,
22+
String? proxyHost,
23+
String? proxyPort,
24+
String? proxyUsername,
25+
String? proxyPassword,
2126
}) async {
22-
final client = httpClientManager.createClient(requestId, noSSL: noSSL);
27+
final clientManager = HttpClientManager();
28+
final client = clientManager.createClient(
29+
requestId,
30+
noSSL: noSSL,
31+
proxyHost: isProxyEnabled == true ? proxyHost : null,
32+
proxyPort: isProxyEnabled == true ? proxyPort : null,
33+
proxyUsername: isProxyEnabled == true ? proxyUsername : null,
34+
proxyPassword: isProxyEnabled == true ? proxyPassword : null,
35+
);
2336

2437
(Uri?, String?) uriRec = getValidRequestUri(
2538
requestModel.url,

0 commit comments

Comments
 (0)