fixed ftExtraList to accept e2e encrypted transfers in addition to direct transfers. This fixed the drag+drop of shared files in a distant chat

This commit is contained in:
csoler 2017-05-06 18:20:48 +02:00
parent 59ac5a5bfa
commit b18a186c5f
2 changed files with 100 additions and 59 deletions

View File

@ -53,11 +53,6 @@ void ftExtraList::data_tick()
bool todo = false; bool todo = false;
time_t now = time(NULL); time_t now = time(NULL);
#ifdef DEBUG_ELIST
//std::cerr << "ftExtraList::run() Iteration";
//std::cerr << std::endl;
#endif
{ {
RsStackMutex stack(extMutex); RsStackMutex stack(extMutex);
@ -131,6 +126,7 @@ void ftExtraList::hashAFile()
/* stick it in the available queue */ /* stick it in the available queue */
mFiles[details.info.hash] = details; mFiles[details.info.hash] = details;
mHashOfHash[makeEncryptedHash(details.info.hash)] = details.info.hash ;
/* add to the path->hash map */ /* add to the path->hash map */
mHashedList[details.info.path] = details.info.hash; mHashedList[details.info.path] = details.info.hash;
@ -169,6 +165,7 @@ bool ftExtraList::addExtraFile(std::string path, const RsFileHash& hash,
/* stick it in the available queue */ /* stick it in the available queue */
mFiles[details.info.hash] = details; mFiles[details.info.hash] = details;
mHashOfHash[makeEncryptedHash(details.info.hash)] = details.info.hash ;
IndicateConfigChanged(); IndicateConfigChanged();
@ -190,6 +187,8 @@ bool ftExtraList::removeExtraFile(const RsFileHash& hash, TransferRequestFlags f
RsStackMutex stack(extMutex); RsStackMutex stack(extMutex);
mHashOfHash.erase(makeEncryptedHash(hash)) ;
std::map<RsFileHash, FileDetails>::iterator it; std::map<RsFileHash, FileDetails>::iterator it;
it = mFiles.find(hash); it = mFiles.find(hash);
if (it == mFiles.end()) if (it == mFiles.end())
@ -242,29 +241,26 @@ bool ftExtraList::cleanupOldFiles()
time_t now = time(NULL); time_t now = time(NULL);
std::list<RsFileHash> toRemove; std::list<RsFileHash> toRemove;
std::list<RsFileHash>::iterator rit;
std::map<RsFileHash, FileDetails>::iterator it; for( std::map<RsFileHash, FileDetails>::iterator it = mFiles.begin(); it != mFiles.end(); ++it) /* check timestamps */
for(it = mFiles.begin(); it != mFiles.end(); ++it)
{
/* check timestamps */
if ((time_t)it->second.info.age < now) if ((time_t)it->second.info.age < now)
{
toRemove.push_back(it->first); toRemove.push_back(it->first);
}
}
if (toRemove.size() > 0) if (toRemove.size() > 0)
{ {
std::map<RsFileHash, FileDetails>::iterator it;
/* remove items */ /* remove items */
for(rit = toRemove.begin(); rit != toRemove.end(); ++rit) for(std::list<RsFileHash>::iterator rit = toRemove.begin(); rit != toRemove.end(); ++rit)
{ {
if (mFiles.end() != (it = mFiles.find(*rit))) if (mFiles.end() != (it = mFiles.find(*rit)))
{ {
cleanupEntry(it->second.info.path, it->second.info.transfer_info_flags); cleanupEntry(it->second.info.path, it->second.info.transfer_info_flags);
mFiles.erase(it); mFiles.erase(it);
} }
} mHashOfHash.erase(makeEncryptedHash(*rit)) ;
}
IndicateConfigChanged(); IndicateConfigChanged();
} }
return true; return true;
@ -333,31 +329,71 @@ bool ftExtraList::hashExtraFileDone(std::string path, FileInfo &info)
**/ **/
bool ftExtraList::search(const RsFileHash &hash, FileSearchFlags /*hintflags*/, FileInfo &info) const bool ftExtraList::search(const RsFileHash &hash, FileSearchFlags /*hintflags*/, FileInfo &info) const
{ {
#ifdef DEBUG_ELIST #ifdef DEBUG_ELIST
std::cerr << "ftExtraList::search()"; std::cerr << "ftExtraList::search() hash=" << hash ;
std::cerr << std::endl;
#endif #endif
/* find hash */ /* find hash */
std::map<RsFileHash, FileDetails>::const_iterator fit; std::map<RsFileHash, FileDetails>::const_iterator fit;
if (mFiles.end() == (fit = mFiles.find(hash))) if (mFiles.end() == (fit = mFiles.find(hash)))
{ {
return false; #ifdef DEBUG_ELIST
std::cerr << " not found in mFiles. Trying encrypted... " ;
#endif
// File not found. We try to look for encrypted hash.
std::map<RsFileHash,RsFileHash>::const_iterator hit = mHashOfHash.find(hash) ;
if(hit == mHashOfHash.end())
{
#ifdef DEBUG_ELIST
std::cerr << " not found." << std::endl;
#endif
return false;
}
#ifdef DEBUG_ELIST
std::cerr << " found! Reaching data..." ;
#endif
fit = mFiles.find(hit->second) ;
if(fit == mFiles.end()) // not found. This is an error.
{
#ifdef DEBUG_ELIST
std::cerr << " no data. Returning false." << std::endl;
#endif
return false ;
}
#ifdef DEBUG_ELIST
std::cerr << " ok! Accepting encrypted transfer." << std::endl;
#endif
info = fit->second.info;
info.storage_permission_flags = FileStorageFlags(DIR_FLAGS_ANONYMOUS_DOWNLOAD) ;
info.transfer_info_flags |= RS_FILE_REQ_ENCRYPTED ;
} }
else
{
#ifdef DEBUG_ELIST
std::cerr << " found! Accepting direct transfer" << std::endl;
#endif
info = fit->second.info;
info = fit->second.info; // Unencrypted file transfer: We only allow direct transfers. This is not exactly secure since another friend can
// swarm the file. But the hash being kept secret, there's no risk here.
// Now setup the file storage flags so that the client can know how to handle permissions //
// info.storage_permission_flags = FileStorageFlags(DIR_FLAGS_BROWSABLE) ;
#warning mr-alice: make sure this is right }
info.storage_permission_flags = FileStorageFlags(0) ;//DIR_FLAGS_BROWSABLE_OTHERS ;
if(info.transfer_info_flags & RS_FILE_REQ_ANONYMOUS_ROUTING) info.storage_permission_flags |= DIR_FLAGS_ANONYMOUS_DOWNLOAD ; if(info.transfer_info_flags & RS_FILE_REQ_ANONYMOUS_ROUTING) info.storage_permission_flags |= DIR_FLAGS_ANONYMOUS_DOWNLOAD ;
return true; return true;
} }
RsFileHash ftExtraList::makeEncryptedHash(const RsFileHash& hash)
{
return RsDirUtil::sha1sum(hash.toByteArray(),hash.SIZE_IN_BYTES);
}
/*** /***
* Configuration - store extra files. * Configuration - store extra files.
@ -472,6 +508,8 @@ bool ftExtraList::loadList(std::list<RsItem *>& load)
/* stick it in the available queue */ /* stick it in the available queue */
mFiles[details.info.hash] = details; mFiles[details.info.hash] = details;
mHashOfHash[makeEncryptedHash(details.info.hash)] = details.info.hash ;
delete (*it); delete (*it);
/* short sleep */ /* short sleep */

View File

@ -109,66 +109,69 @@ const uint32_t CLEANUP_PERIOD = 600; /* 10 minutes */
class ftExtraList: public RsTickingThread, public p3Config, public ftSearch class ftExtraList: public RsTickingThread, public p3Config, public ftSearch
{ {
public: public:
ftExtraList(); ftExtraList();
/*** /***
* If the File is alreay Hashed, then just add it in. * If the File is alreay Hashed, then just add it in.
**/ **/
bool addExtraFile(std::string path, const RsFileHash &hash, bool addExtraFile(std::string path, const RsFileHash &hash,
uint64_t size, uint32_t period, TransferRequestFlags flags); uint64_t size, uint32_t period, TransferRequestFlags flags);
bool removeExtraFile(const RsFileHash& hash, TransferRequestFlags flags); bool removeExtraFile(const RsFileHash& hash, TransferRequestFlags flags);
bool moveExtraFile(std::string fname, const RsFileHash& hash, uint64_t size, bool moveExtraFile(std::string fname, const RsFileHash& hash, uint64_t size,
std::string destpath); std::string destpath);
/*** /***
* Hash file, and add to the files, * Hash file, and add to the files,
* file is removed after period. * file is removed after period.
**/ **/
bool hashExtraFile(std::string path, uint32_t period, TransferRequestFlags flags); bool hashExtraFile(std::string path, uint32_t period, TransferRequestFlags flags);
bool hashExtraFileDone(std::string path, FileInfo &info); bool hashExtraFileDone(std::string path, FileInfo &info);
/*** /***
* Search Function - used by File Transfer * Search Function - used by File Transfer
* implementation of ftSearch. * implementation of ftSearch.
* *
**/ **/
virtual bool search(const RsFileHash &hash, FileSearchFlags hintflags, FileInfo &info) const; virtual bool search(const RsFileHash &hash, FileSearchFlags hintflags, FileInfo &info) const;
/*** /***
* Thread Main Loop * Thread Main Loop
**/ **/
virtual void data_tick(); virtual void data_tick();
/*** /***
* Configuration - store extra files. * Configuration - store extra files.
* *
**/ **/
protected: protected:
virtual RsSerialiser *setupSerialiser(); virtual RsSerialiser *setupSerialiser();
virtual bool saveList(bool &cleanup, std::list<RsItem*>&); virtual bool saveList(bool &cleanup, std::list<RsItem*>&);
virtual bool loadList(std::list<RsItem *>& load); virtual bool loadList(std::list<RsItem *>& load);
private: static RsFileHash makeEncryptedHash(const RsFileHash& hash);
/* Worker Functions */ private:
void hashAFile();
bool cleanupOldFiles();
bool cleanupEntry(std::string path, TransferRequestFlags flags);
mutable RsMutex extMutex; /* Worker Functions */
void hashAFile();
bool cleanupOldFiles();
bool cleanupEntry(std::string path, TransferRequestFlags flags);
std::list<FileDetails> mToHash; mutable RsMutex extMutex;
std::map<std::string, RsFileHash> mHashedList; /* path -> hash ( not saved ) */ std::list<FileDetails> mToHash;
std::map<RsFileHash, FileDetails> mFiles;
time_t cleanup ; std::map<std::string, RsFileHash> mHashedList; /* path -> hash ( not saved ) */
std::map<RsFileHash, FileDetails> mFiles;
std::map<RsFileHash, RsFileHash> mHashOfHash; /* sha1(hash) map so as to answer requests to encrypted transfers */
time_t cleanup ;
}; };