2022-04-06 02:44:18 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Error.h>
|
2022-04-07 19:41:20 +03:00
|
|
|
#include <AK/IPv4Address.h>
|
2022-04-06 02:44:18 +03:00
|
|
|
#include <AK/Types.h>
|
2022-04-07 19:41:20 +03:00
|
|
|
#include <AK/URL.h>
|
2022-04-06 02:44:18 +03:00
|
|
|
#include <LibIPC/Forward.h>
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
// FIXME: Username/password support.
|
|
|
|
struct ProxyData {
|
|
|
|
enum Type {
|
|
|
|
Direct,
|
|
|
|
SOCKS5,
|
|
|
|
} type { Type::Direct };
|
|
|
|
|
|
|
|
u32 host_ipv4 { 0 };
|
|
|
|
int port { 0 };
|
|
|
|
|
|
|
|
bool operator==(ProxyData const& other) const = default;
|
2022-04-07 19:41:20 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2023-07-27 12:40:41 +03:00
|
|
|
if (!url.host().has<URL::IPv4Address>())
|
2022-04-07 19:41:20 +03:00
|
|
|
return Error::from_string_literal("Invalid proxy host, must be an IPv4 address");
|
2023-07-27 12:40:41 +03:00
|
|
|
proxy_data.host_ipv4 = url.host().get<URL::IPv4Address>();
|
2022-04-07 19:41:20 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-04-06 02:44:18 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace IPC {
|
2022-11-15 19:24:59 +03:00
|
|
|
|
|
|
|
template<>
|
2023-01-02 07:37:35 +03:00
|
|
|
ErrorOr<void> encode(Encoder&, Core::ProxyData const&);
|
2022-11-15 19:24:59 +03:00
|
|
|
|
|
|
|
template<>
|
2022-12-23 04:40:33 +03:00
|
|
|
ErrorOr<Core::ProxyData> decode(Decoder&);
|
2022-11-15 19:24:59 +03:00
|
|
|
|
2022-04-06 02:44:18 +03:00
|
|
|
}
|