FingerprintManager.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) 2002-2009 Moxie Marlinspike
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  17. * USA
  18. */
  19. #include "FingerprintManager.hpp"
  20. #include "util/Util.hpp"
  21. FingerprintManager* FingerprintManager::manager = new FingerprintManager();
  22. FingerprintManager* FingerprintManager::getInstance() {
  23. return manager;
  24. }
  25. void FingerprintManager::setValidUserAgents(std::string &validAgents) {
  26. if (!validAgents.empty()) {
  27. std::string delimiters(" ,");
  28. Util::tokenizeString(validAgents, delimiters, this->validAgents);
  29. }
  30. }
  31. void FingerprintManager::setUserAgentFor(ip::address &address, std::string &userAgent) {
  32. // std::cerr << "FM: Got Fingerprint (" << address << "):" << userAgent << std::endl;
  33. fingerprints[address] = userAgent;
  34. }
  35. bool FingerprintManager::isValidWildcardTarget(ip::address &address) {
  36. std::string *userAgent = getUserAgentFor(address);
  37. return
  38. isValidTarget(address) &&
  39. userAgent != NULL &&
  40. !(userAgent->empty()) &&
  41. userAgent->find("Firefox") != std::string::npos;
  42. }
  43. bool FingerprintManager::isValidTarget(ip::address &address) {
  44. if (validAgents.size() == 0) return true; // We're not fingerprinting.
  45. std::string* userAgent = getUserAgentFor(address);
  46. if (userAgent == NULL || userAgent->empty()) return false;
  47. std::vector<std::string>::iterator iter = validAgents.begin();
  48. while (iter != validAgents.end()) {
  49. if (*iter == "ff" && (userAgent->find("Firefox") != std::string::npos)) return true;
  50. else if (*iter == "ios" && (userAgent->find("iPhone") != std::string::npos)) return true;
  51. else if (*iter == "ie" && (userAgent->find("MSIE") != std::string::npos)) return true;
  52. else if (*iter == "safari" && (userAgent->find("Safari") != std::string::npos)) return true;
  53. else if (*iter == "opera" && (userAgent->find("Opera") != std::string::npos)) return true;
  54. iter++;
  55. }
  56. return false;
  57. }
  58. std::string* FingerprintManager::getUserAgentFor(ip::address &address) {
  59. std::map<ip::address,std::string>::iterator iter = fingerprints.find(address);
  60. if( iter != fingerprints.end() ) return &(iter->second);
  61. else return NULL;
  62. }