3

我已经阅读了1995 年 12 月的 SSL TUNNELING INTERNET-DRAFT并设置了一个 HTTP 透明代理,它可以完美地处理未加密的流量。

阅读了上述内容并用谷歌搜索了我的大脑,通过代理为安全流量创建隧道的公认方法似乎是:

连接到请求的主机,然后让代理向客户端发送“HTTP 200 ...”确认消息,然后从那时起,只需在客户端和服务器之间传递所有进一步的数据流量。

但是,当我尝试此操作时,客户端(Chrome 浏览器)会响应“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

谁能发现我的方式的错误?

4

2 回答 2

3

您的代码比必要的复杂得多。只需读入一个 char 数组,保存返回的长度,然后在一个循环中从同一个数组中写入那么多字节,直到 recv() 返回零。4行代码,包括两行大括号。不要试图组装整个传入的消息,只要传递进来的任何内容。否则,您只会增加延迟和编程错误。完全摆脱所有 strXXX() 调用。

于 2012-03-06T02:45:16.673 回答
2

我认为您不应该假设流量不包含 ASCIINUL字符:

            strcat(cdata, buff);
        }

        total = 0;
        bytes = strlen(cdata);

如果流中有 ASCII NUL,这些将失败。

于 2012-03-06T02:25:18.630 回答