X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ffreenet%2Fintroductionpuzzleinserter.cpp;h=1fa13c0f88e6e410e601e2d6e14f81df6dba3e90;hb=07319f767b4c4f0e06e9957f217d5107b196617b;hp=a38108415dc8dcdd680983754b4d8d628f1d1a22;hpb=6836fbb5db8464f56e682989996b2210b14231d0;p=fms.git diff --git a/src/freenet/introductionpuzzleinserter.cpp b/src/freenet/introductionpuzzleinserter.cpp index a381084..1fa13c0 100644 --- a/src/freenet/introductionpuzzleinserter.cpp +++ b/src/freenet/introductionpuzzleinserter.cpp @@ -3,9 +3,12 @@ #include "../../include/stringfunctions.h" #include "../../include/option.h" #include "../../include/freenet/captcha/simplecaptcha.h" -#include "../../include/uuidgenerator.h" #include "../../include/base64.h" +#include +#include +#include + #ifdef XMEM #include #endif @@ -31,8 +34,7 @@ void IntroductionPuzzleInserter::CheckForNeededInsert() while(!rs.AtEnd()) { std::string localidentityidstr; - DateTime now; - now.SetToGMTime(); + Poco::DateTime now; if(rs.GetField(0)) { @@ -40,7 +42,7 @@ void IntroductionPuzzleInserter::CheckForNeededInsert() } // if this identity has any non-solved puzzles for today, we don't need to insert a new puzzle - SQLite3DB::Recordset rs2=m_db->Query("SELECT UUID FROM tblIntroductionPuzzleInserts WHERE Day='"+now.Format("%Y-%m-%d")+"' AND FoundSolution='false' AND LocalIdentityID="+localidentityidstr+";"); + SQLite3DB::Recordset rs2=m_db->Query("SELECT UUID FROM tblIntroductionPuzzleInserts WHERE Day='"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"' AND FoundSolution='false' AND LocalIdentityID="+localidentityidstr+";"); // identity doesn't have any non-solved puzzles for today - start a new insert if(rs2.Empty()==true) @@ -91,7 +93,7 @@ const bool IntroductionPuzzleInserter::HandlePutFailed(FCPMessage &message) RemoveFromInsertList(localidentityid); - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleInserter::HandlePutFailed failed to insert puzzle "+idparts[3]); + m_log->debug("IntroductionPuzzleInserter::HandlePutFailed failed to insert puzzle "+idparts[3]); } return true; @@ -99,13 +101,12 @@ const bool IntroductionPuzzleInserter::HandlePutFailed(FCPMessage &message) const bool IntroductionPuzzleInserter::HandlePutSuccessful(FCPMessage &message) { - DateTime now; + Poco::DateTime now; SQLite3DB::Statement st; std::vector idparts; long localidentityid; long insertindex; - now.SetToGMTime(); StringFunctions::Split(message["Identifier"],"|",idparts); // non USK @@ -122,14 +123,14 @@ const bool IntroductionPuzzleInserter::HandlePutSuccessful(FCPMessage &message) st.Finalize(); st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedPuzzle=? WHERE LocalIdentityID=?;"); - st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S")); + st.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S")); st.Bind(1,localidentityid); st.Step(); st.Finalize(); RemoveFromInsertList(localidentityid); - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleInserter::HandlePutSuccessful inserted puzzle "+idparts[3]); + m_log->debug("IntroductionPuzzleInserter::HandlePutSuccessful inserted puzzle "+idparts[3]); } return true; @@ -142,11 +143,12 @@ void IntroductionPuzzleInserter::Initialize() const bool IntroductionPuzzleInserter::StartInsert(const long &localidentityid) { - DateTime now; + Poco::DateTime now; std::string idstring; long index=0; std::string indexstr; - UUIDGenerator uuid; + Poco::UUIDGenerator uuidgen; + Poco::UUID uuid; std::string messagebase; IntroductionPuzzleXML xml; std::string encodedpuzzle; @@ -159,8 +161,7 @@ const bool IntroductionPuzzleInserter::StartInsert(const long &localidentityid) std::string keypart=""; StringFunctions::Convert(localidentityid,idstring); - now.SetToGMTime(); - SQLite3DB::Recordset rs=m_db->Query("SELECT MAX(InsertIndex) FROM tblIntroductionPuzzleInserts WHERE Day='"+now.Format("%Y-%m-%d")+"' AND LocalIdentityID="+idstring+";"); + SQLite3DB::Recordset rs=m_db->Query("SELECT MAX(InsertIndex) FROM tblIntroductionPuzzleInserts WHERE Day='"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"' AND LocalIdentityID="+idstring+";"); if(rs.Empty() || rs.GetField(0)==NULL) { @@ -172,58 +173,76 @@ const bool IntroductionPuzzleInserter::StartInsert(const long &localidentityid) } StringFunctions::Convert(index,indexstr); - SQLite3DB::Recordset rs2=m_db->Query("SELECT PrivateKey,PublicKey FROM tblLocalIdentity WHERE LocalIdentityID="+idstring+";"); - if(rs2.Empty()==false && rs2.GetField(0)!=NULL) + if(index<50) { - privatekey=rs2.GetField(0); - if(rs2.GetField(1)) - { - publickey=rs2.GetField(1); - } - if(publickey.size()>=50) + SQLite3DB::Recordset rs2=m_db->Query("SELECT PrivateKey,PublicKey FROM tblLocalIdentity WHERE LocalIdentityID="+idstring+";"); + if(rs2.Empty()==false && rs2.GetField(0)!=NULL) { - // remove - and ~ - keypart=StringFunctions::Replace(StringFunctions::Replace(publickey.substr(4,43),"-",""),"~",""); + privatekey=rs2.GetField(0); + if(rs2.GetField(1)) + { + publickey=rs2.GetField(1); + } + if(publickey.size()>=50) + { + // remove - and ~ + keypart=StringFunctions::Replace(StringFunctions::Replace(publickey.substr(4,43),"-",""),"~",""); + } } - } - - Option::Instance()->Get("MessageBase",messagebase); - - GenerateCaptcha(encodedpuzzle,solutionstring); - - xml.SetType("captcha"); - xml.SetUUID(uuid.Generate()+"@"+keypart); - xml.SetPuzzleData(encodedpuzzle); - xml.SetMimeType("image/bmp"); - xmldata=xml.GetXML(); - StringFunctions::Convert(xmldata.size(),xmldatasizestr); + Option::Instance()->Get("MessageBase",messagebase); - message.SetName("ClientPut"); - message["URI"]=privatekey+messagebase+"|"+now.Format("%Y-%m-%d")+"|IntroductionPuzzle|"+indexstr+".xml"; - message["Identifier"]=m_fcpuniquename+"|"+idstring+"|"+indexstr+"|"+xml.GetUUID()+"|"+message["URI"]; - message["UploadFrom"]="direct"; - message["DataLength"]=xmldatasizestr; - m_fcp->SendMessage(message); - m_fcp->SendRaw(xmldata.c_str(),xmldata.size()); + GenerateCaptcha(encodedpuzzle,solutionstring); - // insert to USK - message.Reset(); - message.SetName("ClientPutComplexDir"); - message["URI"]="USK"+privatekey.substr(3)+messagebase+"|"+now.Format("%Y.%m.%d")+"|IntroductionPuzzle/0/"; - message["Identifier"]=m_fcpuniquename+"USK|"+message["URI"]; - message["DefaultName"]="IntroductionPuzzle.xml"; - message["Files.0.Name"]="IntroductionPuzzle.xml"; - message["Files.0.UplaodFrom"]="direct"; - message["Files.0.DataLength"]=xmldatasizestr; - m_fcp->SendMessage(message); - m_fcp->SendRaw(xmldata.c_str(),xmldata.size()); - - m_db->Execute("INSERT INTO tblIntroductionPuzzleInserts(UUID,Type,MimeType,LocalIdentityID,PuzzleData,PuzzleSolution) VALUES('"+xml.GetUUID()+"','captcha','image/bmp',"+idstring+",'"+encodedpuzzle+"','"+solutionstring+"');"); - - m_inserting.push_back(localidentityid); + try + { + uuid=uuidgen.createRandom(); + } + catch(...) + { + m_log->fatal("IntroductionPuzzleInserter::StartInsert could not create UUID"); + } - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleInserter::StartInsert started insert for id "+idstring); + xml.SetType("captcha"); + std::string uuidstr=uuid.toString(); + StringFunctions::UpperCase(uuidstr,uuidstr); + xml.SetUUID(uuidstr+"@"+keypart); + xml.SetPuzzleData(encodedpuzzle); + xml.SetMimeType("image/bmp"); + + xmldata=xml.GetXML(); + StringFunctions::Convert(xmldata.size(),xmldatasizestr); + + message.SetName("ClientPut"); + message["URI"]=privatekey+messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|IntroductionPuzzle|"+indexstr+".xml"; + message["Identifier"]=m_fcpuniquename+"|"+idstring+"|"+indexstr+"|"+xml.GetUUID()+"|"+message["URI"]; + message["UploadFrom"]="direct"; + message["DataLength"]=xmldatasizestr; + m_fcp->SendMessage(message); + m_fcp->SendRaw(xmldata.c_str(),xmldata.size()); + + // insert to USK + message.Reset(); + message.SetName("ClientPutComplexDir"); + message["URI"]="USK"+privatekey.substr(3)+messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y.%m.%d")+"|IntroductionPuzzle/0/"; + message["Identifier"]=m_fcpuniquename+"USK|"+message["URI"]; + message["DefaultName"]="IntroductionPuzzle.xml"; + message["Files.0.Name"]="IntroductionPuzzle.xml"; + message["Files.0.UplaodFrom"]="direct"; + message["Files.0.DataLength"]=xmldatasizestr; + m_fcp->SendMessage(message); + m_fcp->SendRaw(xmldata.c_str(),xmldata.size()); + + m_db->Execute("INSERT INTO tblIntroductionPuzzleInserts(UUID,Type,MimeType,LocalIdentityID,PuzzleData,PuzzleSolution) VALUES('"+xml.GetUUID()+"','captcha','image/bmp',"+idstring+",'"+encodedpuzzle+"','"+solutionstring+"');"); + + m_inserting.push_back(localidentityid); + + m_log->debug("IntroductionPuzzleInserter::StartInsert started insert for id "+idstring); + } + else + { + m_log->warning("IntroductionPuzzleInserter::StartInsert already inserted 50 puzzles for "+idstring); + } return true;