SOCKSProxyClient.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/OwnPtr.h>
  8. #include <LibCore/Proxy.h>
  9. #include <LibCore/Socket.h>
  10. namespace Core {
  11. class SOCKSProxyClient final : public Socket {
  12. public:
  13. enum class Version : u8 {
  14. V4 = 0x04,
  15. V5 = 0x05,
  16. };
  17. struct UsernamePasswordAuthenticationData {
  18. ByteString username;
  19. ByteString password;
  20. };
  21. enum class Command : u8 {
  22. Connect = 0x01,
  23. Bind = 0x02,
  24. UDPAssociate = 0x03,
  25. };
  26. using HostOrIPV4 = Variant<ByteString, u32>;
  27. static ErrorOr<NonnullOwnPtr<SOCKSProxyClient>> connect(Socket& underlying, Version, HostOrIPV4 const& target, int target_port, Variant<UsernamePasswordAuthenticationData, Empty> const& auth_data = {}, Command = Command::Connect);
  28. static ErrorOr<NonnullOwnPtr<SOCKSProxyClient>> connect(HostOrIPV4 const& server, int server_port, Version, HostOrIPV4 const& target, int target_port, Variant<UsernamePasswordAuthenticationData, Empty> const& auth_data = {}, Command = Command::Connect);
  29. virtual ~SOCKSProxyClient() override;
  30. // ^Stream::Stream
  31. virtual ErrorOr<Bytes> read_some(Bytes bytes) override { return m_socket.read_some(bytes); }
  32. virtual ErrorOr<size_t> write_some(ReadonlyBytes bytes) override { return m_socket.write_some(bytes); }
  33. virtual bool is_eof() const override { return m_socket.is_eof(); }
  34. virtual bool is_open() const override { return m_socket.is_open(); }
  35. virtual void close() override { m_socket.close(); }
  36. // ^Stream::Socket
  37. virtual ErrorOr<size_t> pending_bytes() const override { return m_socket.pending_bytes(); }
  38. virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_socket.can_read_without_blocking(timeout); }
  39. virtual ErrorOr<void> set_blocking(bool enabled) override { return m_socket.set_blocking(enabled); }
  40. virtual ErrorOr<void> set_close_on_exec(bool enabled) override { return m_socket.set_close_on_exec(enabled); }
  41. virtual void set_notifications_enabled(bool enabled) override { m_socket.set_notifications_enabled(enabled); }
  42. private:
  43. SOCKSProxyClient(Socket& socket, OwnPtr<Socket> own_socket)
  44. : m_socket(socket)
  45. , m_own_underlying_socket(move(own_socket))
  46. {
  47. m_socket.on_ready_to_read = [this] { on_ready_to_read(); };
  48. }
  49. Socket& m_socket;
  50. OwnPtr<Socket> m_own_underlying_socket;
  51. };
  52. }