/* * Copyright (c) 2012-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once #include #include #include #include #include #include namespace Swift { class NetworkEnvironment; class NATTraverser; class NATTraversalGetPublicIPRequest; class NATTraversalForwardPortRequest; class NATTraversalRemovePortForwardingRequest; class SOCKS5BytestreamRegistry; class ConnectionServerFactory; class ConnectionServer; class SOCKS5BytestreamServer; class SOCKS5BytestreamServerResourceUser; class SOCKS5BytestreamServerPortForwardingUser; class SWIFTEN_API SOCKS5BytestreamServerManager { friend class SOCKS5BytestreamServerResourceUser; friend class SOCKS5BytestreamServerPortForwardingUser; public: SOCKS5BytestreamServerManager( SOCKS5BytestreamRegistry* bytestreamRegistry, ConnectionServerFactory* connectionServerFactory, NetworkEnvironment* networkEnvironment, NATTraverser* natTraverser); ~SOCKS5BytestreamServerManager(); std::shared_ptr aquireResourceUser(); std::shared_ptr aquirePortForwardingUser(); void stop(); std::vector getHostAddressPorts() const; std::vector getAssistedHostAddressPorts() const; SOCKS5BytestreamServer* getServer() const { return server; } private: bool isInitialized() const; void initialize(); bool isPortForwardingReady() const; void setupPortForwarding(); void removePortForwarding(); void checkInitializeFinished(); void handleGetPublicIPResult(boost::optional address); void handleForwardPortResult(boost::optional mapping); void handleUnforwardPortResult(boost::optional result); boost::signals2::signal onInitialized; boost::signals2::signal onPortForwardingSetup; private: friend class SOCKS5BytestreamServerInitializeRequest; SOCKS5BytestreamRegistry* bytestreamRegistry; ConnectionServerFactory* connectionServerFactory; NetworkEnvironment* networkEnvironment; NATTraverser* natTraverser; enum { Start, Initializing, Initialized } state; SOCKS5BytestreamServer* server; std::shared_ptr connectionServer; unsigned short connectionServerPort = 0; std::shared_ptr getPublicIPRequest; std::shared_ptr forwardPortRequest; std::shared_ptr unforwardPortRequest; boost::optional publicAddress; boost::optional portMapping; bool attemptedPortMapping_; std::weak_ptr s5bServerResourceUser_; std::weak_ptr s5bServerPortForwardingUser_; }; }