2012-07-10 80 views
1

我正在製作一個Web應用程序模糊器和我的代理服務器,我使用的是由Alex Ott開發的開源代碼(現在)。我注意到,儘管當我從某些網站發出請求時,那些不會被捕獲,所以我真的想用C++編寫自己的代理,但我絕對不知道從哪裏開始。有人可以向我解釋嗎?如何在C++中使用Boost創建代理服務器

最終目標是真正能夠捕獲和寫入通過代理到達文件的每個請求,我已經在做,但現在我的代理服務器沒有捕獲所有這些請求,知道在那裏。

編輯:由於問題不清楚,這裏是:我想知道代碼是用C++編寫的使用Boost擴展庫的代理服務器是什麼。過去四個月同樣的問題。

+0

我想你是指代理服務器? – Specksynder 2012-07-10 20:51:06

+0

我不敢問「你有什麼試過?」。如果你不知道你可以從閱讀高級Unix網絡編程VOL1開始。 – Aftnix 2012-07-10 20:51:52

+0

我強烈建議您使用現有的代理。這裏有一個在模糊測試中證明對我有用,它支持寫入文件:http://www.parosproxy.org/ – 2012-07-10 20:56:42

回答

10

那麼,這裏有一個有點功能的例子,讓你開始。它在兩個連接之間轉發。請注意,這個簡單的示例不適用於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; 
} 
+0

我愛你。這難以置信。 – barndog 2012-07-10 22:53:14

+0

我很樂意提供幫助。 – Chad 2012-07-11 14:33:49

相關問題