@
neoblackcap 这位仁兄要求并不高,本屌丝 菜鸟一枚  DNSProxy 初版代码,花了 2 天学习 Boost 的时间写的,
#include "stdafx.h"
#include <iostream>
#include <boost/asio.hpp>
#include <vector>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
using namespace std;
using namespace boost::asio;
int thread_count = 0;
boost::mutex mu;
void handle_request(ip::udp::endpoint & request_ep, vector<unsigned char> &buff, int bytes, ip::udp::socket *back_socket) {
	mu.lock();
	thread_count++;
	mu.unlock();
	io_service dns;
	ip::udp::endpoint dns_server_ep(ip::address::from_string("223.5.5.5"), 53);
	ip::udp::socket sock(dns, ip::udp::endpoint(ip::udp::v4(), 0));
	sock.send_to(buffer(buff, bytes), dns_server_ep);
	vector<unsigned char> data(2048, 0);
	int nativeSocket = sock.native();
	fd_set fileDescriptorSet;
	struct timeval timeStruct;
	// set the timeout to 30 seconds
	timeStruct.tv_sec = 10;
	timeStruct.tv_usec = 0;
	FD_ZERO(&fileDescriptorSet);
	FD_SET(nativeSocket, &fileDescriptorSet);
	select(nativeSocket + 1, &fileDescriptorSet, NULL, NULL, &timeStruct);
	if (!FD_ISSET(nativeSocket, &fileDescriptorSet)) { // timeout
		std::string sMsg("TIMEOUT on read client data. Client IP: ");
		sMsg.append(sock.remote_endpoint().address().to_string());
		cout << sMsg << endl;
		return;
	}
	int recv_bytes = sock.receive_from(buffer(data), dns_server_ep);
	back_socket->send_to(buffer(data, recv_bytes), request_ep);
	sock.close();
	mu.lock();
	thread_count--;
	mu.unlock();
	cout << "alive thread is :" + to_string(thread_count) << endl;
}
void handle_connections() {
	io_service service;
	ip::udp::socket sock(service, ip::udp::endpoint(ip::udp::v4(), 53));
	while (true) {
		ip::udp::endpoint sender_ep;
		vector<unsigned char> buff(1024, 0);
		int bytes = sock.receive_from(buffer(buff), sender_ep);
		boost::thread m_thread(boost::bind(handle_request, sender_ep, buff, bytes, &sock));
		m_thread.detach();
	}
}
int main(int argc, char* argv[]) {
	handle_connections();
}