1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 08:34:57 +00:00

pro: Accept an optional proxy to tunnel the download through

For now, this only accepts the format `socks5://ip:port` (i.e. the
hostname has to be an ipv4, and the port must be present).
This commit is contained in:
Ali Mohammad Pur 2022-04-07 21:11:20 +04:30 committed by Andreas Kling
parent 45867435c4
commit f9fc28931f
2 changed files with 32 additions and 2 deletions

View file

@ -151,6 +151,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
bool save_at_provided_name = false;
bool should_follow_url = false;
char const* data = nullptr;
StringView proxy_spec;
String method = "GET";
HashMap<String, String, CaseInsensitiveStringTraits> request_headers;
@ -175,6 +176,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
request_headers.set(header.substring_view(0, split.value()), header.substring_view(split.value() + 1));
return true;
} });
args_parser.add_option(proxy_spec, "Specify a proxy server to use for this request (proto://ip:port)", "proxy", 'p', "proxy");
args_parser.add_positional_argument(url_str, "URL to download from", "url");
args_parser.parse(arguments);
@ -189,6 +191,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
return 1;
}
Core::ProxyData proxy_data = TRY(Core::ProxyData::parse_url(proxy_spec));
Core::EventLoop loop;
bool received_actual_headers = false;
bool should_save_stream_data = false;
@ -287,7 +291,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
Core::deferred_invoke([&, was_following_url, url = location.value()] {
warnln("{}Following to {}", was_following_url ? "" : "\n", url);
request = protocol_client->start_request(method, url, request_headers, ReadonlyBytes {});
request = protocol_client->start_request(method, url, request_headers, ReadonlyBytes {}, proxy_data);
setup_request();
});
}
@ -309,7 +313,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
request->stream_into(output_stream);
};
request = protocol_client->start_request(method, url, request_headers, data ? StringView { data }.bytes() : ReadonlyBytes {});
request = protocol_client->start_request(method, url, request_headers, data ? StringView { data }.bytes() : ReadonlyBytes {}, proxy_data);
setup_request();
dbgln("started request with id {}", request->id());