/*
* File name: WorkingThread.cpp
* Version: 1.0.0.2
* Programmer: A.George
* Organization:
* Date(start): 5.23.2000
* Date(finish):
* Description:
Working thread.
*/
#include "stdafx.h"
#include "WorkingThread.h"
#include "winsock.h"
#include <stdio.h>
#include <io.h>
#include <afxinet.h>
#define SITEBASE "g:\\ag\\personalproxy"
/////////////////////////////////////////////////////////////////////////////
int g_nConnectionCount;
HANDLE g_hConnectionCount;
unsigned long g_ulIpRefused[100];
int g_nRefused;
GLOBAL globalInfo;
/////////////////////////////////////////////////////////////////////////////
int (*ProcessCommand)(SOCKET sck, char* pszCommand, char* buf);
/////////////////////////////////////////////////////////////////////////////
BYTE Table(BYTE ot)
{
if(ot>=0 && ot<=25)return 'A' + ot;
else if(ot>=26 && ot<=51)return 'a' + (ot-26);
else if(ot>=52 && ot<=61)return '0' + (ot-52);
else if(ot==62)return '+';
else if(ot==63)return '/';
else return '=';
}
void Base64(char* pszSource, char* pszCode)
{
BYTE ot;
int i;
i=0;
while(pszSource[i])
{
ot = (0xFC & pszSource[i])>>2; *pszCode = Table(ot); i++; pszCode++;
if(!pszSource[i])
{
ot = (0x03 & pszSource[i-1])<<4; *pszCode = Table(ot); pszCode++;
break;
}
ot = (0x03 & pszSource[i-1])<<4 | (0xF0 & pszSource[i])>>4; *pszCode = Table(ot); pszCode++; i++;
if(!pszSource[i])
{
ot = (0x0F & pszSource[i-1])<<2; *pszCode = Table(ot); pszCode++;
break;
}
ot = (0x0F & pszSource[i-1])<<2 | (0xC0 & pszSource[i])>>6; *pszCode = Table(ot); pszCode++;
ot = 0x3F & pszSource[i]; *pszCode = Table(ot), i++; pszCode++;
}
*pszCode = NULL;
}
BYTE GetSixBits(char* pszStr, int& i)
{
char c;
while(1)
{
if(i>=strlen(pszStr))break;
c = pszStr[i++];
if(isupper(c)) return c-'A';
else if(islower(c)) return c-'a'+26;
else if(isdigit(c)) return c-'0'+52;
else if(c=='+') return 62;
else if(c=='/') return 63;
else if(c=='=') return 0;
}
return 0;
}
void Unbase64(char* pszText, char* pszCode)
{
int nLen, i;
BYTE bySixBits0, bySixBits1, bySixBits2, bySixBits3, c;
nLen = strlen(pszCode);
for(i=0; i<nLen; )
{
bySixBits0 = GetSixBits(pszCode, i);
if(i<nLen)bySixBits1 = GetSixBits(pszCode, i);
if(i<nLen)bySixBits2 = GetSixBits(pszCode, i);
if(i<nLen)bySixBits3 = GetSixBits(pszCode, i);
c = bySixBits0<<2 | bySixBits1>>4; *pszText = c; pszText++;
c = bySixBits1<<4 | bySixBits2>>2; *pszText = c; pszText++;
c = bySixBits2<<6 | bySixBits3; *pszText = c; pszText++;
}
*pszText = NULL;
}
DWORD RecvHeader(SOCKET socket, char *buf, DWORD dwSize, HTTPHEADER* pHeader)
{
char * pEnd, *pSize, *pType, *pLocation;
int nRet, nTotal;
memset(pHeader, 0, sizeof(HTTPHEADER));
nTotal = 0;
do{
nRet = recv(socket, buf+nTotal, dwSize-nTotal, 0);
if(nRet==SOCKET_ERROR)return SOCKET_ERROR;
nTotal += nRet;
buf[nRet] = NULL;
pEnd = strstr(buf, "\r\n\r\n");
if(pEnd)
{
pSize = strstr(buf, "Content-length: ");
if(pSize)sscanf(pSize+16, "%ud", &(pHeader->dwContSize));
else pHeader->dwContSize = -1;
if(DWORD(pEnd-buf+4)<dwSize)
pHeader->pContBegin = pEnd+4;
else
pHeader->pContBegin = NULL;
sscanf(buf, "HTTP/%d.%d %d %s", &pHeader->nVerMajor, &pHeader->nVerMinor,
&pHeader->dwHttpStatus, pHeader->pszStatus);
pType = strstr(buf, "Content-type: ");
if(pType)sscanf(pType+14, "%s", &(pHeader->pszType));
pLocation = strstr(buf, "Location: ");
if(pLocation)sscanf(pLocation+10, "s", &(pHeader->pszLocation));
return nTotal;
}
}while(!pEnd && nRet);
return nTotal;
}
int RecvRequest(SOCKET sck, char * buf, DWORD dwSize, REQUESTHEADER * pRequestHeader)
{
char * pEnd, pszCode[32], pszText[32], *pAuthorization, *pRange, *pSize;
int iRet;
DWORD nTotal;
pRequestHeader->dwFlag = REQUEST_UNAUTHORIZED;
memset(buf, 0, dwSize);
nTotal = 0;
do{
iRet = recv(sck, buf+nTotal, dwSize-nTotal-1, 0);
if(iRet==SOCKET_ERROR)
{
return SOCKET_ERROR;
}
nTotal += iRet;
buf[nTotal] = NULL;
pEnd = strstr(buf, "\r\n\r\n");
if(pEnd)
{
pAuthorization = strstr(buf, "Proxy-Authorization: ");
if(!pAuthorization)
{
pRequestHeader->pszUserPass[0] = NULL;
return 0;
}
sscanf(pAuthorization+21, "%*s%s", pszCode);
Unbase64(pszText, pszCode);
if(strcmp(pszText, "user:ag"))
{
strcpy(pRequestHeader->pszUserPass, pszText);
return 0;
}
else
pRequestHeader->dwFlag = 0;
pRequestHeader->pszAuthBegin = pAuthorization;
pRequestHeader->pszAuthEnd = strstr(pAuthorization, "\r\n")+2;
pRequestHeader->pszVirtualRes = strstr(buf, "GET "); //"http://www.personalproxy.com/");
if(pRequestHeader->pszVirtualRes)
{
if(!strncmp(pRequestHeader->pszVirtualRes+4, "http://www.personalproxy.com", 28))
{
pRequestHeader->dwFlag |= VIRTUAL_RESOURCE;
sscanf(pRequestHeader->pszVirtualRes+4, "%s", pRequestHeader->pszUrl);
}
}
pRange = strstr(buf, "RANGE: bytes");
if(pRange)
{
pRange = strstr(pRange, "=");
if(pRange)
{
sscanf(pRange+1, "%d", &pRequestHeader->dwRange);
pRequestHeader->dwFlag |= PARTIAL_CONTENT;
}
}
pSize = strstr(buf, "Content-Length: ");
if(pSize)
{
sscanf(pSize+16, "%d", &pRequestHeader->dwContSize);
}
else
pRequestHeader->dwContSize = 0;
pRequestHeader->pszContBegin = pEnd+4;
return nTotal;
}
}while(!pEnd && iRet);
return 0;
}
void UnCh(char* pszStr)
{
char pszLine[512];
int i, j, nLen;
BYTE b1, b2;
i=0; j=0;
nLen = strlen(pszStr);
while(i<nLen)
{
if(pszStr[i]=='%')
{
i++;
sscanf(pszStr+i, "%2X", &b1);
i += 2;
}
else
{
pszLine[j++] = pszStr[i++];
continue;
}
if(i>=nLen)
{
pszLine[j++] = b1;
break;
}
if(pszStr[i]=='%')
{
i++;
sscanf(pszStr+i, "%2X", &b2);
i += 2;
pszLine[j++] = b2;
pszLine[j++] = b1;
}
else
{
pszLine[j++] = b1;
pszLine[j++] = pszStr[i++];
}
}
pszLine[j]=0;
strcpy(pszStr, pszLine);
}
void ContentType(char* pszFile, char* pszType)
{
char drive[4], dir[MAX_PATH], fname[MAX_PATH], ext[16];
_splitpath(pszFile, drive, dir, fname, ext);
_strlwr(ext);
if(strstr(pszFile, "multipart"))
strcpy(pszType, "multipart/mixed");
else if(!*ext)
strcpy(pszType, "text/html");
else if(!strcmp(ext, ".jpg"))
strcpy(pszType, "image/jpeg");
else if(!strcmp(ext, ".jpeg"))
strcpy(pszType, "image/gif");
else if(!strcmp(ext, ".htm"))
strcpy(pszType, "text/html");
else if(!strcmp(ext, ".html"))
strcpy(pszType, "text/html");
else if(!strcmp(ext, ".zip"))
strcpy(pszType, "application/x-compressed");
else if(!strcmp(ext, ".mpeg"))
strcpy(pszType, "video/mpeg");
else strcpy(pszType, "application/octet-stream");
}
void HttpService(char * pszUrl, SOCKET sck, char* buf, REQUESTHEADER* pRequestHeader)
{
CString strServer, strObject;
DWORD dwService, dwSize, dwFileLen;
unsigned short nPort;
int iRet;
char pszFile[512], pszLine[512], pszType[16];
char pszCommand[128], *pQuestionMark;
FILE* pFileContent;
AfxParseURL(pszUrl, dwService, strServer, strObject, nPort);
if(strObject=="/")
sprintf(pszFile, "%s%s", SITEBASE, "\\VirtualPage.html");
else
sprintf(pszFile, "%s%s", SITEBASE, strObject);
UnCh(pszFile);
// dwService 3
// nPort 80
// + strObject {"/count.dll?a=1&b=2"}
// + strServer {"www.ms.com"}
// + this 0x0012fe74 {CTryDlg hWnd=0x01460920}
if(strstr(pszFile, "myextension"))
{
pQuestionMark = strstr(pszFile, "?");
if(pQuestionMark)
{
strcpy(pszCommand, pQuestionMark+1);
*pQuestionMark = NULL;
}
else
* pszCommand = NULL;
HMODULE hModule = LoadLibrary(pszFile);
if(!hModule)
{
OUTPU
评论0
最新资源