You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
4.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_PROXYSERVER_H_
  28. #define TALK_BASE_PROXYSERVER_H_
  29. #include <list>
  30. #include "talk/base/asyncsocket.h"
  31. #include "talk/base/socketadapters.h"
  32. #include "talk/base/socketaddress.h"
  33. #include "talk/base/stream.h"
  34. namespace talk_base {
  35. class SocketFactory;
  36. // ProxyServer is a base class that allows for easy construction of proxy
  37. // servers. With its helper class ProxyBinding, it contains all the necessary
  38. // logic for receiving and bridging connections. The specific client-server
  39. // proxy protocol is implemented by an instance of the AsyncProxyServerSocket
  40. // class; children of ProxyServer implement WrapSocket appropriately to return
  41. // the correct protocol handler.
  42. class ProxyBinding : public sigslot::has_slots<> {
  43. public:
  44. ProxyBinding(AsyncProxyServerSocket* in_socket, AsyncSocket* out_socket);
  45. sigslot::signal1<ProxyBinding*> SignalDestroyed;
  46. private:
  47. void OnConnectRequest(AsyncProxyServerSocket* socket,
  48. const SocketAddress& addr);
  49. void OnInternalRead(AsyncSocket* socket);
  50. void OnInternalWrite(AsyncSocket* socket);
  51. void OnInternalClose(AsyncSocket* socket, int err);
  52. void OnExternalConnect(AsyncSocket* socket);
  53. void OnExternalRead(AsyncSocket* socket);
  54. void OnExternalWrite(AsyncSocket* socket);
  55. void OnExternalClose(AsyncSocket* socket, int err);
  56. static void Read(AsyncSocket* socket, FifoBuffer* buffer);
  57. static void Write(AsyncSocket* socket, FifoBuffer* buffer);
  58. void Destroy();
  59. static const int kBufferSize = 4096;
  60. scoped_ptr<AsyncProxyServerSocket> int_socket_;
  61. scoped_ptr<AsyncSocket> ext_socket_;
  62. bool connected_;
  63. FifoBuffer out_buffer_;
  64. FifoBuffer in_buffer_;
  65. DISALLOW_EVIL_CONSTRUCTORS(ProxyBinding);
  66. };
  67. class ProxyServer : public sigslot::has_slots<> {
  68. public:
  69. ProxyServer(SocketFactory* int_factory, const SocketAddress& int_addr,
  70. SocketFactory* ext_factory, const SocketAddress& ext_ip);
  71. virtual ~ProxyServer();
  72. protected:
  73. void OnAcceptEvent(AsyncSocket* socket);
  74. virtual AsyncProxyServerSocket* WrapSocket(AsyncSocket* socket) = 0;
  75. void OnBindingDestroyed(ProxyBinding* binding);
  76. private:
  77. typedef std::list<ProxyBinding*> BindingList;
  78. SocketFactory* ext_factory_;
  79. SocketAddress ext_ip_;
  80. scoped_ptr<AsyncSocket> server_socket_;
  81. BindingList bindings_;
  82. DISALLOW_EVIL_CONSTRUCTORS(ProxyServer);
  83. };
  84. // SocksProxyServer is a simple extension of ProxyServer to implement SOCKS.
  85. class SocksProxyServer : public ProxyServer {
  86. public:
  87. SocksProxyServer(SocketFactory* int_factory, const SocketAddress& int_addr,
  88. SocketFactory* ext_factory, const SocketAddress& ext_ip)
  89. : ProxyServer(int_factory, int_addr, ext_factory, ext_ip) {
  90. }
  91. protected:
  92. AsyncProxyServerSocket* WrapSocket(AsyncSocket* socket) {
  93. return new AsyncSocksProxyServerSocket(socket);
  94. }
  95. DISALLOW_EVIL_CONSTRUCTORS(SocksProxyServer);
  96. };
  97. } // namespace talk_base
  98. #endif // TALK_BASE_PROXYSERVER_H_