2 u012747454 u012747454 于 2016.04.28 21:17 提问

关于一个简单的代理服务器程序,运行程序出现错误,求解答,新手无从入手啊

求大家帮帮忙,我是真的一点也看不懂,而老师交代的不能不做,这个程序是网上载的,我调试后可跳出程序,但是运行程序会出错,因为本人什么也不懂,所以只能麻烦各位了。。需要提供什么材料可以说,最好能加下我Q:597305256 分不多,只能意思一下,帮忙解决的话万分感激,求大家帮个忙,真的是急了才没办法了。。
图片说明
#include "StdAfx.h"
#include "ProxyServer.h"
#include "HttpProxyWnd.h"

#define MAX_REQUEST 1024 * 4
#define MAX_HOST_NAME 128
#define DEFAULTPORT 80
#define HEADLEN 7 //http://
#define GET_METHOD_LEN 4 //GET_SPACE

ProxyParam *ProxyPtr = NULL;
TCHAR HeaderA[1024 * 4] = {0};
TCHAR HeaderB[1024 * 4] = {0};
CHAR HostList[1024 * 4] = {0};

System::String szProxy = _T("");//010.000.000.172:80
System::String szTimeout = _T("6000");
System::String szOrig = _T("010.000.000.001");
System::String szPort = _T("8008");
System::String szFilter = _T("");
TCHAR chNotifytext[1024*2] = {0};

Proxydata lproxydata;
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

//Sunday算法。
//*************************************************************************
//Sunday算法是比较快速的字符串匹配算法,比KMP和Booyer-moore算法都快,这三种算法都需要提前进行预处理,
//Booyer-moore是反向比对字符串,Kmp和Sunday则是正向比对,各自的预处理方法都不同。
//Sunday算法主要思想是让指向主串的指针在匹配过程中,跳过尽可能多的长度,再和模式串进行匹配。平均状况下时
//间复杂度:O(主串长度/模式串长度)
//*************************************************************************

inline bool sunday(const char* ori ,const char* pat)
{
int i=0;
int j=0;
int olen=strlen(ori);
int plen=strlen(pat);
const int max_size=255;
int next[max_size] = {0};
vector res;

for (i=0;i<max_size;++i)    {
    next[i]=plen+1;
}

for (i=0;i<plen;++i){
    next[pat[i]]=plen-i;
}

i=0;

while(i<=olen-plen)
{
    while(j<plen)
    {
        if (ori[i+j]!=pat[j])
        {
            break;
        }
        ++j;
    }

    if (j==plen)
    {
        res.push_back(i);
        j=0;
    }
    i+=next[ori[i+plen]];
}

if (0 < res.size()){
    return true;
}

return false;

}

///
/// 根据字符串解析主机名称 端口
///
inline bool ParseHostName(char RecvBuf, char *HostName, u_short *Port, bool *PortExsit)
{
char *pHost = strstr(RecvBuf, "Host: ");
if (pHost == NULL) { return false; /
未能发现主机*/ }
pHost += (sizeof("Host: ") - 1);

char *pHostEnd = strstr(pHost, "\r\n");
if (pHostEnd == NULL) { return false; /*无效的主机*/ }
int nHost = pHostEnd - pHost;
if (nHost > MAX_HOST_NAME) { return false; /*无效的主机*/ }
memcpy(HostName, pHost, nHost);
HostName[nHost] = 0;

char *pPort = strstr(HostName, ":");
if (pPort == NULL) { *Port = DEFAULTPORT; return true; }
*Port = (u_short)atoi(pPort + (sizeof(":") - 1));
*PortExsit = true;

HostName[pPort - HostName] = 0;
return true;

}

///
/// 判断是否本地主机
///
inline bool IsLocalHost(char *Host)
{
int Length = ProxyPtr->LocalIP.Length;
while(Length > 0)
{
if (strstr(Host, ProxyPtr->LocalIP.IP[Length - 1]) != NULL) { return true; };
Length -= 1;
}
return false;
}

///
/// 过滤关键字
///
inline void ErrorNetAddress(System::Net::Sockets *ClientSocket, char *Text)
{
char *pstr =
"HTTP/1.1 502 Bad Gateway\r\n"
"Content-Type: text/html\r\n"
"Server: Easy-Proxy\r\n"
"Connection: close\r\n\r\n"
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n"
"\r\n

\r\n\r\n"
"错误:您所请求的网址(URL)无法获取\r\n"
"&lt;!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}PRE{font-family:sans-serif}--&gt;\r\n"
"\r\n\r\n"
"

错误

\r\n

您所请求的网址(URL)无法获取

\r\n"
"
\r\n发生了下列的错误: \r\n"
"
    \r\n
  • \r\n\r\n";
    int TextLength = strlen(pstr);
    ClientSocket->Send(pstr, &TextLength);
    
    if (Text == NULL){ Text = "您当前打开的网址含非法字符"; }
    TextLength = strlen(Text);
    ClientSocket->Send(Text, &TextLength);
    
    pstr =  
        "\r\n</STRONG>\r\n</UL>\r\n请联系服务器管理员:<A HREF=\"mailto:123456@foxmail.com\">123456@foxmail.com</A>.\r\n"
        "<BR clear=\"all\">\r\n<HR noshade buf_size=\"1px\">\r\n"
        "<ADDRESS>\r\nGenerated by WTP/1.1 ";
    TextLength = strlen(pstr);
    ClientSocket->Send(pstr, &TextLength);
    
    pstr = ProxyPtr->LocalIP.IP[ProxyPtr->LocalIP.Length - 1];
    TextLength = strlen(pstr);
    ClientSocket->Send(pstr, &TextLength);
    
    pstr = " (HTTP/1.1 GDGZ-PS-GW001-WAP01 (infoX-WISG, 123456 Technologies))\r\n</ADDRESS>\r\n</BODY>\r\n</HTML>";
    TextLength = strlen(pstr);
    ClientSocket->Send(pstr, &TextLength);
    
    ClientSocket->Shutdown();
    

    }

    ///
    /// 显示一个错误网页给访问客户端
    ///
    inline void ShowToClient(System::Net::Sockets *ClientSocket, char *Text)
    {
    char *pstr =
    "HTTP/1.1 502 Bad Gateway\r\n"
    "Content-Type: text/html\r\n"
    "Server: Easy-Proxy\r\n"
    "Connection: close\r\n\r\n"
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n"
    "\r\n

    \r\n\r\n"
    "错误:您所请求的网址(URL)无法获取\r\n"
    "&lt;!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}PRE{font-family:sans-serif}--&gt;\r\n"
    "\r\n\r\n"
    "

    错误

    \r\n

    您所请求的网址(URL)无法获取

    \r\n"
    "
    \r\n发生了下列的错误: \r\n"
    "
      \r\n
    • \r\n\r\n";
      int TextLength = strlen(pstr);
      ClientSocket->Send(pstr, &TextLength);
      
      if (Text == NULL){ Text = "Text参数为null"; }
      TextLength = strlen(Text);
      ClientSocket->Send(Text, &TextLength);
      
      pstr =  
          "\r\n</STRONG>\r\n</UL>\r\n本缓存服务器管理员:<A HREF=\"mailto:123456@foxmail.com\">123456@foxmail.com</A>.\r\n"
          "<BR clear=\"all\">\r\n<HR noshade buf_size=\"1px\">\r\n"
          "<ADDRESS>\r\nGenerated by WTP/1.1 ";
      TextLength = strlen(pstr);
      ClientSocket->Send(pstr, &TextLength);
      
      pstr = ProxyPtr->LocalIP.IP[ProxyPtr->LocalIP.Length - 1];
      TextLength = strlen(pstr);
      ClientSocket->Send(pstr, &TextLength);
      
      pstr = " (HTTP/1.1 GDGZ-PS-GW001-WAP01 (infoX-WISG, 123456 Technologies))\r\n</ADDRESS>\r\n</BODY>\r\n</HTML>";
      TextLength = strlen(pstr);
      ClientSocket->Send(pstr, &TextLength);
      
      ClientSocket->Shutdown();
      

      }

      ///
      /// 判断主机名是否在白名单列表
      ///
      inline bool IsInWhiteList(char *hostname)
      {
      char *p = HostList;
      char *p1;
      while((p1 = strstr(p, "\r\n")) != NULL)
      {
      p1[0] = 0;
      if (strstr(hostname, p) != NULL)
      {
      p1[0] = '\r';
      return true;
      }
      p1[0] = '\r';
      p = p1 + 2;
      }
      return false;
      }

      ///
      /// 读取数据包头
      ///
      inline char *GetHeaderValueA(char *headers, char *name)
      {
      char *pheader = strstr(headers, name);
      if (pheader != NULL)
      {
      pheader += (strlen(name) + 2);
      char *pheaderEnd = strstr(pheader, "\r\n");
      name = (char *)AppBase::Allocate((pheaderEnd - pheader) + 1,RLIB_FUCTION,RLIB_FILE);
      memcpy(name, pheader, (pheaderEnd - pheader));
      pheader = name;
      }
      return pheader;
      }

      ///
      /// 读取数据包头
      ///
      inline char *GetHeaderA(char *headers, char *name)
      {
      char *pheader = strstr(headers, name);
      if (pheader != NULL)
      {
      char *pheaderEnd = strstr(pheader, "\r\n");
      name = (char *)AppBase::Allocate((pheaderEnd - pheader + 2) + 1, RLIB_FUCTION, RLIB_FILE);
      memcpy(name, pheader, (pheaderEnd - pheader + 2));
      pheader = name;
      }
      return pheader;
      }

      ///
      /// 读取数据包头
      ///
      inline TCHAR *GetHeader(TCHAR *headers, TCHAR *name)
      {
      TCHAR *pheader = _tcsstr(headers, name);
      if (pheader != NULL)
      {
      TCHAR *pheaderEnd = _tcsstr(pheader, T("\r\n"));
      name = (TCHAR *)AppBase::Allocate(TSIZE(pheaderEnd - pheader + 2), RLIB_FUCTION, RLIB_FILE);
      memcpy(name, pheader, TSIZE((pheaderEnd - pheader + 2)) - sizeof(TCHAR));
      pheader = name;
      }
      return pheader;
      }

      ///
      /// 本地网页请求处理
      ///
      inline void LocalProcess(char * /*RecvBuf*/, RequestParam *lpParameter)
      {
      //输出网页
      ShowToClient(lpParameter->ClientSocket, "当前配置不允许请求内网数据");
      }

      ///
      /// 接收请求
      ///
      inline int RecvRequest(char **RecvBuf, int *pbuf_size, System::Net::Sockets * ClientSocket)
      {
      int buf_size = *pbuf_size; //缓冲区可用大小
      bool retend = false; //是否收完请求头 \r\n\r\n之前

      int retval = 0; //已接收大小
      int retcur = 0; //最近一次接收大小
      int postlen = 0; //记录POST数据大小(如果有) + 头部大小

      BEGINRecv:
      while((retcur = ClientSocket->Recv(*RecvBuf + retval, buf_size)) > 0)
      {
      retval += retcur;
      if(!retend) //如果没收完头部
      {
      char *pend = strstr(*RecvBuf, "\r\n\r\n");
      if (pend != NULL) //收完头部
      {
      if ((*RecvBuf)[0] != 'P')
      {
      //非POST方式
      goto ENDRecv;
      }
      //以下默认为POST方式
      char *post_len_str = GetHeaderValueA(*RecvBuf, "Content-Length");
      if(post_len_str == NULL)
      {
      ShowToClient(ClientSocket, "POST关键字段丢失, 无法完成请求");
      goto ERRORRecv;
      }
      postlen = atoi(post_len_str);
      AppBase::Collect(post_len_str);
      postlen += ((pend - *RecvBuf) + 4);
      retend = true;
      }
      //判断POST数据是否接收完毕
      if (retend && (retval >= postlen))
      {
      goto ENDRecv;
      }
      buf_size -= retcur;

      CHECK:
      if (buf_size <= 0)
      {
      buf_size += MAX_REQUEST;
      *pbuf_size += MAX_REQUEST;
      *RecvBuf = (char *)AppBase::ReAlloc(*RecvBuf, *pbuf_size);
      goto CHECK;
      }
      goto BEGINRecv; //循环直到收完头部
      }
      //此处是POST数据的接收过程, 判断是否接收完毕
      if (retval >= postlen)
      {
      goto ENDRecv;
      }
      buf_size -= retcur;

      CHECK_B:
      if (buf_size <= 0)
      {
      buf_size += MAX_REQUEST;
      *pbuf_size += MAX_REQUEST;
      *RecvBuf = (char *)AppBase::ReAlloc(*RecvBuf, *pbuf_size);
      goto CHECK_B;
      }
      }
      ShowToClient(ClientSocket, "接收请求时发生异常, 请检查客户端");

      ERRORRecv:
      return 0;

      ENDRecv:
      return retval;
      }

      还有一部分代码 好像这边打不进去了,麻烦大家帮帮忙好吗 求各位了 T T 我的QQ 597305256 真的是急了 不然不会那么唐突的

2个回答

u012747454
u012747454   2016.04.28 21:17
已采纳

麻烦加一下Q帮我看一下啊 我不是骗子 也不是开网店什么 我单纯就想解决这个问题,求大家帮忙看一下 看一下不损失什么的

dongfeng9ge
dongfeng9ge   Ds   Rxr 2016.04.29 15:21

我想知道你请求的具体URL是什么

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!