글쎄, 시작하기에 다소 기능적인 예가 있습니다. 두 연결 사이를 전달합니다. 이 간단한 예제는 웹 브라우저에서 작동하지 않을 것입니다. 클라이언트가 여러 연결을 시도하고이 예제가 하나의 연결 만 수신하기 때문입니다. 이것을 (아주 간단한) 기반으로 사용하면 약간의 진전이있을 수 있습니다.
재미있는 일은 데이터가 수신 될 때 실행되는 콜백 인 handle_read
에서 발생합니다. 이 함수는 소켓간에 데이터를 전달합니다. 원래 우리가 소켓을 통과 한 순서가 "로컬"및 "원격"연결을 위해 호출되었을 때 (read_from
및 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;
}
프록시 서버를 의미하는 것 같습니까? – Specksynder
'무엇을 시도 했습니까?'라는 질문에 두려워합니다. 무엇이든지 모른다면 Advanced Unix Network Programming VOL1을 읽어보십시오. – Aftnix
기존 프록시를 사용하는 것이 좋습니다. 퍼지 작업을 할 때 유용하다는 것이 입증 된 파일은 다음과 같습니다. http://www.parosproxy.org/ –