├── .gitignore ├── LICENSE.md ├── README.md ├── doc ├── creation tunnel.jpg ├── ipc communication.jpg ├── ipc objects.jpg ├── ipc_arch.png ├── manager_component.jpg ├── rpc_arch.png ├── signal_component.jpg ├── thread_component.jpg ├── twainet.mdj └── twainet.odp ├── logo.png └── src ├── cpp ├── CMakeLists.txt ├── apps │ ├── CMakeLists.txt │ ├── deamon │ │ ├── CMakeLists.txt │ │ ├── application │ │ │ ├── application.cpp │ │ │ ├── application.h │ │ │ ├── config.cpp │ │ │ ├── config.h │ │ │ ├── configs.cpp │ │ │ ├── configs.h │ │ │ └── main.cpp │ │ ├── deamon.vcproj │ │ ├── deamon.vcxproj │ │ ├── deamon.vcxproj.filters │ │ ├── module │ │ │ ├── deamon_module.cpp │ │ │ └── deamon_module.h │ │ ├── twainet.conf │ │ ├── twndeamon │ │ └── twndeamon.service │ └── remote_terminal │ │ ├── CMakeLists.txt │ │ ├── application │ │ ├── application.cpp │ │ ├── application.h │ │ └── main.cpp │ │ ├── messages │ │ └── terminal.proto │ │ ├── module │ │ ├── terminal_module.cpp │ │ └── terminal_module.h │ │ └── terminal │ │ ├── auto_complete_helper.h │ │ ├── commands.cpp │ │ ├── commands.h │ │ ├── console.cpp │ │ ├── console.h │ │ ├── terminal.cpp │ │ ├── terminal.h │ │ ├── terminal_state.cpp │ │ └── terminal_state.h ├── common │ ├── SimpleIni.h │ ├── common.h │ ├── common_func.cpp │ ├── common_func.h │ ├── cpu_id.cpp │ ├── dir.cpp │ ├── dir.h │ ├── file.cpp │ ├── file.h │ ├── ini_file.cpp │ ├── ini_file.h │ ├── memory_cleaner.cpp │ ├── network.cpp │ ├── process.cpp │ ├── process.h │ ├── service_manager.cpp │ ├── service_manager.h │ ├── tstring.cpp │ ├── tstring.h │ ├── user.cpp │ └── user.h ├── external │ ├── libntlm │ │ ├── bin │ │ │ ├── libntlm-x64.dll │ │ │ └── libntlm-x86.dll │ │ └── include │ │ │ └── ntlm.h │ ├── openssl │ │ ├── lib │ │ │ ├── x64 │ │ │ │ ├── libeay32.lib │ │ │ │ └── ssleay32.lib │ │ │ └── x86 │ │ │ │ ├── libeay32.lib │ │ │ │ └── ssleay32.lib │ │ └── openssl │ │ │ ├── aes.h │ │ │ ├── asn1.h │ │ │ ├── asn1_mac.h │ │ │ ├── asn1t.h │ │ │ ├── bio.h │ │ │ ├── blowfish.h │ │ │ ├── bn.h │ │ │ ├── buffer.h │ │ │ ├── cast.h │ │ │ ├── comp.h │ │ │ ├── conf.h │ │ │ ├── conf_api.h │ │ │ ├── crypto.h │ │ │ ├── des.h │ │ │ ├── des_old.h │ │ │ ├── dh.h │ │ │ ├── dsa.h │ │ │ ├── dso.h │ │ │ ├── dtls1.h │ │ │ ├── e_os2.h │ │ │ ├── ebcdic.h │ │ │ ├── ec.h │ │ │ ├── ecdh.h │ │ │ ├── ecdsa.h │ │ │ ├── engine.h │ │ │ ├── err.h │ │ │ ├── evp.h │ │ │ ├── hmac.h │ │ │ ├── idea.h │ │ │ ├── krb5_asn.h │ │ │ ├── kssl.h │ │ │ ├── lhash.h │ │ │ ├── md2.h │ │ │ ├── md4.h │ │ │ ├── md5.h │ │ │ ├── obj_mac.h │ │ │ ├── objects.h │ │ │ ├── ocsp.h │ │ │ ├── opensslconf.h │ │ │ ├── opensslv.h │ │ │ ├── ossl_typ.h │ │ │ ├── pem.h │ │ │ ├── pem2.h │ │ │ ├── pkcs12.h │ │ │ ├── pkcs7.h │ │ │ ├── pq_compat.h │ │ │ ├── pqueue.h │ │ │ ├── rand.h │ │ │ ├── rc2.h │ │ │ ├── rc4.h │ │ │ ├── ripemd.h │ │ │ ├── rsa.h │ │ │ ├── safestack.h │ │ │ ├── sha.h │ │ │ ├── ssl.h │ │ │ ├── ssl2.h │ │ │ ├── ssl23.h │ │ │ ├── ssl3.h │ │ │ ├── stack.h │ │ │ ├── store.h │ │ │ ├── symhacks.h │ │ │ ├── tls1.h │ │ │ ├── tmdiff.h │ │ │ ├── txt_db.h │ │ │ ├── ui.h │ │ │ ├── ui_compat.h │ │ │ ├── vssver.scc │ │ │ ├── x509.h │ │ │ ├── x509_vfy.h │ │ │ └── x509v3.h │ ├── protobuf-2.5.0 │ │ ├── LICENSE.txt │ │ ├── install.bat │ │ ├── install.sh │ │ └── vsprojects │ │ │ ├── config.h │ │ │ ├── libprotobuf.vcproj │ │ │ ├── libprotobuf.vcxproj │ │ │ └── libprotobuf.vcxproj.filters │ ├── udt4 │ │ ├── CMakeLists.txt │ │ ├── LICENSE.txt │ │ ├── src │ │ │ ├── Makefile │ │ │ ├── api.cpp │ │ │ ├── api.h │ │ │ ├── buffer.cpp │ │ │ ├── buffer.h │ │ │ ├── cache.cpp │ │ │ ├── cache.h │ │ │ ├── ccc.cpp │ │ │ ├── ccc.h │ │ │ ├── channel.cpp │ │ │ ├── channel.h │ │ │ ├── common.cpp │ │ │ ├── common.h │ │ │ ├── core.cpp │ │ │ ├── core.h │ │ │ ├── epoll.cpp │ │ │ ├── epoll.h │ │ │ ├── list.cpp │ │ │ ├── list.h │ │ │ ├── md5.cpp │ │ │ ├── md5.h │ │ │ ├── packet.cpp │ │ │ ├── packet.h │ │ │ ├── queue.cpp │ │ │ ├── queue.h │ │ │ ├── udt.h │ │ │ ├── window.cpp │ │ │ └── window.h │ │ └── win │ │ │ ├── udt.vcproj │ │ │ ├── udt.vcxproj │ │ │ └── udt.vcxproj.filters │ └── winpcap │ │ └── install.bat ├── include │ ├── appinterface.h │ ├── client_module_name.h │ ├── critical_section.h │ ├── data_message.h │ ├── deamon_message.h │ ├── default_app.h │ ├── deserializer.h │ ├── module.h │ ├── object_manager.h │ ├── ref.h │ ├── semaphore.h │ ├── serializer.h │ ├── singleton.h │ └── twainet.h ├── lib │ ├── CMakeLists.txt │ ├── client_server_lib │ │ ├── CMakeLists.txt │ │ ├── client_server_lib.vcproj │ │ ├── client_server_lib.vcxproj │ │ ├── client_server_lib.vcxproj.filters │ │ ├── connector │ │ │ ├── client_server_connector.cpp │ │ │ └── client_server_connector.h │ │ ├── message │ │ │ ├── client_server_messages.cpp │ │ │ └── client_server_messages.h │ │ └── module │ │ │ ├── client_server_module.cpp │ │ │ ├── client_server_module.h │ │ │ ├── client_server_signal_handler.cpp │ │ │ └── client_server_signal_handler.h │ ├── connector_lib │ │ ├── CMakeLists.txt │ │ ├── common │ │ │ ├── aes.cpp │ │ │ └── aes.h │ │ ├── connector │ │ │ ├── connector.cpp │ │ │ ├── connector.h │ │ │ ├── connector_factory.h │ │ │ ├── connector_manager.cpp │ │ │ └── connector_manager.h │ │ ├── connector_lib.vcproj │ │ ├── connector_lib.vcxproj │ │ ├── connector_lib.vcxproj.filters │ │ ├── handler │ │ │ ├── data_handler.h │ │ │ ├── message_handler.cpp │ │ │ ├── message_handler.h │ │ │ └── sender.h │ │ ├── message │ │ │ ├── connector_messages.cpp │ │ │ └── connector_messages.h │ │ ├── signal │ │ │ ├── receiver_func.h │ │ │ ├── signal.cpp │ │ │ ├── signal.h │ │ │ ├── signal_manager.cpp │ │ │ ├── signal_manager.h │ │ │ ├── signal_message.h │ │ │ ├── signal_owner.cpp │ │ │ ├── signal_owner.h │ │ │ ├── signal_receiver.cpp │ │ │ └── signal_receiver.h │ │ ├── socket │ │ │ ├── any_socket.h │ │ │ ├── proxy_socket.cpp │ │ │ ├── proxy_socket.h │ │ │ ├── secure_socket.cpp │ │ │ ├── secure_socket.h │ │ │ ├── socket_factories.cpp │ │ │ ├── socket_factories.h │ │ │ ├── socket_factory.h │ │ │ ├── tcp_socket.cpp │ │ │ ├── tcp_socket.h │ │ │ ├── udp_socket.cpp │ │ │ ├── udp_socket.h │ │ │ ├── udt_socket.cpp │ │ │ └── udt_socket.h │ │ └── thread │ │ │ ├── address.h │ │ │ ├── connect_thread.cpp │ │ │ ├── connect_thread.h │ │ │ ├── listen_thread.cpp │ │ │ └── listen_thread.h │ ├── ipc_lib │ │ ├── CMakeLists.txt │ │ ├── connector │ │ │ ├── internal_connector.cpp │ │ │ ├── internal_connector.h │ │ │ ├── ipc_connector.cpp │ │ │ ├── ipc_connector.h │ │ │ ├── ipc_connector_factory.h │ │ │ ├── ipc_handler.cpp │ │ │ ├── ipc_handler.h │ │ │ └── proto_message.h │ │ ├── ipc_lib.vcproj │ │ ├── ipc_lib.vcxproj │ │ ├── ipc_lib.vcxproj.filters │ │ ├── module │ │ │ ├── ipc_module.cpp │ │ │ ├── ipc_module.h │ │ │ ├── ipc_object_name.cpp │ │ │ ├── ipc_object_name.h │ │ │ ├── ipc_signal_handler.cpp │ │ │ └── ipc_signal_handler.h │ │ └── thread │ │ │ ├── ipc_checker_thread.cpp │ │ │ ├── ipc_checker_thread.h │ │ │ ├── ping_thread.cpp │ │ │ └── ping_thread.h │ ├── thread_lib │ │ ├── CMakeLists.txt │ │ ├── common │ │ │ ├── managers_container.cpp │ │ │ ├── managers_container.h │ │ │ ├── thread_singleton.h │ │ │ ├── timer.cpp │ │ │ └── timer.h │ │ ├── thread │ │ │ ├── thread.cpp │ │ │ ├── thread.h │ │ │ ├── thread_impl.cpp │ │ │ ├── thread_impl.h │ │ │ ├── thread_manager.cpp │ │ │ └── thread_manager.h │ │ ├── thread_lib.vcproj │ │ ├── thread_lib.vcxproj │ │ └── thread_lib.vcxproj.filters │ ├── tunnel_lib │ │ ├── CMakeLists.txt │ │ ├── connector │ │ │ ├── tunnel_connector.cpp │ │ │ ├── tunnel_connector.h │ │ │ ├── tunnel_server_connector.cpp │ │ │ └── tunnel_server_connector.h │ │ ├── message │ │ │ ├── tunnel_messages.cpp │ │ │ └── tunnel_messages.h │ │ ├── module │ │ │ ├── client_signal_handler.cpp │ │ │ ├── client_signal_handler.h │ │ │ ├── server_signal_handler.cpp │ │ │ ├── server_signal_handler.h │ │ │ ├── tunnel_containers.cpp │ │ │ ├── tunnel_containers.h │ │ │ ├── tunnel_module.cpp │ │ │ └── tunnel_module.h │ │ ├── thread │ │ │ ├── external_connect_thread.cpp │ │ │ ├── external_connect_thread.h │ │ │ ├── external_listen_thread.cpp │ │ │ ├── external_listen_thread.h │ │ │ ├── external_recv_thread.cpp │ │ │ ├── external_recv_thread.h │ │ │ ├── relay_listen_thread.cpp │ │ │ ├── relay_listen_thread.h │ │ │ ├── tunnel_checker_thread.cpp │ │ │ ├── tunnel_checker_thread.h │ │ │ └── tunnel_server_listen_address.h │ │ ├── tunnel_lib.vcproj │ │ ├── tunnel_lib.vcxproj │ │ └── tunnel_lib.vcxproj.filters │ └── twainet │ │ ├── CMakeLists.txt │ │ ├── application │ │ ├── application.cpp │ │ ├── application.h │ │ ├── dllmain.cpp │ │ └── twainet.cpp │ │ ├── message │ │ ├── NotificationMessages.cpp │ │ └── NotificationMessages.h │ │ ├── module │ │ ├── twainet_module.cpp │ │ └── twainet_module.h │ │ ├── resource.h │ │ ├── twainet.rc │ │ ├── twainet.vcproj │ │ ├── twainet.vcxproj │ │ └── twainet.vcxproj.filters ├── test │ ├── Application.cpp │ ├── Application.h │ ├── CMakeLists.txt │ ├── stdafx.cpp │ ├── stdafx.h │ ├── targetver.h │ ├── test.cpp │ ├── test.vcproj │ ├── test.vcxproj │ ├── test.vcxproj.filters │ ├── test_module.cpp │ └── test_module.h ├── twainet.plugins ├── twainet.trusted ├── twainet2008.sln ├── twainet2010.sln ├── utils │ ├── ConvertUTF.c │ ├── ConvertUTF.h │ ├── base64.cpp │ ├── base64.h │ ├── logger.cpp │ ├── logger.h │ ├── path_parser.cpp │ ├── path_parser.h │ ├── utils.cpp │ └── utils.h └── version.h ├── js ├── install.bat ├── install.js ├── node_modules │ ├── clientServerConnector.js │ ├── clientServerModule.js │ ├── ipcConnector.js │ ├── ipcModule.js │ ├── relayServerModule.js │ ├── twainetConnector.js │ ├── twainetModule.js │ ├── twainetUtils.js │ └── udpServer.js ├── server.bat └── server.js └── messages ├── client_server.proto ├── deamon.proto ├── interconn.proto ├── ipc.proto └── protoc.exe /.gitignore: -------------------------------------------------------------------------------- 1 | /output 2 | /src/cpp/build 3 | /src/messages/*.pb.* 4 | /src/cpp/apps/remote_terminal/messages/*.pb.* 5 | -------------------------------------------------------------------------------- /doc/creation tunnel.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/doc/creation tunnel.jpg -------------------------------------------------------------------------------- /doc/ipc communication.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/doc/ipc communication.jpg -------------------------------------------------------------------------------- /doc/ipc objects.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/doc/ipc objects.jpg -------------------------------------------------------------------------------- /doc/ipc_arch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/doc/ipc_arch.png -------------------------------------------------------------------------------- /doc/manager_component.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/doc/manager_component.jpg -------------------------------------------------------------------------------- /doc/rpc_arch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/doc/rpc_arch.png -------------------------------------------------------------------------------- /doc/signal_component.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/doc/signal_component.jpg -------------------------------------------------------------------------------- /doc/thread_component.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/doc/thread_component.jpg -------------------------------------------------------------------------------- /doc/twainet.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/doc/twainet.odp -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/logo.png -------------------------------------------------------------------------------- /src/cpp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | project(twainet) 3 | 4 | if(NOT CMAKE_BUILD_TYPE) 5 | set(CMAKE_BUILD_TYPE "Debug") 6 | # set(CMAKE_BUILD_TYPE "Release") 7 | endif(NOT CMAKE_BUILD_TYPE) 8 | 9 | if(NOT USE_SYSTEM_PROTOBUF) 10 | # set(USE_SYSTEM_PROTOBUF 0) 11 | set(USE_SYSTEM_PROTOBUF 1) 12 | endif(NOT USE_SYSTEM_PROTOBUF) 13 | 14 | add_subdirectory(lib) 15 | add_subdirectory(external/udt4) 16 | add_subdirectory(apps) 17 | add_subdirectory(test) 18 | 19 | add_custom_target(pre-build 20 | COMMAND ./install.sh 21 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/external/protobuf-2.5.0 22 | ) 23 | 24 | add_custom_target(uninstall 25 | COMMAND xargs rm < install_manifest.txt 26 | ) 27 | 28 | add_custom_target(clean-all 29 | COMMAND rm -r * 30 | ) 31 | -------------------------------------------------------------------------------- /src/cpp/apps/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | 3 | if(NOT CMAKE_BUILD_TYPE) 4 | set(CMAKE_BUILD_TYPE "Debug") 5 | # set(CMAKE_BUILD_TYPE "Release") 6 | endif(NOT CMAKE_BUILD_TYPE) 7 | 8 | add_subdirectory(deamon) 9 | add_subdirectory(remote_terminal) 10 | -------------------------------------------------------------------------------- /src/cpp/apps/deamon/application/application.cpp: -------------------------------------------------------------------------------- 1 | #include "application.h" 2 | #include "module/deamon_module.h" 3 | #ifndef WIN32 4 | # include 5 | #endif/*WIN32*/ 6 | 7 | DeamonApplication::DeamonApplication() 8 | { 9 | } 10 | 11 | DeamonApplication::~DeamonApplication() 12 | { 13 | } 14 | 15 | void DeamonApplication::OnModuleCreationFailed(Twainet::Module module) 16 | { 17 | if(strcmp(Twainet::GetModuleName(module).m_name, COORDINATOR_NAME) == 0) 18 | { 19 | Stop(); 20 | } 21 | } 22 | 23 | void DeamonApplication::OnServerCreationFailed(Twainet::Module module) 24 | { 25 | if(strcmp(Twainet::GetModuleName(module).m_name, COORDINATOR_NAME) == 0) 26 | { 27 | Stop(); 28 | } 29 | } 30 | 31 | void DeamonApplication::InitializeApplication() 32 | { 33 | DeamonModule* module = new DeamonModule; 34 | AddModule(module); 35 | module->Init(); 36 | } 37 | 38 | std::string DeamonApplication::GetAppName() 39 | { 40 | return "TwainetDeamon"; 41 | } 42 | 43 | std::string DeamonApplication::GetDescription() 44 | { 45 | return "Twainet deamon application"; 46 | } -------------------------------------------------------------------------------- /src/cpp/apps/deamon/application/application.h: -------------------------------------------------------------------------------- 1 | #ifndef APPLICATION_H 2 | #define APPLICATION_H 3 | 4 | #include 5 | #include 6 | #include "include/module.h" 7 | #include "include/default_app.h" 8 | 9 | class DeamonApplication : public DefaultApplication 10 | { 11 | friend class Singleton; 12 | DeamonApplication(); 13 | public: 14 | virtual ~DeamonApplication(); 15 | 16 | virtual void InitializeApplication(); 17 | 18 | static std::string GetAppName(); 19 | static std::string GetDescription(); 20 | protected: 21 | friend class IApplication; 22 | virtual void OnModuleCreationFailed(Twainet::Module module); 23 | virtual void OnServerCreationFailed(Twainet::Module module); 24 | }; 25 | 26 | #endif/*APPLICATION_H*/ -------------------------------------------------------------------------------- /src/cpp/apps/deamon/application/config.cpp: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include "common/dir.h" 3 | #include "common/common.h" 4 | #include "common/file.h" 5 | #include "utils/utils.h" 6 | 7 | #define MAX_BUFFER_LEN 1024 8 | 9 | Config::Config() 10 | : m_file(Dir::GetConfigDir() + "/twainet.conf") 11 | { 12 | m_file.Load(); 13 | } 14 | 15 | Config::~Config() 16 | { 17 | m_file.Save(); 18 | } 19 | 20 | std::string Config::GetTrustedFileName() 21 | { 22 | return m_file.getString("common", "trustedFileName", "twainet.trusted"); 23 | } 24 | 25 | std::string Config::GetPluginsFileName() 26 | { 27 | return m_file.getString("common", "pluginsFileName", "twainet.plugins"); 28 | } 29 | 30 | int Config::GetLocalServerPort() 31 | { 32 | return m_file.getLong("common", "localPort", g_localServerPort); 33 | } 34 | 35 | void Config::SetLocalServerPort(int port) 36 | { 37 | m_file.setLong("common", "localPort", port); 38 | } 39 | 40 | FileConfig::FileConfig(const std::string& filePath) 41 | : m_filePath(filePath){} 42 | 43 | FileConfig::~FileConfig(){} 44 | 45 | std::vector FileConfig::Read() 46 | { 47 | std::vector dataContainer; 48 | File configFile(m_filePath); 49 | int filesize = configFile.GetFileSize(), filepos = 0; 50 | char* data = new char[MAX_BUFFER_LEN]; 51 | char* dataPos = data; 52 | while(filepos < filesize) 53 | { 54 | unsigned int size = MAX_BUFFER_LEN - (unsigned int)(dataPos - data) ; 55 | memset(dataPos, 0, size); 56 | if(!configFile.Read(dataPos, &size)) 57 | { 58 | break; 59 | } 60 | filepos += size; 61 | 62 | std::vector lines = CommonUtils::DelimitString(data, "\n"); 63 | int pos = 0; 64 | for(int i = 0; i < (int)lines.size(); i++) 65 | { 66 | pos += lines[i].size(); 67 | std::string line = lines[i]; 68 | if(line[line.size() - 1] == '\r') 69 | line.erase(line.size() - 1, 1); 70 | if(line[0] == '#' || line.empty()) 71 | continue; 72 | 73 | dataContainer.push_back(line); 74 | } 75 | } 76 | 77 | delete data; 78 | return dataContainer; 79 | } 80 | 81 | void FileConfig::Write(const std::string& description, const std::vector& data) 82 | { 83 | File configFile(m_filePath); 84 | configFile.Open("wt"); 85 | configFile.Write("# ", 2); 86 | configFile.Write(description.c_str(), description.size()); 87 | configFile.Write("\n", 2); 88 | for(std::vector::const_iterator it = data.begin(); 89 | it != data.end(); it++) 90 | { 91 | configFile.Write(it->c_str(), it->size()); 92 | configFile.Write("\n", 1); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/cpp/apps/deamon/application/config.h: -------------------------------------------------------------------------------- 1 | #ifndef CONFIG_H 2 | #define CONFIG_H 3 | 4 | #include 5 | #include 6 | #include "common/ini_file.h" 7 | #include "include/singleton.h" 8 | 9 | class Config : public Singleton 10 | { 11 | private: 12 | friend class Singleton; 13 | Config(); 14 | virtual ~Config(); 15 | 16 | public: 17 | std::string GetTrustedFileName(); 18 | std::string GetPluginsFileName(); 19 | int GetLocalServerPort(); 20 | void SetLocalServerPort(int port); 21 | private: 22 | IniFile m_file; 23 | }; 24 | 25 | class FileConfig 26 | { 27 | public: 28 | FileConfig(const std::string& filePath); 29 | virtual ~FileConfig(); 30 | protected: 31 | std::vector Read(); 32 | void Write(const std::string& description, const std::vector& data); 33 | private: 34 | std::string m_filePath; 35 | }; 36 | 37 | #endif/*CONFIG_H*/ -------------------------------------------------------------------------------- /src/cpp/apps/deamon/application/configs.cpp: -------------------------------------------------------------------------------- 1 | #include "configs.h" 2 | #include 3 | 4 | TrustedConfig::TrustedConfig(const std::string& filePath) 5 | : FileConfig(filePath) 6 | { 7 | m_trustedModules = Read(); 8 | } 9 | 10 | TrustedConfig::~TrustedConfig() 11 | { 12 | Write("Module names that will get local server credentials", m_trustedModules); 13 | } 14 | 15 | std::vector TrustedConfig::getTrustedModules() 16 | { 17 | return m_trustedModules; 18 | } 19 | 20 | void TrustedConfig::setTrustedModules(const std::vector& modules) 21 | { 22 | m_trustedModules = modules; 23 | } 24 | 25 | PluginsConfig::PluginsConfig(const std::string& filePath) 26 | : FileConfig(filePath) 27 | { 28 | m_pluginsPaths = Read(); 29 | } 30 | 31 | PluginsConfig::~PluginsConfig() 32 | { 33 | Write("Plugin pathes", m_pluginsPaths); 34 | } 35 | 36 | std::vector PluginsConfig::getPluginsPaths() 37 | { 38 | return m_pluginsPaths; 39 | } 40 | 41 | void PluginsConfig::setPluginsPath(const std::vector& paths) 42 | { 43 | m_pluginsPaths = paths; 44 | } -------------------------------------------------------------------------------- /src/cpp/apps/deamon/application/configs.h: -------------------------------------------------------------------------------- 1 | #ifndef CONFIGS_H 2 | #define CONFIGS_H 3 | 4 | #include "config.h" 5 | 6 | class TrustedConfig : public FileConfig 7 | { 8 | public: 9 | TrustedConfig(const std::string& filePath); 10 | ~TrustedConfig(); 11 | 12 | std::vector getTrustedModules(); 13 | void setTrustedModules(const std::vector& modules); 14 | private: 15 | std::vector m_trustedModules; 16 | }; 17 | 18 | class PluginsConfig : public FileConfig 19 | { 20 | public: 21 | PluginsConfig(const std::string& filePath); 22 | ~PluginsConfig(); 23 | 24 | std::vector getPluginsPaths(); 25 | void setPluginsPath(const std::vector& paths); 26 | private: 27 | std::vector m_pluginsPaths; 28 | }; 29 | 30 | #endif/*CONFIGS_H*/ -------------------------------------------------------------------------------- /src/cpp/apps/deamon/module/deamon_module.h: -------------------------------------------------------------------------------- 1 | #ifndef DEAMON_MODULE_H 2 | #define DEAMON_MODULE_H 3 | 4 | #include "include/module.h" 5 | 6 | #define COORDINATOR_NAME "twndeamon" 7 | 8 | class DeamonModule; 9 | 10 | typedef UserMessage TestMessage; 11 | 12 | class DeamonModule : public Module 13 | { 14 | public: 15 | DeamonModule(); 16 | virtual ~DeamonModule(); 17 | 18 | void Init(); 19 | 20 | protected: 21 | virtual void OnModuleConnected(const Twainet::ModuleName& moduleName); 22 | 23 | protected: 24 | virtual void OnConfigChanged(const SetConfig& msg); 25 | 26 | private: 27 | template friend class UserMessage; 28 | void onMessage(const Test& test, Twainet::ModuleName path); 29 | private: 30 | void ReadConfig(); 31 | 32 | private: 33 | Twainet::UserPassword m_userPassword; 34 | std::vector m_trustedModules; 35 | std::vector m_pluginPaths; 36 | }; 37 | 38 | #endif/*DEAMON_MODULE_H*/ 39 | -------------------------------------------------------------------------------- /src/cpp/apps/deamon/twainet.conf: -------------------------------------------------------------------------------- 1 | [common] 2 | pluginsFileName=twainet.plugins 3 | trustedFileName=twainet.trusted 4 | localPort=2486 5 | -------------------------------------------------------------------------------- /src/cpp/apps/deamon/twndeamon: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ### BEGIN INIT INFO 3 | # Provides: twndeamon 4 | # Required-Start: $remote_fs $syslog $network-manager 5 | # Required-Stop: $remote_fs $syslog $network-manager 6 | # Default-Start: 3 4 5 7 | # Default-Stop: 3 4 5 8 | # Short-Description: twainet deamon 9 | # Description: Twainet is a simple interprocess messaging system, used 10 | # for sending messages between applications. 11 | ### END INIT INFO 12 | # -*- coding: utf-8 -*- 13 | # Copyright © 2015 Alexey Volokitin 14 | 15 | DAEMON=/usr/bin/twndeamon 16 | NAME=twndeamon 17 | PIDDIR=/var/run 18 | PIDFILE=$PIDDIR/$NAME.pid 19 | DESC="twainet deamon" 20 | 21 | test -x $DAEMON || exit 0 22 | 23 | . /lib/lsb/init-functions 24 | 25 | start_it_up() 26 | { 27 | if [ -e $PIDFILE ]; then 28 | if $0 status > /dev/null ; then 29 | log_success_msg "$DESC already started; not starting." 30 | return 31 | else 32 | log_success_msg "Removing stale PID file $PIDFILE." 33 | rm -f $PIDFILE 34 | fi 35 | fi 36 | 37 | log_daemon_msg "Starting $DESC" "$NAME" 38 | start-stop-daemon --start --oknodo --pidfile $PIDFILE \ 39 | --exec $DAEMON -- start 40 | log_end_msg $? 41 | } 42 | 43 | shut_it_down() 44 | { 45 | log_daemon_msg "Stopping $DESC" "$NAME" 46 | start-stop-daemon --stop --retry 5 --oknodo --pidfile $PIDFILE 47 | log_end_msg $? 48 | rm -f $PIDFILE 49 | } 50 | 51 | case "$1" in 52 | start) 53 | start_it_up 54 | ;; 55 | stop) 56 | shut_it_down 57 | ;; 58 | restart) 59 | shut_it_down 60 | start_it_up 61 | ;; 62 | status) 63 | status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $? 64 | ;; 65 | *) 66 | echo "Usage: /etc/init.d/$NAME {start|stop|restart|status}" >&2 67 | exit 2 68 | ;; 69 | esac -------------------------------------------------------------------------------- /src/cpp/apps/deamon/twndeamon.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=twainet deamon 3 | After=syslog.target 4 | 5 | [Service] 6 | ExecStart=/usr/bin/twndeamon -start 7 | Type=forking 8 | Restart=always 9 | 10 | [Install] 11 | WantedBy=multi-user.target 12 | -------------------------------------------------------------------------------- /src/cpp/apps/remote_terminal/application/application.cpp: -------------------------------------------------------------------------------- 1 | #include "application.h" 2 | 3 | ApplicationTerminal::ApplicationTerminal() 4 | { 5 | 6 | } 7 | 8 | ApplicationTerminal::~ApplicationTerminal() 9 | { 10 | } 11 | 12 | void ApplicationTerminal::InitializeApplication() 13 | { 14 | TerminalModule* module = new TerminalModule; 15 | AddModule(module); 16 | module->Init(); 17 | } 18 | 19 | std::string ApplicationTerminal::GetAppName() 20 | { 21 | return "Remote Terminal"; 22 | } 23 | 24 | std::string ApplicationTerminal::GetDescription() 25 | { 26 | return "Remote terminal for clever home"; 27 | } 28 | 29 | void ApplicationTerminal::OnModuleCreationFailed(Twainet::Module module) 30 | { 31 | Stop(); 32 | } 33 | 34 | void ApplicationTerminal::OnServerCreationFailed(Twainet::Module module) 35 | { 36 | Stop(); 37 | } 38 | -------------------------------------------------------------------------------- /src/cpp/apps/remote_terminal/application/application.h: -------------------------------------------------------------------------------- 1 | #ifndef APPLICATION_H 2 | #define APPLICATION_H 3 | 4 | #include "include/twainet.h" 5 | #include "include/default_app.h" 6 | #include "module/terminal_module.h" 7 | 8 | class ApplicationTerminal : public DefaultApplication 9 | { 10 | friend class Singleton; 11 | ApplicationTerminal(); 12 | public: 13 | ~ApplicationTerminal(); 14 | 15 | virtual void InitializeApplication(); 16 | 17 | static std::string GetAppName(); 18 | static std::string GetDescription(); 19 | protected: 20 | friend class IApplication; 21 | virtual void OnModuleCreationFailed(Twainet::Module module); 22 | virtual void OnServerCreationFailed(Twainet::Module module); 23 | }; 24 | 25 | #endif/*APPLICATION_H*/ 26 | -------------------------------------------------------------------------------- /src/cpp/apps/remote_terminal/application/main.cpp: -------------------------------------------------------------------------------- 1 | #ifdef WIN32 2 | # include "stdafx.h" 3 | # include 4 | #else 5 | # include 6 | # include 7 | #endif/*WIN32*/ 8 | #include 9 | #include 10 | #include "include/twainet.h" 11 | #include "application.h" 12 | #include "terminal/terminal.h" 13 | #include "common/common_func.h" 14 | 15 | #ifdef WIN32 16 | int _tmain(int argc, _TCHAR* argv[]) 17 | #else 18 | int main(int argc, char* argv[]) 19 | #endif/*WIN32*/ 20 | { 21 | // printf("begin\n"); 22 | Terminal::GetInstance(); 23 | ApplicationTerminal::GetInstance().Run(); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /src/cpp/apps/remote_terminal/messages/terminal.proto: -------------------------------------------------------------------------------- 1 | package terminal; 2 | 3 | message term_name 4 | { 5 | required string name = 1; 6 | } 7 | 8 | message log 9 | { 10 | required uint64 time = 1; 11 | required string data = 2; 12 | } 13 | 14 | message get_command_list 15 | { 16 | } 17 | 18 | message command_list 19 | { 20 | repeated command cmd = 1; 21 | } 22 | 23 | message command 24 | { 25 | required string cmd = 1; 26 | repeated string args = 2; 27 | } 28 | -------------------------------------------------------------------------------- /src/cpp/apps/remote_terminal/module/terminal_module.h: -------------------------------------------------------------------------------- 1 | #ifndef TERMINAL_MODULE_H 2 | #define TERMINAL_MODULE_H 3 | 4 | #include "include/module.h" 5 | 6 | #pragma warning(disable:4244 4267) 7 | #include "messages/terminal.pb.h" 8 | using namespace terminal; 9 | #pragma warning(default:4244 4267) 10 | 11 | class TerminalModule; 12 | 13 | typedef UserMessage TermNameMessage; 14 | typedef UserMessage LogMessage; 15 | typedef UserMessage CommandMessage; 16 | typedef UserMessage GetCommandListMessage; 17 | typedef UserMessage CommandListMessage; 18 | 19 | struct ModuleNameCmp 20 | { 21 | bool operator()(const Twainet::ModuleName& module1, const Twainet::ModuleName& module2) 22 | { 23 | int ret = strcmp(module1.m_name, module2.m_name); 24 | if(ret == 0) { 25 | ret = strcmp(module1.m_host, module2.m_host); 26 | if(ret == 0) { 27 | ret = strcmp(module1.m_connId, module2.m_connId); 28 | } 29 | } 30 | return ret < 0; 31 | } 32 | }; 33 | 34 | class TerminalModule : public Module 35 | { 36 | public: 37 | TerminalModule(); 38 | virtual ~TerminalModule(); 39 | 40 | void Init(); 41 | 42 | std::vector getTerminalNames(); 43 | 44 | bool toMessage(const DataMessage& msg, const std::string& termName); 45 | 46 | protected: 47 | virtual void OnModuleConnected(const Twainet::ModuleName& moduleName); 48 | virtual void OnModuleDisconnected(const Twainet::ModuleName & moduleName); 49 | 50 | private: 51 | template friend class UserMessage; 52 | void onMessage(const term_name& msg, Twainet::ModuleName path); 53 | void onMessage(const log& msg, Twainet::ModuleName path); 54 | void onMessage(const command_list& msg, Twainet::ModuleName path); 55 | 56 | //stubs 57 | void onMessage(const command& msg, Twainet::ModuleName path){} 58 | void onMessage(const get_command_list& msg, Twainet::ModuleName path){} 59 | 60 | private: 61 | Twainet::UserPassword m_userPassword; 62 | CriticalSection m_cs; 63 | std::map m_terminalMap; 64 | }; 65 | 66 | #endif/*TERMINAL_MODULE_H*/ 67 | -------------------------------------------------------------------------------- /src/cpp/apps/remote_terminal/terminal/auto_complete_helper.h: -------------------------------------------------------------------------------- 1 | #ifndef AUTO_COMPLETE_HELPER_H 2 | #define AUTO_COMPLETE_HELPER_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class AutoCompleteHelper 9 | { 10 | public: 11 | std::vector autoCompleteHelper(const std::string& word, const std::vector& words) 12 | { 13 | std::vector usewords; 14 | for(auto word_ : words) { 15 | if(word.size() <= word_.size() && memcmp(word.data(), word_.data(), word.size()) == 0) { 16 | usewords.push_back(word_); 17 | } 18 | } 19 | 20 | return usewords; 21 | } 22 | }; 23 | 24 | #endif/*AUTO_COMPLETE_HELPER_H*/ 25 | -------------------------------------------------------------------------------- /src/cpp/apps/remote_terminal/terminal/commands.h: -------------------------------------------------------------------------------- 1 | class TermState; 2 | #ifndef COMMANDS_H 3 | #define COMMANDS_H 4 | 5 | #include 6 | #include 7 | #include "terminal_state.h" 8 | #include "auto_complete_helper.h" 9 | 10 | class Terminal; 11 | 12 | class Command 13 | { 14 | public: 15 | explicit Command(const std::string& command) 16 | : m_command(command){} 17 | virtual ~Command(){} 18 | 19 | virtual void Execute(const std::vector& args) = 0; 20 | virtual bool Check(const std::string& command, const std::vector& args) const; 21 | virtual std::string GetCommand() const { return m_command; } 22 | virtual std::vector GetArgs(const std::vector& args) = 0; 23 | 24 | bool operator == (const std::string& command) const { 25 | return m_command == command; 26 | } 27 | bool operator != (const std::string& command) const { 28 | return m_command != command; 29 | } 30 | protected: 31 | std::string m_command; 32 | }; 33 | 34 | class UseCommand : public Command, public AutoCompleteHelper 35 | { 36 | public: 37 | UseCommand(); 38 | virtual ~UseCommand(); 39 | 40 | virtual void Execute(const std::vector& args); 41 | virtual bool Check(const std::string& command, const std::vector& args) const; 42 | virtual std::vector GetArgs(const std::vector& args); 43 | }; 44 | 45 | class TerminalCommand : public Command 46 | { 47 | public: 48 | TerminalCommand(const std::string& command, const std::vector& args, TerminalState* state); 49 | virtual ~TerminalCommand(); 50 | 51 | virtual void Execute(const std::vector& args); 52 | virtual bool Check(const std::string& command, const std::vector& args) const; 53 | virtual std::vector GetArgs(const std::vector& args){ return std::vector(); } 54 | private: 55 | std::vector m_args; 56 | TerminalState* m_state; 57 | }; 58 | 59 | class ExitCommand : public Command 60 | { 61 | public: 62 | ExitCommand(TerminalState* state); 63 | virtual ~ExitCommand(); 64 | 65 | virtual void Execute(const std::vector& args); 66 | virtual bool Check(const std::string& command, const std::vector& args) const; 67 | virtual std::vector GetArgs(const std::vector& args){ return std::vector(); } 68 | private: 69 | TerminalState* m_state; 70 | }; 71 | 72 | #endif/*COMMANDS_H*/ 73 | -------------------------------------------------------------------------------- /src/cpp/apps/remote_terminal/terminal/console.h: -------------------------------------------------------------------------------- 1 | #ifndef CONSOLE_H 2 | #define CONSOLE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "thread_lib/thread/thread_impl.h" 9 | 10 | class Console : public ThreadImpl 11 | { 12 | public: 13 | enum AccumTypeChar 14 | { 15 | ARROW_UP, 16 | ARROW_DOWN, 17 | ARROW_LEFT, 18 | ARROW_RIGHT, 19 | DELETE, 20 | END, 21 | HOME, 22 | PAGEUP, 23 | PAGEDOWN, 24 | INSERT 25 | }; 26 | Console(); 27 | virtual ~Console(); 28 | void Init(); 29 | void DeInit(); 30 | bool Write(const std::string& log); 31 | bool Read(std::string& buf); 32 | void SetTermName(const std::string& termName); 33 | protected: 34 | virtual void ThreadFunc(); 35 | virtual void OnStop(){} 36 | virtual void OnStart(){} 37 | virtual void Stop(){} 38 | protected: 39 | void printName(); 40 | void clearLine(); 41 | int checkAccumeChars(AccumTypeChar* ac_char); 42 | bool useChar(char simb, std::string& buf); 43 | private: 44 | std::string m_command; 45 | std::string m_termName; 46 | FILE* m_stream; 47 | CriticalSection m_cs; 48 | std::vector m_accumchars; 49 | std::vector m_history; 50 | int m_historyCounter; 51 | bool m_tab; 52 | int m_carpos; 53 | struct termios old; 54 | std::map > m_templates; 55 | }; 56 | 57 | #endif/*CONSOLE_H*/ 58 | -------------------------------------------------------------------------------- /src/cpp/apps/remote_terminal/terminal/terminal.h: -------------------------------------------------------------------------------- 1 | #ifndef TERMINAL_H 2 | #define TERMINAL_H 3 | 4 | #include 5 | #include 6 | #include "console.h" 7 | #include "terminal_state.h" 8 | #include "auto_complete_helper.h" 9 | #include "include/module.h" 10 | #include "thread_lib/common/thread_singleton.h" 11 | 12 | class TerminalModule; 13 | 14 | class Terminal : public ThreadSingleton, public AutoCompleteHelper 15 | { 16 | friend class Singleton; 17 | Terminal(); 18 | public: 19 | ~Terminal(); 20 | 21 | virtual void ThreadFunc(); 22 | virtual void OnStop(); 23 | virtual void OnStart(); 24 | virtual void Stop(); 25 | 26 | void log(const std::string& termName, uint64_t time, const std::string& data); 27 | 28 | void onTerminalDisconnected(const std::string& terminalName); 29 | 30 | bool autoComplete(std::string& command); 31 | 32 | void setCurrentState(TerminalState* state); 33 | TerminalState* getCurrentState(); 34 | 35 | void addTerminalModule(TerminalModule* module); 36 | TerminalModule* getTerminalModule(); 37 | std::vector getTerminalNames(); 38 | private: 39 | Console m_console; 40 | UseTerminal m_rootState; 41 | TerminalState* m_currentState; 42 | TerminalModule* m_terminalModule; 43 | }; 44 | 45 | #endif/*TERMINAL_H*/ 46 | -------------------------------------------------------------------------------- /src/cpp/apps/remote_terminal/terminal/terminal_state.h: -------------------------------------------------------------------------------- 1 | #ifndef TERMINAL_STATE_H 2 | #define TERMINAL_STATE_H 3 | 4 | #include 5 | #include "include/object_manager.h" 6 | 7 | class Command; 8 | 9 | class TerminalState 10 | { 11 | struct CommandChecker 12 | { 13 | std::string cmd; 14 | const std::vector& args; 15 | bool find; 16 | std::vector& avail_args; 17 | }; 18 | public: 19 | TerminalState(); 20 | virtual ~TerminalState(); 21 | 22 | void AddCommand(Command* command); 23 | 24 | virtual bool Check(const std::string& command, const std::vector& args); 25 | virtual void Execute(const std::string& command, const std::vector& args); 26 | virtual std::vector GetCommands(); 27 | virtual std::vector GetArgs(const std::string& command, const std::vector& args); 28 | virtual std::string GetTerminalName() = 0; 29 | virtual void Exit() = 0; 30 | protected: 31 | template friend class Reference; 32 | template friend class ReferenceObject; 33 | bool DeleteCommand(const Command* command); 34 | bool CheckCommand(const CommandChecker& cmd, const Command* command); 35 | bool ExecuteCommand(const CommandChecker& cmd, const Command* command); 36 | bool GetCommand(const std::vector& commands, const Command* command); 37 | bool GetArgsCommand(const CommandChecker& cmd, const Command* command); 38 | private: 39 | ObjectManager m_commands; 40 | }; 41 | 42 | class UseTerminal : public TerminalState 43 | { 44 | public: 45 | UseTerminal(); 46 | virtual std::string GetTerminalName(){ return ""; } 47 | virtual void Exit(){} 48 | }; 49 | 50 | class TerminalCommands : public TerminalState 51 | { 52 | public: 53 | TerminalCommands(const std::string& termName, TerminalState* parent); 54 | virtual ~TerminalCommands(); 55 | 56 | virtual std::string GetTerminalName(){ return m_terminalName; } 57 | virtual void Exit(); 58 | private: 59 | std::string m_terminalName; 60 | TerminalState* m_parent; 61 | }; 62 | 63 | #endif/*TERMINAL_STATE_H*/ 64 | -------------------------------------------------------------------------------- /src/cpp/common/common.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_COMMON_H 2 | #define UTILS_COMMON_H 3 | 4 | #include 5 | 6 | const int g_ipcCoordinatorPortCount = 1; 7 | const int g_ipcCoordinatorPorts[g_ipcCoordinatorPortCount] = {2041}; 8 | const int g_localServerPort = 2486; 9 | const int g_internalServerPort = 2054; 10 | 11 | #endif // UTILS_COMMON_H -------------------------------------------------------------------------------- /src/cpp/common/common_func.cpp: -------------------------------------------------------------------------------- 1 | #ifdef WIN32 2 | #include 3 | #else 4 | #include 5 | #include 6 | #include 7 | #endif 8 | 9 | #include "common_func.h" 10 | 11 | 12 | std::string CreateGUID() 13 | { 14 | std::string guidStr; 15 | #ifdef WIN32 16 | std::wstring guidWStr; 17 | guidWStr.resize(50); 18 | GUID guid; 19 | CoCreateGuid(&guid); 20 | StringFromGUID2(guid, (wchar_t*)guidWStr.c_str(), (int)guidWStr.size()); 21 | g_wcstombs(guidStr, guidWStr); 22 | #else 23 | uuid_t out; 24 | uuid_generate_time(out); 25 | for(int i = 0; i < sizeof(out); i++) 26 | { 27 | char num[10] = {0}; 28 | snprintf(num, 10, "%d", out[i]); 29 | guidStr.append(num); 30 | } 31 | #endif 32 | return guidStr.c_str(); 33 | } 34 | 35 | std::string RandString(int size) 36 | { 37 | std::string GUID = CreateGUID(); 38 | size_t charN = 0; 39 | char erazeChars[] = {'{', '}', '-'}; 40 | while(charN < 3) 41 | { 42 | size_t pos = GUID.find(erazeChars[charN]); 43 | if(pos == -1) 44 | { 45 | charN++; 46 | continue; 47 | } 48 | 49 | GUID.erase(pos, 1); 50 | } 51 | 52 | return size < (int)GUID.size() ? std::string(GUID.begin(), GUID.begin() + size) : GUID; 53 | } 54 | 55 | int GetError() 56 | { 57 | #ifdef WIN32 58 | return ::GetLastError(); 59 | #else 60 | return errno; 61 | #endif 62 | } 63 | -------------------------------------------------------------------------------- /src/cpp/common/common_func.h: -------------------------------------------------------------------------------- 1 | #ifndef GUID_GENERATOR_H 2 | #define GUID_GENERATOR_H 3 | 4 | #include "common/tstring.h" 5 | 6 | std::string CreateGUID(); 7 | std::string RandString(int size); 8 | int GetError(); 9 | 10 | #endif/*GUID_GENERATOR_H*/ -------------------------------------------------------------------------------- /src/cpp/common/cpu_id.cpp: -------------------------------------------------------------------------------- 1 | #ifndef CPU_ID_H 2 | #define CPU_ID_H 3 | 4 | #ifdef _WIN32 5 | 6 | #include 7 | 8 | unsigned short getCpuHash() 9 | { 10 | int cpuinfo[4] = { 0, 0, 0, 0 }; 11 | unsigned short hash = 0; 12 | 13 | __cpuid(cpuinfo, 0); 14 | unsigned short* ptr = (unsigned short*)(&cpuinfo[0]); 15 | for (unsigned int i = 0; i < 8; i++ ) 16 | hash += ptr[i]; 17 | 18 | return hash; 19 | } 20 | 21 | #elif DARWIN 22 | 23 | #include 24 | 25 | unsigned short getCpuHash() 26 | { 27 | const NXArchInfo* info = NXGetLocalArchInfo(); 28 | unsigned short val = 0; 29 | val += (unsigned short)info->cputype; 30 | val += (unsigned short)info->cpusubtype; 31 | return val; 32 | } 33 | 34 | #else 35 | 36 | static void getCpuid( unsigned int* p, unsigned int ax ) 37 | { 38 | __asm __volatile 39 | ( "movl %%ebx, %%esi\n\t" 40 | "cpuid\n\t" 41 | "xchgl %%ebx, %%esi" 42 | : "=a" (p[0]), "=S" (p[1]), 43 | "=c" (p[2]), "=d" (p[3]) 44 | : "0" (ax) 45 | ); 46 | } 47 | 48 | unsigned short getCpuHash() 49 | { 50 | unsigned int cpuinfo[4] = { 0, 0, 0, 0 }; 51 | getCpuid( cpuinfo, 0 ); 52 | unsigned short hash = 0; 53 | unsigned int* ptr = (&cpuinfo[0]); 54 | for ( unsigned int i = 0; i < 4; i++ ) 55 | hash += (ptr[i] & 0xFFFF) + ( ptr[i] >> 16 ); 56 | 57 | return hash; 58 | } 59 | 60 | #endif/*_WIN32*/ 61 | 62 | #endif/*CPU_ID_H*/ -------------------------------------------------------------------------------- /src/cpp/common/dir.cpp: -------------------------------------------------------------------------------- 1 | #include "dir.h" 2 | #ifdef WIN32 3 | #include 4 | #else 5 | #include 6 | #include 7 | #endif/*WIN32*/ 8 | #include "utils/path_parser.h" 9 | 10 | #ifdef WIN32 11 | #include 12 | #include 13 | #include 14 | 15 | #define getcwd _getcwd 16 | #endif 17 | 18 | std::string Dir::GetCurrentDir() 19 | { 20 | char dirname[500]; 21 | getcwd(dirname, 500); 22 | return dirname; 23 | } 24 | 25 | std::string Dir::GetProcessDir() 26 | { 27 | #ifdef WIN32 28 | char filename[500]; 29 | GetModuleFileNameA(NULL, filename, 500); 30 | PathParser parser(filename); 31 | return parser.GetFolder(); 32 | #else 33 | return ""; 34 | #endif 35 | } 36 | 37 | std::string Dir::GetCommonDir() 38 | { 39 | #ifdef WIN32 40 | char pszPath[MAX_PATH + 1] = {0}; 41 | HRESULT res = SHGetFolderPathA(NULL, CSIDL_COMMON_APPDATA, NULL, SHGFP_TYPE_CURRENT, pszPath); 42 | return pszPath; 43 | #else 44 | return "/usr/local/share"; 45 | #endif 46 | } 47 | 48 | std::string Dir::GetConfigDir() 49 | { 50 | std::string configDir; 51 | #ifdef WIN32 52 | char pszPath[MAX_PATH + 1] = {0}; 53 | HRESULT res = SHGetFolderPathA(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, pszPath); 54 | configDir = pszPath; 55 | configDir += "\\twainet"; 56 | #else 57 | if(geteuid() == 0) 58 | { 59 | configDir = "/etc"; 60 | } 61 | else 62 | { 63 | struct passwd* pw = getpwuid(getuid()); 64 | configDir = pw->pw_dir; 65 | configDir += "/.config"; 66 | } 67 | #endif 68 | return configDir; 69 | } 70 | -------------------------------------------------------------------------------- /src/cpp/common/dir.h: -------------------------------------------------------------------------------- 1 | #ifndef DIR_H 2 | #define DIR_H 3 | 4 | #include 5 | 6 | class Dir 7 | { 8 | public: 9 | static std::string GetCurrentDir(); 10 | static std::string GetProcessDir(); 11 | static std::string GetCommonDir(); 12 | static std::string GetConfigDir(); 13 | }; 14 | 15 | #endif/*DIR_H*/ -------------------------------------------------------------------------------- /src/cpp/common/file.cpp: -------------------------------------------------------------------------------- 1 | #include "file.h" 2 | 3 | #include "utils/path_parser.h" 4 | 5 | #ifdef WIN32 6 | #include 7 | #include 8 | 9 | #define access _access 10 | #define mkdir(file, param) _mkdir(file) 11 | #define fileno _fileno 12 | 13 | #else 14 | #include 15 | #endif/*WIN32*/ 16 | 17 | #include 18 | 19 | File::File(const std::string& filePath) 20 | : m_file(0) 21 | { 22 | PathParser parser(filePath); 23 | m_fileDir = parser.GetFolder(); 24 | m_fileName = parser.GetFile(); 25 | } 26 | 27 | File::~File() 28 | { 29 | Close(); 30 | } 31 | 32 | bool File::Delete() 33 | { 34 | std::string filePath = m_fileDir + "/" + m_fileName; 35 | return ::remove(filePath.c_str()) == 0; 36 | } 37 | 38 | bool File::IsExist() 39 | { 40 | std::string filePath = m_fileDir + "/" + m_fileName; 41 | return ::access(filePath.c_str(), 0) == 0; 42 | } 43 | 44 | bool File::Create() 45 | { 46 | CreateDir(); 47 | return Open("wb"); 48 | } 49 | 50 | bool File::Write(const char* data, unsigned int len) 51 | { 52 | if (m_file == 0 && !Open("a+b")) 53 | { 54 | return false; 55 | } 56 | 57 | size_t writelen = ::fwrite(data, sizeof(char), len, m_file); 58 | return writelen == len; 59 | } 60 | 61 | bool File::Read(char* data, unsigned int* len) 62 | { 63 | if (m_file == 0 && !Open("rb")) 64 | { 65 | *len = 0; 66 | return false; 67 | } 68 | 69 | size_t readlen = ::fread(data, sizeof(char), *len, m_file); 70 | 71 | if(*len != readlen) 72 | { 73 | *len = (unsigned int)readlen; 74 | return true; 75 | } 76 | 77 | return true; 78 | } 79 | 80 | bool File::Open(const std::string& mode) 81 | { 82 | Close(); 83 | std::string filePath = m_fileDir + "/" + m_fileName; 84 | #ifdef WIN32 85 | fopen_s(&m_file, filePath.c_str(), mode.c_str()); 86 | #else 87 | m_file = fopen(filePath.c_str(), mode.c_str()); 88 | #endif 89 | return m_file != 0; 90 | } 91 | 92 | bool File::Close() 93 | { 94 | int res = -1; 95 | if(m_file != 0) 96 | { 97 | ::fclose(m_file); 98 | } 99 | m_file = 0; 100 | 101 | return res == 0; 102 | } 103 | 104 | void File::CreateDir() 105 | { 106 | size_t pos = -1; 107 | do 108 | { 109 | std::string folder; 110 | pos = m_fileDir.find('/', pos + 1); 111 | if(pos != -1) 112 | { 113 | folder.append(m_fileDir.begin(), m_fileDir.begin() + pos); 114 | } 115 | else 116 | { 117 | folder.append(m_fileDir.begin(), m_fileDir.end()); 118 | } 119 | 120 | if(::access(folder.c_str(), 0) != 0) 121 | { 122 | ::mkdir(folder.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 123 | } 124 | } while (pos != -1); 125 | } 126 | 127 | int File::GetFileSize() 128 | { 129 | if (m_file == 0 && !Open("rb")) 130 | { 131 | return false; 132 | } 133 | 134 | struct stat st; 135 | ::fstat(fileno(m_file), &st); 136 | return st.st_size; 137 | } 138 | 139 | -------------------------------------------------------------------------------- /src/cpp/common/file.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_H 2 | #define FILE_H 3 | 4 | #include 5 | #include 6 | 7 | class File 8 | { 9 | public: 10 | File(const std::string& filePath); 11 | ~File(); 12 | 13 | bool Delete(); 14 | bool IsExist(); 15 | bool Create(); 16 | bool Open(const std::string& mode); 17 | bool Close(); 18 | bool Write(const char* data, unsigned int len); 19 | bool Read(char* data, unsigned int* len); 20 | int GetFileSize(); 21 | void CreateDir(); 22 | private: 23 | FILE* m_file; 24 | std::string m_fileDir; 25 | std::string m_fileName; 26 | }; 27 | 28 | #endif/*FILE_H*/ -------------------------------------------------------------------------------- /src/cpp/common/ini_file.cpp: -------------------------------------------------------------------------------- 1 | #include "ini_file.h" 2 | #ifndef WIN32 3 | #include 4 | #endif/*WIN32*/ 5 | 6 | IniFile::IniFile(const std::string& filePath) 7 | : m_filePath(filePath) 8 | { 9 | } 10 | 11 | IniFile::~IniFile() 12 | { 13 | } 14 | 15 | bool IniFile::getBool(const char* section, const char* key, bool defVal) 16 | { 17 | return m_iniFile.GetBoolValue(section, key, defVal); 18 | } 19 | 20 | bool IniFile::setBool(const char* section, const char* key, bool value) 21 | { 22 | return SI_OK == m_iniFile.SetBoolValue(section, key, value); 23 | } 24 | 25 | long IniFile::getLong(const char* section, const char* key, long defVal) 26 | { 27 | return m_iniFile.GetLongValue(section, key, defVal); 28 | } 29 | 30 | bool IniFile::setLong(const char* section, const char* key, long value) 31 | { 32 | return SI_OK == m_iniFile.SetLongValue(section, key, value); 33 | } 34 | 35 | const char* IniFile::getString(const char* section, const char* key, const char* defVal) 36 | { 37 | const char* res = m_iniFile.GetValue(section, key, defVal); 38 | if (!res) 39 | { 40 | return ""; 41 | } 42 | 43 | return res; 44 | } 45 | 46 | bool IniFile::setString(const char* section, const char* key, const char* value) 47 | { 48 | return SI_OK == m_iniFile.SetValue(section, key, value); 49 | } 50 | 51 | bool IniFile::Load() 52 | { 53 | return m_iniFile.LoadFile(m_filePath.c_str()) == SI_OK; 54 | } 55 | 56 | bool IniFile::Save() 57 | { 58 | bool ok = m_iniFile.SaveFile(m_filePath.c_str()) == SI_OK; 59 | #ifndef WIN32 60 | //change file mode so others could read it 61 | chmod(m_filePath.c_str(), S_IRUSR |S_IWUSR |S_IRGRP|S_IWGRP|S_IWOTH|S_IROTH); 62 | #endif 63 | 64 | return ok; 65 | } -------------------------------------------------------------------------------- /src/cpp/common/ini_file.h: -------------------------------------------------------------------------------- 1 | #ifndef INI_FILE_H 2 | #define INI_FILE_H 3 | 4 | #ifdef WIN32 5 | #define SI_HAS_WIDE_FILE 6 | #endif 7 | #include "SimpleIni.h" 8 | 9 | class IniFile 10 | { 11 | public: 12 | IniFile(const std::string& filePath); 13 | ~IniFile(); 14 | 15 | bool getBool(const char* section, const char* key, bool defVal = false); 16 | bool setBool(const char* section, const char* key, bool value); 17 | long getLong(const char* section, const char* key, long defVal = 0); 18 | bool setLong(const char* section, const char* key, long value); 19 | const char* getString(const char* section, const char* key, const char* defVal = NULL); 20 | bool setString(const char* section, const char* key, const char* value); 21 | 22 | bool Load(); 23 | bool Save(); 24 | private: 25 | CSimpleIniA m_iniFile; 26 | std::string m_filePath; 27 | }; 28 | 29 | #endif/*INI_FILE_H*/ -------------------------------------------------------------------------------- /src/cpp/common/memory_cleaner.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #ifdef WIN32 5 | # include 6 | #endif/*WIN32*/ 7 | 8 | #ifdef SUPPORT_SSL 9 | # include 10 | # include 11 | # include 12 | # include 13 | #endif/*SUPPORT_SSL*/ 14 | 15 | void clear_type_info_cache() 16 | { 17 | __type_info_node* & node = __type_info_root_node.next; 18 | while(node) 19 | { 20 | if (node->memPtr) 21 | { 22 | delete node->memPtr; 23 | } 24 | __type_info_node* tempNode = node; 25 | node = node->next; 26 | delete tempNode; 27 | } 28 | } 29 | 30 | #ifdef SUPPORT_SSL 31 | void clear_ssl() 32 | { 33 | ERR_remove_state(0); 34 | ENGINE_cleanup(); 35 | CONF_modules_unload(1); 36 | ERR_free_strings(); 37 | EVP_cleanup(); 38 | CRYPTO_cleanup_all_ex_data(); 39 | } 40 | 41 | #endif/*SUPPORT_SSL*/ -------------------------------------------------------------------------------- /src/cpp/common/process.cpp: -------------------------------------------------------------------------------- 1 | #ifdef WIN32 2 | # include 3 | # include 4 | # define _WINSOCKAPI_ // prevent inclusion of winsock.h, we need winsock2.h 5 | # include 6 | # include 7 | #else 8 | # include 9 | # include 10 | #endif 11 | 12 | #include "utils/utils.h" 13 | 14 | std::string app_self_name() 15 | { 16 | #ifdef WIN32 17 | char tmp[MAX_PATH*2] = {0}; 18 | ::GetModuleFileNameA(NULL, tmp, MAX_PATH*2); 19 | 20 | std::vector vstr = CommonUtils::DelimitString(tmp, "\\"); 21 | if(vstr.empty()) 22 | vstr = CommonUtils::DelimitString(tmp, "/"); 23 | 24 | if(vstr.empty()) 25 | return ""; 26 | 27 | return (vstr.end() - 1)->c_str(); 28 | #elif __APPLE__ 29 | std::string strname = ""; 30 | int mib[5] = {CTL_KERN, KERN_ARGMAX, 0}; 31 | size_t len = sizeof(int); 32 | size_t nmax = 0; 33 | sysctl((int*)mib, 2 , &nmax, &len, NULL, 0); 34 | 35 | char* progargv = (char* )malloc(nmax + 1); 36 | mib[1] = KERN_PROCARGS; 37 | mib[2] = getpid(); 38 | char szrealpath[1024] = {0}; 39 | int n = sysctl((int*)mib, 3 , progargv, &nmax, NULL, 0); 40 | if (n == 0) 41 | { 42 | realpath(progargv, szrealpath); 43 | } 44 | free(progargv); 45 | for (int i = strlen(szrealpath); i > 0; i--) 46 | { 47 | if (szrealpath[i] == '/') 48 | { 49 | strname = szrealpath + i +1; 50 | break; 51 | } 52 | } 53 | return strname; 54 | #else 55 | std::string strname; 56 | char szBuf[1024] = {0}; 57 | ssize_t n = readlink("/proc/self/exe", szBuf, sizeof(szBuf)); 58 | if(n > 0 && n < sizeof(szBuf)) 59 | { 60 | for(int i = strlen(szBuf) - 1; i > 0; i--) 61 | { 62 | if(szBuf[i] == '/') 63 | { 64 | strname = szBuf + i + 1; 65 | break; 66 | } 67 | } 68 | } 69 | return strname; 70 | #endif/*WIN32*/ 71 | } -------------------------------------------------------------------------------- /src/cpp/common/process.h: -------------------------------------------------------------------------------- 1 | #ifndef PROCESS_H 2 | #define PROCESS_H 3 | 4 | #include 5 | 6 | std::string app_self_name(); 7 | 8 | #endif/*PROCESS_H*/ -------------------------------------------------------------------------------- /src/cpp/common/service_manager.h: -------------------------------------------------------------------------------- 1 | #ifndef SERVICE_MANAGER_H 2 | #define SERVICE_MANAGER_H 3 | 4 | #ifdef WIN32 5 | # include 6 | #else 7 | # include 8 | # include 9 | # include 10 | # include 11 | # include 12 | # include 13 | #endif/*WIN32*/ 14 | 15 | #include 16 | 17 | class ServiceManager 18 | { 19 | public: 20 | ServiceManager(const std::string& serviceName); 21 | ~ServiceManager(); 22 | 23 | bool Start(); 24 | bool Stop(); 25 | bool Install(const std::string& descr, const std::string& binPath); 26 | bool Remove(); 27 | protected: 28 | #ifdef WIN32 29 | bool OpenService(); 30 | bool QueryStatus(SERVICE_STATUS_PROCESS& ssp); 31 | #endif/*WIN32*/ 32 | private: 33 | std::string m_serviceName; 34 | 35 | #ifdef WIN32 36 | SC_HANDLE m_scManager; 37 | SC_HANDLE m_service; 38 | #endif/*WIN32*/ 39 | }; 40 | 41 | #ifndef WIN32 42 | std::string get_process_pid_filename(); 43 | #endif/*WIN32*/ 44 | 45 | #endif/*SERVICE_MANAGER_H*/ -------------------------------------------------------------------------------- /src/cpp/common/tstring.cpp: -------------------------------------------------------------------------------- 1 | #ifdef WIN32 2 | #include 3 | #else 4 | #include 5 | #endif/*WIN32*/ 6 | 7 | #include "tstring.h" 8 | 9 | void g_mbstowcs(std::wstring& dest, const std::string& src) 10 | { 11 | wchar_t* ldest; 12 | unsigned int rv = 0, c = 0; 13 | bool end = false; 14 | 15 | while (c < 2) 16 | { 17 | ldest = (wchar_t*)dest.c_str(); 18 | #ifdef WIN32 19 | rv = MultiByteToWideChar(CP_ACP, 0, (char*)src.c_str(), (int)src.size(), ldest, rv); 20 | #else 21 | rv = mbstowcs(ldest, src.c_str(), rv); 22 | #endif 23 | dest.resize(rv); 24 | c++; 25 | } 26 | } 27 | 28 | void g_wcstombs(std::string& dest, const std::wstring& src) 29 | { 30 | const wchar_t* lsrc; 31 | int rv = 0, c = 0; 32 | 33 | while (c < 2) 34 | { 35 | lsrc = src.c_str(); 36 | #ifdef WIN32 37 | rv = WideCharToMultiByte(CP_ACP, 0, lsrc, (int)src.size(), (char*)dest.c_str(), rv, 0, 0); 38 | #else 39 | rv = wcstombs((char*)dest.c_str(), lsrc, rv); 40 | #endif 41 | dest.resize(rv); 42 | c++; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/cpp/common/tstring.h: -------------------------------------------------------------------------------- 1 | #ifndef TSTRING_H 2 | #define TSTRING_H 3 | 4 | #include 5 | 6 | #ifndef TCHAR 7 | # ifdef _UNICODE 8 | # define TCHAR wchar_t 9 | # else 10 | # define TCHAR char; 11 | # endif/*_UNICODE*/ 12 | #endif/*TCHAR*/ 13 | 14 | namespace std 15 | { 16 | #ifdef _UNICODE 17 | typedef wstring tstring; 18 | #else 19 | typedef string tstring; 20 | #endif/*_UNICODE*/ 21 | }; 22 | 23 | void g_mbstowcs(std::wstring& dest, const std::string& src); 24 | void g_wcstombs(std::string& dest, const std::wstring& src); 25 | 26 | 27 | #endif/*TSTRING_H*/ -------------------------------------------------------------------------------- /src/cpp/common/user.cpp: -------------------------------------------------------------------------------- 1 | #include "user.h" 2 | #include 3 | #ifdef WIN32 4 | #include 5 | #else 6 | #include 7 | #include 8 | #include 9 | #endif/*WIN32*/ 10 | 11 | std::string GetUserName() 12 | { 13 | #ifdef WIN32 14 | char user[250]; 15 | DWORD len = sizeof(user); 16 | ::GetUserNameA(user, &len); 17 | return user; 18 | #else 19 | register struct passwd *pw; 20 | register uid_t uid; 21 | uid = geteuid (); 22 | pw = getpwuid (uid); 23 | return pw->pw_name; 24 | #endif/*WIN32*/ 25 | } 26 | -------------------------------------------------------------------------------- /src/cpp/common/user.h: -------------------------------------------------------------------------------- 1 | #ifndef USER_H 2 | #define USER_H 3 | 4 | #include 5 | 6 | std::string GetUserName(); 7 | 8 | #endif/*USER_H*/ 9 | -------------------------------------------------------------------------------- /src/cpp/external/libntlm/bin/libntlm-x64.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/src/cpp/external/libntlm/bin/libntlm-x64.dll -------------------------------------------------------------------------------- /src/cpp/external/libntlm/bin/libntlm-x86.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/src/cpp/external/libntlm/bin/libntlm-x86.dll -------------------------------------------------------------------------------- /src/cpp/external/openssl/lib/x64/libeay32.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/src/cpp/external/openssl/lib/x64/libeay32.lib -------------------------------------------------------------------------------- /src/cpp/external/openssl/lib/x64/ssleay32.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/src/cpp/external/openssl/lib/x64/ssleay32.lib -------------------------------------------------------------------------------- /src/cpp/external/openssl/lib/x86/libeay32.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/src/cpp/external/openssl/lib/x86/libeay32.lib -------------------------------------------------------------------------------- /src/cpp/external/openssl/lib/x86/ssleay32.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/src/cpp/external/openssl/lib/x86/ssleay32.lib -------------------------------------------------------------------------------- /src/cpp/external/openssl/openssl/comp.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef HEADER_COMP_H 3 | #define HEADER_COMP_H 4 | 5 | #include 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | typedef struct comp_ctx_st COMP_CTX; 12 | 13 | typedef struct comp_method_st 14 | { 15 | int type; /* NID for compression library */ 16 | const char *name; /* A text string to identify the library */ 17 | int (*init)(COMP_CTX *ctx); 18 | void (*finish)(COMP_CTX *ctx); 19 | int (*compress)(COMP_CTX *ctx, 20 | unsigned char *out, unsigned int olen, 21 | unsigned char *in, unsigned int ilen); 22 | int (*expand)(COMP_CTX *ctx, 23 | unsigned char *out, unsigned int olen, 24 | unsigned char *in, unsigned int ilen); 25 | /* The following two do NOTHING, but are kept for backward compatibility */ 26 | long (*ctrl)(void); 27 | long (*callback_ctrl)(void); 28 | } COMP_METHOD; 29 | 30 | struct comp_ctx_st 31 | { 32 | COMP_METHOD *meth; 33 | unsigned long compress_in; 34 | unsigned long compress_out; 35 | unsigned long expand_in; 36 | unsigned long expand_out; 37 | 38 | CRYPTO_EX_DATA ex_data; 39 | }; 40 | 41 | 42 | COMP_CTX *COMP_CTX_new(COMP_METHOD *meth); 43 | void COMP_CTX_free(COMP_CTX *ctx); 44 | int COMP_compress_block(COMP_CTX *ctx, unsigned char *out, int olen, 45 | unsigned char *in, int ilen); 46 | int COMP_expand_block(COMP_CTX *ctx, unsigned char *out, int olen, 47 | unsigned char *in, int ilen); 48 | COMP_METHOD *COMP_rle(void ); 49 | COMP_METHOD *COMP_zlib(void ); 50 | 51 | /* BEGIN ERROR CODES */ 52 | /* The following lines are auto generated by the script mkerr.pl. Any changes 53 | * made after this point may be overwritten when the script is next run. 54 | */ 55 | void ERR_load_COMP_strings(void); 56 | 57 | /* Error codes for the COMP functions. */ 58 | 59 | /* Function codes. */ 60 | 61 | /* Reason codes. */ 62 | 63 | #ifdef __cplusplus 64 | } 65 | #endif 66 | #endif 67 | -------------------------------------------------------------------------------- /src/cpp/external/openssl/openssl/ebcdic.h: -------------------------------------------------------------------------------- 1 | /* crypto/ebcdic.h */ 2 | 3 | #ifndef HEADER_EBCDIC_H 4 | #define HEADER_EBCDIC_H 5 | 6 | #include 7 | 8 | /* Avoid name clashes with other applications */ 9 | #define os_toascii _openssl_os_toascii 10 | #define os_toebcdic _openssl_os_toebcdic 11 | #define ebcdic2ascii _openssl_ebcdic2ascii 12 | #define ascii2ebcdic _openssl_ascii2ebcdic 13 | 14 | extern const unsigned char os_toascii[256]; 15 | extern const unsigned char os_toebcdic[256]; 16 | void *ebcdic2ascii(void *dest, const void *srce, size_t count); 17 | void *ascii2ebcdic(void *dest, const void *srce, size_t count); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /src/cpp/external/openssl/openssl/vssver.scc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/src/cpp/external/openssl/openssl/vssver.scc -------------------------------------------------------------------------------- /src/cpp/external/protobuf-2.5.0/LICENSE.txt: -------------------------------------------------------------------------------- 1 | This license applies to all parts of Protocol Buffers except the following: 2 | 3 | - Atomicops support for generic gcc, located in 4 | src/google/protobuf/stubs/atomicops_internals_generic_gcc.h. 5 | This file is copyrighted by Red Hat Inc. 6 | 7 | - Atomicops support for AIX/POWER, located in 8 | src/google/protobuf/stubs/atomicops_internals_aix.h. 9 | This file is copyrighted by Bloomberg Finance LP. 10 | 11 | Copyright 2014, Google Inc. All rights reserved. 12 | 13 | Redistribution and use in source and binary forms, with or without 14 | modification, are permitted provided that the following conditions are 15 | met: 16 | 17 | * Redistributions of source code must retain the above copyright 18 | notice, this list of conditions and the following disclaimer. 19 | * Redistributions in binary form must reproduce the above 20 | copyright notice, this list of conditions and the following disclaimer 21 | in the documentation and/or other materials provided with the 22 | distribution. 23 | * Neither the name of Google Inc. nor the names of its 24 | contributors may be used to endorse or promote products derived from 25 | this software without specific prior written permission. 26 | 27 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 28 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 29 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 30 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 31 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 32 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 33 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 34 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 35 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 36 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 37 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | 39 | Code generated by the Protocol Buffer compiler is owned by the owner 40 | of the input file used when generating it. This code is not 41 | standalone and requires a support library to be linked with it. This 42 | support library is itself covered by the above license. 43 | -------------------------------------------------------------------------------- /src/cpp/external/protobuf-2.5.0/install.bat: -------------------------------------------------------------------------------- 1 | md src 2 | svn checkout https://github.com/google/protobuf/tags/v2.5.0/src src -------------------------------------------------------------------------------- /src/cpp/external/protobuf-2.5.0/install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #create output dirs 3 | mkdir -p ../../../../output/lib/Debug 4 | mkdir -p ../../../../output/lib/Release 5 | #compile protobuf 6 | svn checkout https://github.com/google/protobuf/tags/v2.5.0 v2.5.0 7 | cd v2.5.0 8 | autoreconf -f -i -Wall,no-obsolete 9 | ./configure 10 | autoreconf -f -i -Wall,no-obsolete 11 | 12 | #compile Release library 13 | C=/usr/bin/gcc ./configure -disable-shared CXXFLAGS="-fPIC" 14 | make 15 | rm ../../../../../output/lib/Release/libprotobuf.a 16 | cp src/.libs/libprotobuf.a ../../../../../output/lib/Release 17 | cp src/protoc ../../../../messages 18 | 19 | make clean 20 | 21 | #compile Debug library 22 | C=/usr/bin/gcc ./configure -disable-shared CXXFLAGS="-g3 -ggdb3 -fPIC" 23 | make 24 | cp src/.libs/libprotobuf.a ../../../../../output/lib/Debug 25 | cd .. 26 | rm -rf v2.5.0 27 | 28 | #checkout src 29 | rm -rf src 30 | svn checkout https://github.com/google/protobuf/tags/v2.5.0/src src 31 | -------------------------------------------------------------------------------- /src/cpp/external/protobuf-2.5.0/vsprojects/config.h: -------------------------------------------------------------------------------- 1 | /* protobuf config.h for MSVC. On other platforms, this is generated 2 | * automatically by autoheader / autoconf / configure. */ 3 | 4 | /* the location of */ 5 | #define HASH_MAP_H 6 | 7 | /* the namespace of hash_map/hash_set */ 8 | // Apparently Microsoft decided to move hash_map *back* to the std namespace 9 | // in MSVC 2010: 10 | // http://blogs.msdn.com/vcblog/archive/2009/05/25/stl-breaking-changes-in-visual-studio-2010-beta-1.aspx 11 | // TODO(kenton): Use unordered_map instead, which is available in MSVC 2010. 12 | #if _MSC_VER < 1310 || _MSC_VER >= 1600 13 | #define HASH_NAMESPACE std 14 | #else 15 | #define HASH_NAMESPACE stdext 16 | #endif 17 | 18 | /* the location of */ 19 | #define HASH_SET_H 20 | 21 | /* define if the compiler has hash_map */ 22 | #define HAVE_HASH_MAP 1 23 | 24 | /* define if the compiler has hash_set */ 25 | #define HAVE_HASH_SET 1 26 | 27 | /* define if you want to use zlib. See readme.txt for additional 28 | * requirements. */ 29 | // #define HAVE_ZLIB 1 30 | -------------------------------------------------------------------------------- /src/cpp/external/udt4/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | project(udt) 3 | 4 | if(NOT CMAKE_BUILD_TYPE) 5 | set(CMAKE_BUILD_TYPE "Debug") 6 | # set(CMAKE_BUILD_TYPE "Release") 7 | endif(NOT CMAKE_BUILD_TYPE) 8 | 9 | set (SRC_${PROJECT_NAME} 10 | src/api.cpp 11 | src/buffer.cpp 12 | src/cache.cpp 13 | src/ccc.cpp 14 | src/channel.cpp 15 | src/common.cpp 16 | src/core.cpp 17 | src/epoll.cpp 18 | src/list.cpp 19 | src/md5.cpp 20 | src/packet.cpp 21 | src/queue.cpp 22 | src/window.cpp 23 | ) 24 | add_definitions(-fPIC) 25 | 26 | include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src) 27 | 28 | add_library (${PROJECT_NAME} STATIC ${SRC_${PROJECT_NAME}}) 29 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../../../output/${PROJECT_NAME}) 30 | set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../../output/lib/${CMAKE_BUILD_TYPE}) -------------------------------------------------------------------------------- /src/cpp/external/udt4/LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2001 - 2011, The Board of Trustees of the University of Illinois. 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are 6 | met: 7 | 8 | * Redistributions of source code must retain the above 9 | copyright notice, this list of conditions and the 10 | following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the 13 | above copyright notice, this list of conditions 14 | and the following disclaimer in the documentation 15 | and/or other materials provided with the distribution. 16 | 17 | * Neither the name of the University of Illinois 18 | nor the names of its contributors may be used to 19 | endorse or promote products derived from this 20 | software without specific prior written permission. 21 | 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 23 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 24 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 26 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 27 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 28 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 29 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 31 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 32 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | -------------------------------------------------------------------------------- /src/cpp/external/udt4/src/Makefile: -------------------------------------------------------------------------------- 1 | C++ = g++ 2 | 3 | ifndef os 4 | os = LINUX 5 | endif 6 | 7 | ifndef arch 8 | arch = IA32 9 | endif 10 | 11 | CCFLAGS = -fPIC -Wall -D$(os) -finline-functions -O3 #-msse3 12 | 13 | ifeq ($(arch), IA32) 14 | CCFLAGS += -DIA32 15 | endif 16 | 17 | ifeq ($(arch), POWERPC) 18 | CCFLAGS += -mcpu=powerpc 19 | endif 20 | 21 | ifeq ($(arch), SPARC) 22 | CCFLAGS += -mcpu=sparc 23 | endif 24 | 25 | ifeq ($(arch), IA64) 26 | CCFLAGS += -DIA64 27 | endif 28 | 29 | ifeq ($(arch), AMD64) 30 | CCFLAGS += -DAMD64 31 | endif 32 | 33 | OBJS = md5.o common.o window.o list.o buffer.o packet.o channel.o queue.o ccc.o cache.o core.o api.o 34 | DIR = $(shell pwd) 35 | 36 | all: libudt.so libudt.a udt 37 | 38 | %.o: %.cpp %.h udt.h 39 | $(C++) $(CCFLAGS) $< -c 40 | 41 | libudt.so: $(OBJS) 42 | ifneq ($(os), OSX) 43 | $(C++) -shared -o $@ $^ 44 | else 45 | $(C++) -dynamiclib -o libudt.dylib -lstdc++ -lpthread -lm $^ 46 | endif 47 | 48 | libudt.a: $(OBJS) 49 | ar -rcs $@ $^ 50 | 51 | udt: 52 | cp udt.h udt 53 | 54 | clean: 55 | rm -f *.o *.so *.dylib *.a udt 56 | 57 | install: 58 | export LD_LIBRARY_PATH=$(DIR):$$LD_LIBRARY_PATH 59 | -------------------------------------------------------------------------------- /src/cpp/external/winpcap/install.bat: -------------------------------------------------------------------------------- 1 | md include 2 | md library 3 | svn checkout https://github.com/patmarion/winpcap/trunk/Include include 4 | svn checkout https://github.com/patmarion/winpcap/trunk/Lib library -------------------------------------------------------------------------------- /src/cpp/include/client_module_name.h: -------------------------------------------------------------------------------- 1 | #ifndef CLIENT_MODULE_NAME_H 2 | #define CLIENT_MODULE_NAME_H 3 | 4 | #include 5 | 6 | class ClientModuleName 7 | { 8 | public: 9 | ClientModuleName(){} 10 | ClientModuleName(const ClientModuleName& name) 11 | { 12 | operator = (name); 13 | } 14 | ClientModuleName(const std::string& moduleName, const std::string& hostClient) 15 | : m_moduleName(moduleName), m_hostClient(hostClient){} 16 | ~ClientModuleName(){} 17 | 18 | void operator = (const ClientModuleName& name) 19 | { 20 | m_moduleName = name.m_moduleName; 21 | m_hostClient = name.m_hostClient; 22 | } 23 | 24 | bool operator == (const ClientModuleName& name) const 25 | { 26 | return m_hostClient == name.m_hostClient; 27 | } 28 | 29 | bool operator != (const ClientModuleName& name) const 30 | { 31 | return !operator == (name); 32 | } 33 | 34 | bool operator < (const ClientModuleName& name) const 35 | { 36 | return m_hostClient < name.m_hostClient; 37 | } 38 | 39 | public: 40 | std::string m_moduleName; 41 | std::string m_hostClient; 42 | }; 43 | 44 | #endif/*CLIENT_MODULE_NAME_H*/ -------------------------------------------------------------------------------- /src/cpp/include/critical_section.h: -------------------------------------------------------------------------------- 1 | #ifndef CRITICAL_SECTION_H 2 | #define CRITICAL_SECTION_H 3 | 4 | #ifdef WIN32 5 | # define WIN32_LEAN_AND_MEAN 6 | # include 7 | # include 8 | #else 9 | # include 10 | # include 11 | #endif/*WIN32*/ 12 | 13 | class CriticalSection 14 | { 15 | public: 16 | CriticalSection() 17 | { 18 | #ifdef WIN32 19 | InitializeCriticalSection(&m_cs); 20 | #else 21 | pthread_mutex_init(&m_cs, 0); 22 | m_threadlock = 0; 23 | #endif 24 | } 25 | ~CriticalSection() 26 | { 27 | #ifdef WIN32 28 | DeleteCriticalSection(&m_cs); 29 | #else 30 | pthread_mutex_destroy (&m_cs); 31 | m_threadlock = 0; 32 | #endif 33 | } 34 | 35 | void Enter() 36 | { 37 | #ifdef WIN32 38 | EnterCriticalSection(&m_cs); 39 | #else 40 | if(m_threadlock != pthread_self()) 41 | { 42 | pthread_mutex_lock (&m_cs); 43 | m_threadlock = pthread_self(); 44 | } 45 | else 46 | m_cs.__data.__count++; 47 | #endif 48 | } 49 | 50 | void Leave() 51 | { 52 | #ifdef WIN32 53 | LeaveCriticalSection(&m_cs); 54 | #else 55 | if(m_threadlock == pthread_self() && 56 | m_cs.__data.__count == 0) 57 | { 58 | m_threadlock = 0; 59 | pthread_mutex_unlock(&m_cs); 60 | } 61 | else 62 | m_cs.__data.__count--; 63 | #endif 64 | } 65 | private: 66 | #ifdef WIN32 67 | CRITICAL_SECTION m_cs; 68 | #else 69 | pthread_mutex_t m_cs; 70 | pthread_t m_threadlock; 71 | #endif/*WIN32*/ 72 | }; 73 | 74 | class CSLocker 75 | { 76 | public: 77 | CSLocker(CriticalSection* cs) 78 | : m_cs(cs) 79 | { 80 | m_cs->Enter(); 81 | } 82 | 83 | ~CSLocker() 84 | { 85 | m_cs->Leave(); 86 | } 87 | private: 88 | CriticalSection* m_cs; 89 | }; 90 | 91 | #endif/*CRITICAL_SECTION_H*/ -------------------------------------------------------------------------------- /src/cpp/include/data_message.h: -------------------------------------------------------------------------------- 1 | #ifndef DATA_MESSAGE_H 2 | #define DATA_MESSAGE_H 3 | 4 | #include "serializer.h" 5 | #include "deserializer.h" 6 | 7 | #include 8 | #include 9 | 10 | class DataMessage 11 | : public Serializer 12 | , public Deserializer 13 | { 14 | public: 15 | virtual ~DataMessage(){}; 16 | 17 | virtual void onMessage(){}; 18 | virtual bool serialize(char* data, int len){return false;} 19 | virtual bool deserialize(char* data, int& len){return false;} 20 | virtual std::string GetName()const{return typeid(*this).name();} 21 | virtual std::string GetDeserializeName()const{return GetName();} 22 | }; 23 | 24 | #endif // DATA_MESSAGE_H 25 | -------------------------------------------------------------------------------- /src/cpp/include/deamon_message.h: -------------------------------------------------------------------------------- 1 | #ifndef DEAMON_MESSAGE_H 2 | #define DEAMON_MESSAGE_H 3 | 4 | #include 5 | #include "data_message.h" 6 | 7 | class MessageAttr 8 | { 9 | public: 10 | MessageAttr(){} 11 | virtual ~MessageAttr(){} 12 | void SetPath(const Twainet::ModuleName& path) 13 | { 14 | m_path = path; 15 | } 16 | 17 | Twainet::ModuleName GetPath() 18 | { 19 | return m_path; 20 | } 21 | private: 22 | Twainet::ModuleName m_path; 23 | }; 24 | 25 | template 26 | class UserMessage : public DataMessage, public MessageAttr, public TMessage 27 | { 28 | public: 29 | UserMessage(THandler* handler) 30 | : m_handler(handler){} 31 | UserMessage(THandler* handler, const TMessage& msg) 32 | : m_handler(handler) 33 | { 34 | TMessage::operator = (msg); 35 | } 36 | 37 | virtual void onMessage() 38 | { 39 | TMessage* msg = static_cast(this); 40 | m_handler->onMessage(*msg, GetPath()); 41 | } 42 | 43 | virtual bool serialize(char* data, int len) 44 | { 45 | TMessage::Clear(); 46 | return TMessage::ParseFromArray(data, len); 47 | } 48 | 49 | virtual bool deserialize(char* data, int& len) 50 | { 51 | int size = this->ByteSize(); 52 | if (size > len) 53 | { 54 | len = size; 55 | return false; 56 | } 57 | 58 | len = size; 59 | return this->SerializeToArray(data, len); 60 | } 61 | 62 | virtual std::string GetName()const 63 | { 64 | return this->GetTypeName(); 65 | } 66 | 67 | static std::string GetMessageName() 68 | { 69 | TMessage msg; 70 | return msg.GetTypeName(); 71 | } 72 | private: 73 | THandler* m_handler; 74 | }; 75 | 76 | #endif/*DEAMON_MESSAGE_H*/ 77 | -------------------------------------------------------------------------------- /src/cpp/include/deserializer.h: -------------------------------------------------------------------------------- 1 | #ifndef DESERIALIZER_H 2 | #define DESERIALIZER_H 3 | 4 | class Deserializer 5 | { 6 | public: 7 | virtual bool deserialize(char* data, int& len) = 0; 8 | }; 9 | 10 | #endif // DESERIALIZER_H 11 | -------------------------------------------------------------------------------- /src/cpp/include/ref.h: -------------------------------------------------------------------------------- 1 | #ifndef REF_H 2 | #define REF_H 3 | 4 | template 5 | class Reference 6 | { 7 | public: 8 | Reference(TClass* ref, TFunc f) 9 | : m_ref(ref), m_func(f){} 10 | 11 | template 12 | TReturn operator ()(const TObject& obj) 13 | { 14 | return (m_ref->*m_func)(obj); 15 | } 16 | private: 17 | TClass* m_ref; 18 | TFunc m_func; 19 | }; 20 | 21 | template 22 | class ReferenceObject 23 | { 24 | public: 25 | ReferenceObject(TClass* ref, TFunc f, const TObject& obj) 26 | : m_ref(ref), m_func(f), m_obj(obj){} 27 | 28 | template 29 | TReturn operator ()(const TRefObject& obj) 30 | { 31 | return (m_ref->*m_func)(m_obj, obj); 32 | } 33 | 34 | private: 35 | TClass* m_ref; 36 | TFunc m_func; 37 | const TObject& m_obj; 38 | }; 39 | 40 | template 41 | class OwnReference 42 | { 43 | public: 44 | OwnReference(TFunc f) 45 | : m_func(f){} 46 | 47 | template 48 | TReturn operator ()(const TObject& obj) 49 | { 50 | TReturn ret={0}; 51 | if(m_func) 52 | return (const_cast(obj).*m_func)(); 53 | return ret; 54 | } 55 | 56 | private: 57 | TFunc m_func; 58 | }; 59 | 60 | template 61 | class OwnReference 62 | { 63 | public: 64 | OwnReference(TFunc f) 65 | : m_func(f){} 66 | 67 | template 68 | void operator ()(const TObject& obj) 69 | { 70 | if(m_func) 71 | (const_cast(obj).*m_func)(); 72 | } 73 | 74 | private: 75 | TFunc m_func; 76 | }; 77 | 78 | template 79 | Reference Ref(TClass* ref, TFunc func) 80 | { 81 | return Reference(ref, func); 82 | } 83 | 84 | template 85 | ReferenceObject Ref(TClass* ref, TFunc func, const TObject& obj) 86 | { 87 | return ReferenceObject(ref, func, obj); 88 | } 89 | 90 | template 91 | OwnReference Ref(TFunc func, TReturn ret) 92 | { 93 | return OwnReference(func); 94 | } 95 | 96 | template 97 | OwnReference Ref(TFunc func) 98 | { 99 | return OwnReference(func); 100 | } 101 | 102 | #endif/*REF_H*/ 103 | -------------------------------------------------------------------------------- /src/cpp/include/serializer.h: -------------------------------------------------------------------------------- 1 | #ifndef SERIALIZER_H 2 | #define SERIALIZER_H 3 | 4 | class Serializer 5 | { 6 | public: 7 | virtual bool serialize(char* data, int len) = 0; 8 | }; 9 | 10 | #endif // SERIALIZER_H -------------------------------------------------------------------------------- /src/cpp/include/singleton.h: -------------------------------------------------------------------------------- 1 | #ifndef SINGLETON_H 2 | #define SINGLETON_H 3 | 4 | template 5 | class Singleton 6 | { 7 | public: 8 | static Object& GetInstance() 9 | { 10 | static Object object; 11 | return object; 12 | } 13 | virtual ~Singleton(){} 14 | protected: 15 | Singleton(){} 16 | }; 17 | 18 | #endif/*SINGLETON_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | 3 | if(NOT CMAKE_BUILD_TYPE) 4 | set(CMAKE_BUILD_TYPE "Debug") 5 | # set(CMAKE_BUILD_TYPE "Release") 6 | endif(NOT CMAKE_BUILD_TYPE) 7 | 8 | add_subdirectory(thread_lib) 9 | add_subdirectory(connector_lib) 10 | add_subdirectory(ipc_lib) 11 | add_subdirectory(client_server_lib) 12 | add_subdirectory(tunnel_lib) 13 | add_subdirectory(twainet) -------------------------------------------------------------------------------- /src/cpp/lib/client_server_lib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | project(client_server_lib) 3 | 4 | if(NOT CMAKE_BUILD_TYPE) 5 | set(CMAKE_BUILD_TYPE "Debug") 6 | # set(CMAKE_BUILD_TYPE "Release") 7 | endif(NOT CMAKE_BUILD_TYPE) 8 | 9 | if(NOT USE_SYSTEM_PROTOBUF) 10 | set(PROTOC_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../messages/protoc) 11 | # execute_process(COMMAND 12 | # WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) 13 | include_directories(${CMAKE_CURRENT_SOURCE_DIR} 14 | ${CMAKE_CURRENT_SOURCE_DIR}/.. 15 | ${CMAKE_CURRENT_SOURCE_DIR}/../.. 16 | ${CMAKE_CURRENT_SOURCE_DIR}/../../external/protobuf-2.5.0/src) 17 | else(NOT USE_SYSTEM_PROTOBUF) 18 | set(PROTOC_PATH protoc) 19 | include_directories(${CMAKE_CURRENT_SOURCE_DIR} 20 | ${CMAKE_CURRENT_SOURCE_DIR}/.. 21 | ${CMAKE_CURRENT_SOURCE_DIR}/../..) 22 | endif(NOT USE_SYSTEM_PROTOBUF) 23 | 24 | set (SRC_${PROJECT_NAME} 25 | connector/client_server_connector.cpp 26 | message/client_server_messages.cpp 27 | module/client_server_module.cpp 28 | module/client_server_signal_handler.cpp 29 | ) 30 | 31 | set (SRC_GEN_${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/../../../messages/client_server.pb.cc 32 | ${CMAKE_CURRENT_SOURCE_DIR}/../../../messages/client_server.pb.h) 33 | 34 | set_source_files_properties(${SRC_GEN_${PROJECT_NAME}} PROPERTIES GENERATED TRUE) 35 | add_custom_command( OUTPUT ${SRC_GEN_${PROJECT_NAME}} 36 | COMMAND ${PROTOC_PATH} 37 | ARGS --proto_path=. --cpp_out=. client_server.proto 38 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../../messages 39 | DEPENDS ../../../messages/client_server.proto) 40 | 41 | add_definitions(-fPIC) 42 | 43 | add_library (${PROJECT_NAME} STATIC ${SRC_${PROJECT_NAME}} ${SRC_GEN_${PROJECT_NAME}}) 44 | set(CMAKE_LIBRARY_OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../output/${PROJECT_NAME}) 45 | set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../../output/lib/${CMAKE_BUILD_TYPE}) 46 | -------------------------------------------------------------------------------- /src/cpp/lib/client_server_lib/client_server_lib.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {ce96900e-9b3c-4106-90f7-a24a4825f9a4} 6 | 7 | 8 | {4ad67137-3070-457e-bddf-f4d5109314b4} 9 | 10 | 11 | {64ef5015-470e-4354-8ba7-13fe90faa8e2} 12 | 13 | 14 | {4a805f07-6882-4250-9a2b-6176038b43a2} 15 | 16 | 17 | 18 | 19 | module 20 | 21 | 22 | module 23 | 24 | 25 | connector 26 | 27 | 28 | messages 29 | 30 | 31 | message 32 | 33 | 34 | 35 | 36 | module 37 | 38 | 39 | module 40 | 41 | 42 | connector 43 | 44 | 45 | messages 46 | 47 | 48 | message 49 | 50 | 51 | 52 | 53 | messages 54 | 55 | 56 | -------------------------------------------------------------------------------- /src/cpp/lib/client_server_lib/message/client_server_messages.cpp: -------------------------------------------------------------------------------- 1 | #include "client_server_messages.h" 2 | 3 | ClientServerConnectedMessage::ClientServerConnectedMessage(const std::string& id) 4 | : ConnectedMessage(id, false) 5 | { 6 | } 7 | 8 | std::string ClientServerConnectedMessage::GetMessageName() 9 | { 10 | return typeid(ClientServerConnectedMessage).name(); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /src/cpp/lib/client_server_lib/message/client_server_messages.h: -------------------------------------------------------------------------------- 1 | #ifndef CLIENT_SERVER_MESSAGES_H 2 | #define CLIENT_SERVER_MESSAGES_H 3 | 4 | #include "connector_lib/message/connector_messages.h" 5 | 6 | class ClientServerConnectedMessage : public ConnectedMessage 7 | { 8 | public: 9 | ClientServerConnectedMessage(const std::string& id); 10 | static std::string GetMessageName(); 11 | }; 12 | 13 | #endif/*CLIENT_SERVER_MESSAGES_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/client_server_lib/module/client_server_signal_handler.h: -------------------------------------------------------------------------------- 1 | #ifndef CLIENT_SERVER_SIGNAL_HANDLER_H 2 | #define CLIENT_SERVER_SIGNAL_HANDLER_H 3 | 4 | #include "connector_lib/message/connector_messages.h" 5 | #include "client_server_lib/message/client_server_messages.h" 6 | #include "client_server_lib/connector/client_server_connector.h" 7 | 8 | class ClientServerModule; 9 | 10 | class ClientServerSignalHandler : public SignalReceiver 11 | { 12 | public: 13 | ClientServerSignalHandler(ClientServerModule* module); 14 | ~ClientServerSignalHandler(); 15 | 16 | void onAddClientServerConnector(const ConnectorMessage& msg); 17 | void onErrorConnect(const ConnectErrorMessage& msg); 18 | void onCreatedListener(const CreatedListenerMessage& msg); 19 | void onErrorListener(const ListenErrorMessage& msg); 20 | void onConnected(const ClientServerConnectedMessage& msg); 21 | void onDisconnected(const DisconnectedMessage& msg); 22 | 23 | void onLogin(const LoginMessage& msg); 24 | void onLoginResult(const LoginResultMessage& msg); 25 | private: 26 | ClientServerModule* m_module; 27 | }; 28 | 29 | #endif/*CLIENT_SERVER_SIGNAL_HANDLER_H*/ 30 | -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | project(connector_lib) 3 | 4 | if(NOT CMAKE_BUILD_TYPE) 5 | set(CMAKE_BUILD_TYPE "Debug") 6 | # set(CMAKE_BUILD_TYPE "Release") 7 | endif(NOT CMAKE_BUILD_TYPE) 8 | 9 | set (SRC_${PROJECT_NAME} 10 | ../../common/common_func.cpp 11 | ../../utils/base64.cpp 12 | common/aes.cpp 13 | connector/connector.cpp 14 | connector/connector_manager.cpp 15 | handler/message_handler.cpp 16 | message/connector_messages.cpp 17 | signal/signal.cpp 18 | signal/signal_manager.cpp 19 | signal/signal_owner.cpp 20 | signal/signal_receiver.cpp 21 | socket/secure_socket.cpp 22 | socket/proxy_socket.cpp 23 | socket/socket_factories.cpp 24 | socket/tcp_socket.cpp 25 | socket/udp_socket.cpp 26 | socket/udt_socket.cpp 27 | thread/connect_thread.cpp 28 | thread/listen_thread.cpp 29 | ) 30 | 31 | add_definitions(-fPIC) 32 | 33 | include_directories(${CMAKE_CURRENT_SOURCE_DIR} 34 | ${CMAKE_CURRENT_SOURCE_DIR}/.. 35 | ${CMAKE_CURRENT_SOURCE_DIR}/../.. 36 | ${CMAKE_CURRENT_SOURCE_DIR}/../../external/udt4/src 37 | ${CMAKE_CURRENT_SOURCE_DIR}/../../external/openssl 38 | ${CMAKE_CURRENT_SOURCE_DIR}/../../external/libntlm/include) 39 | 40 | add_library (${PROJECT_NAME} STATIC ${SRC_${PROJECT_NAME}}) 41 | set(CMAKE_LIBRARY_OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../output/${PROJECT_NAME}) 42 | set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../../output/lib/${CMAKE_BUILD_TYPE}) 43 | -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/common/aes.cpp: -------------------------------------------------------------------------------- 1 | #include "aes.h" 2 | #include "openssl/aes.h" 3 | #include "openssl/rand.h" 4 | #include 5 | 6 | int AESGenerateKey(byte* key, int keylen) 7 | { 8 | if(keylen > 32 || !key || keylen%8 != 0) 9 | { 10 | return -1; 11 | } 12 | for(int i = 0; i < keylen; i = i + 8) 13 | { 14 | if(!RAND_bytes(key, sizeof(key))) 15 | { 16 | return 0; 17 | } 18 | 19 | key = key + 8; 20 | } 21 | 22 | return keylen; 23 | } 24 | 25 | int GetEncriptedDataLen(int datalen) 26 | { 27 | int rest = 0; 28 | if(datalen%AES_BLOCK_SIZE != 0) 29 | { 30 | rest = (datalen/AES_BLOCK_SIZE + 1)*AES_BLOCK_SIZE - datalen; 31 | } 32 | 33 | return datalen + rest; 34 | } 35 | 36 | int AESEncrypt(byte* key, int keylength, 37 | byte* data, int datalen, 38 | byte* encryptedData, int encryptedDataLen) 39 | { 40 | 41 | int realDataLen = GetEncriptedDataLen(datalen); 42 | 43 | if(encryptedDataLen < realDataLen || !encryptedData) 44 | { 45 | return -1; 46 | } 47 | else if(realDataLen > MAX_BUFFER_LEN || !data || realDataLen == 0) 48 | { 49 | return -2; 50 | } 51 | else if(keylength > 32 || !key || keylength%8 != 0) 52 | { 53 | return -3; 54 | } 55 | 56 | unsigned char tempdata[MAX_BUFFER_LEN] = {0}; 57 | memset(tempdata, 0, MAX_BUFFER_LEN); 58 | memcpy(tempdata, data, datalen); 59 | 60 | unsigned char iv[16] = "123456789abcdef"; 61 | AES_KEY aesKey = {0}; 62 | int keyCrypt = AES_set_encrypt_key(key, keylength*8, &aesKey); 63 | if(keyCrypt) 64 | { 65 | return 0; 66 | } 67 | 68 | AES_cbc_encrypt((unsigned char*)tempdata, (unsigned char*)encryptedData, realDataLen, &aesKey, iv, AES_ENCRYPT); 69 | 70 | return realDataLen; 71 | } 72 | 73 | int AESDecrypt(byte* key, int keylength, 74 | byte* data, int datalen, 75 | byte* decryptedData, int decryptedDataLen) 76 | { 77 | if(decryptedDataLen > datalen || !decryptedData) 78 | { 79 | return -1; 80 | } 81 | else if(datalen > MAX_BUFFER_LEN || !data || datalen%AES_BLOCK_SIZE != 0) 82 | { 83 | return -2; 84 | } 85 | else if(keylength > 32 || !key || keylength%8 != 0) 86 | { 87 | return -3; 88 | } 89 | else if (datalen == 0) 90 | { 91 | return 0; 92 | } 93 | 94 | unsigned char iv[16] = "123456789abcdef"; 95 | AES_KEY aesKey = {0}; 96 | int keyCrypt = AES_set_decrypt_key(key, keylength*8, &aesKey); 97 | if(keyCrypt) 98 | { 99 | return 0; 100 | } 101 | 102 | char decryptData[MAX_BUFFER_LEN] = {0}; 103 | AES_cbc_encrypt((unsigned char*)data, (unsigned char*)decryptData, datalen, &aesKey, iv, AES_DECRYPT); 104 | memcpy(decryptedData, decryptData, decryptedDataLen); 105 | 106 | return decryptedDataLen; 107 | } -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/common/aes.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef unsigned char byte; 4 | 5 | #define MAX_BUFFER_LEN 8192 6 | 7 | //datalen must be less MAX_BUFFER_LEN 8 | //return -1 - incorrect input data 9 | //return 0 - open ssl internal error 10 | int AESGenerateKey(byte* key, int keylen); 11 | 12 | //datalen must be less MAX_BUFFER_LEN 13 | //return size encripted data 14 | //return -3 - key is incorrect 15 | //return -2 - incorrect input data 16 | //return -1 - incorrect output data 17 | //return 0 - open ssl internal error 18 | int AESEncrypt(byte* key, int keylength, 19 | byte* data, int datalen, 20 | byte* encryptedData, int encryptedDataLen); 21 | 22 | //datalen must be less MAX_BUFFER_LEN 23 | //return size decripted data 24 | //return -3 - key is incorrect 25 | //return -2 - incorrect input data 26 | //return -1 - incorrect output data 27 | //return 0 - open ssl internal error 28 | int AESDecrypt(byte* key, int keylength, 29 | byte* data, int datalen, 30 | byte* decryptedData, int decryptedDataLen); 31 | 32 | int GetEncriptedDataLen(int datalen); -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/connector/connector.cpp: -------------------------------------------------------------------------------- 1 | #include "connector.h" 2 | 3 | Connector::Connector(AnySocket* socket) 4 | : m_socket(socket) 5 | { 6 | } 7 | 8 | Connector::~Connector() 9 | { 10 | Stop(); 11 | if(m_socket) 12 | { 13 | delete m_socket; 14 | } 15 | } 16 | 17 | void Connector::SetConnectorId(const std::string& connectorId) 18 | { 19 | m_connectorId = connectorId; 20 | } 21 | 22 | std::string Connector::GetConnectorId() 23 | { 24 | return m_connectorId; 25 | } 26 | 27 | void Connector::SetAccessId(const std::string& accessId) 28 | { 29 | m_accessId = accessId; 30 | } 31 | 32 | std::string Connector::GetAccessId() 33 | { 34 | return m_accessId; 35 | } 36 | 37 | void Connector::Stop() 38 | { 39 | if(m_socket) 40 | { 41 | m_socket->Close(); 42 | } 43 | } 44 | 45 | bool Connector::SendData(char* data, int len) 46 | { 47 | if(m_socket) 48 | { 49 | return m_socket->Send(data, len); 50 | } 51 | return false; 52 | } 53 | 54 | void Connector::SetId(const std::string& id) 55 | { 56 | if(!id.empty()) 57 | { 58 | m_id = id; 59 | } 60 | } 61 | 62 | std::string Connector::GetId()const 63 | { 64 | return m_id; 65 | } 66 | 67 | void Connector::SetRemoteAddr(const std::string& ip, int port) 68 | { 69 | m_remoteIp = ip; 70 | m_remotePort = port; 71 | } 72 | -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/connector/connector.h: -------------------------------------------------------------------------------- 1 | #ifndef CONNECTOR_H 2 | #define CONNECTOR_H 3 | 4 | #include "../socket/any_socket.h" 5 | #include "../handler/message_handler.h" 6 | #include "../signal/signal_receiver.h" 7 | 8 | #include "thread_lib/thread/thread_impl.h" 9 | 10 | class Connector : public MessageHandler, public ThreadImpl 11 | { 12 | public: 13 | Connector(AnySocket* socket); 14 | virtual ~Connector(); 15 | 16 | void SetId(const std::string& id); 17 | std::string GetId()const; 18 | void SetRemoteAddr(const std::string& ip, int port); 19 | void SetConnectorId(const std::string& connectorId); 20 | std::string GetConnectorId(); 21 | void SetAccessId(const std::string& accessId); 22 | std::string GetAccessId(); 23 | 24 | virtual void Stop(); 25 | virtual void onNewConnector(const Connector* connector){}; 26 | protected: 27 | virtual bool SendData(char* data, int len); 28 | protected: 29 | AnySocket* m_socket; 30 | std::string m_id; 31 | std::string m_connectorId; 32 | std::string m_accessId; 33 | 34 | std::string m_remoteIp; 35 | int m_remotePort; 36 | }; 37 | 38 | #endif/*CONNECTOR_H*/ 39 | -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/connector/connector_factory.h: -------------------------------------------------------------------------------- 1 | #ifndef CONNECTOR_FACTORY_H 2 | #define CONNECTOR_FACTORY_H 3 | 4 | class Connector; 5 | class AnySocket; 6 | 7 | class ConnectorFactory 8 | { 9 | public: 10 | ConnectorFactory(){} 11 | virtual ~ConnectorFactory(){} 12 | public: 13 | virtual Connector* CreateConnector(AnySocket* socket) = 0; 14 | virtual ConnectorFactory* Clone() = 0; 15 | }; 16 | 17 | template 18 | class SimpleConnectorFactory : public ConnectorFactory 19 | { 20 | public: 21 | SimpleConnectorFactory(); 22 | virtual ~SimpleConnectorFactory (); 23 | Connector* CreateConnector(AnySocket* socket); 24 | ConnectorFactory* Clone(); 25 | }; 26 | 27 | template 28 | SimpleConnectorFactory::SimpleConnectorFactory() 29 | { 30 | } 31 | 32 | template 33 | SimpleConnectorFactory::~SimpleConnectorFactory() 34 | { 35 | } 36 | 37 | template 38 | Connector* SimpleConnectorFactory::CreateConnector(AnySocket* socket) 39 | { 40 | return static_cast(new TConnector(socket)); 41 | } 42 | 43 | template 44 | ConnectorFactory* SimpleConnectorFactory::Clone() 45 | { 46 | return new SimpleConnectorFactory(); 47 | } 48 | 49 | #endif/*CONNECTOR_FACTORY_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/connector/connector_manager.h: -------------------------------------------------------------------------------- 1 | #ifndef CONNECTOR_MANAGER_H 2 | #define CONNECTOR_MANAGER_H 3 | 4 | #include "connector.h" 5 | #include "include/object_manager.h" 6 | #include "thread_lib/common/managers_container.h" 7 | #include "connector_lib/signal/signal.h" 8 | #include "connector_lib/signal/signal_owner.h" 9 | 10 | class ConnectorManager : public SignalOwner, public DynamicManager 11 | { 12 | public: 13 | ConnectorManager(); 14 | ~ConnectorManager(); 15 | 16 | void AddConnection(Connector* conn); 17 | void StopConnection(const std::string& moduleName); 18 | void StopAllConnection(); 19 | 20 | protected: 21 | class ModuleInfo 22 | { 23 | public: 24 | ModuleInfo(const std::string& id, const std::string& connid, const std::string& accessid) 25 | : m_id(id), m_connid(connid), m_accessid(accessid){} 26 | std::string m_id; 27 | std::string m_connid; 28 | std::string m_accessid; 29 | }; 30 | 31 | template friend class Reference; 32 | template friend class ReferenceObject; 33 | bool CheckConnection(const std::vector& discModules, const Connector* msg); 34 | bool StopConnector(const std::vector& discModules, const Connector* msg); 35 | bool StopConnectors(const Connector* connector); 36 | bool SubscribeConnector(const Connector* newConnector, const Connector* connector); 37 | bool StopConnectionByName(const std::string& moduleName, const Connector* connector); 38 | protected: 39 | virtual void ManagerFunc(); 40 | virtual void ManagerStart(); 41 | virtual void ManagerStop(); 42 | private: 43 | ObjectManager m_connectors; 44 | }; 45 | 46 | #endif/*CONNECTOR_MANAGER_H*/ 47 | -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/handler/data_handler.h: -------------------------------------------------------------------------------- 1 | #ifndef DATA_HANDLER_H 2 | #define DATA_HANDLER_H 3 | 4 | #include "include/data_message.h" 5 | 6 | class DataHandler 7 | { 8 | public: 9 | virtual bool onData(char* data, int len) = 0; 10 | virtual bool toMessage(const DataMessage& msg) = 0; 11 | protected: 12 | DataHandler(void){} 13 | virtual ~DataHandler(void){} 14 | }; 15 | 16 | 17 | #endif // DATA_HANDLER_H 18 | -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/handler/message_handler.cpp: -------------------------------------------------------------------------------- 1 | #include "message_handler.h" 2 | #ifndef WIN32 3 | # include 4 | #endif/*WIN32*/ 5 | 6 | MessageHandler::~MessageHandler(void) 7 | { 8 | std::map::iterator it; 9 | for (it = m_messages.begin(); it != m_messages.end(); ++it) 10 | { 11 | delete it->second; 12 | } 13 | 14 | m_messages.clear(); 15 | } 16 | 17 | void MessageHandler::addMessage(DataMessage* msg) 18 | { 19 | std::map::iterator it = m_messages.find(msg->GetName()); 20 | if(it != m_messages.end()) { 21 | it->second = msg; 22 | } else { 23 | m_messages.insert(std::make_pair(msg->GetName(), msg)); 24 | } 25 | } 26 | 27 | bool MessageHandler::onData(char* data, int len) 28 | { 29 | int typeLen = 0; 30 | memcpy(&typeLen, data, sizeof(int)); 31 | int headerLen = sizeof(int) + typeLen; 32 | if (typeLen 33 | && len >= headerLen) 34 | { 35 | std::string type("", typeLen); 36 | memcpy((char*)type.c_str(), data + sizeof(unsigned int), typeLen); 37 | return onData(type, data + headerLen, len - headerLen); 38 | } 39 | return false; 40 | } 41 | 42 | bool MessageHandler::toMessage(const DataMessage& msg) 43 | { 44 | char* data = 0; 45 | int len = 0; 46 | deserialize(msg, data, len); 47 | data = new char[len]; 48 | bool res = deserialize(msg, data, len) && SendData(data, len); 49 | delete[] data; 50 | return res; 51 | } 52 | 53 | bool MessageHandler::deserialize(const DataMessage& msg, char* data, int& len) 54 | { 55 | std::string type = msg.GetDeserializeName(); 56 | int typeLen = (int)type.size(); 57 | int headerLen = sizeof(int) + typeLen; 58 | int msgBodyLen = (len > headerLen ? (len - headerLen) : 0); 59 | 60 | bool res = const_cast(msg).deserialize(data + headerLen, msgBodyLen); 61 | len = headerLen + msgBodyLen; 62 | 63 | if(res && data) 64 | { 65 | memcpy(data, &typeLen, sizeof(unsigned int)); 66 | memcpy(data + sizeof(unsigned int), type.c_str(), typeLen); 67 | } 68 | 69 | return res; 70 | } 71 | 72 | bool MessageHandler::onData(const std::string& type, char* data, int len) 73 | { 74 | std::map::iterator it = m_messages.find(type); 75 | if (it != m_messages.end()) 76 | { 77 | if (len >= 0) 78 | { 79 | it->second->serialize(data, len); 80 | it->second->onMessage(); 81 | return true; 82 | } 83 | } 84 | return false; 85 | } 86 | -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/handler/message_handler.h: -------------------------------------------------------------------------------- 1 | #ifndef MESSAGE_HANDLER_H 2 | #define MESSAGE_HANDLER_H 3 | 4 | #include "data_handler.h" 5 | #include "sender.h" 6 | 7 | #include 8 | #include 9 | 10 | class MessageHandler 11 | : public DataHandler 12 | , virtual public Sender 13 | { 14 | protected: 15 | virtual bool onData(char* data, int len); 16 | virtual bool toMessage(const DataMessage& msg); 17 | 18 | protected: 19 | MessageHandler(){} 20 | virtual ~MessageHandler(void); 21 | 22 | void addMessage(DataMessage* msg); 23 | bool deserialize(const DataMessage& msg, char* data, int& len); 24 | 25 | bool onData(const std::string& type, char* data, int len); 26 | private: 27 | std::map m_messages; 28 | }; 29 | 30 | #endif // MESSAGE_HANDLER_H 31 | -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/handler/sender.h: -------------------------------------------------------------------------------- 1 | #ifndef SENDER_H 2 | #define SENDER_H 3 | 4 | class Sender 5 | { 6 | public: 7 | virtual ~Sender(){} 8 | virtual bool SendData(char* data, int len) = 0; 9 | }; 10 | 11 | #endif/*SENDER_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/signal/receiver_func.h: -------------------------------------------------------------------------------- 1 | #ifndef RECEIVER_FUNC_H 2 | #define RECEIVER_FUNC_H 3 | 4 | class SignalReceiver; 5 | 6 | class IReceiverFunc 7 | { 8 | public: 9 | virtual ~IReceiverFunc(){} 10 | virtual bool isSignal(const DataMessage& msg) = 0; 11 | virtual void onSignal(const DataMessage& msg) = 0; 12 | virtual SignalReceiver* GetReciever() const = 0; 13 | }; 14 | 15 | template 16 | class ReceiverFunc : public IReceiverFunc 17 | { 18 | friend class Signal; 19 | public: 20 | typedef void (TReciever::*SignalFunction)(const DataMessage& msg); 21 | 22 | ReceiverFunc(TReciever* reciever, const std::string& typeSignal, SignalFunction func) 23 | : m_receiver(reciever), m_func(func), m_typeSignal(typeSignal) 24 | { 25 | } 26 | 27 | protected: 28 | bool isSignal(const DataMessage& msg) 29 | { 30 | return m_typeSignal == msg.GetName(); 31 | } 32 | 33 | void onSignal(const DataMessage& msg) 34 | { 35 | if(m_typeSignal != msg.GetName()) 36 | { 37 | return; 38 | } 39 | 40 | (m_receiver->*m_func)(msg); 41 | } 42 | 43 | SignalReceiver* GetReciever() const 44 | { 45 | return m_receiver; 46 | } 47 | 48 | private: 49 | SignalFunction m_func; 50 | TReciever* m_receiver; 51 | std::string m_typeSignal; 52 | }; 53 | 54 | #endif/*RECEIVER_FUNC_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/signal/signal.cpp: -------------------------------------------------------------------------------- 1 | #include "signal.h" 2 | #include "signal_receiver.h" 3 | #include "receiver_func.h" 4 | #include "signal_manager.h" 5 | #include "include/ref.h" 6 | #include "thread_lib/thread/thread_manager.h" 7 | 8 | Signal::Signal(SignalOwner* owner) 9 | : m_owner(owner) 10 | { 11 | SignalManager::GetInstance().AddSignal(this); 12 | } 13 | 14 | Signal::~Signal(void) 15 | { 16 | } 17 | 18 | void Signal::onSignal(const DataMessage& msg) 19 | { 20 | m_receiverFunctions.ProcessingObjects(Ref(this, &Signal::onSignalFunc, msg)); 21 | } 22 | 23 | void Signal::subscribe(IReceiverFunc* obj) 24 | { 25 | m_receiverFunctions.AddObject(obj); 26 | } 27 | 28 | void Signal::unsubscribe(SignalReceiver* receiver) 29 | { 30 | m_receiverFunctions.CheckObjects(Ref(this, &Signal::unsubsribeFunc, receiver)); 31 | } 32 | 33 | void Signal::removeOwner() 34 | { 35 | m_owner = 0; 36 | m_receiverFunctions.CheckObjects(Ref(this, &Signal::unsubsribeReceiver)); 37 | } 38 | 39 | bool Signal::onSignalFunc(const DataMessage& msg, const IReceiverFunc* obj) 40 | { 41 | if(const_cast(obj)->isSignal(msg)) 42 | { 43 | const_cast(obj)->onSignal(msg); 44 | } 45 | return true; 46 | } 47 | 48 | bool Signal::unsubsribeFunc(const SignalReceiver* receiver, const IReceiverFunc* signal) 49 | { 50 | if(const_cast(signal)->GetReciever() == receiver) 51 | { 52 | delete signal; 53 | return true; 54 | } 55 | 56 | return false; 57 | } 58 | 59 | bool Signal::unsubsribeReceiver(const IReceiverFunc* signal) 60 | { 61 | bool res = const_cast(signal)->GetReciever()->onRemoveSignal(this); 62 | if(res) 63 | { 64 | delete signal; 65 | } 66 | return res; 67 | } 68 | 69 | bool Signal::CheckSignal() 70 | { 71 | return !m_owner && !m_receiverFunctions.GetObjectList().size(); 72 | } -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/signal/signal.h: -------------------------------------------------------------------------------- 1 | #ifndef SIGNAL_H 2 | #define SIGNAL_H 3 | 4 | #include "include/object_manager.h" 5 | #include "include/data_message.h" 6 | #include "thread_lib/thread/thread_impl.h" 7 | #include "receiver_func.h" 8 | 9 | class SignalOwner; 10 | 11 | class Signal 12 | { 13 | public: 14 | Signal(SignalOwner* owner); 15 | 16 | void onSignal(const DataMessage& msg); 17 | void removeOwner(); 18 | protected: 19 | friend class SignalManager; 20 | bool CheckSignal(); 21 | virtual ~Signal(void); 22 | protected: 23 | friend class SignalReceiver; 24 | void subscribe(IReceiverFunc* obj); 25 | void unsubscribe(SignalReceiver* receiver); 26 | private: 27 | bool onSignalFunc(const DataMessage& msg, const IReceiverFunc* obj); 28 | bool unsubsribeFunc(const SignalReceiver* receiver, const IReceiverFunc* signal); 29 | bool unsubsribeReceiver(const IReceiverFunc* signal); 30 | ObjectManager m_receiverFunctions; 31 | SignalOwner* m_owner; 32 | }; 33 | 34 | #endif // SIGNAL_H 35 | -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/signal/signal_manager.cpp: -------------------------------------------------------------------------------- 1 | #include "signal_manager.h" 2 | #include "signal.h" 3 | 4 | #include "include/ref.h" 5 | 6 | SignalManager::SignalManager() 7 | { 8 | } 9 | 10 | SignalManager::~SignalManager() 11 | { 12 | } 13 | 14 | void SignalManager::AddSignal(Signal* signal) 15 | { 16 | m_signals.AddObject(signal); 17 | } 18 | 19 | bool SignalManager::CheckSignal(const std::vector& signals, const Signal* signal) 20 | { 21 | Signal* signal_ = const_cast(signal); 22 | if(signal_->CheckSignal()) 23 | { 24 | const_cast&>(signals).push_back(signal_); 25 | return true; 26 | } 27 | 28 | return false; 29 | } 30 | 31 | void SignalManager::ManagerFunc() 32 | { 33 | std::vector signals; 34 | m_signals.CheckObjects(Ref(this, &SignalManager::CheckSignal, signals)); 35 | 36 | for(std::vector::iterator it = signals.begin(); it != signals.end(); it++) 37 | { 38 | delete *it; 39 | } 40 | } -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/signal/signal_manager.h: -------------------------------------------------------------------------------- 1 | #ifndef SIGNAL_MANAGER_H 2 | #define SIGNAL_MANAGER_H 3 | 4 | #include "thread_lib/common/managers_container.h" 5 | #include "include/object_manager.h" 6 | 7 | class Signal; 8 | 9 | class SignalManager : public ManagerCreator 10 | { 11 | protected: 12 | friend class ManagerCreator; 13 | SignalManager(); 14 | ~SignalManager(); 15 | public: 16 | void AddSignal(Signal* signal); 17 | protected: 18 | template friend class ReferenceObject; 19 | bool CheckSignal(const std::vector& signals, const Signal* signal); 20 | protected: 21 | virtual void ManagerFunc(); 22 | private: 23 | ObjectManager m_signals; 24 | }; 25 | 26 | #endif/*SIGNAL_MANAGER_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/signal/signal_message.h: -------------------------------------------------------------------------------- 1 | #ifndef SIGNAL_MESSAGE_H 2 | #define SIGNAL_MESSAGE_H 3 | 4 | #include "include/data_message.h" 5 | 6 | template 7 | class SignalMessage : public DataMessage, public TMessage 8 | { 9 | public: 10 | SignalMessage(const TMessage& msg) 11 | { 12 | TMessage::operator = (msg); 13 | } 14 | 15 | static std::string GetMessageName() 16 | { 17 | return typeid(SignalMessage).name(); 18 | } 19 | }; 20 | 21 | #endif/*SIGNAL_MESSAGE_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/signal/signal_owner.cpp: -------------------------------------------------------------------------------- 1 | #include "signal_owner.h" 2 | 3 | SignalOwner::SignalOwner() 4 | { 5 | m_signal = new Signal(this); 6 | } 7 | 8 | SignalOwner::~SignalOwner() 9 | { 10 | m_signal->removeOwner(); 11 | } 12 | 13 | void SignalOwner::onSignal(const DataMessage& msg) 14 | { 15 | m_signal->onSignal(msg); 16 | } 17 | 18 | void SignalOwner::addSubscriber(SignalReceiver* receiver, IReceiverFunc* func) 19 | { 20 | receiver->addSignal(m_signal, func); 21 | } 22 | -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/signal/signal_owner.h: -------------------------------------------------------------------------------- 1 | #ifndef SIGNAL_OWNER_H 2 | #define SIGNAL_OWNER_H 3 | 4 | #include "include/data_message.h" 5 | #include "connector_lib/signal/signal_receiver.h" 6 | 7 | class SignalOwner 8 | { 9 | public: 10 | SignalOwner(); 11 | virtual ~SignalOwner(); 12 | 13 | void addSubscriber(SignalReceiver* receiver, IReceiverFunc* func); 14 | protected: 15 | void onSignal(const DataMessage& msg); 16 | 17 | private: 18 | Signal* m_signal; 19 | }; 20 | 21 | #endif/*SIGNAL_OWNER_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/signal/signal_receiver.cpp: -------------------------------------------------------------------------------- 1 | #include "signal_receiver.h" 2 | #include "include/ref.h" 3 | 4 | SignalReceiver::SignalReceiver(void) 5 | : m_isRemove(false) 6 | { 7 | } 8 | 9 | SignalReceiver::~SignalReceiver(void) 10 | { 11 | removeReceiver(); 12 | } 13 | 14 | void SignalReceiver::removeSignal(Signal* signal) 15 | { 16 | CSLocker locker(&m_cs); 17 | if(!m_isRemove) 18 | { 19 | m_signals.RemoveObject(signal); 20 | signal->unsubscribe(this); 21 | } 22 | } 23 | 24 | void SignalReceiver::addSignal(Signal* signal, IReceiverFunc* func) 25 | { 26 | CSLocker locker(&m_cs); 27 | if(!m_isRemove) 28 | { 29 | m_signals.AddObject(signal); 30 | signal->subscribe(func); 31 | } 32 | else 33 | { 34 | delete func; 35 | } 36 | } 37 | 38 | bool SignalReceiver::onRemoveSignal(Signal* signal) 39 | { 40 | CSLocker locker(&m_cs); 41 | if(!m_isRemove) 42 | { 43 | m_signals.RemoveObject(signal); 44 | return true; 45 | } 46 | 47 | return false; 48 | } 49 | 50 | void SignalReceiver::removeReceiver() 51 | { 52 | { 53 | CSLocker locker(&m_cs); 54 | m_isRemove = true; 55 | } 56 | 57 | m_signals.CheckObjects(Ref(this, &SignalReceiver::removeSignals)); 58 | } 59 | 60 | bool SignalReceiver::removeSignals(const Signal* signal) 61 | { 62 | const_cast(signal)->unsubscribe(this); 63 | return true; 64 | } 65 | 66 | -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/signal/signal_receiver.h: -------------------------------------------------------------------------------- 1 | #ifndef SIGNAL_RECEIVER_H 2 | #define SIGNAL_RECEIVER_H 3 | 4 | #include "signal.h" 5 | #include "receiver_func.h" 6 | #include "include/object_manager.h" 7 | 8 | #define SIGNAL_FUNC(ClassObject, Class, MessageName, Func)\ 9 | new ReceiverFunc(ClassObject, MessageName::GetMessageName(), reinterpret_cast::SignalFunction>(\ 10 | static_cast(&Class::Func))) 11 | 12 | class SignalReceiver 13 | { 14 | friend class Signal; 15 | public: 16 | SignalReceiver(void); 17 | virtual ~SignalReceiver(void); 18 | 19 | //Removing receiver as subscriber. 20 | //After call this method object of this class will never subscribe on signals. 21 | //Let's call on to delete object. 22 | void removeReceiver(); 23 | 24 | void removeSignal(Signal* signal); 25 | void addSignal(Signal* signal, IReceiverFunc* func); 26 | protected: 27 | bool removeSignals(const Signal* signal); 28 | bool onRemoveSignal(Signal* signal); 29 | private: 30 | ObjectManager m_signals; 31 | CriticalSection m_cs; 32 | bool m_isRemove; 33 | }; 34 | 35 | #endif // SIGNAL_RECEIVER_H 36 | -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/socket/any_socket.h: -------------------------------------------------------------------------------- 1 | #ifndef ANY_SOCKET_H 2 | #define ANY_SOCKET_H 3 | 4 | #include 5 | 6 | #ifdef WIN32 7 | #define WIN32_LEAN_AND_MEAN 8 | #include 9 | #include 10 | #include 11 | #else 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #define SD_BOTH SHUT_RDWR 21 | #endif/*WIN32*/ 22 | 23 | #ifndef INVALID_SOCKET 24 | # define INVALID_SOCKET -1 25 | #endif/*INVALID_SOCKET*/ 26 | #ifndef SOCKET_ERROR 27 | # define SOCKET_ERROR -1 28 | #endif/*SOCKET_ERROR*/ 29 | 30 | class AnySocket 31 | { 32 | public: 33 | enum IPVersion 34 | { 35 | IPV4 = AF_INET, 36 | IPV6 = AF_INET6 37 | }; 38 | 39 | AnySocket(){} 40 | AnySocket(IPVersion ipv) : m_ipv(ipv){} 41 | virtual ~AnySocket(){} 42 | public: 43 | virtual bool Bind(const std::string& host, int port) = 0; 44 | virtual bool Listen(int limit) = 0; 45 | virtual int Accept(std::string& ip, int& port) = 0; 46 | virtual bool Connect(const std::string& host, int port) = 0; 47 | virtual bool Send(char* data, int len) = 0; 48 | virtual bool Recv(char* data, int len) = 0; 49 | virtual bool Close() = 0; 50 | virtual void GetIPPort(std::string& ip, int& port) = 0; 51 | virtual int GetSocket() = 0; 52 | virtual int GetMaxBufferSize() = 0; 53 | 54 | IPVersion m_ipv; 55 | }; 56 | 57 | #endif/*ANY_SOCKET_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/socket/proxy_socket.h: -------------------------------------------------------------------------------- 1 | #ifndef PROXY_SOCKET_H 2 | #define PROXY_SOCKET_H 3 | 4 | #include "secure_socket.h" 5 | 6 | class ProxySocket 7 | { 8 | enum digestAuthType 9 | { 10 | DIGEST_AUTH_UNKNOWN = 1, 11 | DIGEST_AUTH = 2, 12 | DIGEST_AUTH_INTEGRITY = 8 13 | }; 14 | 15 | static const std::string auth_header_BASIC; 16 | static const std::string auth_header_DIGEST; 17 | static const std::string auth_header_NTLM; 18 | static const std::string auth_header_Negotiate; 19 | public: 20 | ProxySocket(const std::string& ip, int port); 21 | 22 | void SetUserName(const std::string& userName); 23 | void SetPassword(const std::string& password); 24 | 25 | bool PerformProxyConnection(const std::string& ip, int port); 26 | protected: 27 | virtual bool SendData(char* data, int len) = 0; 28 | virtual bool RecvData(char* data, int len) = 0; 29 | virtual bool ReConnect() = 0; 30 | 31 | private: 32 | bool sendConnectCmd(const std::string& httpheaders); 33 | bool recvProxyResponse(int& responseCode, std::string& proxyResponse); 34 | bool performProxyAuthtorization(const std::string& proxyResponse); 35 | bool performBasicProxyAuthentication(const std::string& proxyResponse); 36 | bool performDigestProxyAuthentication(const std::string& proxyResponse); 37 | bool performNtlmProxyAuthentication(const std::string& proxyResponse); 38 | char getDigestAuthType(const std::string &authString); 39 | const std::string encryptToMd5(const std::string &sourceString); 40 | const std::string generateStandartHeaders(); 41 | 42 | protected: 43 | std::string m_ip; 44 | int m_port; 45 | private: 46 | std::string m_userName; 47 | std::string m_password; 48 | 49 | std::string m_targetIp; 50 | int m_targetPort; 51 | }; 52 | 53 | class ProxyTCPSocket : public ProxySocket, public TCPSocket 54 | { 55 | public: 56 | ProxyTCPSocket(const std::string& ip, int port, IPVersion ipv); 57 | ProxyTCPSocket(int socket, const std::string& ip, int port); 58 | 59 | bool Connect(const std::string& ip, int port); 60 | 61 | protected: 62 | virtual bool SendData(char* data, int len); 63 | virtual bool RecvData(char* data, int len); 64 | virtual bool ReConnect(); 65 | }; 66 | 67 | class SecureProxyTCPSocket : public ProxySocket, public SecureTCPSocket 68 | { 69 | public: 70 | SecureProxyTCPSocket(const std::string& ip, int port, IPVersion ipv); 71 | SecureProxyTCPSocket(int socket, const std::string& ip, int port); 72 | 73 | bool Connect(const std::string& ip, int port); 74 | 75 | protected: 76 | virtual bool SendData(char* data, int len); 77 | virtual bool RecvData(char* data, int len); 78 | virtual bool ReConnect(); 79 | }; 80 | 81 | #endif/*PROXY_SOCKET_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/socket/secure_socket.h: -------------------------------------------------------------------------------- 1 | #ifndef SECURE_SOCKET_H 2 | #define SECURE_SOCKET_H 3 | 4 | #include 5 | #include "udt_socket.h" 6 | #include "tcp_socket.h" 7 | 8 | class AnySocket; 9 | 10 | class SecureSocket 11 | { 12 | public: 13 | SecureSocket(); 14 | virtual ~SecureSocket(); 15 | 16 | virtual bool Send(char* data, int len); 17 | virtual bool Recv(char* data, int len); 18 | protected: 19 | bool PerformSslVerify(const std::string& tlsString); 20 | 21 | virtual bool SendData(char* data, int len) = 0; 22 | virtual bool RecvData(char* data, int len) = 0; 23 | 24 | bool GetData(char* data, int len); 25 | protected: 26 | bool m_bInit; 27 | unsigned char m_key[32]; 28 | std::string m_recvdata; 29 | protected: 30 | static std::string startTls; 31 | static std::string expecTls; 32 | }; 33 | 34 | 35 | class SecureUDTSocket : public SecureSocket, public UDTSocket 36 | { 37 | public: 38 | SecureUDTSocket(IPVersion ipv); 39 | explicit SecureUDTSocket(int socket, IPVersion ipv, bool isUdp); 40 | explicit SecureUDTSocket(int udpSocket, int socket); 41 | 42 | virtual bool Connect(const std::string& host, int port); 43 | virtual bool Send(char* data, int len); 44 | virtual bool Recv(char* data, int len); 45 | protected: 46 | virtual bool SendData(char* data, int len); 47 | virtual bool RecvData(char* data, int len); 48 | }; 49 | 50 | class SecureTCPSocket : public SecureSocket, public TCPSocket 51 | { 52 | public: 53 | SecureTCPSocket(IPVersion ipv); 54 | explicit SecureTCPSocket(int socket); 55 | 56 | virtual bool Connect(const std::string& host, int port); 57 | virtual bool Send(char* data, int len); 58 | virtual bool Recv(char* data, int len); 59 | protected: 60 | virtual bool SendData(char* data, int len); 61 | virtual bool RecvData(char* data, int len); 62 | }; 63 | 64 | #endif/*SECURE_SOCKET_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/socket/socket_factories.h: -------------------------------------------------------------------------------- 1 | #ifndef SOCKET_FACTORIES_H 2 | #define SOCKET_FACTORIES_H 3 | 4 | #include "socket_factory.h" 5 | #include 6 | 7 | class TCPSocketFactory : public SocketFactory 8 | { 9 | public: 10 | TCPSocketFactory(int ipv); 11 | AnySocket* CreateSocket(); 12 | AnySocket* CreateSocket(int socket); 13 | SocketFactory* Clone(); 14 | private: 15 | int m_ipv; 16 | }; 17 | 18 | class TCPProxySocketFactory : public SocketFactory 19 | { 20 | public: 21 | TCPProxySocketFactory(const std::string& ip, int port, const std::string& user, const std::string& pass, int ipv); 22 | 23 | AnySocket* CreateSocket(); 24 | AnySocket* CreateSocket(int socket); 25 | SocketFactory* Clone(); 26 | 27 | private: 28 | const std::string& m_ip; 29 | int m_port; 30 | const std::string& m_user; 31 | const std::string& m_pass; 32 | int m_ipv; 33 | }; 34 | 35 | class TCPSecureSocketFactory : public SocketFactory 36 | { 37 | public: 38 | TCPSecureSocketFactory(int ipv); 39 | AnySocket* CreateSocket(); 40 | AnySocket* CreateSocket(int socket); 41 | SocketFactory* Clone(); 42 | private: 43 | int m_ipv; 44 | }; 45 | 46 | class TCPSecureProxySocketFactory : public SocketFactory 47 | { 48 | public: 49 | TCPSecureProxySocketFactory(const std::string& ip, int port, const std::string& user, const std::string& pass, int ipv); 50 | 51 | AnySocket* CreateSocket(); 52 | AnySocket* CreateSocket(int socket); 53 | SocketFactory* Clone(); 54 | 55 | private: 56 | const std::string& m_ip; 57 | int m_port; 58 | const std::string& m_user; 59 | const std::string& m_pass; 60 | int m_ipv; 61 | }; 62 | 63 | class UDPSocketFactory : public SocketFactory 64 | { 65 | public: 66 | UDPSocketFactory(int ipv); 67 | AnySocket* CreateSocket(); 68 | AnySocket* CreateSocket(int socket); 69 | SocketFactory* Clone(); 70 | private: 71 | int m_ipv; 72 | }; 73 | 74 | class UDTSocketFactory : public SocketFactory 75 | { 76 | public: 77 | UDTSocketFactory(int ipv); 78 | 79 | AnySocket* CreateSocket(); 80 | AnySocket* CreateSocket(int socket); 81 | SocketFactory* Clone(); 82 | 83 | void SetUdpSocket(int udpSocket); 84 | private: 85 | int m_udpSocket; 86 | int m_ipv; 87 | }; 88 | 89 | class UDTSecureSocketFactory : public SocketFactory 90 | { 91 | public: 92 | UDTSecureSocketFactory(int ipv); 93 | 94 | AnySocket* CreateSocket(); 95 | AnySocket* CreateSocket(int socket); 96 | SocketFactory* Clone(); 97 | 98 | void SetUdpSocket(int udpSocket); 99 | private: 100 | int m_udpSocket; 101 | int m_ipv; 102 | }; 103 | 104 | #endif/*SOCKET_FACTORIES_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/socket/socket_factory.h: -------------------------------------------------------------------------------- 1 | #ifndef SOCKET_FACTORY_H 2 | #define SOCKET_FACTORY_H 3 | 4 | class AnySocket; 5 | 6 | class SocketFactory 7 | { 8 | public: 9 | virtual ~SocketFactory(){} 10 | virtual AnySocket* CreateSocket() = 0; 11 | virtual AnySocket* CreateSocket(int socket) = 0; 12 | virtual SocketFactory* Clone() = 0; 13 | }; 14 | 15 | #endif/*SOCKET_FACTORY_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/socket/tcp_socket.h: -------------------------------------------------------------------------------- 1 | #ifndef TCP_SOCKET_H 2 | #define TCP_SOCKET_H 3 | 4 | #include 5 | #include "any_socket.h" 6 | #include "include/critical_section.h" 7 | 8 | #include "openssl/ssl.h" 9 | #include "openssl/err.h" 10 | 11 | class TCPSocket : public AnySocket 12 | { 13 | public: 14 | TCPSocket(IPVersion ipv); 15 | explicit TCPSocket(int socket); 16 | virtual ~TCPSocket(); 17 | 18 | virtual bool Bind(const std::string& host, int port); 19 | virtual bool Listen(int limit); 20 | virtual int Accept(std::string& ip, int& port); 21 | virtual bool Connect(const std::string& host, int port); 22 | virtual bool Send(char* data, int len); 23 | virtual bool Recv(char* data, int len); 24 | virtual void GetIPPort(std::string& ip, int& port); 25 | virtual bool Close(); 26 | virtual int GetSocket(); 27 | virtual int GetMaxBufferSize(); 28 | protected: 29 | void Initialize(); 30 | private: 31 | int m_socket; 32 | CriticalSection m_cs; 33 | }; 34 | 35 | #endif/*TCP_SOCKET_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/socket/udp_socket.h: -------------------------------------------------------------------------------- 1 | #ifndef UDP_SOCKET_H 2 | #define UDP_SOCKET_H 3 | 4 | #include 5 | #include "any_socket.h" 6 | #include "include/critical_section.h" 7 | 8 | class UDPSocket : public AnySocket 9 | { 10 | public: 11 | UDPSocket(IPVersion ipv); 12 | explicit UDPSocket(int socket); 13 | virtual ~UDPSocket(); 14 | 15 | virtual bool Bind(const std::string& host, int port); 16 | virtual bool Listen(int limit); 17 | virtual int Accept(std::string& ip, int& port); 18 | virtual bool Connect(const std::string& host, int port); 19 | virtual bool Send(char* data, int len); 20 | virtual bool Recv(char* data, int len); 21 | virtual void GetIPPort(std::string& ip, int& port); 22 | virtual bool Close(); 23 | virtual int GetSocket(); 24 | virtual int GetMaxBufferSize(); 25 | 26 | bool RecvFrom(char* data, int len, std::string& ip, int& port); 27 | void SetSendAddr(const std::string& host, int port); 28 | protected: 29 | int m_socket; 30 | std::string m_host; 31 | int m_port; 32 | CriticalSection m_cs; 33 | }; 34 | 35 | #endif/*UDP_SOCKET_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/socket/udt_socket.h: -------------------------------------------------------------------------------- 1 | #ifndef UDT_SOCKET_H 2 | #define UDT_SOCKET_H 3 | 4 | #include "any_socket.h" 5 | #include "include/critical_section.h" 6 | 7 | class UDTSocket : public AnySocket 8 | { 9 | public: 10 | UDTSocket(IPVersion ipv); 11 | explicit UDTSocket(int socket, IPVersion ipv, bool isUdp); 12 | explicit UDTSocket(int udpSocket, int socket); 13 | virtual ~UDTSocket(); 14 | 15 | virtual bool Bind(const std::string& host, int port); 16 | virtual bool Listen(int limit); 17 | virtual int Accept(std::string& ip, int& port); 18 | virtual bool Connect(const std::string& host, int port); 19 | virtual bool Send(char* data, int len); 20 | virtual bool Recv(char* data, int len); 21 | virtual bool Close(); 22 | virtual void GetIPPort(std::string& ip, int& port); 23 | virtual int GetSocket(); 24 | virtual int GetMaxBufferSize(); 25 | private: 26 | int m_udpSocket; 27 | int m_socket; 28 | 29 | CriticalSection m_cs; 30 | }; 31 | 32 | #endif/*UDT_SOCKET_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/thread/address.h: -------------------------------------------------------------------------------- 1 | #ifndef ADDRESS_H 2 | #define ADDRESS_H 3 | 4 | #include 5 | 6 | class SocketFactory; 7 | class ConnectorFactory; 8 | 9 | class Address 10 | { 11 | public: 12 | std::string m_localIP; 13 | unsigned int m_localPort; 14 | 15 | void operator = (const Address& address) 16 | { 17 | m_localIP = address.m_localIP; 18 | m_localPort = address.m_localPort; 19 | } 20 | }; 21 | 22 | class ConnectorAddress : public Address 23 | { 24 | public: 25 | SocketFactory* m_socketFactory; 26 | ConnectorFactory* m_connectorFactory; 27 | std::string m_id; 28 | 29 | void operator = (const ConnectorAddress& address) 30 | { 31 | Address::operator = (address); 32 | m_socketFactory = address.m_socketFactory; 33 | m_connectorFactory = address.m_connectorFactory; 34 | m_id = address.m_id; 35 | } 36 | }; 37 | 38 | class ConnectAddress : public ConnectorAddress 39 | { 40 | public: 41 | std::string m_ip; 42 | unsigned int m_port; 43 | std::string m_moduleName; 44 | void operator = (const ConnectAddress& address) 45 | { 46 | ConnectorAddress::operator = (address); 47 | m_ip = address.m_ip; 48 | m_port = address.m_port; 49 | } 50 | }; 51 | 52 | class ListenAddress : public ConnectorAddress 53 | { 54 | public: 55 | unsigned int m_acceptCount; 56 | }; 57 | 58 | #endif/*ADDRESS_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/thread/connect_thread.cpp: -------------------------------------------------------------------------------- 1 | #include "connect_thread.h" 2 | #include "common/common_func.h" 3 | #include "socket/any_socket.h" 4 | #include "socket/socket_factory.h" 5 | #include "connector/connector.h" 6 | #include "connector/connector_factory.h" 7 | #include "message/connector_messages.h" 8 | 9 | ConnectThread::ConnectThread(const ConnectAddress& address) 10 | : m_address(address), m_socket(0) 11 | { 12 | } 13 | 14 | ConnectThread::~ConnectThread() 15 | { 16 | if(m_socket) 17 | { 18 | delete m_socket; 19 | m_socket = 0; 20 | } 21 | delete m_address.m_connectorFactory; 22 | delete m_address.m_socketFactory; 23 | } 24 | 25 | void ConnectThread::ThreadFunc() 26 | { 27 | m_socket = m_address.m_socketFactory->CreateSocket(); 28 | m_socket->Bind(m_address.m_localIP, m_address.m_localPort); 29 | 30 | if(m_socket->Connect(m_address.m_ip, m_address.m_port)) 31 | { 32 | Connector* connector = m_address.m_connectorFactory->CreateConnector(m_socket); 33 | connector->SetId(m_address.m_id); 34 | connector->SetRemoteAddr(m_address.m_ip, m_address.m_port); 35 | m_socket = 0; 36 | ConnectorMessage msg(connector); 37 | onSignal(msg); 38 | } 39 | else 40 | { 41 | ConnectErrorMessage errMsg(m_address.m_moduleName, "", GetError()); 42 | onSignal(errMsg); 43 | delete m_socket; 44 | m_socket = 0; 45 | } 46 | } 47 | 48 | void ConnectThread::Stop() 49 | { 50 | if (m_socket) 51 | { 52 | m_socket->Close(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/thread/connect_thread.h: -------------------------------------------------------------------------------- 1 | #ifndef CONNECT_THREAD_H 2 | #define CONNECT_THREAD_H 3 | 4 | #include "../thread/address.h" 5 | #include "thread_lib/thread/thread_impl.h" 6 | #include "../signal/signal.h" 7 | #include "../signal/signal_owner.h" 8 | 9 | class AnySocket; 10 | 11 | class ConnectThread : public SignalOwner, public ThreadImpl 12 | { 13 | public: 14 | ConnectThread(const ConnectAddress& address); 15 | ~ConnectThread(); 16 | 17 | virtual void Stop(); 18 | protected: 19 | virtual void ThreadFunc(); 20 | 21 | protected: 22 | ConnectAddress m_address; 23 | AnySocket* m_socket; 24 | }; 25 | 26 | #endif/*CONNECT_THREAD_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/thread/listen_thread.cpp: -------------------------------------------------------------------------------- 1 | #include "listen_thread.h" 2 | #include "common/common_func.h" 3 | #include "socket/socket_factory.h" 4 | #include "socket/any_socket.h" 5 | #include "connector/connector.h" 6 | #include "connector/connector_factory.h" 7 | #include "message/connector_messages.h" 8 | 9 | ListenThread::ListenThread(const ListenAddress& address) 10 | : m_address(address), m_socket(0) 11 | { 12 | } 13 | 14 | ListenThread::~ListenThread() 15 | { 16 | if(m_socket) 17 | { 18 | delete m_socket; 19 | m_socket = 0; 20 | } 21 | delete m_address.m_connectorFactory; 22 | delete m_address.m_socketFactory; 23 | } 24 | 25 | void ListenThread::ThreadFunc() 26 | { 27 | m_socket = m_address.m_socketFactory->CreateSocket(); 28 | if(!m_socket->Bind(m_address.m_localIP, m_address.m_localPort) || 29 | !m_socket->Listen(SOMAXCONN)) 30 | { 31 | char printdata[100] = {0}; 32 | sprintf(printdata, "listen on %s:%d finished with error", m_address.m_localIP.c_str(), m_address.m_localPort); 33 | ListenErrorMessage errMsg(m_address.m_id, printdata, GetError()); 34 | onSignal(errMsg); 35 | delete m_socket; 36 | m_socket = 0; 37 | return; 38 | } 39 | 40 | std::string ip; 41 | int port = 0; 42 | m_socket->GetIPPort(ip, port); 43 | CreatedListenerMessage clMsg(ip, port, m_address.m_id); 44 | onSignal(clMsg); 45 | 46 | bool infinite = (m_address.m_acceptCount == -1); 47 | int count = m_address.m_acceptCount; 48 | while(!IsStop()) 49 | { 50 | std::string ip; 51 | int port; 52 | int sock = m_socket->Accept(ip, port); 53 | if(sock != INVALID_SOCKET) 54 | { 55 | AnySocket* socket = m_address.m_socketFactory->CreateSocket(sock); 56 | Connector* connector = m_address.m_connectorFactory->CreateConnector(socket); 57 | connector->SetId(m_address.m_id); 58 | connector->SetRemoteAddr(ip, port); 59 | ConnectorMessage msg(connector); 60 | onSignal(msg); 61 | } 62 | else 63 | { 64 | ErrorMessage errMsg("", GetError()); 65 | onSignal(errMsg); 66 | delete m_socket; 67 | m_socket = 0; 68 | return; 69 | } 70 | 71 | if(!infinite && --count <= 0) 72 | { 73 | delete m_socket; 74 | m_socket = 0; 75 | return; 76 | } 77 | } 78 | } 79 | 80 | void ListenThread::Stop() 81 | { 82 | StopThread(); 83 | 84 | if(m_socket) 85 | { 86 | m_socket->Close(); 87 | } 88 | 89 | } -------------------------------------------------------------------------------- /src/cpp/lib/connector_lib/thread/listen_thread.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTEN_THREAD_H 2 | #define LISTEN_THREAD_H 3 | 4 | #include "address.h" 5 | #include "thread_lib/thread/thread_impl.h" 6 | #include "../signal/signal.h" 7 | #include "../signal/signal_owner.h" 8 | 9 | class AnySocket; 10 | 11 | class ListenThread : public SignalOwner, public ThreadImpl 12 | { 13 | public: 14 | ListenThread(const ListenAddress& address); 15 | virtual ~ListenThread(); 16 | 17 | virtual void Stop(); 18 | protected: 19 | virtual void ThreadFunc(); 20 | 21 | private: 22 | ListenAddress m_address; 23 | AnySocket* m_socket; 24 | }; 25 | 26 | #endif/*LISTEN_THREAD_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/ipc_lib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | project(ipc_lib) 3 | 4 | if(NOT CMAKE_BUILD_TYPE) 5 | set(CMAKE_BUILD_TYPE "Debug") 6 | # set(CMAKE_BUILD_TYPE "Release") 7 | endif(NOT CMAKE_BUILD_TYPE) 8 | 9 | if(NOT USE_SYSTEM_PROTOBUF) 10 | set(PROTOC_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../messages/protoc) 11 | include_directories(${CMAKE_CURRENT_SOURCE_DIR} 12 | ${CMAKE_CURRENT_SOURCE_DIR}/.. 13 | ${CMAKE_CURRENT_SOURCE_DIR}/../.. 14 | ${CMAKE_CURRENT_SOURCE_DIR}/../../../messages 15 | ${CMAKE_CURRENT_SOURCE_DIR}/../../external/protobuf-2.5.0/src) 16 | else(NOT USE_SYSTEM_PROTOBUF) 17 | set(PROTOC_PATH protoc) 18 | include_directories(${CMAKE_CURRENT_SOURCE_DIR} 19 | ${CMAKE_CURRENT_SOURCE_DIR}/.. 20 | ${CMAKE_CURRENT_SOURCE_DIR}/../.. 21 | ${CMAKE_CURRENT_SOURCE_DIR}/../../../messages) 22 | endif(NOT USE_SYSTEM_PROTOBUF) 23 | 24 | set (SRC_${PROJECT_NAME} 25 | ../../common/tstring.cpp 26 | ../../common/user.cpp 27 | ../../utils/utils.cpp 28 | connector/ipc_connector.cpp 29 | connector/ipc_handler.cpp 30 | connector/internal_connector.cpp 31 | module/ipc_module.cpp 32 | module/ipc_object_name.cpp 33 | module/ipc_signal_handler.cpp 34 | thread/ipc_checker_thread.cpp 35 | thread/ping_thread.cpp 36 | ) 37 | 38 | set (SRC_GEN_${PROJECT_NAME} 39 | ${CMAKE_CURRENT_SOURCE_DIR}/../../../messages/ipc.pb.cc 40 | ${CMAKE_CURRENT_SOURCE_DIR}/../../../messages/ipc.pb.h 41 | ${CMAKE_CURRENT_SOURCE_DIR}/../../../messages/interconn.pb.cc 42 | ${CMAKE_CURRENT_SOURCE_DIR}/../../../messages/interconn.pb.h) 43 | 44 | set_source_files_properties(${SRC_GEN_${PROJECT_NAME}} PROPERTIES GENERATED TRUE) 45 | add_custom_command( OUTPUT ${SRC_GEN_${PROJECT_NAME}} 46 | COMMAND ${PROTOC_PATH} 47 | ARGS --proto_path=. --cpp_out=. ipc.proto interconn.proto 48 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../../messages 49 | DEPENDS ../../../messages/ipc.proto ../../../messages/interconn.proto) 50 | 51 | add_definitions(-fPIC) 52 | 53 | add_library (${PROJECT_NAME} STATIC ${SRC_${PROJECT_NAME}} ${SRC_GEN_${PROJECT_NAME}}) 54 | set(CMAKE_LIBRARY_OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../output/${PROJECT_NAME}) 55 | set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../../output/lib/${CMAKE_BUILD_TYPE}) 56 | -------------------------------------------------------------------------------- /src/cpp/lib/ipc_lib/connector/internal_connector.cpp: -------------------------------------------------------------------------------- 1 | #include "internal_connector.h" 2 | 3 | InternalConnector::InternalConnector(AnySocket* socket) 4 | : Connector(socket), m_maxBufferSize(0) 5 | { 6 | } 7 | 8 | InternalConnector::~InternalConnector() 9 | { 10 | removeReceiver(); 11 | } 12 | 13 | void InternalConnector::SubscribeConnector(::SignalOwner* owner) 14 | { 15 | owner->addSubscriber(this, SIGNAL_FUNC(this, InternalConnector, InternalConnectionDataMessage, onInternalConnectionDataMessage)); 16 | } 17 | 18 | void InternalConnector::ThreadFunc() 19 | { 20 | int sizedata = m_socket->GetMaxBufferSize(); 21 | char *data = new char[sizedata]; 22 | while(!IsStop()) 23 | { 24 | int res = recv(m_socket->GetSocket(), data, sizedata, 0); 25 | if (res <= 0) 26 | { 27 | break; 28 | } 29 | 30 | InternalConnectionData icd; 31 | icd.set_id(GetId()); 32 | icd.set_data(data, res); 33 | InternalConnectionDataSignal icdSig(icd); 34 | onSignal(icdSig); 35 | } 36 | 37 | delete data; 38 | } 39 | 40 | void InternalConnector::OnStart() 41 | { 42 | } 43 | 44 | void InternalConnector::OnStop() 45 | { 46 | } 47 | 48 | void InternalConnector::onInternalConnectionDataMessage(const InternalConnectionDataMessage& msg) 49 | { 50 | if(msg.id() == GetId()) 51 | { 52 | if(!m_socket->Send((char*)msg.data().c_str(), (int)msg.data().size())) 53 | { 54 | Stop(); 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /src/cpp/lib/ipc_lib/connector/internal_connector.h: -------------------------------------------------------------------------------- 1 | #ifndef INTERNAL_CONNECTOR_H 2 | #define INTERNAL_CONNECTOR_H 3 | 4 | #include "connector_lib/connector/connector.h" 5 | #include "connector_lib/signal/signal_receiver.h" 6 | #include "connector_lib/signal/signal_owner.h" 7 | #include "ipc_connector.h" 8 | 9 | class InternalConnector : public Connector, public SignalReceiver, protected SignalOwner 10 | { 11 | public: 12 | InternalConnector(AnySocket* socket); 13 | virtual ~InternalConnector(); 14 | 15 | void SubscribeConnector(::SignalOwner* owner); 16 | protected: 17 | friend class IPCConnector; 18 | friend class Signal; 19 | void onInternalConnectionDataMessage(const InternalConnectionDataMessage& msg); 20 | protected: 21 | virtual void ThreadFunc(); 22 | virtual void OnStart(); 23 | virtual void OnStop(); 24 | private: 25 | int m_maxBufferSize; 26 | }; 27 | 28 | #endif/*INTERNAL_CONNECTOR_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/ipc_lib/connector/ipc_connector_factory.h: -------------------------------------------------------------------------------- 1 | #ifndef IPC_CONNECTOR_FACTORY_H 2 | #define IPC_CONNECTOR_FACTORY_H 3 | 4 | #include "connector_lib/connector/connector_factory.h" 5 | #include "ipc_lib/module/ipc_object_name.h" 6 | 7 | #include 8 | 9 | template 10 | class IPCConnectorFactory : public ConnectorFactory 11 | { 12 | public: 13 | IPCConnectorFactory(const IPCObjectName& moduleName); 14 | virtual ~IPCConnectorFactory(); 15 | Connector* CreateConnector(AnySocket* socket); 16 | ConnectorFactory* Clone(); 17 | private: 18 | IPCObjectName m_moduleName; 19 | }; 20 | 21 | template 22 | IPCConnectorFactory::IPCConnectorFactory(const IPCObjectName& moduleName) 23 | : m_moduleName(moduleName) 24 | { 25 | } 26 | 27 | template 28 | IPCConnectorFactory::~IPCConnectorFactory() 29 | { 30 | } 31 | 32 | template 33 | Connector* IPCConnectorFactory::CreateConnector(AnySocket* socket) 34 | { 35 | return static_cast(new TConnector(socket, m_moduleName)); 36 | } 37 | 38 | template 39 | ConnectorFactory* IPCConnectorFactory::Clone() 40 | { 41 | return new IPCConnectorFactory(m_moduleName); 42 | } 43 | 44 | #endif/*IPC_CONNECTOR_FACTORY_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/ipc_lib/connector/ipc_handler.h: -------------------------------------------------------------------------------- 1 | #ifndef IPC_HANDLER_H 2 | #define IPC_HANDLER_H 3 | 4 | #pragma warning(disable:4244 4267) 5 | #include "../messages/ipc.pb.h" 6 | #include "../messages/interconn.pb.h" 7 | using namespace ipc; 8 | using namespace interconn; 9 | #pragma warning(default:4244 4267) 10 | 11 | #include "connector_lib/signal/signal_message.h" 12 | #include "proto_message.h" 13 | 14 | typedef ProtoMessage IPCProtoMessage; 15 | typedef ProtoMessage ModuleNameMessage; 16 | typedef ProtoMessage AddIPCObjectMessage; 17 | typedef ProtoMessage UpdateIPCObjectMessage; 18 | typedef ProtoMessage ChangeIPCNameMessage; 19 | typedef ProtoMessage RemoveIPCObjectMessage; 20 | typedef ProtoMessage IPCObjectListMessage; 21 | typedef ProtoMessage ModuleStateMessage; 22 | typedef ProtoMessage PingMessage; 23 | typedef SignalMessage IPCMessageSignal; 24 | 25 | typedef ProtoMessage InitInternalConnectionMessage; 26 | typedef ProtoMessage InternalConnectionStatusMessage; 27 | typedef ProtoMessage InternalConnectionDataMessage; 28 | typedef SignalMessage InternalConnectionDataSignal; 29 | 30 | class IPCConnector; 31 | 32 | class IPCHandler 33 | { 34 | public: 35 | IPCHandler(IPCConnector* connector); 36 | virtual ~IPCHandler(); 37 | 38 | void onMessage(const ModuleName& msg); 39 | void onMessage(const ModuleState& msg); 40 | void onMessage(const AddIPCObject& msg); 41 | void onMessage(const RemoveIPCObject& msg); 42 | void onMessage(const IPCMessage& msg); 43 | void onMessage(const IPCObjectList& msg); 44 | void onMessage(const ChangeIPCName& msg); 45 | void onMessage(const UpdateIPCObject& msg); 46 | void onMessage(const Ping& msg); 47 | 48 | void onMessage(const InitInternalConnection& msg); 49 | void onMessage(const InternalConnectionStatus& msg); 50 | void onMessage(const InternalConnectionData& msg); 51 | private: 52 | IPCConnector* m_connector; 53 | }; 54 | 55 | #endif/*IPC_HANDLER_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/ipc_lib/module/ipc_object_name.cpp: -------------------------------------------------------------------------------- 1 | #include "ipc_object_name.h" 2 | #include "utils/utils.h" 3 | 4 | IPCObjectName::IPCObjectName(const std::string& ipcName, const std::string& hostName, const std::string& connId) 5 | { 6 | set_module_name(ipcName); 7 | set_host_name(hostName); 8 | set_conn_id(connId); 9 | m_moduleNameString = GetModuleNameString(); 10 | } 11 | 12 | IPCObjectName::IPCObjectName(const IPCName& ipcName) 13 | : IPCName(ipcName) 14 | { 15 | m_moduleNameString = GetModuleNameString(); 16 | } 17 | 18 | IPCObjectName::~IPCObjectName() 19 | { 20 | } 21 | 22 | bool IPCObjectName::operator == (const IPCName& ipcName) const 23 | { 24 | return module_name() == ipcName.module_name() && 25 | host_name() == ipcName.host_name() && 26 | conn_id() == ipcName.conn_id(); 27 | } 28 | 29 | bool IPCObjectName::operator < (const IPCName& ipcName) const 30 | { 31 | if (module_name() < ipcName.module_name() || 32 | module_name() == ipcName.module_name() && host_name() < ipcName.host_name() || 33 | module_name() == ipcName.module_name() && host_name() == ipcName.host_name() && conn_id() < ipcName.conn_id()) 34 | return true; 35 | else 36 | return false; 37 | } 38 | 39 | std::string IPCObjectName::GetModuleNameString() const 40 | { 41 | std::string moduleNameString = module_name(); 42 | if(!host_name().empty()) 43 | { 44 | moduleNameString.append("."); 45 | moduleNameString.append(host_name()); 46 | } 47 | if(!conn_id().empty()) 48 | { 49 | moduleNameString.append("."); 50 | moduleNameString.append(conn_id()); 51 | } 52 | 53 | return moduleNameString; 54 | } 55 | 56 | IPCObjectName IPCObjectName::GetIPCName(const std::string& ipcName) 57 | { 58 | IPCObjectName name(""); 59 | name.m_moduleNameString = ipcName; 60 | std::vector strings = CommonUtils::DelimitString(ipcName, "."); 61 | if(!strings.empty()) 62 | { 63 | name.set_module_name(strings[0]); 64 | } 65 | if(strings.size() > 1) 66 | { 67 | name.set_host_name(strings[1]); 68 | } 69 | if(strings.size() > 2) 70 | { 71 | name.set_conn_id(strings[2]); 72 | } 73 | 74 | return name; 75 | } 76 | -------------------------------------------------------------------------------- /src/cpp/lib/ipc_lib/module/ipc_object_name.h: -------------------------------------------------------------------------------- 1 | #ifndef IPC_OBJECT_NAME_H 2 | #define IPC_OBJECT_NAME_H 3 | 4 | #pragma warning(disable:4244 4267) 5 | #include "../messages/ipc.pb.h" 6 | using namespace ipc; 7 | #pragma warning(default:4244 4267) 8 | 9 | class IPCObjectName : public IPCName 10 | { 11 | public: 12 | IPCObjectName(const std::string& ipcName, const std::string& hostName = "", const std::string& connId = ""); 13 | IPCObjectName(const IPCName& ipcName); 14 | ~IPCObjectName(); 15 | 16 | bool operator == (const IPCName& object) const; 17 | bool operator < (const IPCName& object) const; 18 | 19 | std::string GetModuleNameString() const; 20 | 21 | static IPCObjectName GetIPCName(const std::string& ipcName); 22 | protected: 23 | void ModuleNameString(); 24 | private: 25 | std::string m_moduleNameString; 26 | }; 27 | 28 | #endif/*IPC_OBJECT_NAME_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/ipc_lib/module/ipc_signal_handler.h: -------------------------------------------------------------------------------- 1 | #ifndef IPC_SIGNAL_HANDLER_H 2 | #define IPC_SIGNAL_HANDLER_H 3 | 4 | #include "connector_lib/message/connector_messages.h" 5 | #include "ipc_lib/connector/ipc_connector.h" 6 | 7 | class IPCModule; 8 | 9 | class IPCSignalHandler : public SignalReceiver 10 | { 11 | public: 12 | IPCSignalHandler(IPCModule* module); 13 | ~IPCSignalHandler(); 14 | 15 | void getListenPort(const ListenerParamMessage& msg); 16 | void onCreatedListener(const CreatedListenerMessage& msg); 17 | void onErrorListener(const ListenErrorMessage& msg); 18 | void onAddConnector(const ConnectorMessage& msg); 19 | void onErrorConnect(const ConnectErrorMessage& msg); 20 | void onModuleName(const ModuleNameMessage& msg); 21 | void onAddIPCObject(const AddIPCObjectMessage& msg); 22 | void onRemoveIPCObject(const RemoveIPCObjectMessage& msg); 23 | void onDisconnected(const DisconnectedMessage& msg); 24 | void onIPCObjectList(const IPCObjectListMessage& msg); 25 | void onUpdateIPCObject(const UpdateIPCObjectMessage& msg); 26 | void onIPCMessage(const IPCProtoMessage& msg); 27 | void onIPCMessage(const IPCMessageSignal& msg); 28 | void onConnected(const ConnectedMessage& msg); 29 | void onInternalConnectionStatusMessage(const InternalConnectionStatusMessage& msg); 30 | private: 31 | IPCModule* m_module; 32 | }; 33 | 34 | #endif/*IPC_SIGNAL_HANDLER_H*/ 35 | -------------------------------------------------------------------------------- /src/cpp/lib/ipc_lib/thread/ipc_checker_thread.cpp: -------------------------------------------------------------------------------- 1 | #include "ipc_checker_thread.h" 2 | #include "connector/ipc_connector.h" 3 | 4 | IPCCheckerThread::IPCCheckerThread(IPCConnector* connector) 5 | : m_connector(connector), m_count(50) 6 | { 7 | ManagersContainer::GetInstance().AddManager(static_cast(this)); 8 | } 9 | 10 | IPCCheckerThread::~IPCCheckerThread() 11 | { 12 | } 13 | 14 | void IPCCheckerThread::ManagerFunc() 15 | { 16 | if(--m_count > 0 || IsStop()) 17 | { 18 | return; 19 | } 20 | 21 | if(!m_count) 22 | { 23 | m_connector->Stop(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/cpp/lib/ipc_lib/thread/ipc_checker_thread.h: -------------------------------------------------------------------------------- 1 | #ifndef IPC_CHECKER_THREAD_H 2 | #define IPC_CHECKER_THREAD_H 3 | 4 | #include "thread_lib/common/managers_container.h" 5 | 6 | class IPCConnector; 7 | 8 | class IPCCheckerThread : public DynamicManager 9 | { 10 | public: 11 | IPCCheckerThread(IPCConnector* connector); 12 | virtual ~IPCCheckerThread(); 13 | protected: 14 | void ManagerFunc(); 15 | void ManagerStart(){}; 16 | void ManagerStop(){}; 17 | private: 18 | IPCConnector* m_connector; 19 | int m_count; 20 | }; 21 | 22 | #endif/*IPC_CHECKER_THREAD_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/ipc_lib/thread/ping_thread.cpp: -------------------------------------------------------------------------------- 1 | #include "ping_thread.h" 2 | #include "connector/ipc_connector.h" 3 | 4 | PingThread::PingThread(IPCConnector* connector) 5 | : m_connector(connector) 6 | { 7 | ManagersContainer::GetInstance().AddManager(static_cast(this)); 8 | } 9 | 10 | PingThread::~PingThread() 11 | { 12 | } 13 | 14 | void PingThread::ManagerFunc() 15 | { 16 | if(!IsStop()) 17 | { 18 | PingMessage msg(&m_connector->m_handler); 19 | m_connector->toMessage(msg); 20 | } 21 | } -------------------------------------------------------------------------------- /src/cpp/lib/ipc_lib/thread/ping_thread.h: -------------------------------------------------------------------------------- 1 | #ifndef PING_THREAD_H 2 | #define PING_THREAD_H 3 | 4 | #include "thread_lib/common/managers_container.h" 5 | 6 | class IPCConnector; 7 | 8 | class PingThread: public DynamicManager 9 | { 10 | public: 11 | PingThread(IPCConnector* connector); 12 | virtual ~PingThread(); 13 | 14 | protected: 15 | virtual void ManagerStart(){} 16 | virtual void ManagerStop(){} 17 | virtual void ManagerFunc(); 18 | 19 | private: 20 | IPCConnector* m_connector; 21 | }; 22 | 23 | #endif/*PING_THREAD_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/thread_lib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | project(thread_lib) 3 | 4 | if(NOT CMAKE_BUILD_TYPE) 5 | set(CMAKE_BUILD_TYPE "Debug") 6 | # set(CMAKE_BUILD_TYPE "Release") 7 | endif(NOT CMAKE_BUILD_TYPE) 8 | 9 | set (SRC_${PROJECT_NAME} 10 | common/timer.cpp 11 | common/managers_container.cpp 12 | thread/thread.cpp 13 | thread/thread_impl.cpp 14 | thread/thread_manager.cpp 15 | ) 16 | 17 | add_definitions(-fPIC) 18 | 19 | include_directories(${CMAKE_CURRENT_SOURCE_DIR}/.. 20 | ${CMAKE_CURRENT_SOURCE_DIR}/../..) 21 | 22 | add_library (${PROJECT_NAME} STATIC ${SRC_${PROJECT_NAME}}) 23 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../../../output/${PROJECT_NAME}) 24 | set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../../output/lib/${CMAKE_BUILD_TYPE}) 25 | -------------------------------------------------------------------------------- /src/cpp/lib/thread_lib/common/managers_container.h: -------------------------------------------------------------------------------- 1 | #ifndef MANAGERS_CONTAINER_H 2 | #define MANAGERS_CONTAINER_H 3 | 4 | #include "include/object_manager.h" 5 | #include "../common/thread_singleton.h" 6 | 7 | class IManager 8 | { 9 | private: 10 | friend class DynamicManager; 11 | friend class StaticManager; 12 | IManager(){} 13 | 14 | public: 15 | virtual ~IManager(){} 16 | 17 | virtual void ManagerFunc() = 0; 18 | virtual void ManagerStart() = 0; 19 | virtual void ManagerStop() = 0; 20 | virtual bool IsStop() = 0; 21 | }; 22 | 23 | class DynamicManager : public IManager 24 | { 25 | public: 26 | DynamicManager(); 27 | virtual ~DynamicManager(); 28 | virtual bool IsStop(); 29 | 30 | void Stop(); 31 | private: 32 | bool m_isStop; 33 | }; 34 | 35 | template class ManagerCreator; 36 | 37 | class ManagersContainer : public ThreadSingleton 38 | { 39 | protected: 40 | friend class Singleton; 41 | ManagersContainer(); 42 | ~ManagersContainer(); 43 | 44 | public: 45 | friend class ManagerCreator; 46 | void AddManager(IManager* manager); 47 | void RemoveManager(IManager* manager); 48 | 49 | protected: 50 | template friend class ReferenceObject; 51 | bool RunManager(const std::vector& managers, const IManager* manager); 52 | bool CheckManager(const std::vector& managers, const IManager* manager); 53 | protected: 54 | virtual void ThreadFunc(); 55 | virtual void OnStop(); 56 | virtual void OnStart(); 57 | virtual void Stop(); 58 | private: 59 | ObjectManager m_managers; 60 | bool m_isExit; 61 | }; 62 | 63 | template 64 | class ManagerCreator : public Singleton >, public DynamicManager 65 | { 66 | static Object* object; 67 | public: 68 | static Object& GetInstance() 69 | { 70 | if(!object) 71 | object = new Object; 72 | Singleton >::GetInstance(); 73 | return *object; 74 | } 75 | 76 | virtual bool IsStop() 77 | { 78 | return false; 79 | } 80 | 81 | protected: 82 | friend class Singleton; 83 | ManagerCreator() 84 | { 85 | if(object) 86 | ManagersContainer::GetInstance().AddManager(static_cast(object)); 87 | } 88 | 89 | virtual ~ManagerCreator() 90 | { 91 | if(object) 92 | object = 0; 93 | } 94 | 95 | protected: 96 | virtual void ManagerFunc(){}; 97 | virtual void ManagerStart(){}; 98 | virtual void ManagerStop(){}; 99 | }; 100 | 101 | template 102 | Object* ManagerCreator::object = 0; 103 | 104 | #endif/*MANAGERS_CONTAINER_H*/ 105 | -------------------------------------------------------------------------------- /src/cpp/lib/thread_lib/common/thread_singleton.h: -------------------------------------------------------------------------------- 1 | #ifndef THREAD_SINGLETON_H 2 | #define THREAD_SINGLETON_H 3 | 4 | #include "include/singleton.h" 5 | #include "../thread/thread.h" 6 | 7 | template 8 | class ThreadSingleton : 9 | public Singleton, public Thread 10 | { 11 | protected: 12 | ThreadSingleton(){} 13 | public: 14 | virtual ~ThreadSingleton(){} 15 | }; 16 | 17 | #endif/*THREAD_SINGLETON_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/thread_lib/common/timer.h: -------------------------------------------------------------------------------- 1 | #ifndef TIMER_H 2 | #define TIMER_H 3 | 4 | #ifdef WIN32 5 | #else 6 | # include 7 | # include 8 | #endif/*WIN32*/ 9 | 10 | #include 11 | #include 12 | #include "managers_container.h" 13 | 14 | struct ITimerProc 15 | { 16 | virtual void OnTimer() = 0; 17 | }; 18 | 19 | class Timer 20 | { 21 | public: 22 | Timer(); 23 | explicit Timer(int ms, ITimerProc* proc); 24 | Timer(const Timer& timer); 25 | ~Timer(); 26 | 27 | void operator = (const Timer& timer); 28 | bool operator == (const Timer& timer); 29 | 30 | ITimerProc* GetTimerProc(); 31 | int GetId(); 32 | int Create(int ms); 33 | void Destroy(); 34 | private: 35 | #ifdef WIN32 36 | #else 37 | timer_t t_id; 38 | #endif/*WIN32*/ 39 | int m_id; 40 | ITimerProc* m_proc; 41 | }; 42 | 43 | class TimerManager : public ManagerCreator 44 | { 45 | protected: 46 | friend class ManagerCreator; 47 | TimerManager(); 48 | ~TimerManager(); 49 | public: 50 | int CreateTimer(int ms, ITimerProc* proc); 51 | void DeleteTimer(int id); 52 | Timer GetTimer(int id); 53 | protected: 54 | friend class Timer; 55 | void AddTimer(int id, Timer timer); 56 | void RemoveTimer(int id); 57 | int GetNextTimerId(); 58 | protected: 59 | virtual void ManagerStop(); 60 | private: 61 | CriticalSection m_cs; 62 | std::map m_timers; 63 | }; 64 | 65 | #endif/*TIMER_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/thread_lib/thread/thread.h: -------------------------------------------------------------------------------- 1 | #ifndef THREAD_H 2 | #define THREAD_H 3 | 4 | #include "include/critical_section.h" 5 | #include "include/semaphore.h" 6 | 7 | #ifdef WIN32 8 | #define WIN32_LEAN_AND_MEAN 9 | #include 10 | #include 11 | #else 12 | #include 13 | #include 14 | #endif/*WIN32*/ 15 | 16 | class Thread 17 | { 18 | public: 19 | enum ThreadState 20 | { 21 | THREAD_STOPPED = 0, 22 | THREAD_RUNNING, 23 | THREAD_STOP_PENDING, 24 | THREAD_START_PENDING 25 | }; 26 | private: 27 | template friend class ThreadSingleton; 28 | friend class ThreadImpl; 29 | Thread(); 30 | virtual ~Thread(); 31 | 32 | protected: 33 | friend class ThreadManager; 34 | void StopThread(); 35 | 36 | public: 37 | bool Start(); 38 | void Join(); 39 | bool IsRunning(); 40 | bool IsStopped() const; 41 | bool IsStop(); 42 | bool WaitStop(int timeout); 43 | bool WaitRun(int timeout); 44 | 45 | static void sleep(unsigned long millisec); 46 | 47 | public: 48 | #ifdef WIN32 49 | static unsigned int __stdcall ThreadProc(void* arg); 50 | #else 51 | static void* ThreadProc(void* arg); 52 | #endif 53 | 54 | virtual void ThreadFunc() = 0; 55 | virtual void OnStop() = 0; 56 | virtual void OnStart() = 0; 57 | virtual void Stop() = 0; 58 | 59 | private: 60 | #ifdef WIN32 61 | typedef HANDLE ThreadHandle; 62 | unsigned int m_threadId; 63 | #else 64 | typedef pthread_t ThreadHandle; 65 | #endif 66 | ThreadHandle m_handle; 67 | Semaphore m_startSemaphore; 68 | Semaphore m_stopSemaphore; 69 | ThreadState m_state; 70 | CriticalSection m_cs; 71 | }; 72 | 73 | #endif/*THREAD_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/thread_lib/thread/thread_impl.cpp: -------------------------------------------------------------------------------- 1 | #include "thread_impl.h" 2 | #include "thread_manager.h" 3 | 4 | ThreadImpl::ThreadImpl() 5 | { 6 | } 7 | 8 | ThreadImpl::~ThreadImpl() 9 | { 10 | } 11 | 12 | void ThreadImpl::OnStop() 13 | { 14 | ThreadManager::GetInstance().RemoveThread(this); 15 | } 16 | 17 | void ThreadImpl::OnStart() 18 | { 19 | ThreadManager::GetInstance().AddThread(this); 20 | } 21 | -------------------------------------------------------------------------------- /src/cpp/lib/thread_lib/thread/thread_impl.h: -------------------------------------------------------------------------------- 1 | #ifndef THREAD_IMPL_H 2 | #define THREAD_IMPL_H 3 | 4 | #include "thread.h" 5 | 6 | class ThreadImpl : public Thread 7 | { 8 | public: 9 | ThreadImpl(); 10 | virtual ~ThreadImpl(); 11 | protected: 12 | virtual void OnStop(); 13 | virtual void OnStart(); 14 | }; 15 | 16 | #endif/*THREAD_IMPL_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/thread_lib/thread/thread_manager.cpp: -------------------------------------------------------------------------------- 1 | #include "thread_manager.h" 2 | 3 | #include "include/ref.h" 4 | 5 | ThreadManager::ThreadManager() 6 | { 7 | } 8 | 9 | ThreadManager::~ThreadManager() 10 | { 11 | } 12 | 13 | void ThreadManager::AddThread(Thread* thread) 14 | { 15 | m_threads.AddObject(thread); 16 | } 17 | 18 | void ThreadManager::RemoveThread(Thread* thread) 19 | { 20 | thread->StopThread(); 21 | } 22 | 23 | bool ThreadManager::CheckThread(const std::vector& threads, const Thread* thread) 24 | { 25 | Thread* thread_ = const_cast(thread); 26 | if(thread_->IsStopped()) 27 | { 28 | const_cast&>(threads).push_back(thread_); 29 | return true; 30 | } 31 | 32 | return false; 33 | } 34 | 35 | bool ThreadManager::StopThread(const std::vector& threads, const Thread* thread) 36 | { 37 | Thread* thread_ = const_cast(thread); 38 | thread_->Join(); 39 | const_cast&>(threads).push_back(thread_); 40 | return true; 41 | } 42 | 43 | void ThreadManager::ManagerFunc() 44 | { 45 | std::vector threads; 46 | m_threads.CheckObjects(Ref(this, &ThreadManager::CheckThread, threads)); 47 | 48 | for(std::vector::iterator it = threads.begin(); it != threads.end(); it++) 49 | { 50 | delete *it; 51 | } 52 | } 53 | 54 | void ThreadManager::ManagerStop() 55 | { 56 | std::vector threads; 57 | m_threads.CheckObjects(Ref(this, &ThreadManager::StopThread, threads)); 58 | 59 | for(std::vector::iterator it = threads.begin(); it != threads.end(); it++) 60 | { 61 | delete *it; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/cpp/lib/thread_lib/thread/thread_manager.h: -------------------------------------------------------------------------------- 1 | #ifndef THREAD_MANAGER_H 2 | #define THREAD_MANAGER_H 3 | 4 | #include "thread.h" 5 | #include "include/object_manager.h" 6 | #include "thread_lib/common/managers_container.h" 7 | 8 | class ThreadManager : public ManagerCreator 9 | { 10 | protected: 11 | friend class ManagerCreator; 12 | ThreadManager(); 13 | virtual ~ThreadManager(); 14 | public: 15 | void AddThread(Thread* thread); 16 | void RemoveThread(Thread* thread); 17 | 18 | protected: 19 | template friend class ReferenceObject; 20 | bool CheckThread(const std::vector& threads, const Thread* thread); 21 | bool StopThread(const std::vector& threads, const Thread* thread); 22 | protected: 23 | virtual void ManagerFunc(); 24 | virtual void ManagerStop(); 25 | private: 26 | ObjectManager m_threads; 27 | }; 28 | 29 | #endif/*THREAD_MANAGER_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/thread_lib/thread_lib.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {72667719-d3ca-4770-a9e8-5deeb76b836a} 6 | 7 | 8 | {ec06c155-9da9-4204-9153-cacc6fce29f8} 9 | 10 | 11 | {3f3011fa-f10e-431c-958f-083d82c3e184} 12 | 13 | 14 | 15 | 16 | thread 17 | 18 | 19 | thread 20 | 21 | 22 | thread 23 | 24 | 25 | common 26 | 27 | 28 | 29 | 30 | thread 31 | 32 | 33 | thread 34 | 35 | 36 | thread 37 | 38 | 39 | common 40 | 41 | 42 | include 43 | 44 | 45 | include 46 | 47 | 48 | include 49 | 50 | 51 | include 52 | 53 | 54 | include 55 | 56 | 57 | -------------------------------------------------------------------------------- /src/cpp/lib/tunnel_lib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | project(tunnel_lib) 3 | 4 | if(NOT CMAKE_BUILD_TYPE) 5 | set(CMAKE_BUILD_TYPE "Debug") 6 | # set(CMAKE_BUILD_TYPE "Release") 7 | endif(NOT CMAKE_BUILD_TYPE) 8 | 9 | set (SRC_${PROJECT_NAME} 10 | ../../common/network.cpp 11 | connector/tunnel_connector.cpp 12 | connector/tunnel_server_connector.cpp 13 | message/tunnel_messages.cpp 14 | module/client_signal_handler.cpp 15 | module/server_signal_handler.cpp 16 | module/tunnel_containers.cpp 17 | module/tunnel_module.cpp 18 | thread/external_connect_thread.cpp 19 | thread/external_listen_thread.cpp 20 | thread/external_recv_thread.cpp 21 | thread/relay_listen_thread.cpp 22 | thread/tunnel_checker_thread.cpp 23 | ) 24 | 25 | add_definitions(-fPIC) 26 | 27 | if(NOT USE_SYSTEM_PROTOBUF) 28 | include_directories(${CMAKE_CURRENT_SOURCE_DIR} 29 | ${CMAKE_CURRENT_SOURCE_DIR}/.. 30 | ${CMAKE_CURRENT_SOURCE_DIR}/../.. 31 | ${CMAKE_CURRENT_SOURCE_DIR}/../../../messages 32 | ${CMAKE_CURRENT_SOURCE_DIR}/../../external/protobuf-2.5.0/src) 33 | else(NOT USE_SYSTEM_PROTOBUF) 34 | include_directories(${CMAKE_CURRENT_SOURCE_DIR} 35 | ${CMAKE_CURRENT_SOURCE_DIR}/.. 36 | ${CMAKE_CURRENT_SOURCE_DIR}/../.. 37 | ${CMAKE_CURRENT_SOURCE_DIR}/../../../messages) 38 | endif(NOT USE_SYSTEM_PROTOBUF) 39 | 40 | add_library (${PROJECT_NAME} STATIC ${SRC_${PROJECT_NAME}}) 41 | set(CMAKE_LIBRARY_OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../output/${PROJECT_NAME}) 42 | set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../../output/lib/${CMAKE_BUILD_TYPE}) 43 | -------------------------------------------------------------------------------- /src/cpp/lib/tunnel_lib/connector/tunnel_connector.h: -------------------------------------------------------------------------------- 1 | #ifndef TUNNEL_CONNECTOR_H 2 | #define TUNNEL_CONNECTOR_H 3 | 4 | #include "ipc_lib/connector/ipc_connector.h" 5 | #include "ipc_lib/thread/ping_thread.h" 6 | 7 | #pragma warning(disable:4244 4267) 8 | #include "../messages/client_server.pb.h" 9 | using namespace client_server; 10 | #pragma warning(default:4244 4267) 11 | 12 | class TunnelConnector; 13 | 14 | class TunnelConnector : public IPCConnector 15 | { 16 | friend class TunnelServerConnector; 17 | public: 18 | enum TypeConnection 19 | { 20 | UNKNOWN, 21 | LOCAL_TCP, 22 | LOCAL_UDP, 23 | EXTERNAL, 24 | RELAY_TCP, 25 | RELAY_UDP 26 | }; 27 | 28 | TunnelConnector(AnySocket* socket, const IPCObjectName& moduleName); 29 | virtual ~TunnelConnector(); 30 | 31 | void SetServerFlag(); 32 | void SetTypeConnection(TypeConnection type); 33 | TypeConnection GetTypeConnection(); 34 | protected: 35 | virtual void OnStart(); 36 | virtual void OnStop(); 37 | virtual bool SetModuleName(const IPCObjectName& moduleName); 38 | virtual void OnConnected(); 39 | 40 | protected: 41 | template friend class ProtoMessage; 42 | void onMessage(const ModuleName& msg); 43 | void onMessage(const ModuleState& msg); 44 | void onMessage(const InternalConnectionData& msg); 45 | void onMessage(const InternalConnectionStatus& msg); 46 | void onMessage(const InitInternalConnection& msg); 47 | private: 48 | bool m_isServer; 49 | PingThread *m_pingThread; 50 | TypeConnection m_type; 51 | }; 52 | 53 | #endif/*TUNNEL_CONNECTOR_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/tunnel_lib/connector/tunnel_server_connector.h: -------------------------------------------------------------------------------- 1 | #ifndef TUNNEL_SERVER_CONNECTOR_H 2 | #define TUNNEL_SERVER_CONNECTOR_H 3 | 4 | #include "tunnel_connector.h" 5 | #include "ipc_lib/connector/ipc_connector.h" 6 | 7 | class TunnelServerConnector : public IPCConnector 8 | { 9 | public: 10 | TunnelServerConnector(TunnelConnector* connectorOne, TunnelConnector* connectorTwo); 11 | virtual ~TunnelServerConnector(); 12 | 13 | virtual void Stop(); 14 | protected: 15 | virtual void OnStart(); 16 | virtual void OnStop(); 17 | virtual bool SetModuleName(const IPCObjectName& moduleName); 18 | virtual void ThreadFunc(); 19 | protected: 20 | friend class Signal; 21 | void onIPCMessageOne(const IPCMessageSignal& msg); 22 | void onIPCMessageTwo(const IPCMessageSignal& msg); 23 | void onModuleState(const ModuleStateMessage& msg); 24 | void onModuleName(const ModuleNameMessage& msg); 25 | void onInternalConnectionDataOne(const InternalConnectionDataMessage& msg); 26 | void onInternalConnectionDataTwo(const InternalConnectionDataMessage& msg); 27 | void onInternalConnectionStatus(const InternalConnectionStatusMessage& msg); 28 | void onInitInternalConnection(const InitInternalConnectionMessage& msg); 29 | private: 30 | TunnelConnector* m_connectorOne; 31 | TunnelConnector* m_connectorTwo; 32 | }; 33 | 34 | #endif/*TUNNEL__SERVER_CONNECTOR_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/tunnel_lib/message/tunnel_messages.cpp: -------------------------------------------------------------------------------- 1 | #include "tunnel_messages.h" 2 | 3 | /************************************************************************/ 4 | /* CreatedServerListenerMessage */ 5 | /************************************************************************/ 6 | CreatedServerListenerMessage::CreatedServerListenerMessage(const std::string& id, const std::string& sessionId, const std::string& ip, int port) 7 | : m_ip(ip), m_port(port), m_id(id), m_sessionId(sessionId) 8 | { 9 | } 10 | 11 | std::string CreatedServerListenerMessage::GetMessageName() 12 | { 13 | return typeid(CreatedServerListenerMessage).name(); 14 | } 15 | 16 | /************************************************************************/ 17 | /* GotExternalAddressMessage */ 18 | /************************************************************************/ 19 | GotExternalAddressMessage::GotExternalAddressMessage(const std::string& id, const std::string& sessionId, const std::string& ip, int port) 20 | : CreatedServerListenerMessage(id, sessionId, ip, port) 21 | { 22 | } 23 | 24 | std::string GotExternalAddressMessage::GetMessageName() 25 | { 26 | return typeid(GotExternalAddressMessage).name(); 27 | } 28 | 29 | /************************************************************************/ 30 | /* TunnelConnectedMessage */ 31 | /************************************************************************/ 32 | TunnelConnectedMessage::TunnelConnectedMessage(const std::string& id, TunnelConnector::TypeConnection type) 33 | : ConnectedMessage(id, false), m_type(type) 34 | { 35 | } 36 | 37 | std::string TunnelConnectedMessage::GetMessageName() 38 | { 39 | return typeid(TunnelConnectedMessage).name(); 40 | } -------------------------------------------------------------------------------- /src/cpp/lib/tunnel_lib/message/tunnel_messages.h: -------------------------------------------------------------------------------- 1 | #ifndef TUNNEL_MESSAGES_H 2 | #define TUNNEL_MESSAGES_H 3 | 4 | #include "include/data_message.h" 5 | #include "connector_lib/message/connector_messages.h" 6 | #include "tunnel_lib/connector/tunnel_connector.h" 7 | 8 | class CreatedServerListenerMessage : public DataMessage 9 | { 10 | public: 11 | CreatedServerListenerMessage(const std::string& id, const std::string& sessionId, const std::string& ip, int port); 12 | static std::string GetMessageName(); 13 | public: 14 | std::string m_ip; 15 | int m_port; 16 | std::string m_id; 17 | std::string m_sessionId; 18 | }; 19 | 20 | class GotExternalAddressMessage : public CreatedServerListenerMessage 21 | { 22 | public: 23 | GotExternalAddressMessage(const std::string& id, const std::string& sessionId, const std::string& ip, int port); 24 | static std::string GetMessageName(); 25 | }; 26 | 27 | class TunnelConnectedMessage : public ConnectedMessage 28 | { 29 | public: 30 | TunnelConnectedMessage(const std::string& id, TunnelConnector::TypeConnection type); 31 | static std::string GetMessageName(); 32 | public: 33 | TunnelConnector::TypeConnection m_type; 34 | }; 35 | 36 | #endif/*TUNNEL_MESSAGES_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/tunnel_lib/module/client_signal_handler.h: -------------------------------------------------------------------------------- 1 | #ifndef CLIENT_SIGNAL_HANDLER_H 2 | #define CLIENT_SIGNAL_HANDLER_H 3 | 4 | #include "../message/tunnel_messages.h" 5 | #include "client_server_lib/connector/client_server_connector.h" 6 | 7 | class TunnelModule; 8 | 9 | class ClientSignalHandler : public SignalReceiver 10 | { 11 | public: 12 | ClientSignalHandler(TunnelModule* module); 13 | ~ClientSignalHandler(); 14 | 15 | void onInitTunnel(const InitTunnelMessage& msg); 16 | void onTryConnectTo(const TryConnectToMessage& msg); 17 | void onInitTunnelStarted(const InitTunnelStartedMessage& msg); 18 | 19 | void onCreatedLocalListener(const CreatedListenerMessage& msg); 20 | void onErrorLocalListener(const ListenErrorMessage& msg); 21 | void onErrorLocalTCPConnect(const ConnectErrorMessage& msg); 22 | void onErrorLocalUDPConnect(const ConnectErrorMessage& msg); 23 | void onAddLocalTCPConnector(const ConnectorMessage& msg); 24 | void onAddLocalUDPConnector(const ConnectorMessage& msg); 25 | void onErrorExternalConnect(const ConnectErrorMessage& msg); 26 | void onAddExternalConnector(const ConnectorMessage& msg); 27 | void onAddRelayTCPConnector(const ConnectorMessage& msg); 28 | void onAddRelayUDPConnector(const ConnectorMessage& msg); 29 | void onErrorRelayTCPConnect(const ConnectErrorMessage& msg); 30 | void onErrorRelayUDPConnect(const ConnectErrorMessage& msg); 31 | 32 | void onModuleName(const ModuleNameMessage& msg); 33 | void onConnected(const TunnelConnectedMessage& msg); 34 | private: 35 | TunnelModule* m_module; 36 | }; 37 | 38 | #endif/*CLIENT_SIGNAL_HANDLER_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/tunnel_lib/module/server_signal_handler.h: -------------------------------------------------------------------------------- 1 | #ifndef SERVER_SIGNAL_HANDLER_H 2 | #define SERVER_SIGNAL_HANDLER_H 3 | 4 | #include "../message/tunnel_messages.h" 5 | #include "client_server_lib/connector/client_server_connector.h" 6 | 7 | class TunnelModule; 8 | 9 | class ServerSignalHandler : public SignalReceiver 10 | { 11 | public: 12 | ServerSignalHandler(TunnelModule* module); 13 | ~ServerSignalHandler(); 14 | 15 | void CheckServers(); 16 | 17 | void onInitTunnel(const InitTunnelSignal& msg); 18 | void onPeerData(const PeerDataSignal& msg); 19 | void onAvailablePearTypes(const AvailablePearTypesSignal& msg); 20 | void onInitTunnelComplete(const InitTunnelCompleteMessage& msg); 21 | 22 | void onCreatedExternalListener(const CreatedServerListenerMessage& msg); 23 | void onGotExternalAddress(const GotExternalAddressMessage& msg); 24 | void onErrorExternalListener(const ListenErrorMessage& msg); 25 | void onCreatedRelayTCPListener(const CreatedServerListenerMessage& msg); 26 | void onErrorRelayListener(const ListenErrorMessage& msg); 27 | void onAddRelayServerConnector(const ConnectorMessage& msg); 28 | void onCreatedRelayUDPListener(const CreatedServerListenerMessage& msg); 29 | void onAddRelayUDPServerConnector(const ConnectorMessage& msg); 30 | private: 31 | TunnelModule* m_module; 32 | }; 33 | 34 | #endif/*SERVER_SIGNAL_HANDLER_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/tunnel_lib/module/tunnel_containers.h: -------------------------------------------------------------------------------- 1 | #ifndef TUNNEL_CONTAINERS_H 2 | #define TUNNEL_CONTAINERS_H 3 | 4 | #include 5 | 6 | #pragma warning(disable:4244 4267) 7 | #include "../messages/client_server.pb.h" 8 | using namespace client_server; 9 | #pragma warning(default:4244 4267) 10 | 11 | class ExternalConnectThread; 12 | class ListenThread; 13 | class Thread; 14 | class UDPSocket; 15 | 16 | class TunnelConnect 17 | { 18 | public: 19 | TunnelConnect(const std::string& sessionId); 20 | ~TunnelConnect(); 21 | 22 | ListenThread* m_localListenThread; 23 | UDPSocket* m_localUdpSocket; 24 | ExternalConnectThread* m_externalConnectThread; 25 | std::string m_sessionId; 26 | time_t m_creationTime; 27 | }; 28 | 29 | class TunnelServer 30 | { 31 | public: 32 | TunnelServer(const std::string& sessionIdOne, const std::string& sessionIdTwo); 33 | ~TunnelServer(); 34 | 35 | Thread* m_thread; 36 | std::string m_sessionIdOne; 37 | std::string m_sessionIdTwo; 38 | time_t m_creationTime; 39 | }; 40 | 41 | class PeerType : public PeerData 42 | { 43 | public: 44 | PeerType(); 45 | PeerType(const PeerData& data); 46 | virtual ~PeerType(); 47 | 48 | bool operator == (const PeerType& peerType) const; 49 | bool operator != (const PeerType& peerType) const; 50 | bool operator < (const PeerType& peerType) const; 51 | }; 52 | 53 | class TunnelStep : public PeerType 54 | { 55 | public: 56 | TunnelStep(); 57 | TunnelStep(const TunnelStep& data); 58 | TunnelStep(const PeerData& data); 59 | virtual ~TunnelStep(); 60 | 61 | void operator = (const TunnelStep& step); 62 | 63 | time_t m_creationTime; 64 | }; 65 | 66 | class AvailableTypes : public AvailablePearTypes 67 | { 68 | public: 69 | AvailableTypes(); 70 | AvailableTypes(const AvailablePearTypes& types); 71 | virtual ~AvailableTypes(); 72 | 73 | bool operator == (const AvailableTypes& types) const; 74 | bool operator != (const AvailableTypes& types) const; 75 | bool operator < (const AvailableTypes& types) const; 76 | }; 77 | 78 | #endif/*TUNNEL_CONTAINERS_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/tunnel_lib/module/tunnel_module.h: -------------------------------------------------------------------------------- 1 | #ifndef TUNNEL_MODULE_H 2 | #define TUNNEL_MODULE_H 3 | 4 | #include "tunnel_containers.h" 5 | #include "client_signal_handler.h" 6 | #include "server_signal_handler.h" 7 | #include "../thread/tunnel_checker_thread.h" 8 | #include "client_server_lib/module/client_server_module.h" 9 | 10 | class TunnelModule : public ClientServerModule 11 | { 12 | public: 13 | static const std::string m_tunnelIPCName; 14 | static const std::string m_tunnelAccessId; 15 | public: 16 | TunnelModule(const IPCObjectName& ipcName, ConnectorFactory* factory, int ipv); 17 | virtual ~TunnelModule(); 18 | public: 19 | void InitNewTunnel(const std::string& extSessionId, TunnelConnector::TypeConnection type); 20 | void DestroyTunnel(const std::string& extSessionId); 21 | void SetTypeTunnel(const std::string& oneSessionId, const std::string& twoSessionId, TunnelConnector::TypeConnection type); 22 | void SetTunnelAvailableType(const std::vector& type); 23 | bool CheckTunnelAvailableType(const std::string& session_id, TunnelType type); 24 | protected: 25 | virtual void OnNewConnector(Connector* connector); 26 | virtual void OnTunnelConnectFailed(const std::string& sessionId); 27 | virtual void OnTunnelConnected(const std::string& sessionId, TunnelConnector::TypeConnection type); 28 | virtual void FillIPCObjectList(std::vector& ipcList); 29 | private: 30 | friend class ClientSignalHandler; 31 | friend class ServerSignalHandler; 32 | void CreateLocalListenThread(const std::string& extSessionId); 33 | void CreateLocalUDPSocket(const std::string& extSessionId); 34 | void CreateLocalConnectThread(const std::string& extSessionId, const std::string& ip, int port, bool isTCP); 35 | void InitExternalConnectThread(const std::string& extSessionId, const std::string& ip, int port); 36 | void CreateExternalConnectThread(const std::string& extSessionId, const std::string& ip, int port); 37 | void CreateRelayConnectThread(const std::string& extSessionId, const std::string& ip, int port, bool isTCP); 38 | 39 | friend class TunnelCheckerThread; 40 | void CheckTunnels(); 41 | private: 42 | std::map m_tunnels; 43 | std::map m_servers; 44 | CriticalSection m_cs; 45 | ObjectManager m_avalablePeerTypes; 46 | ObjectManager m_typePeers; 47 | ObjectManager m_tunnelsStep; 48 | TunnelCheckerThread* m_tunnelChecker; 49 | ClientSignalHandler m_clientSignalHandler; 50 | ServerSignalHandler m_serverSignalHandler; 51 | }; 52 | 53 | #endif/*TUNNEL_MODULE_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/tunnel_lib/thread/external_connect_thread.cpp: -------------------------------------------------------------------------------- 1 | #include "external_connect_thread.h" 2 | #include "common/common_func.h" 3 | #include "connector_lib/socket/udp_socket.h" 4 | #include "connector_lib/socket/udt_socket.h" 5 | #include "connector_lib/socket/socket_factories.h" 6 | #include "connector_lib/connector/connector_factory.h" 7 | #include "connector_lib/message/connector_messages.h" 8 | 9 | ExternalConnectThread::ExternalConnectThread(const ConnectAddress& address) 10 | : ConnectThread(address), m_udpSocket(0) 11 | { 12 | } 13 | 14 | ExternalConnectThread::~ExternalConnectThread() 15 | { 16 | CSLocker lock(&m_csSocket); 17 | if(m_udpSocket) 18 | { 19 | delete m_udpSocket; 20 | m_udpSocket = 0; 21 | } 22 | } 23 | 24 | void ExternalConnectThread::Stop() 25 | { 26 | StopThread(); 27 | m_csSocket.Enter(); 28 | if(m_udpSocket) 29 | { 30 | m_udpSocket->Close(); 31 | } 32 | m_csSocket.Leave(); 33 | 34 | m_changeAddr.Set(); 35 | ConnectThread::Stop(); 36 | } 37 | 38 | void ExternalConnectThread::ChangeConnectAddress(const ConnectAddress& address) 39 | { 40 | delete m_address.m_connectorFactory; 41 | delete m_address.m_socketFactory; 42 | 43 | m_address = address; 44 | m_changeAddr.Set(); 45 | } 46 | 47 | void ExternalConnectThread::ThreadFunc() 48 | { 49 | m_udpSocket = m_address.m_socketFactory->CreateSocket(); 50 | UDPSocket* socket = dynamic_cast(m_udpSocket); 51 | if(!socket || !socket->Bind(m_address.m_localIP, m_address.m_localPort)) 52 | { 53 | SignalError(); 54 | return; 55 | } 56 | 57 | socket->SetSendAddr(m_address.m_ip, m_address.m_port); 58 | std::string data = m_address.m_moduleName; 59 | int size = m_address.m_moduleName.size(); 60 | while(!IsStop()) 61 | { 62 | if(m_changeAddr.Wait(1000) == Semaphore::TIMEOUT) 63 | { 64 | if (!socket->Send((char*)&size, sizeof(size)) || 65 | !socket->Send((char*)data.c_str(), data.size() * sizeof(char))) 66 | { 67 | SignalError(); 68 | return; 69 | } 70 | } 71 | else 72 | { 73 | break; 74 | } 75 | } 76 | 77 | UDTSecureSocketFactory* udtSocketFactory = dynamic_cast(m_address.m_socketFactory); 78 | if(!udtSocketFactory) 79 | { 80 | SignalError(); 81 | return; 82 | } 83 | 84 | 85 | udtSocketFactory->SetUdpSocket(socket->GetSocket()); 86 | m_csSocket.Enter(); 87 | m_udpSocket = 0; 88 | m_csSocket.Leave(); 89 | ConnectThread::ThreadFunc(); 90 | } 91 | 92 | void ExternalConnectThread::SignalError() 93 | { 94 | m_csSocket.Enter(); 95 | if(m_udpSocket) 96 | { 97 | m_udpSocket->Close(); 98 | } 99 | m_csSocket.Leave(); 100 | 101 | ConnectErrorMessage errMsg(m_address.m_moduleName, "", GetError()); 102 | onSignal(errMsg); 103 | } 104 | -------------------------------------------------------------------------------- /src/cpp/lib/tunnel_lib/thread/external_connect_thread.h: -------------------------------------------------------------------------------- 1 | #ifndef EXTERNAL_CONNECT_THREAD_H 2 | #define EXTERNAL_CONNECT_THREAD_H 3 | 4 | #include "connector_lib/thread/connect_thread.h" 5 | 6 | class ExternalConnectThread : public ConnectThread 7 | { 8 | public: 9 | ExternalConnectThread(const ConnectAddress& address); 10 | virtual ~ExternalConnectThread(); 11 | 12 | virtual void Stop(); 13 | 14 | void ChangeConnectAddress(const ConnectAddress& address); 15 | protected: 16 | virtual void OnStart(){} 17 | virtual void OnStop(){} 18 | virtual void ThreadFunc(); 19 | 20 | void SignalError(); 21 | 22 | private: 23 | AnySocket* m_udpSocket; 24 | CriticalSection m_csSocket; 25 | Semaphore m_changeAddr; 26 | }; 27 | 28 | #endif/*EXTERNAL_CONNECT_THREAD_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/tunnel_lib/thread/external_listen_thread.h: -------------------------------------------------------------------------------- 1 | #ifndef EXTERNAL_LISTEN_THREAD_H 2 | #define EXTERNAL_LISTEN_THREAD_H 3 | 4 | #include 5 | #include "external_recv_thread.h" 6 | #include "tunnel_lib/thread/tunnel_server_listen_address.h" 7 | #include "thread_lib/thread/thread_impl.h" 8 | #include "connector_lib/signal/signal_owner.h" 9 | #include "connector_lib/signal/signal_receiver.h" 10 | #include "connector_lib/message/connector_messages.h" 11 | #include "include/semaphore.h" 12 | 13 | class UDPSocket; 14 | 15 | class ExternalListenThread : public SignalOwner, public SignalReceiver, public ThreadImpl 16 | { 17 | public: 18 | ExternalListenThread(const TunnelServerListenAddress& address); 19 | virtual ~ExternalListenThread(); 20 | 21 | virtual void Stop(); 22 | protected: 23 | virtual void OnStart(){} 24 | virtual void OnStop(){} 25 | virtual void ThreadFunc(); 26 | 27 | void SignalError(); 28 | protected: 29 | friend class Signal; 30 | void onListenErrorMessage(const ListenErrorMessage& msg); 31 | void onCreatedListenerMessage(const CreatedListenerMessage& msg); 32 | private: 33 | TunnelServerListenAddress m_address; 34 | ExternalRecvThread* m_recvThreadOne; 35 | ExternalRecvThread* m_recvThreadTwo; 36 | std::map m_addresses; 37 | CriticalSection m_cs; 38 | Semaphore m_semafor; 39 | }; 40 | 41 | #endif/*EXTERNAL_LISTEN_THREAD_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/tunnel_lib/thread/external_recv_thread.cpp: -------------------------------------------------------------------------------- 1 | #include "external_recv_thread.h" 2 | #include "common/common_func.h" 3 | #include "connector_lib/socket/udp_socket.h" 4 | #include "connector_lib/message/connector_messages.h" 5 | 6 | ExternalRecvThread::ExternalRecvThread(UDPSocket* socket, const std::string& id) 7 | : m_socket(socket), m_id(id) 8 | { 9 | } 10 | 11 | ExternalRecvThread::~ExternalRecvThread() 12 | { 13 | if(m_socket) 14 | { 15 | delete m_socket; 16 | m_socket = 0; 17 | } 18 | } 19 | 20 | void ExternalRecvThread::Stop() 21 | { 22 | StopThread(); 23 | 24 | if (m_socket) 25 | { 26 | m_socket->Close(); 27 | } 28 | } 29 | 30 | void ExternalRecvThread::ThreadFunc() 31 | { 32 | int len = 0; 33 | while(!IsStop()) 34 | { 35 | if (!m_socket->Recv((char*)&len, sizeof(int))) 36 | { 37 | ListenErrorMessage msg(m_id, "", GetError()); 38 | onSignal(msg); 39 | break; 40 | } 41 | 42 | if(len < 0 || len > 0xffff) 43 | { 44 | ListenErrorMessage msg(m_id, "", GetError()); 45 | onSignal(msg); 46 | break; 47 | } 48 | 49 | std::string data; 50 | data.resize(len); 51 | std::string ip; 52 | int port; 53 | if(!m_socket->RecvFrom((char*)data.c_str(), len, ip, port)) 54 | { 55 | ListenErrorMessage msg(m_id, "", GetError()); 56 | onSignal(msg); 57 | break; 58 | } 59 | 60 | if(m_id == data) 61 | { 62 | CreatedListenerMessage msg(ip, port, m_id); 63 | onSignal(msg); 64 | } 65 | } 66 | } -------------------------------------------------------------------------------- /src/cpp/lib/tunnel_lib/thread/external_recv_thread.h: -------------------------------------------------------------------------------- 1 | #ifndef EXTERNAL_RECV_THREAD_H 2 | #define EXTERNAL_RECV_THREAD_H 3 | 4 | #include "thread_lib/thread/thread_impl.h" 5 | #include "connector_lib/signal/signal_owner.h" 6 | 7 | class UDPSocket; 8 | 9 | class ExternalRecvThread : public SignalOwner, public ThreadImpl 10 | { 11 | public: 12 | ExternalRecvThread(UDPSocket* socket, const std::string& id); 13 | virtual ~ExternalRecvThread(); 14 | 15 | virtual void Stop(); 16 | protected: 17 | virtual void OnStart(){} 18 | virtual void OnStop(){} 19 | virtual void ThreadFunc(); 20 | private: 21 | UDPSocket* m_socket; 22 | std::string m_id; 23 | }; 24 | 25 | #endif/*EXTERNAL_RECV_THREAD_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/tunnel_lib/thread/relay_listen_thread.h: -------------------------------------------------------------------------------- 1 | #ifndef RELAY_LISTEN_THREAD_H 2 | #define RELAY_LISTEN_THREAD_H 3 | 4 | #include 5 | #include "include/semaphore.h" 6 | #include "thread_lib/thread/thread_impl.h" 7 | #include "connector_lib/signal/signal_owner.h" 8 | #include "connector_lib/signal/signal_receiver.h" 9 | #include "connector_lib/message/connector_messages.h" 10 | #include "tunnel_lib/thread/tunnel_server_listen_address.h" 11 | 12 | class ListenThread; 13 | 14 | class RelayListenThread : public SignalOwner, public SignalReceiver, public ThreadImpl 15 | { 16 | public: 17 | RelayListenThread(const TunnelServerListenAddress& address); 18 | virtual ~RelayListenThread(); 19 | 20 | virtual void Stop(); 21 | protected: 22 | virtual void OnStart(){} 23 | virtual void OnStop(){} 24 | virtual void ThreadFunc(); 25 | 26 | void SignalError(); 27 | protected: 28 | friend class Signal; 29 | void onListenErrorMessage(const ListenErrorMessage& msg); 30 | void onCreatedListenerMessage(const CreatedListenerMessage& msg); 31 | void onAddConnector(const ConnectorMessage& msg); 32 | private: 33 | TunnelServerListenAddress m_address; 34 | ListenThread* m_listenThreadOne; 35 | ListenThread* m_listenThreadTwo; 36 | Connector* m_connectorOne; 37 | Connector* m_connectorTwo; 38 | std::map m_addresses; 39 | CriticalSection m_cs; 40 | Semaphore m_addrSemafor; 41 | Semaphore m_connectSemafor; 42 | }; 43 | 44 | #endif/*RELAY_LISTEN_THREAD_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/tunnel_lib/thread/tunnel_checker_thread.cpp: -------------------------------------------------------------------------------- 1 | #include "tunnel_checker_thread.h" 2 | #include "module/tunnel_module.h" 3 | 4 | TunnelCheckerThread::TunnelCheckerThread(TunnelModule* module) 5 | : m_module(module) 6 | { 7 | ManagersContainer::GetInstance().AddManager(static_cast(this)); 8 | } 9 | 10 | TunnelCheckerThread::~TunnelCheckerThread() 11 | { 12 | } 13 | 14 | void TunnelCheckerThread::ManagerFunc() 15 | { 16 | if(!IsStop()) 17 | { 18 | m_module->CheckTunnels(); 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /src/cpp/lib/tunnel_lib/thread/tunnel_checker_thread.h: -------------------------------------------------------------------------------- 1 | #ifndef TUNNEL_CHECKER_THREAD_H 2 | #define TUNNEL_CHECKER_THREAD_H 3 | 4 | #include "thread_lib/common/managers_container.h" 5 | 6 | class TunnelModule; 7 | 8 | class TunnelCheckerThread : public DynamicManager 9 | { 10 | public: 11 | TunnelCheckerThread(TunnelModule* module); 12 | virtual ~TunnelCheckerThread(); 13 | 14 | protected: 15 | virtual void ManagerStart(){} 16 | virtual void ManagerStop(){} 17 | virtual void ManagerFunc(); 18 | private: 19 | TunnelModule* m_module; 20 | }; 21 | 22 | #endif/*TUNNEL_CHECKER_THREAD_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/tunnel_lib/thread/tunnel_server_listen_address.h: -------------------------------------------------------------------------------- 1 | #ifndef TUNNEL_SERVER_LISTEN_ADDRESS_H 2 | #define TUNNEL_SERVER_LISTEN_ADDRESS_H 3 | 4 | #include "connector_lib/thread/address.h" 5 | 6 | class TunnelServerListenAddress : public Address 7 | { 8 | public: 9 | std::string m_sessionIdOne; 10 | std::string m_sessionIdTwo; 11 | std::string m_id; 12 | SocketFactory* m_socketFactory; 13 | 14 | void operator = (const TunnelServerListenAddress& address) 15 | { 16 | Address::operator = (address); 17 | m_socketFactory = address.m_socketFactory; 18 | m_sessionIdOne = address.m_sessionIdOne; 19 | m_sessionIdTwo = address.m_sessionIdTwo; 20 | m_id = address.m_id; 21 | } 22 | }; 23 | 24 | #endif/*TUNNEL_SERVER_LISTEN_ADDRESS_H*/ -------------------------------------------------------------------------------- /src/cpp/lib/twainet/application/application.h: -------------------------------------------------------------------------------- 1 | #ifndef APPLICATION_H 2 | #define APPLICATION_H 3 | 4 | #include "include/twainet.h" 5 | #include "twainet/module/twainet_module.h" 6 | #include "twainet/message/NotificationMessages.h" 7 | #include "thread_lib/common/managers_container.h" 8 | 9 | class Application : public ThreadSingleton 10 | { 11 | protected: 12 | friend class Singleton; 13 | Application(); 14 | ~Application(); 15 | virtual void ThreadFunc(); 16 | virtual void OnStop(); 17 | virtual void OnStart(); 18 | virtual void Stop(); 19 | public: 20 | 21 | void Init(const Twainet::TwainetCallback& callback); 22 | bool IsInited(); 23 | void Deinit(); 24 | TwainetModule* CreateModule(const char* moduleName, int ipv); 25 | void DeleteModule(TwainetModule* module); 26 | protected: 27 | friend class TwainetModule; 28 | void AddNotifycationMessage(NotificationMessage* message); 29 | private: 30 | CriticalSection m_csModules; 31 | std::vector m_modules; 32 | CriticalSection m_csMessages; 33 | std::vector m_messages; 34 | Semaphore m_semaphore; 35 | Twainet::TwainetCallback m_callbacks; 36 | bool m_bInit; 37 | }; 38 | 39 | #endif/*APPLICATION_H*/ 40 | -------------------------------------------------------------------------------- /src/cpp/lib/twainet/application/dllmain.cpp: -------------------------------------------------------------------------------- 1 | // dllmain.cpp : Defines the entry point for the DLL application. 2 | #ifndef _WIN32_WINNT // Specifies that the minimum required platform is Windows Vista. 3 | #define _WIN32_WINNT 0x0600 // Change this to the appropriate value to target other versions of Windows. 4 | #endif/*_WIN32_WINNT*/ 5 | #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers 6 | 7 | #include 8 | #include 9 | #include "udt.h" 10 | 11 | BOOL APIENTRY DllMain( HMODULE hModule, 12 | DWORD ul_reason_for_call, 13 | LPVOID lpReserved 14 | ) 15 | { 16 | switch (ul_reason_for_call) 17 | { 18 | case DLL_PROCESS_ATTACH: 19 | break; 20 | 21 | case DLL_PROCESS_DETACH: 22 | break; 23 | 24 | case DLL_THREAD_ATTACH: 25 | break; 26 | 27 | case DLL_THREAD_DETACH: 28 | break; 29 | } 30 | return TRUE; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /src/cpp/lib/twainet/resource.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/src/cpp/lib/twainet/resource.h -------------------------------------------------------------------------------- /src/cpp/lib/twainet/twainet.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/src/cpp/lib/twainet/twainet.rc -------------------------------------------------------------------------------- /src/cpp/test/Application.h: -------------------------------------------------------------------------------- 1 | #ifndef APPLICATION_H 2 | #define APPLICATION_H 3 | 4 | #include "include/twainet.h" 5 | #include "include/appinterface.h" 6 | #include "test_module.h" 7 | 8 | class ApplicationTest : public IApplication 9 | { 10 | public: 11 | ApplicationTest(); 12 | ~ApplicationTest(); 13 | 14 | protected: 15 | friend class IApplication; 16 | void OnModuleCreationFailed(Twainet::Module module); 17 | void OnServerCreationFailed(Twainet::Module module); 18 | void OnTunnelCreationFailed(Twainet::Module module, const char* sessionId); 19 | void OnServerConnected(Twainet::Module module, const char* sessionId); 20 | void OnClientConnected(Twainet::Module module, const char* sessionId); 21 | void OnClientDisconnected(Twainet::Module module, const char* sessionId); 22 | void OnClientConnectionFailed(Twainet::Module module); 23 | void OnClientAuthFailed(Twainet::Module module); 24 | void OnServerDisconnected(Twainet::Module module); 25 | void OnModuleConnected(Twainet::Module module, const Twainet::ModuleName& moduleId); 26 | void OnModuleDisconnected(Twainet::Module module, const Twainet::ModuleName& moduleId); 27 | void OnModuleConnectionFailed(Twainet::Module module, const Twainet::ModuleName& moduleId); 28 | void OnTunnelConnected(Twainet::Module module, const char* sessionId, Twainet::TypeConnection type); 29 | void OnTunnelDisconnected(Twainet::Module module, const char* sessionId); 30 | void OnMessageRecv(Twainet::Module module, const Twainet::Message& msg); 31 | void OnInternalConnectionStatusChanged(Twainet::Module module, const char* moduleName, 32 | Twainet::InternalConnectionStatus status, int port); 33 | void OnModuleListChanged(Twainet::Module module); 34 | 35 | protected: 36 | virtual void InitializeApplication(); 37 | virtual void ShutdownApplication(); 38 | 39 | private: 40 | IModule* m_module; 41 | }; 42 | 43 | extern Twainet::TwainetCallback tc; 44 | 45 | #endif/*APPLICATION_H*/ -------------------------------------------------------------------------------- /src/cpp/test/stdafx.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | -------------------------------------------------------------------------------- /src/cpp/test/stdafx.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "targetver.h" 4 | 5 | #define WIN32_LEAN_AND_MEAN 6 | #include 7 | #include 8 | #include -------------------------------------------------------------------------------- /src/cpp/test/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // The following macros define the minimum required platform. The minimum required platform 4 | // is the earliest version of Windows, Internet Explorer etc. that has the necessary features to run 5 | // your application. The macros work by enabling all features available on platform versions up to and 6 | // including the version specified. 7 | 8 | // Modify the following defines if you have to target a platform prior to the ones specified below. 9 | // Refer to MSDN for the latest info on corresponding values for different platforms. 10 | #ifndef _WIN32_WINNT // Specifies that the minimum required platform is Windows Vista. 11 | #define _WIN32_WINNT 0x0600 // Change this to the appropriate value to target other versions of Windows. 12 | #endif 13 | 14 | -------------------------------------------------------------------------------- /src/cpp/test/test.cpp: -------------------------------------------------------------------------------- 1 | // test.cpp : Defines the entry point for the console application. 2 | // 3 | #ifdef WIN32 4 | # include "stdafx.h" 5 | # include 6 | #else 7 | # include 8 | # include 9 | #endif/*WIN32*/ 10 | #include 11 | #include 12 | #include "include/twainet.h" 13 | #include "Application.h" 14 | #include "common/common_func.h" 15 | #include "thread_lib/thread/thread_manager.h" 16 | 17 | #ifdef WIN32 18 | int _tmain(int argc, _TCHAR* argv[]) 19 | #else 20 | int main(int argc, char* argv[]) 21 | #endif/*WIN32*/ 22 | { 23 | // printf("begin\n"); 24 | Twainet::UseLog("/home/avolokitin/twainet.log"); 25 | ApplicationTest::GetInstance().Run(); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /src/cpp/test/test.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | Source Files 26 | 27 | 28 | Source Files 29 | 30 | 31 | Source Files 32 | 33 | 34 | Source Files 35 | 36 | 37 | 38 | 39 | Header Files 40 | 41 | 42 | Header Files 43 | 44 | 45 | Header Files 46 | 47 | 48 | Header Files 49 | 50 | 51 | Header Files 52 | 53 | 54 | Header Files 55 | 56 | 57 | -------------------------------------------------------------------------------- /src/cpp/test/test_module.cpp: -------------------------------------------------------------------------------- 1 | #include "test_module.h" 2 | #include "common/common_func.h" 3 | #include "apps/deamon/module/deamon_module.h" 4 | #include 5 | #include 6 | 7 | TestModule::TestModule() 8 | : Module("twntest2", Twainet::IPV4, false) 9 | { 10 | } 11 | 12 | TestModule::~TestModule() 13 | { 14 | } 15 | 16 | void TestModule::OnModuleConnected(const Twainet::ModuleName& moduleId) 17 | { 18 | if(strcmp(moduleId.m_name, COORDINATOR_NAME) == 0) { 19 | Twainet::UserPassword usr_pwd; 20 | strcpy(usr_pwd.m_user, "test"); 21 | strcpy(usr_pwd.m_pass, "test"); 22 | Twainet::ConnectToServer(m_module, "localhost", 5200, usr_pwd); 23 | // Twainet::CreateServer(m_module, 5200, Twainet::IPV4, true); 24 | } 25 | } 26 | 27 | void TestModule::OnModuleListChanged() 28 | { 29 | Module::OnModuleListChanged(); 30 | 31 | Twainet::ModuleName* names = 0; 32 | int sizeNames = 0; 33 | Twainet::GetExistingModules(GetModule(), names, sizeNames); 34 | names = new Twainet::ModuleName[sizeNames]; 35 | sizeNames = Twainet::GetExistingModules(GetModule(), names, sizeNames); 36 | 37 | printf("moduleNames changed:\n"); 38 | for(int i = 0; i < sizeNames; i++) 39 | { 40 | printf("%d:%s.%s.%s\n", i, names[i].m_name, names[i].m_host, names[i].m_connId); 41 | } 42 | printf("\n"); 43 | 44 | delete names; 45 | } 46 | 47 | void TestModule::OnServerConnected(const char* sessionId) 48 | { 49 | Module::OnServerConnected(sessionId); 50 | 51 | Twainet::ModuleName moduleName = {0}; 52 | strcpy(moduleName.m_name, Twainet::ServerModuleName); 53 | strcpy(moduleName.m_host, sessionId); 54 | TermNameMessage cnMsg(this); 55 | cnMsg.set_name("test"); 56 | toMessage(cnMsg, moduleName); 57 | } 58 | 59 | -------------------------------------------------------------------------------- /src/cpp/test/test_module.h: -------------------------------------------------------------------------------- 1 | #ifndef TEST_MODULE_H 2 | #define TEST_MODULE_H 3 | 4 | #include "include/module.h" 5 | 6 | class TestModule; 7 | 8 | #include "apps/remote_terminal/messages/terminal.pb.h" 9 | using namespace terminal; 10 | 11 | typedef UserMessage TermNameMessage; 12 | 13 | class TestModule : public Module 14 | { 15 | public: 16 | TestModule(); 17 | virtual ~TestModule(); 18 | 19 | public: 20 | virtual void OnModuleConnected(const Twainet::ModuleName& moduleId); 21 | virtual void OnModuleListChanged(); 22 | virtual void OnServerConnected(const char* sessionId); 23 | private: 24 | template friend class UserMessage; 25 | void onMessage(const term_name& test, Twainet::ModuleName path){} 26 | }; 27 | 28 | #endif/*TEST_MODULE_H*/ 29 | -------------------------------------------------------------------------------- /src/cpp/twainet.plugins: -------------------------------------------------------------------------------- 1 | # Plugin pathes 2 | -------------------------------------------------------------------------------- /src/cpp/twainet.trusted: -------------------------------------------------------------------------------- 1 | # Modules name that will get local server credentials 2 | twnproxy -------------------------------------------------------------------------------- /src/cpp/utils/base64.h: -------------------------------------------------------------------------------- 1 | #ifndef BASE64_H 2 | #define BASE64_H 3 | 4 | #ifndef WIN32 5 | # include 6 | # include 7 | #endif/*WIN32*/ 8 | 9 | #include 10 | 11 | std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len); 12 | std::string base64_decode(std::string const& encoded_string, unsigned char* out = NULL, int* len = NULL); 13 | int base64_ntop(unsigned char const *src, size_t srclength, char *target, size_t targsize); 14 | int base64_pton(char const *src, unsigned char *target, size_t targsize); 15 | 16 | #endif/*BASE64_H*/ -------------------------------------------------------------------------------- /src/cpp/utils/logger.cpp: -------------------------------------------------------------------------------- 1 | #include "logger.h" 2 | #include 3 | #include 4 | #include 5 | #include "utils/utils.h" 6 | 7 | Logger::Logger() 8 | : m_logFile(0) 9 | { 10 | } 11 | 12 | Logger::~Logger() 13 | { 14 | if(m_logFile) 15 | delete m_logFile; 16 | } 17 | 18 | void Logger::Log(TypeLog type, const char* prototype, ...) 19 | { 20 | CSLocker locker(&m_cs); 21 | char printdata[1024] = {0}; 22 | va_list argptr; 23 | va_start(argptr, prototype); 24 | vsnprintf(printdata, 1024, prototype, argptr); 25 | va_end(argptr); 26 | std::string strtype = GetStringType(type); 27 | time_t t; 28 | time(&t); 29 | std::string time_ = CommonUtils::FormatTime(t); 30 | 31 | // printf("%s %s %s", time_.c_str(), strtype.c_str(), printdata); 32 | 33 | if(!m_logFile) 34 | { 35 | return; 36 | } 37 | m_logFile->Write(time_.c_str(), time_.size()); 38 | m_logFile->Write(strtype.c_str(), strtype.size()); 39 | m_logFile->Write(printdata, strlen(printdata)); 40 | } 41 | 42 | void Logger::SetLogFile(const std::string& logFileName) 43 | { 44 | CSLocker locker(&m_cs); 45 | if(m_logFile) 46 | delete m_logFile; 47 | 48 | m_logFile = new File(logFileName); 49 | m_logFile->Delete(); 50 | } 51 | 52 | std::string Logger::GetStringType(TypeLog type) 53 | { 54 | switch(type) 55 | { 56 | case LOG_WARNING: 57 | return " LOG_WARNING:\t"; 58 | case LOG_ERROR: 59 | return " LOG_ERROR:\t\t"; 60 | case LOG_INFO: 61 | return " LOG_INFO:\t\t"; 62 | } 63 | 64 | return ""; 65 | } 66 | -------------------------------------------------------------------------------- /src/cpp/utils/logger.h: -------------------------------------------------------------------------------- 1 | #ifndef LOGGER_H 2 | #define LOGGER_H 3 | 4 | #include 5 | #include "include/singleton.h" 6 | #include "include/critical_section.h" 7 | #include "common/file.h" 8 | 9 | #define LOG_WARNING(Format, ...) Logger::GetInstance().Log(Logger::LOG_WARNING, Format, ## __VA_ARGS__) 10 | #define LOG_INFO(Format, ...) Logger::GetInstance().Log(Logger::LOG_INFO, Format, ## __VA_ARGS__) 11 | #define LOG_ERROR(Format, ...) Logger::GetInstance().Log(Logger::LOG_ERROR, Format, ## __VA_ARGS__) 12 | 13 | class Logger : public Singleton 14 | { 15 | protected: 16 | friend class Singleton; 17 | Logger(); 18 | public: 19 | enum TypeLog 20 | { 21 | LOG_WARNING, 22 | LOG_INFO, 23 | LOG_ERROR 24 | }; 25 | 26 | ~Logger(); 27 | 28 | void Log(TypeLog type, const char* prototype, ...); 29 | void SetLogFile(const std::string& logFileName); 30 | protected: 31 | std::string GetStringType(TypeLog type); 32 | private: 33 | File* m_logFile; 34 | CriticalSection m_cs; 35 | }; 36 | 37 | #endif/*LOGGER_H*/ 38 | -------------------------------------------------------------------------------- /src/cpp/utils/path_parser.cpp: -------------------------------------------------------------------------------- 1 | #include "path_parser.h" 2 | 3 | PathParser::PathParser(const std::string& filePath) 4 | { 5 | std::string path = filePath; 6 | size_t pos = -1; 7 | do 8 | { 9 | pos = path.find('\\', pos + 1); 10 | if(pos != -1) 11 | { 12 | path[pos] ='/'; 13 | } 14 | } while (pos != -1); 15 | 16 | pos = path.find_last_of('/'); 17 | if(pos == -1) 18 | { 19 | pos = 0; 20 | } 21 | 22 | m_folder.insert(m_folder.begin(), path.begin(), path.begin() + pos); 23 | if(m_folder.empty()) 24 | { 25 | m_file.insert(m_file.begin(), path.begin() + pos, path.end()); 26 | } 27 | else 28 | { 29 | m_file.insert(m_file.begin(), path.begin() + pos + 1, path.end()); 30 | } 31 | pos = m_file.find_last_of('.'); 32 | if(pos != -1) 33 | { 34 | m_nameFile.insert(m_nameFile.begin(), m_file.begin(), m_file.begin() + pos); 35 | m_extension.insert(m_extension.begin(), m_file.begin() + pos + 1, m_file.end()); 36 | } 37 | else 38 | { 39 | m_nameFile = m_file; 40 | } 41 | } 42 | 43 | std::string PathParser::GetFolder() 44 | { 45 | return m_folder; 46 | } 47 | 48 | std::string PathParser::GetFile() 49 | { 50 | return m_file; 51 | } 52 | 53 | std::string PathParser::GetFileName() 54 | { 55 | return m_nameFile; 56 | } 57 | 58 | std::string PathParser::GetExtension() 59 | { 60 | return m_extension; 61 | } 62 | -------------------------------------------------------------------------------- /src/cpp/utils/path_parser.h: -------------------------------------------------------------------------------- 1 | #ifndef PATH_PARSER_H 2 | #define PATH_PARSER_H 3 | 4 | #include 5 | 6 | class PathParser 7 | { 8 | public: 9 | PathParser(const std::string& filePath); 10 | 11 | std::string GetFolder(); 12 | std::string GetFile(); 13 | std::string GetFileName(); 14 | std::string GetExtension(); 15 | private: 16 | std::string m_folder; 17 | std::string m_file; 18 | std::string m_nameFile; 19 | std::string m_extension; 20 | }; 21 | 22 | 23 | #endif/*PATH_PARSER_H*/ -------------------------------------------------------------------------------- /src/cpp/utils/utils.cpp: -------------------------------------------------------------------------------- 1 | #include "utils.h" 2 | #include 3 | #include 4 | 5 | std::vector CommonUtils::DelimitString(const std::string& src, const std::string& delimit) 6 | { 7 | std::vector dest; 8 | size_t begin = 0, pos = 0; 9 | while (pos != -1) 10 | { 11 | pos = src.find(delimit.c_str(), begin); 12 | if(pos != -1) 13 | { 14 | if(begin != pos) { 15 | dest.push_back(std::string(src.begin() + begin, src.begin() + pos)); 16 | } 17 | begin = pos + delimit.size(); 18 | } 19 | } 20 | 21 | if(begin != src.size() - 1) { 22 | dest.push_back(std::string(src.begin() + begin, src.end())); 23 | } 24 | return dest; 25 | } 26 | 27 | std::vector CommonUtils::DelimitQString(const std::string& src, const std::string& delimit) 28 | { 29 | std::vector dest; 30 | size_t begin = 0, pos = 0; 31 | while (pos != -1) 32 | { 33 | if(src[begin] == '\'' || src[begin] == '\"') { 34 | pos = src.find(src[begin], begin + 1); 35 | if(pos != -1 && delimit == std::string(src.begin() + pos + 1, src.begin() + pos + 1 + delimit.size())) { 36 | dest.push_back(std::string(src.begin() + begin + 1, src.begin() + pos)); 37 | begin = pos + 1 + delimit.size(); 38 | } 39 | } 40 | pos = src.find(delimit.c_str(), begin); 41 | if(pos != -1) 42 | { 43 | if(begin != pos) { 44 | dest.push_back(std::string(src.begin() + begin, src.begin() + pos)); 45 | } 46 | begin = pos + delimit.size(); 47 | } 48 | } 49 | 50 | if(src.begin() + begin != src.end()) { 51 | dest.push_back(std::string(src.begin() + begin, src.end())); 52 | } 53 | return dest; 54 | } 55 | 56 | std::string CommonUtils::ConcatString(const std::vector& src, const std::string& delimit) 57 | { 58 | std::string dest; 59 | for(std::vector::const_iterator it = src.begin(); 60 | it != src.end(); it++) 61 | { 62 | if(it != src.begin()) 63 | { 64 | dest.append(delimit.c_str()); 65 | } 66 | 67 | dest.append(it->c_str()); 68 | } 69 | 70 | return dest; 71 | } 72 | 73 | std::string CommonUtils::FormatTime(time_t time) 74 | { 75 | std::string res = ""; 76 | 77 | int sec = time % 60; 78 | int min = (time / 60) % 60; 79 | int hour = (time / (60 * 60)) % 60; 80 | int day = (time / (24 * 60 * 60)) % 60; 81 | 82 | char data[20] = {0}; 83 | #ifdef WIN32 84 | sprintf_s(data, 20, "%02d %02d:%02d:%02d", day, hour, min, sec); 85 | #else 86 | sprintf(data, "%02d %02d:%02d:%02d", day, hour, min, sec); 87 | #endif/*WIN32*/ 88 | res = data; 89 | 90 | return res; 91 | } 92 | 93 | time_t CommonUtils::GetCurrentTime() 94 | { 95 | struct timeval tm; 96 | gettimeofday(&tm, NULL); 97 | return tm.tv_sec*1000000 + tm.tv_usec; 98 | } 99 | -------------------------------------------------------------------------------- /src/cpp/utils/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | 4 | #include 5 | #include 6 | 7 | class CommonUtils 8 | { 9 | public: 10 | static std::vector DelimitString(const std::string& src, const std::string& delimit); 11 | static std::vector DelimitQString(const std::string& src, const std::string& delimit); 12 | static std::string ConcatString(const std::vector& src, const std::string& delimit); 13 | static std::string FormatTime(time_t time); 14 | static time_t GetCurrentTime(); 15 | }; 16 | 17 | #endif/*UTILS_H*/ 18 | -------------------------------------------------------------------------------- /src/cpp/version.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/src/cpp/version.h -------------------------------------------------------------------------------- /src/js/install.bat: -------------------------------------------------------------------------------- 1 | call npm install https://github.com/dcodeIO/ProtoBuf.js/tarball/master 2 | node install.js 3 | rmdir /s /q node_modules\udt 4 | "%PROGRAMFILES%\7-zip\7z.exe" x node_modules\udt.zip -onode_modules 5 | cd node_modules 6 | del udt.zip 7 | rename udt-master udt 8 | cd udt 9 | call npm install https://github.com/bigeasy/packet/tarball/master -------------------------------------------------------------------------------- /src/js/install.js: -------------------------------------------------------------------------------- 1 | var https = require('https'); 2 | var fs = require('fs'); 3 | 4 | https.get('https://codeload.github.com/bigeasy/udt/zip/master', function(res) { 5 | var stream = fs.createWriteStream("node_modules\\udt.zip"); 6 | stream.once('open', function(fd) { 7 | res.on('data', function(d) { 8 | stream.write(d); 9 | }); 10 | res.on('close', function() { 11 | console.log("close"); 12 | stream.end(); 13 | }); 14 | }); 15 | }).on('error', function(e) { 16 | console.error(e); 17 | }); -------------------------------------------------------------------------------- /src/js/node_modules/clientServerModule.js: -------------------------------------------------------------------------------- 1 | var utils = require('twainetUtils'); 2 | var module = require('ipcModule'); 3 | var conn = require('clientServerConnector'); 4 | var net = require('net'); 5 | var udpServer = require('udpServer'); 6 | var relay = require('relayServerModule'); 7 | 8 | exports.ClientServerModule = function(name) { 9 | module.IPCModule.call(this, name); 10 | } 11 | 12 | exports.client = "ClientName"; 13 | exports.server = "ServerName"; 14 | 15 | utils.extend(exports.ClientServerModule, module.IPCModule); 16 | 17 | exports.ClientServerModule.prototype.StartServer = function(port) { 18 | var self = this; 19 | var server = net.createServer(function(c) { 20 | self.addConnector(conn.ClientServerConnector, c, exports.client, exports.server); 21 | }); 22 | 23 | server.listen(port, function() { //'listening' listener 24 | console.log('server bound'); 25 | }); 26 | } 27 | 28 | exports.ClientServerModule.prototype.Connect = function(host, port) { 29 | var self = this; 30 | var client = net.connect({host: host, port: port}, function() { //'connect' listener 31 | self.addConnector(conn.ClientServerConnector, client, exports.server, exports.client); 32 | }); 33 | } 34 | 35 | exports.ClientServerModule.prototype.initTunnelServer = function(object, pbBuilder) { 36 | var extSessionId = object.get("ext_session_id"), 37 | ownSessionId = object.get("own_session_id"), 38 | initTunnelStarted = pbBuilder.build("client_server.InitTunnelStarted"), 39 | tunnelType = pbBuilder.build("client_server.TunnelType"), 40 | myMessageOne = new initTunnelStarted(ownSessionId, extSessionId), 41 | myMessageTwo = new initTunnelStarted(extSessionId, ownSessionId); 42 | this.notifyMessage(0, myMessageOne); 43 | this.notifyMessage(0, myMessageTwo); 44 | 45 | // object.set("type", tunnelType.TUNNEL_LOCAL); 46 | // this.notifyMessage(0, object); 47 | // object.set("ext_session_id", ownSessionId); 48 | // object.set("own_session_id", extSessionId); 49 | // this.notifyMessage(0, object); 50 | 51 | // var udpServer1 = new udpServer.udpServer(this); 52 | // udpServer1.ownSessionId = ownSessionId; 53 | // udpServer1.extSessionId = extSessionId; 54 | // udpServer1.Start(pbBuilder); 55 | // var udpServer2 = new udpServer.udpServer(this); 56 | // udpServer2.ownSessionId = extSessionId; 57 | // udpServer2.extSessionId = ownSessionId; 58 | // udpServer2.Start(pbBuilder); 59 | 60 | var relayServer = new relay.relayServerModule(this); 61 | relayServer.Start(ownSessionId, extSessionId); 62 | relayServer.Start(extSessionId, ownSessionId); 63 | } -------------------------------------------------------------------------------- /src/js/node_modules/twainetModule.js: -------------------------------------------------------------------------------- 1 | exports.Module = function(name) { 2 | this.name = name; 3 | this.connMap = {}; 4 | } 5 | 6 | exports.Module.prototype.addConnector = function(connector, c, id, moduleName) { 7 | var conn = new connector(c, id, moduleName); 8 | conn.module = this; 9 | 10 | var connId = conn.id; 11 | if(this.connMap[connId] === undefined) { 12 | this.connMap[connId] = []; 13 | this.connMap[connId].push(conn); 14 | } else { 15 | this.connMap[connId].push(conn); 16 | } 17 | this.startConnector(conn); 18 | } 19 | 20 | exports.Module.prototype.startConnector = function(conn) { 21 | conn.start(); 22 | } 23 | 24 | exports.Module.prototype.changeConnectorId = function(oldId, newId) { 25 | if(this.connMap[oldId] !== undefined) { 26 | var conn = this.connMap[oldId]; 27 | delete this.connMap[oldId]; 28 | if(this.connMap[newId] == undefined) { 29 | this.connMap[newId] = conn; 30 | } else { 31 | this.connMap[newId] = this.connMap[newId].concat(conn); 32 | } 33 | } 34 | } 35 | 36 | exports.Module.prototype.onDestroy = function(conn) { 37 | var id = conn.id; 38 | if(this.connMap[id] !== undefined) { 39 | var connArr = this.connMap[id]; 40 | for(var i = 0; i < connArr.length; i++) { 41 | if(connArr[i] == conn) { 42 | connArr.splice(i, 1); 43 | break; 44 | } 45 | } 46 | if(this.connMap[id].length == 0) delete this.connMap[id]; 47 | } 48 | } 49 | 50 | exports.Module.prototype.notifyMessage = function(conn, messageObject) { 51 | for(var id in this.connMap) if(this.connMap.hasOwnProperty(id)) { 52 | for(var i = 0; i < this.connMap[id].length; i++) { 53 | if(this.connMap[id][i] == conn) continue; 54 | this.connMap[id][i].onMessage(messageObject); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/js/node_modules/twainetUtils.js: -------------------------------------------------------------------------------- 1 | var os = require('os'); 2 | 3 | exports.extend = function (Child, Parent) { 4 | var f = function() { }; 5 | f.prototype = Parent.prototype; 6 | Child.prototype = new f(); 7 | Child.prototype.constructor = Child; 8 | Child.superclass = Parent.prototype; 9 | }; 10 | 11 | exports.generateId = function() { 12 | function s4() { 13 | return Math.floor((1 + Math.random()) * 0x10000) 14 | .toString(16) 15 | .substring(1); 16 | }; 17 | 18 | return '{' + s4() + s4() + '-' + s4() + '-' + s4() + '-' + 19 | s4() + '-' + s4() + s4() + s4() + '}'; 20 | } 21 | 22 | exports.rand = function(min, max) { 23 | if( max ) { 24 | return Math.floor(Math.random() * (max - min + 1)) + min; 25 | } else { 26 | return Math.floor(Math.random() * (min + 1)); 27 | } 28 | } 29 | 30 | exports.getAddr = function() { 31 | var netInterface = os.networkInterfaces(); 32 | 33 | for(var name in netInterface) { 34 | return netInterface[name][0].address; 35 | } 36 | } 37 | 38 | Object.prototype.size = function () { 39 | var len = this.length ? --this.length : -1; 40 | for (var k in this) 41 | len++; 42 | return len; 43 | } -------------------------------------------------------------------------------- /src/js/node_modules/udpServer.js: -------------------------------------------------------------------------------- 1 | var dgram = require('dgram'); 2 | var utils = require('twainetUtils'); 3 | 4 | exports.udpServer = function(module) { 5 | this.module = module; 6 | } 7 | 8 | exports.udpServer.prototype.Start = function(pbBuilder) { 9 | var self = this, 10 | server = dgram.createSocket('udp4'); 11 | 12 | var sendInitTunnel = function(address) { 13 | var initTunnel = self.pbBuilder.build("client_server.InitTunnel"), 14 | tunnelConnectAddress = self.pbBuilder.build("client_server.TunnelConnectAddress"), 15 | tunnelType = self.pbBuilder.build("client_server.TunnelType"); 16 | 17 | var myMessage = new initTunnel(self.extSessionId, self.ownSessionId, tunnelType.TUNNEL_EXTERNAL, new tunnelConnectAddress(address.address, address.port)); 18 | self.module.notifyMessage(0, myMessage); 19 | } 20 | 21 | var sendTryConnectTo = function(ownSessionId, extSessionId, address) { 22 | var tryConnectTo = self.pbBuilder.build("client_server.TryConnectTo"), 23 | tunnelConnectAddress = self.pbBuilder.build("client_server.TunnelConnectAddress"), 24 | tunnelType = self.pbBuilder.build("client_server.TunnelType"); 25 | 26 | console.log("udp server created, address - " + address.address + ":" + address.port); 27 | 28 | var myMessage = new tryConnectTo(extSessionId, ownSessionId, tunnelType.TUNNEL_EXTERNAL, new tunnelConnectAddress(address.address, address.port)); 29 | self.module.notifyMessage(0, myMessage); 30 | } 31 | 32 | this.pbBuilder = pbBuilder; 33 | 34 | server.on("listening", function () { 35 | var address = server.address(), 36 | addr = utils.getAddr(); 37 | 38 | sendInitTunnel({address: addr, port: address.port}); 39 | }); 40 | 41 | var lenData = 0, 42 | moduleName = "", 43 | stateEnum = {LENGTH_DATA: 0, MODULE_NAME: 1}; 44 | state = stateEnum.LENGTH_DATA; 45 | server.on('message', function (data, rinfo) { 46 | if(state == stateEnum.LENGTH_DATA) { 47 | state = stateEnum.MODULE_NAME; 48 | lenData = data.readInt32LE(0); 49 | } else if(state == stateEnum.MODULE_NAME){ 50 | state == stateEnum.LENGTH_DATA; 51 | moduleName = data.toString(); 52 | if(moduleName == self.ownSessionId) { 53 | sendTryConnectTo(self.extSessionId, moduleName, rinfo); 54 | server.close(); 55 | } 56 | } 57 | }); 58 | 59 | server.on("error", function (err) { 60 | console.log("server error:\n" + err.stack); 61 | server.close(); 62 | }); 63 | 64 | server.bind(0); 65 | } 66 | -------------------------------------------------------------------------------- /src/js/server.bat: -------------------------------------------------------------------------------- 1 | start /b node server.js -------------------------------------------------------------------------------- /src/js/server.js: -------------------------------------------------------------------------------- 1 | var servermodule = require('clientServerModule'); 2 | var ipcmodule = require('ipcModule'); 3 | 4 | var app = new ipcmodule.IPCModule("Test"); 5 | app.Start(); 6 | 7 | process.on('uncaughtException', function (err) { 8 | console.error(err.stack); 9 | }); 10 | -------------------------------------------------------------------------------- /src/messages/client_server.proto: -------------------------------------------------------------------------------- 1 | package client_server; 2 | 3 | enum ResultCode { 4 | LOGIN_FAILURE = 1; 5 | LOGIN_SUCCESS = 2; 6 | } 7 | 8 | enum TunnelType { 9 | TUNNEL_ALL = 0; 10 | TUNNEL_LOCAL_TCP = 1; 11 | TUNNEL_LOCAL_UDP = 2; 12 | TUNNEL_EXTERNAL = 3; 13 | TUNNEL_RELAY_TCP = 4; 14 | TUNNEL_RELAY_UDP = 5; 15 | } 16 | 17 | message Login { 18 | required string name = 1; 19 | required string password = 2; 20 | optional ResultCode login_result = 3; //for internal usage 21 | } 22 | 23 | message LoginResult { 24 | required ResultCode login_result = 1; 25 | required string own_session_id = 2; 26 | } 27 | 28 | message TunnelConnectAddress { 29 | required string ip = 1; 30 | required int32 port = 2; 31 | } 32 | 33 | message InitTunnel { 34 | required string ext_session_id = 1; 35 | required string own_session_id = 2; 36 | optional TunnelType type = 3; 37 | optional TunnelConnectAddress address = 4; 38 | } 39 | 40 | message TryConnectTo { 41 | required string ext_session_id = 1; 42 | required string own_session_id = 2; 43 | required TunnelType type = 3; 44 | repeated TunnelConnectAddress adresses = 4; 45 | } 46 | 47 | message InitTunnelStarted { 48 | required string ext_session_id = 1; 49 | required string own_session_id = 2; 50 | } 51 | 52 | message InitTunnelComplete { 53 | required string ext_session_id = 1; 54 | required string own_session_id = 2; 55 | } 56 | 57 | message PeerData { 58 | required string one_session_id = 1; 59 | required string two_session_id = 2; 60 | optional TunnelType type = 3; 61 | } 62 | 63 | message AvailablePearTypes { 64 | required string session_id = 1; 65 | repeated TunnelType types = 2; 66 | } 67 | 68 | option optimize_for = LITE_RUNTIME; 69 | -------------------------------------------------------------------------------- /src/messages/deamon.proto: -------------------------------------------------------------------------------- 1 | package deamon; 2 | 3 | message LocalServerAttributes 4 | { 5 | required int32 port = 1; 6 | required string username = 2; 7 | required string password = 3; 8 | } 9 | 10 | message ClientName 11 | { 12 | required string ipc_name = 1; 13 | required string host_name = 2; 14 | } 15 | 16 | message ClientNameList 17 | { 18 | repeated ClientName name_list = 1; 19 | } 20 | 21 | message Plugin 22 | { 23 | required string description = 1; 24 | repeated string name_modules = 2; 25 | required string path = 3; 26 | } 27 | 28 | message InstallPlugin 29 | { 30 | required string plugin_url = 1; 31 | } 32 | 33 | message SetConfig 34 | { 35 | required int32 local_port = 1; 36 | repeated string trusted_modules = 2; 37 | repeated Plugin plugins = 3; 38 | } 39 | 40 | message GetConfig 41 | { 42 | } 43 | 44 | message Test 45 | { 46 | } 47 | -------------------------------------------------------------------------------- /src/messages/interconn.proto: -------------------------------------------------------------------------------- 1 | package interconn; 2 | 3 | import "ipc.proto"; 4 | 5 | enum ConnectionStatus 6 | { 7 | CONN_OPEN = 1; 8 | CONN_CLOSE = 2; 9 | CONN_FAILED = 3; 10 | CONN_EXIST = 4; 11 | CONN_INIT = 5; 12 | } 13 | 14 | message InitInternalConnection 15 | { 16 | required ipc.IPCName target = 1; 17 | required string ip = 2; 18 | required int32 port = 3; 19 | } 20 | 21 | message InternalConnectionStatus 22 | { 23 | required ipc.IPCName target = 1; 24 | required ConnectionStatus status = 2; 25 | optional int32 port = 3; 26 | } 27 | 28 | message InternalConnectionData 29 | { 30 | required string id = 1; 31 | optional bytes data = 2; 32 | } 33 | 34 | option optimize_for = LITE_RUNTIME; -------------------------------------------------------------------------------- /src/messages/ipc.proto: -------------------------------------------------------------------------------- 1 | package ipc; 2 | 3 | message IPCName 4 | { 5 | required string module_name = 1; 6 | optional string host_name = 2; 7 | optional string conn_id = 3; 8 | } 9 | 10 | message AddIPCObject 11 | { 12 | required IPCName ipc_name = 1; 13 | required string ip = 2; 14 | required int32 port = 3; 15 | required string access_id = 4; 16 | } 17 | 18 | message UpdateIPCObject 19 | { 20 | required IPCName ipc_old_name = 1; 21 | required IPCName ipc_new_name = 2; 22 | } 23 | 24 | message ChangeIPCName 25 | { 26 | required IPCName ipc_name = 1; 27 | } 28 | 29 | message IPCObjectList 30 | { 31 | repeated AddIPCObject ipc_object = 1; 32 | optional string access_id = 2; 33 | } 34 | 35 | message ModuleName 36 | { 37 | required IPCName ipc_name = 1; 38 | required string ip = 2; //for ipc communication in local computer only 39 | required int32 port = 3; //for ipc communication in local computer only 40 | required string access_id = 4; //for ipc communication in local computer only 41 | optional bool is_exist = 5; //for internal usage 42 | optional string conn_id = 6; //for internal usage 43 | } 44 | 45 | message ModuleState 46 | { 47 | required bool exist = 1; 48 | required string rndVal = 2; 49 | optional string id = 3; 50 | } 51 | 52 | message RemoveIPCObject 53 | { 54 | required string ipc_name = 1; 55 | } 56 | 57 | message IPCMessage 58 | { 59 | required string message_name = 1; 60 | repeated IPCName ipc_path = 2; 61 | repeated IPCName ipc_sender = 3; 62 | optional bytes message = 4; 63 | } 64 | 65 | message Ping 66 | { 67 | } 68 | 69 | option optimize_for = LITE_RUNTIME; 70 | -------------------------------------------------------------------------------- /src/messages/protoc.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/volok-aleksej/twainet/eabdb17840d02516d8390d8782c0d95713b1e507/src/messages/protoc.exe --------------------------------------------------------------------------------