从浏览器中检索我在 cpp 中的代理的地址(boost::Asio)[关闭]
Posted
技术标签:
【中文标题】从浏览器中检索我在 cpp 中的代理的地址(boost::Asio)[关闭]【英文标题】:Retrieving adress from browser for my proxy in cpp (boost::Asio) [closed] 【发布时间】:2016-08-03 20:54:49 【问题描述】:我正在尝试修改 TCP 代理的示例代码,以便在我的浏览器上使用它。
这是我使用的完整代理:
#include <cstdlib>
#include <cstddef>
#include <iostream>
#include <string>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/thread/mutex.hpp>
namespace tcp_proxy
namespace ip = boost::asio::ip;
class bridge : public boost::enable_shared_from_this<bridge>
public:
typedef ip::tcp::socket socket_type;
typedef boost::shared_ptr<bridge> ptr_type;
bridge(boost::asio::io_service& ios)
: downstream_socket_(ios),
upstream_socket_(ios)
socket_type& downstream_socket()
return downstream_socket_;
socket_type& upstream_socket()
return upstream_socket_;
void start(const std::string& upstream_host, unsigned short upstream_port)
upstream_socket_.async_connect(
ip::tcp::endpoint(
boost::asio::ip::address::from_string(upstream_host),
upstream_port),
boost::bind(&bridge::handle_upstream_connect,
shared_from_this(),
boost::asio::placeholders::error));
void handle_upstream_connect(const boost::system::error_code& error)
if (!error)
upstream_socket_.async_read_some(
boost::asio::buffer(upstream_data_,max_data_length),
boost::bind(&bridge::handle_upstream_read,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
downstream_socket_.async_read_some(
boost::asio::buffer(downstream_data_,max_data_length),
boost::bind(&bridge::handle_downstream_read,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
else
close();
private:
void handle_downstream_write(const boost::system::error_code& error)
if (!error)
upstream_socket_.async_read_some(
boost::asio::buffer(upstream_data_,max_data_length),
boost::bind(&bridge::handle_upstream_read,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
else
close();
void handle_downstream_read(const boost::system::error_code& error,
const size_t& bytes_transferred)
if (!error)
async_write(upstream_socket_,
boost::asio::buffer(downstream_data_,bytes_transferred),
boost::bind(&bridge::handle_upstream_write,
shared_from_this(),
boost::asio::placeholders::error));
else
close();
void handle_upstream_write(const boost::system::error_code& error)
if (!error)
downstream_socket_.async_read_some(
boost::asio::buffer(downstream_data_,max_data_length),
boost::bind(&bridge::handle_downstream_read,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
else
close();
void handle_upstream_read(const boost::system::error_code& error,
const size_t& bytes_transferred)
if (!error)
async_write(downstream_socket_,
boost::asio::buffer(upstream_data_,bytes_transferred),
boost::bind(&bridge::handle_downstream_write,
shared_from_this(),
boost::asio::placeholders::error));
else
close();
void close()
boost::mutex::scoped_lock lock(mutex_);
if (downstream_socket_.is_open())
downstream_socket_.close();
if (upstream_socket_.is_open())
upstream_socket_.close();
socket_type downstream_socket_;
socket_type upstream_socket_;
enum max_data_length = 8192 ; //8KB
unsigned char downstream_data_[max_data_length];
unsigned char upstream_data_[max_data_length];
boost::mutex mutex_;
public:
class acceptor
public:
acceptor(boost::asio::io_service& io_service,
const std::string& local_host, unsigned short local_port,
const std::string& upstream_host, unsigned short upstream_port)
: io_service_(io_service),
localhost_address(boost::asio::ip::address_v4::from_string(local_host)),
acceptor_(io_service_,ip::tcp::endpoint(localhost_address,local_port)),
upstream_port_(upstream_port),
upstream_host_(upstream_host)
bool accept_connections()
try
session_ = boost::shared_ptr<bridge>(new bridge(io_service_));
acceptor_.async_accept(session_->downstream_socket(),
boost::bind(&acceptor::handle_accept,
this,
boost::asio::placeholders::error));
catch(std::exception& e)
std::cerr << "acceptor exception: " << e.what() << std::endl;
return false;
return true;
private:
void handle_accept(const boost::system::error_code& error)
if (!error)
session_->start(upstream_host_,upstream_port_);
if (!accept_connections())
std::cerr << "Failure during call to accept." << std::endl;
else
std::cerr << "Error: " << error.message() << std::endl;
boost::asio::io_service& io_service_;
ip::address_v4 localhost_address;
ip::tcp::acceptor acceptor_;
ptr_type session_;
unsigned short upstream_port_;
std::string upstream_host_;
;
;
int main(int argc, char* argv[])
if (argc != 5)
std::cerr << "usage: tcpproxy_server <local host ip> <local port> <forward host ip> <forward port>" << std::endl;
return 1;
const unsigned short local_port = static_cast<unsigned short>(::atoi(argv[2]));
const unsigned short forward_port = static_cast<unsigned short>(::atoi(argv[4]));
const std::string local_host = argv[1];
const std::string forward_host = argv[3];
boost::asio::io_service ios;
try
tcp_proxy::bridge::acceptor acceptor(ios,
local_host, local_port,
forward_host, forward_port);
acceptor.accept_connections();
ios.run();
catch(std::exception& e)
std::cerr << "Error: " << e.what() << std::endl;
return 1;
return 0;
实际上,这个示例代码需要本地主机和本地端口,以及转发端口和转发主机作为参数。
我想编辑此代码以自动获取浏览器中询问的主机...
根据我的研究,我必须使用 remote_endpoint() 属性。 问题是我不知道如何实现它以便在每次使用时在浏览器上请求另一个地址时“触发”此功能...
我试过这个:
#include <cstdlib>
#include <cstddef>
#include <iostream>
#include <string>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/thread/mutex.hpp>
namespace tcp_proxy
namespace ip = boost::asio::ip;
class bridge : public boost::enable_shared_from_this<bridge>
public:
typedef ip::tcp::socket socket_type;
typedef boost::shared_ptr<bridge> ptr_type;
bridge(boost::asio::io_service& ios)
: downstream_socket_(ios),
upstream_socket_(ios)
socket_type& downstream_socket()
return downstream_socket_;
socket_type& upstream_socket()
return upstream_socket_;
void start(const std::string& upstream_host, unsigned short upstream_port)
upstream_socket_.async_connect(
ip::tcp::endpoint(
boost::asio::ip::address::from_string(upstream_host),
upstream_port),
boost::bind(&bridge::handle_upstream_connect,
shared_from_this(),
boost::asio::placeholders::error));
void handle_upstream_connect(const boost::system::error_code& error)
if (!error)
upstream_socket_.async_read_some(
boost::asio::buffer(upstream_data_, max_data_length),
boost::bind(&bridge::handle_upstream_read,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
downstream_socket_.async_read_some(
boost::asio::buffer(downstream_data_, max_data_length),
boost::bind(&bridge::handle_downstream_read,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
else
close();
private:
void handle_downstream_write(const boost::system::error_code& error)
if (!error)
upstream_socket_.async_read_some(
boost::asio::buffer(upstream_data_, max_data_length),
boost::bind(&bridge::handle_upstream_read,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
else
close();
void handle_downstream_read(const boost::system::error_code& error,
const size_t& bytes_transferred)
if (!error)
async_write(upstream_socket_,
boost::asio::buffer(downstream_data_, bytes_transferred),
boost::bind(&bridge::handle_upstream_write,
shared_from_this(),
boost::asio::placeholders::error));
else
close();
void handle_upstream_write(const boost::system::error_code& error)
if (!error)
downstream_socket_.async_read_some(
boost::asio::buffer(downstream_data_, max_data_length),
boost::bind(&bridge::handle_downstream_read,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
else
close();
void handle_upstream_read(const boost::system::error_code& error,
const size_t& bytes_transferred)
if (!error)
async_write(downstream_socket_,
boost::asio::buffer(upstream_data_, bytes_transferred),
boost::bind(&bridge::handle_downstream_write,
shared_from_this(),
boost::asio::placeholders::error));
else
close();
void close()
boost::mutex::scoped_lock lock(mutex_);
if (downstream_socket_.is_open())
downstream_socket_.close();
if (upstream_socket_.is_open())
upstream_socket_.close();
socket_type downstream_socket_;
socket_type upstream_socket_;
enum max_data_length = 8192 ; //8KB
unsigned char downstream_data_[max_data_length];
unsigned char upstream_data_[max_data_length];
boost::mutex mutex_;
public:
class acceptor
public:
acceptor(boost::asio::io_service& io_service,
const std::string& local_host, unsigned short local_port,
const std::string& upstream_host, unsigned short upstream_port)
: io_service_(io_service),
localhost_address(boost::asio::ip::address_v4::from_string(local_host)),
acceptor_(io_service_, ip::tcp::endpoint(localhost_address, local_port)),
upstream_port_(upstream_port),
upstream_host_(upstream_host)
bool accept_connections()
try
session_ = boost::shared_ptr<bridge>(new bridge(io_service_));
acceptor_.async_accept(session_->downstream_socket(),
boost::bind(&acceptor::handle_accept,
this,
boost::asio::placeholders::error));
catch (std::exception& e)
std::cerr << "acceptor exception: " << e.what() << std::endl;
return false;
return true;
private:
void handle_accept(const boost::system::error_code& error)
if (!error)
session_->start(upstream_host_, upstream_port_);
if (!accept_connections())
std::cerr << "Failure during call to accept." << std::endl;
else
std::cerr << "Error: " << error.message() << std::endl;
boost::asio::io_service& io_service_;
ip::address_v4 localhost_address;
ip::tcp::acceptor acceptor_;
ptr_type session_;
unsigned short upstream_port_;
std::string upstream_host_;
;
;
int main(int argc, char* argv[])
if (argc != 5)
std::cerr << "usage: EvilProxy.exe <local host ip> <local port> <forward host ip> <forward port>" << std::endl;
return 1;
const unsigned short local_port = static_cast<unsigned short>(::atoi(argv[2]));
const unsigned short forward_port = static_cast<unsigned short>(::atoi(argv[4]));
const std::string local_host = argv[1];
const std::string forward_host = argv[3];
boost::asio::io_service ios;
boost::system::error_code ec;
boost::asio::ip::tcp::socket &mySocket = tcp_proxy::bridge(ios).downstream_socket();
try
std::string str_IpDest = mySocket.remote_endpoint().address().to_string(); // Here i try to get the adresse but with this code, the adresse is seeking only at the begining...it's too earl
tcp_proxy::bridge::acceptor acceptor(ios,
local_host, local_port,
str_IpDest, forward_port);
acceptor.accept_connections();
ios.run();
catch (std::exception& e)
std::cerr << "Error: " << e.what() << std::endl;
return 1;
return 0;
我正在做这个
std::string str_IpDest = mySocket.remote_endpoint().address().to_string();
但它被称为伯爵,我不知道每次用户询问另一个地址时如何触发它。
我使用 boost:asio。
任何人都可以帮助我吗? 目标...After 是使用 SSL...但是...after :)
非常感谢:)
问候
【问题讨论】:
没有一点帮助或一点回答。请告诉我为什么我的帖子/问题不好...我需要帮助:( 【参考方案1】:您需要某种协议来在您的浏览器和代理之间进行通信。首先,您的浏览器需要发送类似“连接到”的消息,然后如果连接成功,代理应该发送信息。我相信 HTTP 有一些特定于代理处理的标头。
【讨论】:
谢谢你的回答,你是对的,它是一个TCP代理,而不是一个HTTP代理!以上是关于从浏览器中检索我在 cpp 中的代理的地址(boost::Asio)[关闭]的主要内容,如果未能解决你的问题,请参考以下文章