3

我正在制作一个 Web 应用程序模糊器,对于我的代理服务器,我正在使用由一个名叫 Alex Ott 的人开发的开源代码(目前)。我注意到,当我从一些网站发出请求时,这些请求没有被捕获,所以我真的很想用 C++ 编写自己的代理,但我完全不知道从哪里开始。有人可以向我解释吗?

最终目标实际上是能够捕获通过代理发出的每个请求并将其写入文件,我已经在这样做了,但是我现在拥有的代理服务器并没有捕获所有这些请求,我知道这些请求在那里.

编辑:由于问题不清楚,所以这里是:我想知道使用 Boost 扩展库用 C++ 编写的代理服务器的代码是什么。过去四个月的同样问题。

4

1 回答 1

15

好吧,这是一个有点实用的示例,可以帮助您入门。它在两个连接之间转发。请注意,此简单示例不适用于 Web 浏览器,因为客户端将尝试建立多个连接,而此示例仅侦听一个。使用它作为(非常简单的)基础,您应该能够取得一些进展。

有趣的事情发生在 中handle_read,这是接收到数据时执行的回调。该函数在套接字之间转发数据。请注意,当我们最初为“本地”和“远程”连接调用它时,我们传递套接字的顺序是相反的(read_fromwrite_to)。

#include <iostream>
using namespace std;

#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>

boost::asio::io_service& io_service()
{
   static boost::asio::io_service svc;
   return svc;
}

char local_data[1024] = {0};
char remote_data[1024] = {0};

void handle_read(
   boost::asio::ip::tcp::socket& read_from,
   boost::asio::ip::tcp::socket& write_to,
   char* read_buffer,
   size_t bytes,
   const boost::system::error_code& e)
{
   // this function is called whenever data is received

   // for debugging purposes, show the data in the console window
   // or write to file, or whatever...
   std::string data(read_buffer, read_buffer + bytes);    
   std::cout << data << "\n";

   // forward the received data on to "the other side"    
   write_to.send(
      boost::asio::buffer(read_buffer, bytes));

   // read more data from "this side"
   read_from.async_read_some(
      boost::asio::buffer(read_buffer, 1024),
      boost::bind(handle_read, boost::ref(read_from), boost::ref(write_to), read_buffer, boost::asio::placeholders::bytes_transferred, boost::asio::placeholders::error));
}

int main(int argc, char** argv)
{
   if(argc == 5)
   {
      boost::asio::io_service::work w(io_service());

      boost::thread t(boost::bind(&boost::asio::io_service::run, (&io_service())));

      // extract the connection information from the command line
      boost::asio::ip::address local_address = boost::asio::ip::address::from_string(argv[1]);
      uint16_t local_port = boost::lexical_cast<uint16_t>(argv[2]);
      boost::asio::ip::address remote_address = boost::asio::ip::address::from_string(argv[3]);
      uint16_t remote_port = boost::lexical_cast<uint16_t>(argv[4]);

      boost::asio::ip::tcp::endpoint local_ep(local_address, local_port);
      boost::asio::ip::tcp::endpoint remote_ep(remote_address, remote_port);

      // start listening on the "local" socket -- note this does not
      // have to be local, you could in theory forward through a remote device
      // it's called "local" in the logical sense    
      boost::asio::ip::tcp::acceptor listen(io_service(), local_ep);
      boost::asio::ip::tcp::socket local_socket(io_service());
      listen.accept(local_socket);

      // open the remote connection
      boost::asio::ip::tcp::socket remote_socket(io_service());
      remote_socket.open(remote_ep.protocol());
      remote_socket.connect(remote_ep);

      // start listening for data on the "local" connection
      local_socket.async_receive(
         boost::asio::buffer(local_data, 1024),
         boost::bind(handle_read, boost::ref(local_socket), boost::ref(remote_socket), local_data, boost::asio::placeholders::bytes_transferred, boost::asio::placeholders::error));

      // also listen for data on the "remote" connection
      remote_socket.async_receive(
         boost::asio::buffer(remote_data, 1024),
         boost::bind(handle_read, boost::ref(remote_socket), boost::ref(local_socket), remote_data, boost::asio::placeholders::bytes_transferred, boost::asio::placeholders::error));

      t.join();
   }
   else
   {
      cout << "proxy <local ip> <port> <remote ip> <port>\n";
   }

   return 0;
}
于 2012-07-10T21:36:24.547 回答