Proxy.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Error.h>
  8. #include <AK/IPv4Address.h>
  9. #include <AK/Types.h>
  10. #include <AK/URL.h>
  11. #include <LibIPC/Forward.h>
  12. namespace Core {
  13. // FIXME: Username/password support.
  14. struct ProxyData {
  15. enum Type {
  16. Direct,
  17. SOCKS5,
  18. } type { Type::Direct };
  19. u32 host_ipv4 { 0 };
  20. int port { 0 };
  21. bool operator==(ProxyData const& other) const = default;
  22. static ErrorOr<ProxyData> parse_url(URL const& url)
  23. {
  24. if (!url.is_valid())
  25. return Error::from_string_literal("Invalid proxy URL");
  26. ProxyData proxy_data;
  27. if (url.scheme() != "socks5")
  28. return Error::from_string_literal("Unsupported proxy type");
  29. proxy_data.type = ProxyData::Type::SOCKS5;
  30. auto host_ipv4 = IPv4Address::from_string(url.host());
  31. if (!host_ipv4.has_value())
  32. return Error::from_string_literal("Invalid proxy host, must be an IPv4 address");
  33. proxy_data.host_ipv4 = host_ipv4->to_u32();
  34. auto port = url.port();
  35. if (!port.has_value())
  36. return Error::from_string_literal("Invalid proxy, must have a port");
  37. proxy_data.port = *port;
  38. return proxy_data;
  39. }
  40. };
  41. }
  42. namespace IPC {
  43. template<>
  44. ErrorOr<void> encode(Encoder&, Core::ProxyData const&);
  45. template<>
  46. ErrorOr<Core::ProxyData> decode(Decoder&);
  47. }