Proxy.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. if (!url.host().has<URL::IPv4Address>())
  31. return Error::from_string_literal("Invalid proxy host, must be an IPv4 address");
  32. proxy_data.host_ipv4 = url.host().get<URL::IPv4Address>();
  33. auto port = url.port();
  34. if (!port.has_value())
  35. return Error::from_string_literal("Invalid proxy, must have a port");
  36. proxy_data.port = *port;
  37. return proxy_data;
  38. }
  39. };
  40. }
  41. namespace IPC {
  42. template<>
  43. ErrorOr<void> encode(Encoder&, Core::ProxyData const&);
  44. template<>
  45. ErrorOr<Core::ProxyData> decode(Decoder&);
  46. }