void socks_connection::open_forward_connection(asio::ip::tcp::endpoint target) { printf("socks_connection::open_forward_connection(%s): connecting to %s port %d\n" , command(), target.address().to_string().c_str(), target.port()); m_server_connection.open(target.protocol()); m_server_connection.async_connect(target , std::bind(&socks_connection::on_connected, shared_from_this() , _1)); }
server(asio::io_service& ios, const asio::ip::tcp::endpoint& endpoint, size_t block_size) : io_service_(ios), acceptor_(ios), block_size_(block_size) { acceptor_.open(endpoint.protocol()); acceptor_.set_option(asio::ip::tcp::acceptor::reuse_address(1)); acceptor_.bind(endpoint); acceptor_.listen(); start_accept(); }
server(asio::io_service& ios, const asio::ip::tcp::endpoint& endpoint, size_t block_size) : io_service_(ios), acceptor_(ios), block_size_(block_size) { acceptor_.open(endpoint.protocol()); acceptor_.set_option(asio::ip::tcp::acceptor::reuse_address(1)); acceptor_.bind(endpoint); acceptor_.listen(); session* new_session = new session(io_service_, block_size_); acceptor_.async_accept(new_session->socket(), boost::bind(&server::handle_accept, this, new_session, asio::placeholders::error)); }
void socks_connection::bind_connection(asio::ip::tcp::endpoint target) { printf("socks_connection::bind_connection(%s): binding to %s port %d\n" , command(), target.address().to_string().c_str(), target.port()); error_code ec; m_bind_socket.open(target.protocol(), ec); if (ec) { printf("ERROR: open bind socket failed: (%d) %s\n", ec.value() , ec.message().c_str()); } else { m_bind_socket.bind(target, ec); } int const response = ec ? (m_version == 4 ? 91 : 1) : (m_version == 4 ? 90 : 0); int const len = format_response( m_bind_socket.local_endpoint(), response); if (ec) { printf("ERROR: binding socket to %s %d failed: (%d) %s\n" , target.address().to_string().c_str() , target.port() , ec.value() , ec.message().c_str()); auto self = shared_from_this(); asio::async_write(m_client_connection , asio::const_buffers_1(&m_in_buffer[0], len) , [=](boost::system::error_code const& ec, size_t) { self->close_connection(); }); return; } // send response asio::async_write(m_client_connection , asio::const_buffers_1(&m_in_buffer[0], len) , std::bind(&socks_connection::start_accept, shared_from_this(), _1)); }
void server::connect(const asio::ip::tcp::endpoint& remote_endpoint) { port_type local_port; if (!new_rand_port(local_port)) on_exception("Socket:No port available"); else { socket_ptr socket = std::make_shared<asio::ip::tcp::socket>(main_iosrv); asio::ip::tcp::endpoint::protocol_type ip_protocol = remote_endpoint.protocol(); socket->open(ip_protocol); socket->bind(asio::ip::tcp::endpoint(ip_protocol, local_port)); socket->async_connect(remote_endpoint, [this, local_port, socket](const error_code_type& ec) { try { if (ec) throw(std::runtime_error("Socket Error:" + ec.message())); asio::ip::tcp::socket::keep_alive option(true); socket->set_option(option); std::shared_ptr<pre_session_c> pre_session_c_ptr(std::make_shared<pre_session_c>(local_port, socket, *this, crypto_prov, crypto_srv, main_iosrv, misc_iosrv)); std::unique_lock<std::mutex> lock(pre_session_mutex); pre_sessions.emplace(pre_session_c_ptr); lock.unlock(); pre_session_c_ptr->start(); } catch (std::exception &ex) { on_exception(ex.what()); free_rand_port(local_port); } }); } }
void http_proxy::open_forward_connection(const asio::ip::tcp::endpoint& target) { m_server_connection.open(target.protocol()); m_server_connection.async_connect(target , std::bind(&http_proxy::on_connected, this, _1)); }