如何使在C代理服务器++使用Boost [英] How to make a proxy server in C++ using Boost

查看:130
本文介绍了如何使在C代理服务器++使用Boost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个Web应用程序模糊器和我的代理服务器,我使用的是开源$ C ​​$ C(现在)由名为亚历克斯·奥特家伙的发展。我注意到,虽然,当我做一些网站上,那些没有得到捕获的请求,所以我真的想用C来写我自己的代理+ +,但我完全不知道从哪里开始。可能有人解释给我吗?

I'm making a web application fuzzer and for my proxy server, I'm using opensource code (for now) developed by a guy named Alex Ott. I've noticed though that when I make requests from some websites that those don't get captured so I really want to write my own proxy in C++ but I have absolutely no idea where to start. Could someone explain it to me?

的最终目标是真正能够捕捉并编写每一个来通过代理到一个文件,我已经在做,但我现在已经没有赶上这一切请求代理服务器的请求,那些我知道在那里。

The end goal is really to be able to capture and write every request that comes through the proxy to a file, which I'm already doing but the proxy server I have now isn't catch all this requests, ones I know to be there.

编辑:既然问题是不清楚,那就是:我想知道code是什么写在C ++使用升压扩展库代理服务器。在过去的四个月中同样的问题。

Since the question was unclear, here it is: I want to know what the code is for a proxy server written in C++ using the Boost extension libraries. Same question for the past four months.

推荐答案

嗯,这里是一个有些功能例子让你开始。它把两个连接之间。请注意,这个简单的例子不会为一个Web浏览器的工作,作为客户端会尝试多个连接,而这个例子只是监听之一。以此为一个(很简单)的基础上,你应该能够取得一些进展。

Well, here's a somewhat functional example to get you started. It forwards between two connections. Note that this simple example won't work for a web browser, as the client will attempt to make several connections, and this example only listens on one. Using this as a (very simple) base, you should be able to make some progress.

有趣的东西发生在 handle_read ,这是接收数据时执行回调。此功能转发插槽之间的数据。请注意,当我们最初称它的订单,我们在反向传递的插座(本地和远程连接 read_from write_to )。

The interesting stuff happens in handle_read, which is the callback that is executed when data is received. This function forwards the data between sockets. Notice that when we originally called it for the "local" and "remote" connections that the order we passed the sockets in is reversed (read_from and write_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;
}

这篇关于如何使在C代理服务器++使用Boost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆