📄 proxy.cpp
字号:
settings.saClient = saClient;
// Start the HTTP session and insert it in the list
dwErr = g_pSessionMgr->StartSession(&settings, NULL);
if (ERROR_SUCCESS != dwErr) {
sockClient.close();
IFDBG(DebugOut(ZONE_ERROR, _T("WebProxy: Error adding session to list %d\n"), dwErr));
continue;
}
}
}
exit:
Unlock();
return;
}
DWORD CHttpProxy::SetupServerSockets()
{
DWORD dwRetVal = ERROR_SUCCESS;
ADDRINFO aiHints;
ADDRINFO* paiLocal = NULL;
ADDRINFO* paiTrav = NULL;
CHAR szPort[16];
_itoa(g_pSettings->iPort, szPort, 10);
memset(&aiHints, 0, sizeof(aiHints));
aiHints.ai_family = PF_UNSPEC;
aiHints.ai_socktype = SOCK_STREAM;
aiHints.ai_flags = AI_NUMERICHOST | AI_PASSIVE;
dwRetVal = getaddrinfo(NULL, szPort, &aiHints, &paiLocal);
if (ERROR_SUCCESS != dwRetVal) {
IFDBG(DebugOut(ZONE_ERROR, _T("WebProxy: Error getting addr info: %d.\n"), dwRetVal));
goto exit;
}
m_cSockets = 0;
for (paiTrav = paiLocal; paiTrav && (m_cSockets < MAX_SOCKET_LIST); paiTrav = paiTrav->ai_next) {
m_sockServer[m_cSockets] = socket(paiTrav->ai_family, paiTrav->ai_socktype, paiTrav->ai_protocol);
if (INVALID_SOCKET == m_sockServer[m_cSockets]) {
continue;
}
if (SOCKET_ERROR == bind(m_sockServer[m_cSockets], paiTrav->ai_addr, paiTrav->ai_addrlen)) {
m_sockServer[m_cSockets].close();
dwRetVal = WSAGetLastError();
IFDBG(DebugOut(ZONE_ERROR, _T("WebProxy: Error binding to socket: %d.\n"), dwRetVal));
goto exit;
}
if (SOCKET_ERROR == listen(m_sockServer[m_cSockets], SOMAXCONN)) {
m_sockServer[m_cSockets].close();
dwRetVal = WSAGetLastError();
IFDBG(DebugOut(ZONE_ERROR, _T("WebProxy: Error listening on socket: %d.\n"), dwRetVal));
goto exit;
}
m_cSockets++;
}
exit:
if (paiLocal) {
freeaddrinfo(paiLocal);
}
return dwRetVal;
}
void CHttpProxy::UpdateAdapterInfo(void)
{
DWORD dwRetVal = ERROR_SUCCESS;
sockaddr_in saTmp;
ULONG cbBuffer;
PIP_ADAPTER_INFO pAdapterInfoHead = NULL;
PIP_ADAPTER_INFO pAdapterInfo = NULL;
SOCKADDR_IN saPrivIntf, saPublicIntf;
CReg regICS;
string strPubIntf;
string strPrivIntf;
Lock();
g_pSettings->Lock();
// Get public and private interface names for RG
if (regICS.Open(HKEY_LOCAL_MACHINE, RK_ICS)) {
ce::wstring wstrPubIntf;
ce::wstring wstrPrivIntf;
wstrPubIntf.reserve(MAX_PATH);
if (FALSE == regICS.ValueSZ(RV_PUBLIC_INTF, wstrPubIntf.get_buffer(), wstrPubIntf.capacity())) {
IFDBG(DebugOut(ZONE_WARN, _T("WebProxy: Unable to read Public Interface registry key.\n")));
}
else {
// Get the size of the buffer
int cchPubIntf = WideCharToMultiByte(CP_ACP, 0, wstrPubIntf, -1, strPubIntf.get_buffer(), 0, NULL, NULL);
// Get the name of the public interface
strPubIntf.reserve(cchPubIntf);
WideCharToMultiByte(CP_ACP, 0, wstrPubIntf, -1, strPubIntf.get_buffer(), strPubIntf.capacity(), NULL, NULL);
}
wstrPrivIntf.reserve(MAX_PATH);
if (FALSE == regICS.ValueSZ(RV_PRIVATE_INTF, wstrPrivIntf.get_buffer(), wstrPrivIntf.capacity())) {
IFDBG(DebugOut(ZONE_WARN, _T("WebProxy: Unable to read Private Interface registry key.\n")));
}
else {
// Get the size of the buffer
int cchPrivIntf = WideCharToMultiByte(CP_ACP, 0, wstrPrivIntf, -1, strPrivIntf.get_buffer(), 0, NULL, NULL);
// Get the name of the private interface
strPrivIntf.reserve(cchPrivIntf);
WideCharToMultiByte(CP_ACP, 0, wstrPrivIntf, -1, strPrivIntf.get_buffer(), strPrivIntf.capacity(), NULL, NULL);
}
}
memset(&saPublicIntf, 0, sizeof(SOCKADDR_IN));
memset(&saPrivIntf, 0, sizeof(SOCKADDR_IN));
dwRetVal = GetAdaptersInfo(NULL, &cbBuffer);
if (ERROR_BUFFER_OVERFLOW == dwRetVal) {
pAdapterInfo = (PIP_ADAPTER_INFO)LocalAlloc(0, cbBuffer);
if (NULL == pAdapterInfo) {
IFDBG(DebugOut(ZONE_ERROR, _T("WebProxy: Out of memory.\n")));
goto exit;
}
dwRetVal = GetAdaptersInfo(pAdapterInfo, &cbBuffer);
if (ERROR_SUCCESS != dwRetVal) {
IFDBG(DebugOut(ZONE_ERROR, TEXT("Could not get adapter info.\n")));
goto exit;
}
}
else if (dwRetVal != ERROR_SUCCESS) {
IFDBG(DebugOut(ZONE_ERROR, TEXT("Could not get adapter info.\n")));
goto exit;
}
if (NULL == pAdapterInfo) {
IFDBG(DebugOut(ZONE_ERROR, TEXT("No IP Interface present.\n")));
goto exit;
}
pAdapterInfoHead = pAdapterInfo;
while (pAdapterInfo) {
if ((strPubIntf != "") && (strPubIntf == pAdapterInfo->AdapterName)) {
memset(&saTmp, 0, sizeof(sockaddr));
saTmp.sin_family = AF_INET;
saTmp.sin_addr.S_un.S_addr = inet_addr(pAdapterInfo->IpAddressList.IpAddress.String);
if (INADDR_NONE == saTmp.sin_addr.S_un.S_addr) {
IFDBG(DebugOut(ZONE_ERROR, _T("WebProxy: Error calling inet_addr.\n")));
ASSERT(0);
}
else {
saPublicIntf = saTmp;
}
}
else if ((strPrivIntf != "") && (strPrivIntf == pAdapterInfo->AdapterName)) {
memset(&saTmp, 0, sizeof(sockaddr));
g_pSettings->strPrivateAddrV4 = pAdapterInfo->IpAddressList.IpAddress.String;
g_pSettings->strPrivateMaskV4 = pAdapterInfo->IpAddressList.IpMask.String;
saTmp.sin_family = AF_INET;
saTmp.sin_addr.S_un.S_addr = inet_addr(g_pSettings->strPrivateAddrV4);
if (INADDR_NONE != saTmp.sin_addr.S_un.S_addr) {
saPrivIntf = saTmp;
}
WCHAR wszRegName[64];
swprintf(wszRegName, L"Comm\\%S\\DhcpAllocator\\DhcpOptions", pAdapterInfo->AdapterName);
CReg regDhcpOptions;
if (regDhcpOptions.Open(HKEY_LOCAL_MACHINE, wszRegName)) {
CHAR szOptions[MAX_PATH];
if (regDhcpOptions.ValueBinary(L"252", (LPBYTE)szOptions, MAX_PATH)) {
CHAR* pszStart = szOptions;
CHAR* pszEnd;
CHAR szNewValue[MAX_PATH];
// We need to read the Proxy auto-discovery registry key
// and change only the IP portion of it. Then we have to
// store the name of the URL which will be requested.
if (0 == strncmp(pszStart, "http://", 7)) {
pszStart += 7;
}
pszEnd = pszStart;
while((*pszEnd != '/') && (*pszEnd != ':') && (*pszEnd != '\0')) {
pszEnd++;
}
if (*pszEnd == ':') {
CHAR* p = pszEnd;
while ((*p != '/') && (*p != '\0')) {
p++;
}
g_pSettings->strProxyPacURL = p;
}
strcpy(szNewValue, "http://");
strcat(szNewValue, g_pSettings->strPrivateAddrV4);
strcat(szNewValue, pszEnd);
regDhcpOptions.SetBinary(L"252", (LPBYTE)szNewValue, strlen(szNewValue)+1);
}
}
}
pAdapterInfo = pAdapterInfo->Next;
}
g_pProxyFilter->NotifyAddrChange();
exit:
if (pAdapterInfoHead) {
LocalFree(pAdapterInfoHead);
}
g_pSettings->Unlock();
Unlock();
return;
}
DWORD InitStrings(void)
{
DWORD dwRetVal = ERROR_SUCCESS;
WCHAR* wszTmp;
DWORD cbBuff = DEFAULT_PROXY_STRING_SIZE;
CReg regWebProxy;
if (regWebProxy.Open(HKEY_LOCAL_MACHINE, RK_WEBPROXY)) {
cbBuff = regWebProxy.ValueDW(RV_PROXYSTRINGSIZE, DEFAULT_PROXY_STRING_SIZE);
}
wszTmp = new WCHAR[cbBuff];
if (! wszTmp) {
dwRetVal = ERROR_OUTOFMEMORY;
goto exit;
}
//
// Get URL Not Found string
//
if (0 == LoadString(g_hInst, IDS_URL_NOT_FOUND, wszTmp, cbBuff)) {
dwRetVal = GetLastError();
goto exit;
}
g_pErrors->szURLNotFound = new CHAR[cbBuff];
if (! g_pErrors->szURLNotFound) {
dwRetVal = ERROR_OUTOFMEMORY;
goto exit;
}
g_pErrors->cchURLNotFound = WideCharToMultiByte(CP_ACP, 0, wszTmp, -1, g_pErrors->szURLNotFound, cbBuff, NULL, NULL);
if (0 == g_pErrors->cchURLNotFound) {
dwRetVal = GetLastError();
goto exit;
}
g_pErrors->cchURLNotFound--; // Do not count NULL terminating char
//
// Get Header Not Supported string
//
if (0 == LoadString(g_hInst, IDS_HEADER_NOT_SUPPORT, wszTmp, cbBuff)) {
dwRetVal = GetLastError();
goto exit;
}
g_pErrors->szHeaderNotSupported = new CHAR[cbBuff];
if (! g_pErrors->szHeaderNotSupported) {
dwRetVal = ERROR_OUTOFMEMORY;
goto exit;
}
g_pErrors->cchHeaderNotSupported= WideCharToMultiByte(CP_ACP, 0, wszTmp, -1, g_pErrors->szHeaderNotSupported, cbBuff, NULL, NULL);
if (0 == g_pErrors->cchHeaderNotSupported) {
dwRetVal = GetLastError();
goto exit;
}
g_pErrors->cchHeaderNotSupported--; // Do not count NULL terminating char
//
// Get Protocol Not Supported string
//
if (0 == LoadString(g_hInst, IDS_PROTO_NOT_SUPPORT, wszTmp, cbBuff)) {
dwRetVal = GetLastError();
goto exit;
}
g_pErrors->szProtocolNotSupported = new CHAR[cbBuff];
if (! g_pErrors->szProtocolNotSupported) {
dwRetVal = ERROR_OUTOFMEMORY;
goto exit;
}
g_pErrors->cchProtocolNotSupported = WideCharToMultiByte(CP_ACP, 0, wszTmp, -1, g_pErrors->szProtocolNotSupported, cbBuff, NULL, NULL);
if (0 == g_pErrors->cchProtocolNotSupported) {
dwRetVal = GetLastError();
goto exit;
}
g_pErrors->cchProtocolNotSupported--; // Do not count NULL terminating char
//
// Get Proxy Auth Failed string
//
if (0 == LoadString(g_hInst, IDS_PROXY_AUTH_FAILED, wszTmp, cbBuff)) {
dwRetVal = GetLastError();
goto exit;
}
g_pErrors->szProxyAuthFailed = new CHAR[cbBuff];
if (! g_pErrors->szProxyAuthFailed) {
dwRetVal = ERROR_OUTOFMEMORY;
goto exit;
}
g_pErrors->cchProxyAuthFailed = WideCharToMultiByte(CP_ACP, 0, wszTmp, -1, g_pErrors->szProxyAuthFailed, cbBuff, NULL, NULL);
if (0 == g_pErrors->cchProxyAuthFailed) {
dwRetVal = GetLastError();
goto exit;
}
g_pErrors->cchProxyAuthFailed--; // Do not count NULL terminating char
//
// Get Version Not Supported string
//
if (0 == LoadString(g_hInst, IDS_VER_NOT_SUPPORT, wszTmp, cbBuff)) {
dwRetVal = GetLastError();
goto exit;
}
g_pErrors->szVersionNotSupported = new CHAR[cbBuff];
if (! g_pErrors->szVersionNotSupported) {
dwRetVal = ERROR_OUTOFMEMORY;
goto exit;
}
g_pErrors->cchVersionNotSupported = WideCharToMultiByte(CP_ACP, 0, wszTmp, -1, g_pErrors->szVersionNotSupported, cbBuff, NULL, NULL);
if (0 == g_pErrors->cchVersionNotSupported) {
dwRetVal = GetLastError();
goto exit;
}
g_pErrors->cchVersionNotSupported--; // Do not count NULL terminating char
//
// Get Misc Error string
//
if (0 == LoadString(g_hInst, IDS_MISC_ERROR, wszTmp, cbBuff)) {
dwRetVal = GetLastError();
goto exit;
}
g_pErrors->szMiscError = new CHAR[cbBuff];
if (! g_pErrors->szMiscError) {
dwRetVal = ERROR_OUTOFMEMORY;
goto exit;
}
g_pErrors->cchMiscError = WideCharToMultiByte(CP_ACP, 0, wszTmp, -1, g_pErrors->szMiscError, cbBuff, NULL, NULL);
if (0 == g_pErrors->cchMiscError) {
dwRetVal = GetLastError();
goto exit;
}
g_pErrors->cchMiscError--; // Do not count NULL terminating char
exit:
if (wszTmp) {
delete[] wszTmp;
}
return dwRetVal;
}
void DeinitStrings(void)
{
delete[] g_pErrors->szURLNotFound;
delete[] g_pErrors->szHeaderNotSupported;
delete[] g_pErrors->szProtocolNotSupported;
delete[] g_pErrors->szProxyAuthFailed;
delete[] g_pErrors->szVersionNotSupported;
delete[] g_pErrors->szMiscError;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -