summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCore/Proxy.h
blob: a427569dd139439420d8a523d48bd74823c9422c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
 * Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Error.h>
#include <AK/IPv4Address.h>
#include <AK/Types.h>
#include <AK/URL.h>
#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;

    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;
    }
};
}

namespace IPC {

template<>
ErrorOr<void> encode(Encoder&, Core::ProxyData const&);

template<>
ErrorOr<Core::ProxyData> decode(Decoder&);

}