X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ffreenet%2Fintroductionpuzzleinserter.cpp;h=cac38792d6c68701048bef1bcc96a0373be5f59f;hb=05ef25de71be91442b4cbd22dc7cc45629c5d5bb;hp=e422d29286b2d0cb932a1e69fbb1fd44af786e04;hpb=dec33c63afafabf83c3039e916725cac6faef9b3;p=fms.git diff --git a/src/freenet/introductionpuzzleinserter.cpp b/src/freenet/introductionpuzzleinserter.cpp index e422d29..cac3879 100644 --- a/src/freenet/introductionpuzzleinserter.cpp +++ b/src/freenet/introductionpuzzleinserter.cpp @@ -3,6 +3,10 @@ #include "../../include/stringfunctions.h" #include "../../include/option.h" #include "../../include/freenet/captcha/simplecaptcha.h" +#ifdef ALTERNATE_CAPTCHA +#include "../../include/freenet/captcha/alternatecaptcha1.h" +#include "../../include/freenet/captcha/alternatecaptcha2.h" +#endif #include "../../include/base64.h" #include @@ -33,8 +37,13 @@ void IntroductionPuzzleInserter::CheckForNeededInsert() while(!rs.AtEnd()) { - std::string localidentityidstr; + int localidentityid=0; + std::string localidentityidstr=""; Poco::DateTime now; + float minutesbetweeninserts=0; + minutesbetweeninserts=1440.0/(float)m_maxpuzzleinserts; + Poco::DateTime lastinsert=now; + lastinsert-=Poco::Timespan(0,0,minutesbetweeninserts,0,0); if(rs.GetField(0)) { @@ -47,7 +56,16 @@ void IntroductionPuzzleInserter::CheckForNeededInsert() // identity doesn't have any non-solved puzzles for today - start a new insert if(rs2.Empty()==true) { - StartInsert(rs.GetInt(0)); + // make sure we are on the next day or the appropriate amount of time has elapsed since the last insert + if(m_lastinserted.find(rs.GetInt(0))==m_lastinserted.end() || m_lastinserted[rs.GetInt(0)]<=lastinsert || m_lastinserted[rs.GetInt(0)].day()!=now.day()) + { + StartInsert(rs.GetInt(0)); + m_lastinserted[rs.GetInt(0)]=now; + } + else + { + m_log->trace("IntroductionPuzzleInserter::CheckForNeededInsert waiting to insert puzzle for "+localidentityidstr); + } } rs.Next(); @@ -57,13 +75,26 @@ void IntroductionPuzzleInserter::CheckForNeededInsert() void IntroductionPuzzleInserter::GenerateCaptcha(std::string &encodeddata, std::string &solution) { - SimpleCaptcha captcha; + ICaptcha *cap=0; +#ifdef ALTERNATE_CAPTCHA + if(rand()%2==0) + { + cap=new AlternateCaptcha1(); + } + else + { + cap=new AlternateCaptcha2(); + } + m_log->trace("IntroductionPuzzleInserter::GenerateCaptcha using alternate captcha generator"); +#else + cap=new SimpleCaptcha(); +#endif std::vector puzzle; std::vector puzzlesolution; - captcha.Generate(); - captcha.GetPuzzle(puzzle); - captcha.GetSolution(puzzlesolution); + cap->Generate(); + cap->GetPuzzle(puzzle); + cap->GetSolution(puzzlesolution); encodeddata.clear(); solution.clear(); @@ -71,6 +102,8 @@ void IntroductionPuzzleInserter::GenerateCaptcha(std::string &encodeddata, std:: Base64::Encode(puzzle,encodeddata); solution.insert(solution.begin(),puzzlesolution.begin(),puzzlesolution.end()); + delete cap; + } const bool IntroductionPuzzleInserter::HandlePutFailed(FCPMessage &message) @@ -139,6 +172,7 @@ const bool IntroductionPuzzleInserter::HandlePutSuccessful(FCPMessage &message) void IntroductionPuzzleInserter::Initialize() { m_fcpuniquename="IntroductionPuzzleInserter"; + m_maxpuzzleinserts=50; } const bool IntroductionPuzzleInserter::StartInsert(const long &localidentityid) @@ -173,68 +207,77 @@ 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) + 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); + Option::Instance()->Get("MessageBase",messagebase); - GenerateCaptcha(encodedpuzzle,solutionstring); + GenerateCaptcha(encodedpuzzle,solutionstring); - try - { - uuid=uuidgen.createRandom(); + try + { + uuid=uuidgen.createRandom(); + } + catch(...) + { + m_log->fatal("IntroductionPuzzleInserter::StartInsert could not create UUID"); + } + + 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); } - catch(...) + else { - m_log->fatal("IntroductionPuzzleInserter::StartInsert could not create UUID"); + m_log->warning("IntroductionPuzzleInserter::StartInsert already inserted max puzzles for "+idstring); } - xml.SetType("captcha"); - xml.SetUUID(uuid.toString()+"@"+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); - return true; }