⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connect.cpp

📁 CMUD源码 用来玩MUD游戏的 没开发完
💻 CPP
字号:
// Connect.cpp : implementation file
//

#include "stdafx.h"
#include "g.h"
//#include "Connect.h"
#include "GDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CConnect

CConnect::CConnect(CGDoc* pDoc)
{
	m_pDoc=pDoc;
	m_nProxyType=0;
}

CConnect::~CConnect()
{
}


// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CConnect, CSocket)
	//{{AFX_MSG_MAP(CConnect)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif	// 0

/////////////////////////////////////////////////////////////////////////////
// CConnect member functions

void CConnect::OnReceive(int nErrorCode) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	CSocket::OnReceive(nErrorCode);
	m_pDoc->Receive();
}

void CConnect::OnClose(int nErrorCode) 
{
	// TODO: Add your specialized code here and/or call the base class
	m_pDoc->DisConnect();
	CSocket::OnClose(nErrorCode);
}

void CConnect::SetProxy(int nProxyType, CString ProxyHost, int ProxyPort, 
						CString ProxyUser, CString ProxyPass, BOOL bUseSocks5Logon)
{
	ASSERT(nProxyType>=0);
	ASSERT(nProxyType<4);
	m_nProxyType=nProxyType;
	if (!nProxyType)
		return;
	ASSERT(ProxyHost!="");
	ASSERT(ProxyPort>0);
	ASSERT(ProxyPort<=65535);
	m_ProxyHost=ProxyHost;
	m_nProxyPort=ProxyPort;
	m_ProxyUser=ProxyUser;
	m_ProxyPass=ProxyPass;
	m_bUseSocks5Logon=bUseSocks5Logon;
}

BOOL CConnect::PConnect(const SOCKADDR *lpSockAddr, int nSockAddrLen)
{
	if (!Connect(m_ProxyHost,m_nProxyPort))
	{
		m_nProxyError=PROXYERROR_NOCONN;
		return FALSE;
	}

	LPSOCKADDR_IN sockAddr=(LPSOCKADDR_IN)lpSockAddr;

	//Save server details
	m_nProxyPeerIp=sockAddr->sin_addr.S_un.S_addr;
	m_nProxyPeerPort=sockAddr->sin_port;

	if (m_nProxyType==PROXYTYPE_SOCKS4)
	{ //SOCKS4 proxy

		//Send request
		char command[9];
		memset(command,0,9);
		command[0]=4;
		command[1]=1;
		memcpy(&command[2],&sockAddr->sin_port,2);
		memcpy(&command[4],&sockAddr->sin_addr.S_un.S_addr,4);
		TRY
		{
			Send(command,9,0);
			//Get response
			int num=Receive(command,8);
			if (num!=8)
			{
				m_nProxyError=PROXYERROR_REQUESTFAILED;
				return FALSE;
			}
		}
		CATCH_ALL(e)
		{
			return FALSE;
		}
		END_CATCH_ALL
		if (command[1]!=90)
		{
			m_nProxyError=PROXYERROR_REQUESTFAILED;
			return FALSE;
		}
	}
	else
	{ //SOCKS5 proxy
		//Send initialization request
		unsigned char command[10];
		memset(command,0,10);
		command[0]=5;
		command[1]=m_bUseSocks5Logon?2:1;
		command[2]=m_bUseSocks5Logon?2:0;
		TRY
		{
			Send(command,m_bUseSocks5Logon?4:3,0);
			//Get initialization response
			int num=Receive(command,2);
			if (num!=2)
			{
				m_nProxyError=PROXYERROR_REQUESTFAILED;
				return FALSE;
			}
		}
		CATCH_ALL(e)
		{
			m_nProxyError=PROXYERROR_REQUESTFAILED;
			return FALSE;
		}
		END_CATCH_ALL
		if (command[1]==0xFF)
		{
			m_nProxyError=PROXYERROR_AUTHREQUIRED;
			return FALSE;
		}
		if (command[1])
		{
			if (command[1]!=2)
			{
				m_nProxyError=PROXYERROR_AUTHTYPEUNKNOWN;
				return FALSE;
			}
			if (m_bUseSocks5Logon)
			{
				//Send authentication
				unsigned char *buffer=new unsigned char[3+m_ProxyUser.GetLength()+m_ProxyPass.GetLength()];
				sprintf((char *)buffer,"  %s %s",m_ProxyUser,m_ProxyPass);
				buffer[0]=5;
				buffer[1]=m_ProxyUser.GetLength();
				buffer[2+m_ProxyUser.GetLength()]=m_ProxyPass.GetLength();
				TRY
				{
					Send(buffer,3+m_ProxyUser.GetLength()+m_ProxyPass.GetLength(),0);
					//Get auth response
					int num=Receive(command,2);
					if (num!=2)
					{
						delete [] buffer;
						m_nProxyError=PROXYERROR_AUTHFAILED;
						return FALSE;
					}
				}
				CATCH_ALL(e)
				{
					delete [] buffer;
					m_nProxyError=PROXYERROR_AUTHFAILED;
					return FALSE;
				}
				END_CATCH_ALL
				if (command[1]!=0x00)
				{
					delete [] buffer;
					m_nProxyError=PROXYERROR_AUTHFAILED;
					return FALSE;
				}
				delete [] buffer;
			}
			else
			{
				m_nProxyError=PROXYERROR_AUTHNOLOGON;
				return FALSE;
			}
		}
		//Send request
		memset(command,0,10);
		command[0]=5;
		command[1]=1;
		command[2]=0;
		command[3]=1;
		memcpy(&command[4],&sockAddr->sin_addr.S_un.S_addr,4);
		memcpy(&command[8],&sockAddr->sin_port,2);
		TRY
		{
			Send(command,10,0);
			//Get response
			int num=Receive(command,10);
			if (num!=10)
			{
				m_nProxyError=PROXYERROR_REQUESTFAILED;
				return FALSE;
			}
		}
		CATCH_ALL(e)
		{
			m_nProxyError=PROXYERROR_REQUESTFAILED;
			return FALSE;
		}
		END_CATCH_ALL
		if (command[1]!=0x00)
		{
			m_nProxyError=PROXYERROR_REQUESTFAILED;
			return FALSE;
		}
		
	}

	//It worked! SOCKS is really easy, isn't it?
	return TRUE;
}

BOOL CConnect::Connect(LPCTSTR lpszHostAddress, UINT nHostPort, BOOL bProxy)
{
	if (!bProxy)
		//Connect normally because there is no proxy
		return CSocket::Connect(lpszHostAddress, nHostPort);
	
	//Translate the host address
	ASSERT(lpszHostAddress != NULL);

	SOCKADDR_IN sockAddr;
	memset(&sockAddr,0,sizeof(sockAddr));

	sockAddr.sin_family = AF_INET;
	sockAddr.sin_addr.s_addr = inet_addr(lpszHostAddress);

	if (sockAddr.sin_addr.s_addr == INADDR_NONE)
	{
		LPHOSTENT lphost;
		lphost = gethostbyname(lpszHostAddress);
		if (lphost != NULL)
			sockAddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
		else
		{
			WSASetLastError(WSAEINVAL);
			return FALSE;
		}
	}

	sockAddr.sin_port = htons((u_short)nHostPort);

	return PConnect((SOCKADDR*)&sockAddr, sizeof(sockAddr));
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -