设为首页 加入收藏

TOP

基于 POCO 框架的 C++ 版搜狗代理程序(一)
2014-11-24 08:58:27 】 浏览:1637
Tags:基于 POCO 框架 搜狗 代理 程序
搜狗代理服务器程序,网上已经有用 Python 实现的版本。这个版本在某些情况下(例如迅雷 下载)性能不够好,于是我用 C++ 实现了一个版本,基于 POCO 框架开发,应当具有不错的可移植性(改一下 _snprintf 函数名)。完整的源代码如下:
[Cpp]
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

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)
{
static const string authToken = "5FA7CF6A1809B3B514FA66A1ECA16FEB/30/78e7ff933a9a3063";
string timestamp(hexString((unsigned)time(0)));
string tag(hexString(hashTag(timestamp + request.getHost() + "SogouExplorerProxy")));
try
{
HTTPRequest clientRequest(request.getMethod(), request.getURI(), request.getVersion());
for (Nameva lueCollection::ConstIterator i = request.begin(); i != request.end(
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇NOJ 1548 hash+网络流 下一篇HDU 3265(矩形面积并-分割矩形)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目