基于 POCO 框架的 C++ 版搜狗代理程序
搜狗代理服务器程序,网上已经有用 Python 实现的版本。这个版本在某些情况下(例如迅雷下载)性能不够好,于是我用 C++ 实现了一个版本,基于 POCO 框架开发,应当具有不错的可移植性(改一下 _snprintf 函数名)。完整的源代码如下:[Cpp]
#include <stdio.h>
#include <time.h>
#include <vector>
#include <string>
#include <Poco/ErrorHandler.h>
#include <Poco/Net/Socket.h>
#include <Poco/Net/HTTPServer.h>
#include <Poco/Net/HTTPServerRequest.h>
#include <Poco/Net/HTTPServerRequestImpl.h>
#include <Poco/Net/HTTPServerResponse.h>
#include <Poco/Net/HTTPRequestHandler.h>
#include <Poco/Net/HTTPClientSession.h>
using namespace std;
using namespace Poco;
using namespace Poco::Net;
class EH : public ErrorHandler
{
virtual void exception(const Exception& exc)
{
printf("%s: %s\n", exc.what(), exc.message().c_str());
}
virtual void exception(const std::exception& exc)
{
printf("%s\n", exc.what());
}
virtual void exception()
{
printf("Unknown exception\n");
}
};
const int BUFFER_SIZE = 65536;
class ProxyService : public HTTPRequestHandler
{
HTTPClientSession client;
static unsigned int hashTag(const string &s)
{
unsigned int code = s.length();
for (int i = 0; i < s.length() / 4; ++i)
{
unsigned int a = (s[i * 4] & 0xffu) + ((s[i * 4 + 1] & 0xffu) << 8);
unsigned int b = (s[i * 4 + 2] & 0xffu) + ((s[i * 4 + 3] & 0xffu) << 8);
code += a;
code ^= ((code << 5) ^ b) << 0xb;
code += code >> 0xb;
}
switch (s.length() % 4)
{
case 1:
code += s[s.length() - 1] & 0xffu;
code ^= code << 0xa;
code += code >> 1;
break;
case 2:
code += (s[s.length() - 2] & 0xffu) + ((s[s.length() - 1] & 0xffu) << 8);
code ^= code << 0xb;
code += code >> 0x11;
break;
case 3:
code += (s[s.length() - 3] & 0xffu) + ((s[s.length() - 2] & 0xffu) << 8);
code ^= (code ^ ((s[s.length() - 1] & 0xffu) << 2)) << 0x10;
code += code >> 0xb;
break;
}
code ^= code << 3;
code += code >> 5;
code ^= code << 4;
code += code >> 0x11;
code ^= code << 0x19;
code += code >> 6;
return code;
}
static string hexString(unsigned int x)
{
char buff[20];
_snprintf(buff, 16, "%08x", x);
return string(buff, buff + 8);
}
void write(istream &in, ostream &out)
{
char buffer[BUFFER_SIZE];
streamsize sz;
while (in.good() && out.good())
{
in.read(buffer, sizeof(buffer));
sz = in.gcount();
if (sz <= 0)
break;
if (out.good())
out.write(buffer, sz);
}
}
public:
ProxyService()
{
static char xid = '0';
char host[] = "h0.edu.bj.ie.sogou.com";
char id = xid + 1;
if (id > '9')
id = '0';
host[1] = id;
xid = id;
client.setHost(host);
client.setPort(80);
}
virtual void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
补充:软件开发 , C++ ,