mirror of
https://github.com/klzgrad/naiveproxy.git
synced 2024-11-24 14:26:09 +03:00
152 lines
5.0 KiB
C++
152 lines
5.0 KiB
C++
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#ifndef NET_SOCKET_SOCKS_CLIENT_SOCKET_H_
|
|
#define NET_SOCKET_SOCKS_CLIENT_SOCKET_H_
|
|
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
#include "base/gtest_prod_util.h"
|
|
#include "base/macros.h"
|
|
#include "base/memory/ref_counted.h"
|
|
#include "net/base/address_list.h"
|
|
#include "net/base/completion_callback.h"
|
|
#include "net/base/net_errors.h"
|
|
#include "net/base/net_export.h"
|
|
#include "net/dns/host_resolver.h"
|
|
#include "net/log/net_log_with_source.h"
|
|
#include "net/socket/stream_socket.h"
|
|
#include "net/traffic_annotation/network_traffic_annotation.h"
|
|
|
|
namespace net {
|
|
|
|
class ClientSocketHandle;
|
|
|
|
// The SOCKS client socket implementation
|
|
class NET_EXPORT_PRIVATE SOCKSClientSocket : public StreamSocket {
|
|
public:
|
|
// |req_info| contains the hostname and port to which the socket above will
|
|
// communicate to via the socks layer. For testing the referrer is optional.
|
|
SOCKSClientSocket(std::unique_ptr<ClientSocketHandle> transport_socket,
|
|
const HostResolver::RequestInfo& req_info,
|
|
RequestPriority priority,
|
|
HostResolver* host_resolver,
|
|
const NetworkTrafficAnnotationTag& traffic_annotation);
|
|
|
|
// On destruction Disconnect() is called.
|
|
~SOCKSClientSocket() override;
|
|
|
|
// StreamSocket implementation.
|
|
|
|
// Does the SOCKS handshake and completes the protocol.
|
|
int Connect(CompletionOnceCallback callback) override;
|
|
void Disconnect() override;
|
|
bool IsConnected() const override;
|
|
bool IsConnectedAndIdle() const override;
|
|
const NetLogWithSource& NetLog() const override;
|
|
bool WasEverUsed() const override;
|
|
bool WasAlpnNegotiated() const override;
|
|
NextProto GetNegotiatedProtocol() const override;
|
|
bool GetSSLInfo(SSLInfo* ssl_info) override;
|
|
void GetConnectionAttempts(ConnectionAttempts* out) const override;
|
|
void ClearConnectionAttempts() override {}
|
|
void AddConnectionAttempts(const ConnectionAttempts& attempts) override {}
|
|
int64_t GetTotalReceivedBytes() const override;
|
|
void ApplySocketTag(const SocketTag& tag) override;
|
|
|
|
// Socket implementation.
|
|
int Read(IOBuffer* buf,
|
|
int buf_len,
|
|
CompletionOnceCallback callback) override;
|
|
int Write(IOBuffer* buf,
|
|
int buf_len,
|
|
CompletionOnceCallback callback,
|
|
const NetworkTrafficAnnotationTag& traffic_annotation) override;
|
|
|
|
int SetReceiveBufferSize(int32_t size) override;
|
|
int SetSendBufferSize(int32_t size) override;
|
|
|
|
int GetPeerAddress(IPEndPoint* address) const override;
|
|
int GetLocalAddress(IPEndPoint* address) const override;
|
|
|
|
private:
|
|
FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, CompleteHandshake);
|
|
FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, SOCKS4AFailedDNS);
|
|
FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, SOCKS4AIfDomainInIPv6);
|
|
|
|
enum State {
|
|
STATE_RESOLVE_HOST,
|
|
STATE_RESOLVE_HOST_COMPLETE,
|
|
STATE_HANDSHAKE_WRITE,
|
|
STATE_HANDSHAKE_WRITE_COMPLETE,
|
|
STATE_HANDSHAKE_READ,
|
|
STATE_HANDSHAKE_READ_COMPLETE,
|
|
STATE_NONE,
|
|
};
|
|
|
|
void DoCallback(int result);
|
|
void OnIOComplete(int result);
|
|
void OnReadWriteComplete(CompletionOnceCallback callback, int result);
|
|
|
|
int DoLoop(int last_io_result);
|
|
int DoResolveHost();
|
|
int DoResolveHostComplete(int result);
|
|
int DoHandshakeRead();
|
|
int DoHandshakeReadComplete(int result);
|
|
int DoHandshakeWrite();
|
|
int DoHandshakeWriteComplete(int result);
|
|
|
|
const std::string BuildHandshakeWriteBuffer() const;
|
|
|
|
// Stores the underlying socket.
|
|
std::unique_ptr<ClientSocketHandle> transport_;
|
|
|
|
State next_state_;
|
|
|
|
// Stores the callbacks to the layer above, called on completing Connect().
|
|
CompletionOnceCallback user_callback_;
|
|
|
|
// This IOBuffer is used by the class to read and write
|
|
// SOCKS handshake data. The length contains the expected size to
|
|
// read or write.
|
|
scoped_refptr<IOBuffer> handshake_buf_;
|
|
|
|
// While writing, this buffer stores the complete write handshake data.
|
|
// While reading, it stores the handshake information received so far.
|
|
std::string buffer_;
|
|
|
|
// This becomes true when the SOCKS handshake has completed and the
|
|
// overlying connection is free to communicate.
|
|
bool completed_handshake_;
|
|
|
|
// These contain the bytes sent / received by the SOCKS handshake.
|
|
size_t bytes_sent_;
|
|
size_t bytes_received_;
|
|
|
|
// This becomes true when the socket is used to send or receive data.
|
|
bool was_ever_used_;
|
|
|
|
// Used to resolve the hostname to which the SOCKS proxy will connect.
|
|
HostResolver* host_resolver_;
|
|
std::unique_ptr<HostResolver::Request> request_;
|
|
AddressList addresses_;
|
|
HostResolver::RequestInfo host_request_info_;
|
|
RequestPriority priority_;
|
|
|
|
NetLogWithSource net_log_;
|
|
|
|
// Traffic annotation for socket control.
|
|
NetworkTrafficAnnotationTag traffic_annotation_;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(SOCKSClientSocket);
|
|
};
|
|
|
|
} // namespace net
|
|
|
|
#endif // NET_SOCKET_SOCKS_CLIENT_SOCKET_H_
|