HttpConnectionManager.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "HttpConnectionManager.hpp"
  20. #include <boost/bind.hpp>
  21. #include <boost/asio.hpp>
  22. #include <boost/noncopyable.hpp>
  23. #include <boost/shared_ptr.hpp>
  24. #include <iostream>
  25. #include <string>
  26. #include <map>
  27. #include "../util/Destination.hpp"
  28. #include "../util/Util.hpp"
  29. #include "../FingerprintManager.hpp"
  30. #include "../FirefoxUpdater.hpp"
  31. #include "../UpdateManager.hpp"
  32. // Public
  33. using namespace boost::asio;
  34. HttpConnectionManager::HttpConnectionManager(io_service& io_service, int port,
  35. CertificateManager &certificateManager,
  36. bool denyOCSP)
  37. : acceptor_(io_service, ip::tcp::endpoint(ip::tcp::v4(), port)),
  38. port_(port),
  39. certificateManager(certificateManager),
  40. denyOCSP(denyOCSP)
  41. {
  42. if (port != -1)
  43. acceptIncomingConnection();
  44. }
  45. void HttpConnectionManager::acceptIncomingConnection() {
  46. boost::shared_ptr<ip::tcp::socket> socket(new ip::tcp::socket(acceptor_.get_io_service()));
  47. acceptor_.async_accept(*socket, boost::bind(&HttpConnectionManager::handleClientConnection,
  48. this, socket, placeholders::error));
  49. }
  50. void HttpConnectionManager::bridgeHttpRequest(boost::shared_ptr<ip::tcp::socket> socket,
  51. ip::tcp::endpoint destination)
  52. {
  53. Bridge::ptr bridge = HttpBridge::create(socket, acceptor_.get_io_service(),
  54. FingerprintManager::getInstance());
  55. bridge->getServerSocket().
  56. async_connect(destination, boost::bind(&HttpConnectionManager::handleServerConnection,
  57. this, bridge, placeholders::error));
  58. }
  59. void HttpConnectionManager::handleClientConnection(boost::shared_ptr<ip::tcp::socket> socket,
  60. const boost::system::error_code& error)
  61. {
  62. if (error) {
  63. socket->close();
  64. Logger::logError("HTTP Accept Error...");
  65. return;
  66. }
  67. try {
  68. ip::tcp::endpoint destination;
  69. Destination::getOriginalDestination(*socket, destination);
  70. if (denyOCSP && certificateManager.isOCSPAddress(destination))
  71. OCSPDenier::getInstance()->denyOCSPRequest(socket);
  72. else
  73. bridgeHttpRequest(socket, destination);
  74. } catch (IndeterminateDestinationException &exception) {
  75. std::cerr << "Error: Could not determine original destination..." << std::endl;
  76. }
  77. acceptIncomingConnection();
  78. }
  79. void HttpConnectionManager::handleServerConnection(Bridge::ptr bridge,
  80. const boost::system::error_code& error)
  81. {
  82. if (!error) {
  83. bridge->shuttle();
  84. } else {
  85. Logger::logError("HTTP Connect Error");
  86. bridge->close();
  87. }
  88. }