我已阅读 SSL TUNNELING INTERNET-DRAFT of December 1995并设置与未加密流量完美配合的 HTTP 透明代理。
阅读以上内容并用谷歌搜索后,通过代理为安全流量创建隧道的公认方法似乎是:
连接到请求的主机,然后让代理向客户端发送“HTTP 200...”确认消息,然后从那时起,只需在客户端和服务器之间传递所有进一步的数据流量。
然而,当我尝试此操作时,客户端(Chrome 浏览器)使用我转发给远程主机的三个 wingdings 字符响应“HTTP 200...”消息。此时无响应,连接失败。
这是我在连接到主机后为此使用的代码:
if((*request=='C')&&(*(request+1)=='O')&&(*(request+2)=='N')&&(*(request+3)=='N'))
{
int recvLen;
send(output,htok,strlen(htok),0); //htok looks like "HTTP/1.0 200 Connection Established\nProxy-Agent: this_proxy\r\n\r\n"
std::memset(buff,0,bSize);
int total;
int bytes;
int n;
char cdata[MAXDATA];
while ((recvLen = recv(output, buff, bSize-1,0)) > 0) //recving from client - here we get wingdings
{
memset(cdata,0, MAXDATA);
strcat(cdata, buff);
while(recvLen>=bSize-1)//just in case buff is too small
{
std::memset(buff,0,bSize);
recvLen=recv(output,buff,bSize-1,0);
strcat(cdata, buff);
}
total = 0;
bytes = strlen(cdata);
cout << cdata << endl;//how I see the wingdings
while (total < strlen(cdata))
{
n = send(requestSock, cdata + total, bytes,0);//forwarding to remote host
if(n == SOCKET_ERROR)
{
cout << "secure sending error" << endl;
break;
}
total += n;
bytes -= n;
}
std::memset(buff,0,bSize);
recvLen=recv(requestSock, buff, bSize,0);//get reply from remote host
if (recvLen > 0)
{
do
{
cout<<"Thread "<<threadid<<" [Connection:Secure]: "<<recvLen<<endl;
send(output, buff, recvLen,0);//forward all to client
recvLen= recv(requestSock, buff, bSize,0);
if(0==recvLen || SOCKET_ERROR==recvLen)
{
cout<<"finished secure receiving or socket error"<<endl;
break;
}
}while(true);
}
}//end while, loop checks again for client data
谁能发现我方法的错误?
最佳答案
您的代码比必要的要复杂得多。只需读入一个 char 数组,保存返回的长度,然后在循环中从同一个数组写入那么多字节,直到 recv() 返回零。 4 行代码,其中两行用于大括号。不要试图组合整个传入消息,只需中继传入的任何消息即可。否则,您只是在增加延迟和编程错误。完全摆脱所有 strXXX() 调用。
关于c++ - 如何通过透明代理实现SSL隧道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9577039/