Proxys in Dart/Flutter verwenden
Proxys in Dart/Flutter
Dart ist eine Programmiersprache für Flutter-Anwendungen. Die Arbeit mit Proxys in Dart erfolgt über den Standard-HttpClient und populäre Bibliotheken (dio, http).
Standard-HttpClient
HTTP-Proxy
import 'dart:io';
void main() async {
final client = HttpClient();
// Proxy configuration
client.findProxy = (uri) {
return "PROXY proxy_ip:8080";
};
final request = await client.getUrl(
Uri.parse("https://httpbin.org/ip")
);
final response = await request.close();
await response.transform(utf8.decoder).forEach(print);
client.close();
}
Mit Authentifizierung
import 'dart:io';
void main() async {
final client = HttpClient();
client.findProxy = (uri) {
return "PROXY proxy_ip:8080";
};
// Handling proxy authentication
client.authenticate = (uri, scheme, realm) async {
client.addProxyCredentials(
"proxy_ip",
8080,
"realm",
HttpClientBasicCredentials("username", "password")
);
return true;
};
final request = await client.getUrl(
Uri.parse("https://httpbin.org/ip")
);
final response = await request.close();
await response.transform(utf8.decoder).forEach(print);
client.close();
}
Direkte Verbindung (Kein Proxy)
client.findProxy = (uri) => "DIRECT";
Bedingtes Routing
client.findProxy = (uri) {
if (uri.host.contains("target-site.com")) {
return "PROXY proxy_ip:8080";
}
return "DIRECT";
};
Dio-Bibliothek
Dio ist die beliebteste HTTP-Bibliothek für Flutter.
Installation
# pubspec.yaml
dependencies:
dio: ^5.4.0
dio_proxy_adapter: ^0.0.2 # for proxies
HTTP-Proxy mit Dio
import 'package:dio/dio.dart';
import 'dart:io';
void main() async {
final dio = Dio();
// Proxy configuration via HttpClient adapter
(dio.httpClientAdapter as IOHttpClientAdapter).createHttpClient = () {
final client = HttpClient();
client.findProxy = (uri) {
return "PROXY proxy_ip:8080";
};
// For self-signed certificates (testing)
client.badCertificateCallback = (cert, host, port) => true;
return client;
};
final response = await dio.get("https://httpbin.org/ip");
print(response.data);
}
Mit Authentifizierung
(dio.httpClientAdapter as IOHttpClientAdapter).createHttpClient = () {
final client = HttpClient();
client.findProxy = (uri) => "PROXY proxy_ip:8080";
client.addProxyCredentials(
"proxy_ip",
8080,
"",
HttpClientBasicCredentials("user", "pass")
);
return client;
};
SOCKS5-Proxy
Via socks5_proxy
# pubspec.yaml
dependencies:
socks5_proxy: ^1.0.4
import 'dart:io';
import 'package:socks5_proxy/socks_client.dart';
void main() async {
// Creating a SOCKS5 client
final proxy = SocksTCPClient.connect(
[ProxySettings(InternetAddress("proxy_ip"), 1080,
password: Password("username", "password"))],
InternetAddress("93.184.216.34"), // target IP
80,
);
// Use the socket for HTTP requests
}
Via System-Proxy mit SOCKS
client.findProxy = (uri) {
return "SOCKS5 proxy_ip:1080";
};
Hinweis: Der Standard-HttpClient in Dart unterstützt nur PROXY (HTTP) und DIRECT. Für SOCKS sind Bibliotheken von Drittanbietern erforderlich.
Flutter-Besonderheiten
Android-Konfiguration
Android erfordert eine network_security_config.xml-Einrichtung, um mit Klartext-HTTP-Proxys zu arbeiten:
<!-- android/app/src/main/res/xml/network_security_config.xml -->
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
iOS-Konfiguration
Unter iOS funktionieren Proxys über die URLSession-Systemeinstellungen. Eine Flutter-Anwendung verwendet automatisch den System-Proxy.
Umgebungsvariablen
Im Debug-Modus können Proxys über Variablen gesetzt werden:
// Via launch arguments
// flutter run --dart-define=HTTP_PROXY=http://proxy_ip:8080
Proxy-Rotation
import 'dart:io';
import 'dart:math';
final proxies = [
"PROXY proxy1:8080",
"PROXY proxy2:8080",
"PROXY proxy3:8080",
];
final random = Random();
void main() async {
final client = HttpClient();
client.findProxy = (uri) {
return proxies[random.nextInt(proxies.length)];
};
// Requests will use different proxies
for (var i = 0; i < 10; i++) {
final request = await client.getUrl(
Uri.parse("https://httpbin.org/ip")
);
final response = await request.close();
final body = await response.transform(utf8.decoder).join();
print("Request $i: $body");
}
client.close();
}
Fehlerbehandlung
try {
final request = await client.getUrl(uri);
final response = await request.close();
// handle response
} on SocketException catch (e) {
print("Connection error: $e");
} on HttpException catch (e) {
print("HTTP error: $e");
} on HandshakeException catch (e) {
print("TLS/SSL error: $e");
} catch (e) {
print("Unexpected error: $e");
}
Fazit
Dart und Flutter bieten grundlegende, aber ausreichende Tools für die Arbeit mit HTTP-Proxys. Für fortgeschrittene Szenarien (SOCKS5, Authentifizierung) verwenden Sie Dio mit einem benutzerdefinierten HttpClient-Adapter. Berücksichtigen Sie bei der Entwicklung von Flutter-Anwendungen die plattformspezifischen Besonderheiten von Android und iOS.