I'm trying to add a local proxy server in android mobile app (using Dart/Flutter) to modify HTTP response. This works fine for HTTP requests, but not for HTTPS.
For any HTTPS object, it sends with CONNECT method and I'm not sure how I can handle this. I have this code so far, but not even sure whether I'm doing it right. My goal is to get the original image from the response and validate the image content and block if invalid. Any suggestions or advice would be appreciated!
var proxyServer = await HttpServer.bind(addr, port);
proxyServer.listen((HttpRequest request) async {
if (request.method == 'CONNECT') {
int port = 443;
String host = request.headers.host!;
LogService.log('proxy server will try connecting to $host $port');
var client = HttpClient();
client.connectionTimeout = const Duration(seconds: 5);
client
.open(request.method, host, port, '')
.then((HttpClientRequest clientRequest) async {
var socket = await request.response.detachSocket();
socket.listen((data) {
LogService.log('proxy - listening on socket $data');
// TODO: Parse the data as an HTTP response
});
// Close the connection when either side closes
clientRequest.done.then((_) {
LogService.log('proxy - clientRequest is done');
socket.close();
});
socket.done.then((_) {
LogService.log('proxy - socket is done');
clientRequest.close();
});
});
} else {
...
}
});