Serenity Operating System
at master 64 lines 2.5 kB view raw
1/* 2 * Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#pragma once 8 9#include <AK/OwnPtr.h> 10#include <LibCore/Proxy.h> 11#include <LibCore/Socket.h> 12 13namespace Core { 14class SOCKSProxyClient final : public Socket { 15public: 16 enum class Version : u8 { 17 V4 = 0x04, 18 V5 = 0x05, 19 }; 20 21 struct UsernamePasswordAuthenticationData { 22 DeprecatedString username; 23 DeprecatedString password; 24 }; 25 26 enum class Command : u8 { 27 Connect = 0x01, 28 Bind = 0x02, 29 UDPAssociate = 0x03, 30 }; 31 32 using HostOrIPV4 = Variant<DeprecatedString, u32>; 33 34 static ErrorOr<NonnullOwnPtr<SOCKSProxyClient>> connect(Socket& underlying, Version, HostOrIPV4 const& target, int target_port, Variant<UsernamePasswordAuthenticationData, Empty> const& auth_data = {}, Command = Command::Connect); 35 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); 36 37 virtual ~SOCKSProxyClient() override; 38 39 // ^Stream::Stream 40 virtual ErrorOr<Bytes> read_some(Bytes bytes) override { return m_socket.read_some(bytes); } 41 virtual ErrorOr<size_t> write_some(ReadonlyBytes bytes) override { return m_socket.write_some(bytes); } 42 virtual bool is_eof() const override { return m_socket.is_eof(); } 43 virtual bool is_open() const override { return m_socket.is_open(); } 44 virtual void close() override { m_socket.close(); } 45 46 // ^Stream::Socket 47 virtual ErrorOr<size_t> pending_bytes() const override { return m_socket.pending_bytes(); } 48 virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_socket.can_read_without_blocking(timeout); } 49 virtual ErrorOr<void> set_blocking(bool enabled) override { return m_socket.set_blocking(enabled); } 50 virtual ErrorOr<void> set_close_on_exec(bool enabled) override { return m_socket.set_close_on_exec(enabled); } 51 virtual void set_notifications_enabled(bool enabled) override { m_socket.set_notifications_enabled(enabled); } 52 53private: 54 SOCKSProxyClient(Socket& socket, OwnPtr<Socket> own_socket) 55 : m_socket(socket) 56 , m_own_underlying_socket(move(own_socket)) 57 { 58 m_socket.on_ready_to_read = [this] { on_ready_to_read(); }; 59 } 60 61 Socket& m_socket; 62 OwnPtr<Socket> m_own_underlying_socket; 63}; 64}