mirror of
https://github.com/RGBCube/serenity
synced 2025-05-20 13:35:07 +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:
parent
45867435c4
commit
f9fc28931f
2 changed files with 32 additions and 2 deletions
|
@ -7,7 +7,9 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/Error.h>
|
#include <AK/Error.h>
|
||||||
|
#include <AK/IPv4Address.h>
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
|
#include <AK/URL.h>
|
||||||
#include <LibIPC/Forward.h>
|
#include <LibIPC/Forward.h>
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
@ -22,6 +24,30 @@ struct ProxyData {
|
||||||
int port { 0 };
|
int port { 0 };
|
||||||
|
|
||||||
bool operator==(ProxyData const& other) const = default;
|
bool operator==(ProxyData const& other) const = default;
|
||||||
|
|
||||||
|
static ErrorOr<ProxyData> parse_url(URL const& url)
|
||||||
|
{
|
||||||
|
if (!url.is_valid())
|
||||||
|
return Error::from_string_literal("Invalid proxy URL");
|
||||||
|
|
||||||
|
ProxyData proxy_data;
|
||||||
|
if (url.scheme() != "socks5")
|
||||||
|
return Error::from_string_literal("Unsupported proxy type");
|
||||||
|
|
||||||
|
proxy_data.type = ProxyData::Type::SOCKS5;
|
||||||
|
|
||||||
|
auto host_ipv4 = IPv4Address::from_string(url.host());
|
||||||
|
if (!host_ipv4.has_value())
|
||||||
|
return Error::from_string_literal("Invalid proxy host, must be an IPv4 address");
|
||||||
|
proxy_data.host_ipv4 = host_ipv4->to_u32();
|
||||||
|
|
||||||
|
auto port = url.port();
|
||||||
|
if (!port.has_value())
|
||||||
|
return Error::from_string_literal("Invalid proxy, must have a port");
|
||||||
|
proxy_data.port = *port;
|
||||||
|
|
||||||
|
return proxy_data;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -151,6 +151,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
bool save_at_provided_name = false;
|
bool save_at_provided_name = false;
|
||||||
bool should_follow_url = false;
|
bool should_follow_url = false;
|
||||||
char const* data = nullptr;
|
char const* data = nullptr;
|
||||||
|
StringView proxy_spec;
|
||||||
String method = "GET";
|
String method = "GET";
|
||||||
HashMap<String, String, CaseInsensitiveStringTraits> request_headers;
|
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));
|
request_headers.set(header.substring_view(0, split.value()), header.substring_view(split.value() + 1));
|
||||||
return true;
|
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.add_positional_argument(url_str, "URL to download from", "url");
|
||||||
args_parser.parse(arguments);
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
|
@ -189,6 +191,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Core::ProxyData proxy_data = TRY(Core::ProxyData::parse_url(proxy_spec));
|
||||||
|
|
||||||
Core::EventLoop loop;
|
Core::EventLoop loop;
|
||||||
bool received_actual_headers = false;
|
bool received_actual_headers = false;
|
||||||
bool should_save_stream_data = 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()] {
|
Core::deferred_invoke([&, was_following_url, url = location.value()] {
|
||||||
warnln("{}Following to {}", was_following_url ? "" : "\n", url);
|
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();
|
setup_request();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -309,7 +313,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
request->stream_into(output_stream);
|
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();
|
setup_request();
|
||||||
|
|
||||||
dbgln("started request with id {}", request->id());
|
dbgln("started request with id {}", request->id());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue