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

📄 soc.cpp

📁 socket代理的C++源码,实现了代理服务器的大部分功能
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	}

	//通知代理本机的UDP端口,并申请代理的转发端口
	pucSendBuf[0] = 5;		//ver 5
	pucSendBuf[1] = 3;		//UDP
	pucSendBuf[2] = 0;		//reserved
	pucSendBuf[3] = 1;		//IP V4
	if( pucProxyOuterIP != NULL )
	{
		pucSendBuf[4] = pucProxyOuterIP[0];		//代理往外发数据的IP
		pucSendBuf[5] = pucProxyOuterIP[1];		//
		pucSendBuf[6] = pucProxyOuterIP[2];		//
		pucSendBuf[7] = pucProxyOuterIP[3];		//
	}
	else
	{
		pucSendBuf[4] = 0;		//让代理自动选择往外发数据的IP
		pucSendBuf[5] = 0;		//
		pucSendBuf[6] = 0;		//
		pucSendBuf[7] = 0;		//
	}
	pucSendBuf[8] = nLocalPort / 256;//本地UPD端口
	pucSendBuf[9] = nLocalPort - pucSendBuf[8] * 256;

	if( send( m_socTCPToProxy, (char*)pucSendBuf, 10, 0 ) == SOCKET_ERROR )
	{
		CErrorLog::Instance()->SetSdkError(WSAGetLastError());
		closesocket( m_socTCPToProxy );
		return 8;
	}
	
	nReturn = recv( m_socTCPToProxy, (char*)pucReceiveBuf, 1024, 0 );
	if( nReturn == SOCKET_ERROR )
	{
		CErrorLog::Instance()->SetSdkError(WSAGetLastError());
		closesocket( m_socTCPToProxy );
		return 9;
	}

	if( nReturn < 10 )
	{
		closesocket( m_socTCPToProxy );
		return 10;
	}

	//保存代理的IP
	m_pucProxyIP[0] = pucProxyInterIP[0];
	m_pucProxyIP[1] = pucProxyInterIP[1];
	m_pucProxyIP[2] = pucProxyInterIP[2];
	m_pucProxyIP[3] = pucProxyInterIP[3];
	//保存代理分配的转发端口
	nPorxyAlloPort = pucReceiveBuf[8] * 256 + pucReceiveBuf[9];

	//打开本地的UDP socket
	m_socUDPData = socket( AF_INET, SOCK_DGRAM, 0 );
	if( m_socUDPData == INVALID_SOCKET )
	{
		CErrorLog::Instance()->SetSdkError(WSAGetLastError());
		closesocket( m_socTCPToProxy );
		return 11;
	}

    memset( &uSocLocalAddIn, 0, sizeof(uSocLocalAddIn) );
    uSocLocalAddIn.sin_family = AF_INET;
    uSocLocalAddIn.sin_port = htons( nLocalPort );

	if( bind( m_socUDPData, (sockaddr*) &uSocLocalAddIn, sizeof( uSocLocalAddIn ) ) 
		== SOCKET_ERROR )
	{
		CErrorLog::Instance()->SetSdkError(WSAGetLastError());
		closesocket( m_socTCPToProxy );
		closesocket( m_socUDPData );
		return 12;
	}

	if( WSAAsyncSelect( m_socUDPData, m_pdlgMes->m_hWnd, SOC_NETUDPSOCKS5, 
		FD_READ | FD_WRITE | FD_CLOSE ) == SOCKET_ERROR)
	{
		CErrorLog::Instance()->SetSdkError(WSAGetLastError());
		closesocket( m_socTCPToProxy );
		closesocket( m_socUDPData );
		return 14;
	}

	m_bOpen = true;

	return 0;
}

/////////////////////////////////////////////////////////////////////////////
//
// 关闭socket
// 参数:	none
// 返回值:	none
//
/////////////////////////////////////////////////////////////////////////////
void CUDPSock5::CloseSocket()
{
	if( m_bOpen )
	{
		closesocket( m_socTCPToProxy );
		closesocket( m_socUDPData );
		m_bOpen = false;
	}
}

/////////////////////////////////////////////////////////////////////////////
//
// 发送数据
// 参数:	pucDesIP		目的主机的IP
//			nDesPort		目的主机的端口
//			pucSendBuf		发送的数据
//			nLen			传入时为数据长度,返回时为实际发送的数据长度
// 返回值:	0				成功
//
/////////////////////////////////////////////////////////////////////////////
int CUDPSock5::SendData( BYTE *pucDesIP, int nDesPort, const BYTE *pucSendBuf, int &nLen )
{
	int nSendBytes;

	BYTE *pucSend;

	sockaddr_in uDestination;

	if( !m_bOpen )
	{
		return 2;
	}

	pucSend = new BYTE[nLen + 10];
	if( pucSend == NULL )
	{
		return 3;
	}
	pucSend[0] = 0;				//数据头
	pucSend[1] = 0;				//
	pucSend[2] = 0;				//不分包
	pucSend[3] = 1;				//IP V4
	pucSend[4] = pucDesIP[0];	//接收主机IP
	pucSend[5] = pucDesIP[1];	//
	pucSend[6] = pucDesIP[2];	//
	pucSend[7] = pucDesIP[3];	//
	pucSend[8] = nDesPort / 256;//接收主机端口
	pucSend[9] = nDesPort - pucSend[8] * 256;
	memcpy( &pucSend[10], pucSendBuf, nLen );

    memset( &uDestination, 0, sizeof(uDestination) );
    uDestination.sin_family = AF_INET;
    uDestination.sin_port = htons( nPorxyAlloPort );
	uDestination.sin_addr.S_un.S_un_b.s_b1 = m_pucProxyIP[0];
	uDestination.sin_addr.S_un.S_un_b.s_b2 = m_pucProxyIP[1];
	uDestination.sin_addr.S_un.S_un_b.s_b3 = m_pucProxyIP[2];
	uDestination.sin_addr.S_un.S_un_b.s_b4 = m_pucProxyIP[3];

	nSendBytes = sendto( m_socUDPData, (char*)pucSend, nLen + 10, 0, 
		(sockaddr*)&uDestination, sizeof(uDestination) );
	nLen = nSendBytes;

	delete [] pucSend;

	return 0;
}

CFtp::CFtp()
{
	m_eMode = AUTO;
	m_bMode = false;

	m_hWndMsg = NULL;
	m_nMsg = 0;
	m_pFuncMes = NULL;

	m_bWrite = true;

	memset(m_pucFtpServerIP, 0, 4);
	m_nFtpServerPort = 0;

	m_bClose = true;
	m_bConnect = false;
	m_bUser = false;
	m_bPass = false;
	m_bDownFileSize = false;
	m_bData = false;
	m_bWaitForPort = false;
	m_bList = false;
	m_bDownload = false;
	m_evtLogin.ResetEvent();

	m_psocDataSrv = NULL;
	m_psocDataClt = NULL;
	m_psocCommand = NULL;

	m_unFileSize = 0;
	m_unDownSize = 0;
	m_pfilDown = NULL;
	m_pDataBuf = NULL;
}

CFtp::~CFtp()
{
	if (m_psocDataSrv != NULL)
	{
		delete m_psocDataSrv;
		m_psocDataSrv = NULL;
	}
	if (m_psocDataClt != NULL)
	{
		delete m_psocDataClt;
		m_psocDataClt = NULL;
	}
	if (m_psocCommand != NULL)
	{
		delete m_psocCommand;
		m_psocCommand = NULL;
	}
	if (m_pfilDown != NULL)
	{
		delete m_pfilDown;
		m_pfilDown = NULL;
	}
	if (m_pDataBuf != NULL)
	{
		delete [] m_pDataBuf;
		m_pDataBuf = NULL;
	}
}

//////////////////////////////////////////////////////////////////
//
//	description:
//		连接远程FTP服务器
//	parameter:
//		pucServerIP		FTP服务器IP
//		nServerPort		FTP服务端口
//		strUser			FTP用户名
//		strPass			FTP密码
//	return:	0			正确
//	date:	2006-7-20
//	history:
//
//////////////////////////////////////////////////////////////////
int CFtp::Connect(BYTE *pucServerIP, int nServerPort, 
	CString strUser, CString strPass)
{
	int nRet;

	if (m_psocCommand == NULL)
	{
		m_psocCommand = new CTCPClient;
		if (m_psocCommand == NULL)
		{
			return 2;
		}
	}

	if (m_psocCommand->GetSocketState())
	{
		return 3;
	}

	if (!m_bClose)
	{
		return 4;
	}

	memcpy(m_pucFtpServerIP, pucServerIP, 4);
	m_nFtpServerPort = nServerPort;
	m_strUser = strUser;
	m_strPass = strPass;

	m_psocCommand->SetBufferLen(1024);
	m_psocCommand->SetSocketNotify(&DisposeReplay);
	m_psocCommand->SetExtData(this);
	nRet = m_psocCommand->OpenSocket(m_pucFtpServerIP, m_nFtpServerPort, 0);
	if (nRet)
	{
		return CErrorLog::Instance()->AddError(5, nRet);
	}

	m_bClose = false;
	m_evtLogin.ResetEvent();

	return 0;
}

//////////////////////////////////////////////////////////////////
//
//	description:
//		关闭FTP连接
//	parameter:
//			none
//	return:	none
//	date:	2006-7-20
//	history:
//
//////////////////////////////////////////////////////////////////
void CFtp::Close()
{
	if (m_psocCommand != NULL)
	{
		m_psocCommand->CloseSocket();
	}
	if (m_psocDataSrv != NULL)
	{
		m_psocDataSrv->CloseSocket();
	}
	if (m_psocDataClt != NULL)
	{
		m_psocDataClt->CloseSocket();
	}
	if (m_pfilDown != NULL)
	{
		if (m_pfilDown->m_hFile != INVALID_HANDLE_VALUE)
		{
			m_pfilDown->Close();
		}
	}

	m_bClose = true;
	m_bConnect = false;
	m_bUser = false;
	m_bPass = false;
	m_bDownFileSize = false;
	m_bData = false;
	m_bWaitForPort = false;
	m_bList = false;
	m_bDownload = false;
}

///////////////////////////////////////////////////////////////////////
//
//	description:
//		获取远程目录下的文件列表
//	parameter:
//			none
//	return:	0			正确
//	date:	2006-07-18
//	history:
//
//////////////////////////////////////////////////////////////////////
int CFtp::List()
{
	int nSend;
	int nRet;
	int nSrvPort;
	int nCmdPort;

	CString strCmd;
	CString strDataSrvIP;
	CString strCmdIP;

	BYTE pucSend[0xFF];
	int pnIP[4];

	if (!m_bPass || m_bData)
	{
		return 2;
	}

	if (m_psocDataSrv != NULL)
	{
		if (m_psocDataSrv->GetSocketState())
		{
			m_psocDataSrv->CloseSocket();
		}
	}
	if (m_psocDataClt != NULL)
	{
		if (m_psocDataClt->GetSocketState())
		{
			m_psocDataClt->CloseSocket();
		}
	}

	m_bList = true;
	m_bDownload = false;

	if (m_bMode)
	{
		nRet = OpenDataPort();
		if (nRet)
		{
			return CErrorLog::Instance()->AddError(3, nRet);
		}

		m_psocDataSrv->GetServerIP(strDataSrvIP, nSrvPort);
		m_psocCommand->GetLocalIP(strCmdIP, nCmdPort);
		if (strCmdIP.IsEmpty())
		{
			return 4;
		}
		else
		{
			sscanf(strCmdIP.GetBuffer(), "%d.%d.%d.%d", &pnIP[0], &pnIP[1], 
				&pnIP[2], &pnIP[3]);
		}
		strCmd.Format("PORT %d,%d,%d,%d,%d,%d\n\r", pnIP[0], pnIP[1], pnIP[2],
			pnIP[3], nSrvPort / 256, nSrvPort % 256);
		nSend = strCmd.GetLength();
		memcpy(pucSend, strCmd.GetBuffer(), nSend);
		SendCommand(pucSend, nSend);

		strCmd = "TYPE A\n\r" ;
		nSend = strCmd.GetLength();
		memcpy(pucSend, strCmd.GetBuffer(), nSend);
		SendCommand(pucSend, nSend);

		strCmd = "LIST \n\r";
		nSend = strCmd.GetLength();
		memcpy(pucSend, strCmd.GetBuffer(), nSend);
		SendCommand(pucSend, nSend);
	}
	else
	{
		strCmd = "TYPE A\n\r" ;
		nSend = strCmd.GetLength();
		memcpy(pucSend, strCmd.GetBuffer(), nSend);
		SendCommand(pucSend, nSend);

		strCmd = "PASV \n\r";
		nSend = strCmd.GetLength();
		memcpy(pucSend, strCmd.GetBuffer(), nSend);
		SendCommand(pucSend, nSend);

		m_bWaitForPort = true;
	}

	return 0;
}

///////////////////////////////////////////////////////////////////////
//
//	description:
//		获取当前的远程目录
//	parameter:
//			none
//	return:	0			正确
//	date:	2006-07-20
//	history:
//
//////////////////////////////////////////////////////////////////////
int CFtp::Pwd()
{
	int nSend;

	CString strCmd;

	BYTE *pucSend;

	if (!m_bPass)
	{
		return 2;
	}

	pucSend = new BYTE[0xFF];
	if (pucSend == NULL)
	{
		return 3;
	}

	strCmd = "TYPE A\n\r" ;
	nSend = strCmd.GetLength();
	memcpy(pucSend, strCmd.GetBuffer(), nSend);
	SendCommand(pucSend, nSend);

	strCmd = "PWD \n\r";
	nSend = strCmd.GetLength();
	memcpy(pucSend, strCmd.GetBuffer(), nSend);
	SendCommand(pucSend, nSend);

	delete [] pucSend;

	return 0;
}

///////////////////////////////////////////////////////////////////////
//
//	description:
//		进入上一级目录
//	parameter:
//			none
//	return:	0			正确
//	date:	2006-07-20
//	history:
//
//////////////////////////////////////////////////////////////////////
int CFtp::CDFather()
{
	int nSend;

	CString strCmd;

	BYTE *pucSend;

	if (!m_bPass)
	{
		return 2;
	}

	pucSend = new BYTE[0xFF];
	if (pucSend == NULL)
	{
		return 3;
	}

	strCmd = "TYPE A\n\r" ;
	nSend = strCmd.GetLength();
	memcpy(pucSend, strCmd.GetBuffer(), nSend);
	SendCommand(pucSend, nSend);

	strCmd = "CDUP \n\r";
	nSend = strCmd.GetLength();
	memcpy(pucSend, strCmd.GetBuffer(), nSend);
	SendCommand(pucSend, nSend);

	delete [] pucSend;

	return 0;
}

///////////////////////////////////////////////////////////////////////
//
//	description:
//		进入子目录
//	parameter:
//			strDir		子目录名称
//	return:	0			正确
//	date:	2006-07-20
//	history:
//
//////////////////////////////////////////////////////////////////////
int CFtp::CDSub(CString strDir)
{
	int nSend;

	CString strCmd;

	BYTE *pucSend;

	if (!m_bPass)
	{
		return 2;
	}

	pucSend = new BYTE[0xFF];
	if (pucSend == NULL)
	{
		return 3;
	}

	strCmd = "TYPE A\n\r" ;
	nSend = strCmd.GetLength();
	memcpy(pucSend, strCmd.GetBuffer(), nSend);
	SendCommand(pucSend, nSend);

	strCmd.Format("CWD %s\n\r", strDir);
	nSend = strCmd.GetLength();
	memcpy(pucSend, strCmd.GetBuffer(), nSend);
	SendCommand(pucSend, nSend);

	delete [] pucSend;

	return 0;
}

///////////////////////////////////////////////////////////////////////
//
//	description:
//		下载文件
//	parameter:
//			strFile		下载的文件名称
//			strSave		在本地保存的文件名称
//			unStart		开始下载位置
//	return:	0			正确
//	date:	2006-07-20
//	history:
//
//////////////////////////////////////////////////////////////////////
int CFtp::DownLoad(CString strFile, CString strSave, unsigned int unStart)
{
	int nSend;

	CString strCmd;

	BYTE pucSend[0xFF];

	if (!m_bPass || m_bData)
	{

⌨️ 快捷键说明

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