First commit
This commit is contained in:
		
						commit
						eea1edcb89
					
				
							
								
								
									
										365
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Executable file
									
								
							
							
						
						
									
										365
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Executable file
									
								
							| @ -0,0 +1,365 @@ | ||||
| # Xcode | ||||
| # | ||||
| # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore | ||||
| 
 | ||||
| .DS_Store | ||||
| 
 | ||||
| ## Build generated | ||||
| build/ | ||||
| DerivedData/ | ||||
| 
 | ||||
| ## Various settings | ||||
| *.pbxuser | ||||
| !default.pbxuser | ||||
| *.mode1v3 | ||||
| !default.mode1v3 | ||||
| *.mode2v3 | ||||
| !default.mode2v3 | ||||
| *.perspectivev3 | ||||
| !default.perspectivev3 | ||||
| xcuserdata/ | ||||
| xcshareddata/ | ||||
| xcworkspace/ | ||||
| ## Other | ||||
| *.moved-aside | ||||
| *.xcuserstate | ||||
| 
 | ||||
| ## Obj-C/Swift specific | ||||
| *.hmap | ||||
| *.ipa | ||||
| *.dSYM.zip | ||||
| *.dSYM | ||||
| 
 | ||||
| # CocoaPods | ||||
| # | ||||
| # We recommend against adding the Pods directory to your .gitignore. However | ||||
| # you should judge for yourself, the pros and cons are mentioned at: | ||||
| # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control | ||||
| # | ||||
| # Pods/ | ||||
| 
 | ||||
| # Carthage | ||||
| # | ||||
| # Add this line if you want to avoid checking in source code from Carthage dependencies. | ||||
| # Carthage/Checkouts | ||||
| 
 | ||||
| Carthage/Build | ||||
| 
 | ||||
| # fastlane | ||||
| # | ||||
| # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the  | ||||
| # screenshots whenever they are needed. | ||||
| # For more information about the recommended setup visit: | ||||
| # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md | ||||
| 
 | ||||
| fastlane/report.xml | ||||
| fastlane/screenshots | ||||
| 
 | ||||
| # Code Injection | ||||
| # | ||||
| # After new code Injection tools there's a generated folder /iOSInjectionProject | ||||
| # https://github.com/johnno1962/injectionforxcode | ||||
| 
 | ||||
| iOSInjectionProject/ | ||||
| /proj.ios_mac/.idea | ||||
| 
 | ||||
| ########################### Visual Studio ################ | ||||
| 
 | ||||
| 
 | ||||
| ## Ignore Visual Studio temporary files, build results, and | ||||
| ## files generated by popular Visual Studio add-ons. | ||||
| ## | ||||
| ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore | ||||
| 
 | ||||
| # User-specific files | ||||
| *.suo | ||||
| *.user | ||||
| *.userosscache | ||||
| *.sln.docstates | ||||
| 
 | ||||
| # User-specific files (MonoDevelop/Xamarin Studio) | ||||
| *.userprefs | ||||
| 
 | ||||
| # Build results | ||||
| [Dd]ebug/ | ||||
| [Dd]ebugPublic/ | ||||
| [Rr]elease/ | ||||
| [Rr]eleases/ | ||||
| x64/ | ||||
| x86/ | ||||
| bld/ | ||||
| [Bb]in/ | ||||
| [Oo]bj/ | ||||
| [Ll]og/ | ||||
| 
 | ||||
| # Visual Studio 2015 cache/options directory | ||||
| .vs/ | ||||
| # Uncomment if you have tasks that create the project's static files in wwwroot | ||||
| #wwwroot/ | ||||
| 
 | ||||
| # MSTest test Results | ||||
| [Tt]est[Rr]esult*/ | ||||
| [Bb]uild[Ll]og.* | ||||
| 
 | ||||
| # NUNIT | ||||
| *.VisualState.xml | ||||
| TestResult.xml | ||||
| 
 | ||||
| # Build Results of an ATL Project | ||||
| [Dd]ebugPS/ | ||||
| [Rr]eleasePS/ | ||||
| dlldata.c | ||||
| 
 | ||||
| # .NET Core | ||||
| project.lock.json | ||||
| project.fragment.lock.json | ||||
| artifacts/ | ||||
| **/Properties/launchSettings.json | ||||
| 
 | ||||
| *_i.c | ||||
| *_p.c | ||||
| *_i.h | ||||
| *.ilk | ||||
| *.meta | ||||
| *.obj | ||||
| *.pch | ||||
| *.pdb | ||||
| *.pgc | ||||
| *.pgd | ||||
| *.rsp | ||||
| *.sbr | ||||
| *.tlb | ||||
| *.tli | ||||
| *.tlh | ||||
| *.tmp | ||||
| *.tmp_proj | ||||
| *.log | ||||
| *.vspscc | ||||
| *.vssscc | ||||
| .builds | ||||
| *.pidb | ||||
| *.svclog | ||||
| *.scc | ||||
| 
 | ||||
| # Chutzpah Test files | ||||
| _Chutzpah* | ||||
| 
 | ||||
| # Visual C++ cache files | ||||
| ipch/ | ||||
| *.aps | ||||
| *.ncb | ||||
| *.opendb | ||||
| *.opensdf | ||||
| *.sdf | ||||
| *.cachefile | ||||
| *.VC.db | ||||
| *.VC.VC.opendb | ||||
| 
 | ||||
| # Visual Studio profiler | ||||
| *.psess | ||||
| *.vsp | ||||
| *.vspx | ||||
| *.sap | ||||
| 
 | ||||
| # TFS 2012 Local Workspace | ||||
| $tf/ | ||||
| 
 | ||||
| # Guidance Automation Toolkit | ||||
| *.gpState | ||||
| 
 | ||||
| # ReSharper is a .NET coding add-in | ||||
| _ReSharper*/ | ||||
| *.[Rr]e[Ss]harper | ||||
| *.DotSettings.user | ||||
| 
 | ||||
| # JustCode is a .NET coding add-in | ||||
| .JustCode | ||||
| 
 | ||||
| # TeamCity is a build add-in | ||||
| _TeamCity* | ||||
| 
 | ||||
| # DotCover is a Code Coverage Tool | ||||
| *.dotCover | ||||
| 
 | ||||
| # Visual Studio code coverage results | ||||
| *.coverage | ||||
| *.coveragexml | ||||
| 
 | ||||
| # NCrunch | ||||
| _NCrunch_* | ||||
| .*crunch*.local.xml | ||||
| nCrunchTemp_* | ||||
| 
 | ||||
| # MightyMoose | ||||
| *.mm.* | ||||
| AutoTest.Net/ | ||||
| 
 | ||||
| # Web workbench (sass) | ||||
| .sass-cache/ | ||||
| 
 | ||||
| # Installshield output folder | ||||
| [Ee]xpress/ | ||||
| 
 | ||||
| # DocProject is a documentation generator add-in | ||||
| DocProject/buildhelp/ | ||||
| DocProject/Help/*.HxT | ||||
| DocProject/Help/*.HxC | ||||
| DocProject/Help/*.hhc | ||||
| DocProject/Help/*.hhk | ||||
| DocProject/Help/*.hhp | ||||
| DocProject/Help/Html2 | ||||
| DocProject/Help/html | ||||
| 
 | ||||
| # Click-Once directory | ||||
| publish/ | ||||
| 
 | ||||
| # Publish Web Output | ||||
| *.[Pp]ublish.xml | ||||
| *.azurePubxml | ||||
| # TODO: Comment the next line if you want to checkin your web deploy settings | ||||
| # but database connection strings (with potential passwords) will be unencrypted | ||||
| *.pubxml | ||||
| *.publishproj | ||||
| 
 | ||||
| # Microsoft Azure Web App publish settings. Comment the next line if you want to | ||||
| # checkin your Azure Web App publish settings, but sensitive information contained | ||||
| # in these scripts will be unencrypted | ||||
| PublishScripts/ | ||||
| 
 | ||||
| # NuGet Packages | ||||
| *.nupkg | ||||
| # The packages folder can be ignored because of Package Restore | ||||
| **/packages/* | ||||
| # except build/, which is used as an MSBuild target. | ||||
| !**/packages/build/ | ||||
| # Uncomment if necessary however generally it will be regenerated when needed | ||||
| #!**/packages/repositories.config | ||||
| # NuGet v3's project.json files produces more ignorable files | ||||
| *.nuget.props | ||||
| *.nuget.targets | ||||
| 
 | ||||
| # Microsoft Azure Build Output | ||||
| csx/ | ||||
| *.build.csdef | ||||
| 
 | ||||
| # Microsoft Azure Emulator | ||||
| ecf/ | ||||
| rcf/ | ||||
| 
 | ||||
| # Windows Store app package directories and files | ||||
| AppPackages/ | ||||
| BundleArtifacts/ | ||||
| Package.StoreAssociation.xml | ||||
| _pkginfo.txt | ||||
| 
 | ||||
| # Visual Studio cache files | ||||
| # files ending in .cache can be ignored | ||||
| *.[Cc]ache | ||||
| # but keep track of directories ending in .cache | ||||
| !*.[Cc]ache/ | ||||
| 
 | ||||
| # Others | ||||
| ClientBin/ | ||||
| ~$* | ||||
| *~ | ||||
| *.dbmdl | ||||
| *.dbproj.schemaview | ||||
| *.jfm | ||||
| *.pfx | ||||
| *.publishsettings | ||||
| orleans.codegen.cs | ||||
| 
 | ||||
| # Since there are multiple workflows, uncomment next line to ignore bower_components | ||||
| # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) | ||||
| #bower_components/ | ||||
| 
 | ||||
| # RIA/Silverlight projects | ||||
| Generated_Code/ | ||||
| 
 | ||||
| # Backup & report files from converting an old project file | ||||
| # to a newer Visual Studio version. Backup files are not needed, | ||||
| # because we have git ;-) | ||||
| _UpgradeReport_Files/ | ||||
| Backup*/ | ||||
| UpgradeLog*.XML | ||||
| UpgradeLog*.htm | ||||
| 
 | ||||
| # SQL Server files | ||||
| *.mdf | ||||
| *.ldf | ||||
| *.ndf | ||||
| 
 | ||||
| # Business Intelligence projects | ||||
| *.rdl.data | ||||
| *.bim.layout | ||||
| *.bim_*.settings | ||||
| 
 | ||||
| # Microsoft Fakes | ||||
| FakesAssemblies/ | ||||
| 
 | ||||
| # GhostDoc plugin setting file | ||||
| *.GhostDoc.xml | ||||
| 
 | ||||
| # Node.js Tools for Visual Studio | ||||
| .ntvs_analysis.dat | ||||
| node_modules/ | ||||
| 
 | ||||
| # Typescript v1 declaration files | ||||
| typings/ | ||||
| 
 | ||||
| # Visual Studio 6 build log | ||||
| *.plg | ||||
| 
 | ||||
| # Visual Studio 6 workspace options file | ||||
| *.opt | ||||
| 
 | ||||
| # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) | ||||
| *.vbw | ||||
| 
 | ||||
| # Visual Studio LightSwitch build output | ||||
| **/*.HTMLClient/GeneratedArtifacts | ||||
| **/*.DesktopClient/GeneratedArtifacts | ||||
| **/*.DesktopClient/ModelManifest.xml | ||||
| **/*.Server/GeneratedArtifacts | ||||
| **/*.Server/ModelManifest.xml | ||||
| _Pvt_Extensions | ||||
| 
 | ||||
| # Paket dependency manager | ||||
| .paket/paket.exe | ||||
| paket-files/ | ||||
| 
 | ||||
| # FAKE - F# Make | ||||
| .fake/ | ||||
| 
 | ||||
| # JetBrains Rider | ||||
| .idea/ | ||||
| *.sln.iml | ||||
| 
 | ||||
| # CodeRush | ||||
| .cr/ | ||||
| 
 | ||||
| # Python Tools for Visual Studio (PTVS) | ||||
| __pycache__/ | ||||
| *.pyc | ||||
| 
 | ||||
| # Cake - Uncomment if you are using it | ||||
| # tools/** | ||||
| # !tools/packages.config | ||||
| 
 | ||||
| # Telerik's JustMock configuration file | ||||
| *.jmconfig | ||||
| 
 | ||||
| # BizTalk build output | ||||
| *.btp.cs | ||||
| *.btm.cs | ||||
| *.odx.cs | ||||
| *.xsd.cs | ||||
| 
 | ||||
| #other | ||||
| log.txt | ||||
| 
 | ||||
| desktop.ini | ||||
| 
 | ||||
| xcuserdata | ||||
| */*.xcodeproj/project.xcworkspace | ||||
| 
 | ||||
							
								
								
									
										
											BIN
										
									
								
								libcrypto-1_1.dll
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								libcrypto-1_1.dll
									
									
									
									
									
										Executable file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								libssl-1_1.dll
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								libssl-1_1.dll
									
									
									
									
									
										Executable file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										505
									
								
								main.cpp
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										505
									
								
								main.cpp
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,505 @@ | ||||
| #include <iostream> | ||||
| #include <boost/asio.hpp> | ||||
| #include <boost/lexical_cast.hpp> | ||||
| #include <thread> | ||||
| #include <array> | ||||
| #include <iomanip> | ||||
| 
 | ||||
| #define SSL_R_SHORT_READ 219 | ||||
| #include "ssl/ssl_locl.h" | ||||
| #include <boost/asio/ssl.hpp> | ||||
| 
 | ||||
| 
 | ||||
| #if defined(close) | ||||
| #undef close | ||||
| #endif | ||||
| 
 | ||||
| enum AddressType | ||||
| { | ||||
| 	AT_IPV4 = 0, | ||||
| 	AT_HOST = 3, | ||||
| 	AT_IPV6 = 4 | ||||
| }; | ||||
| 
 | ||||
| class ConnectResponseRecord | ||||
| { | ||||
| public: | ||||
| 	std::vector<unsigned char> rawData; | ||||
| 
 | ||||
| 	enum ConnectResponseType | ||||
| 	{ | ||||
| 		CRT_SUCCESS = 0, | ||||
| 		CRT_GENERAL_SOCKS_SERVER_FAILURE = 1, | ||||
| 		CRT_CONNECTION_NOT_ALLOWED_BY_RULESET = 2, | ||||
| 		CRT_NETWORK_UNREACHABLE = 3, | ||||
| 		CRT_HOST_UNREACHABLE = 4, | ||||
| 		CRT_CONNECTION_REFUSED = 5, | ||||
| 		CRT_TTL_EXPIRED = 6, | ||||
| 		CRT_COMMAND_NOT_SUPPORTED = 7, | ||||
| 		CRT_ADDRESS_TYPE_NOT_SUPPORTED = 8, | ||||
| 		CRT_TO_FF_UNASSIGNED = 9 | ||||
| 	}; | ||||
| 
 | ||||
| 	ConnectResponseType connectResponseType; | ||||
| 
 | ||||
| 	AddressType addressType; | ||||
| 
 | ||||
| 	std::string address; | ||||
| 
 | ||||
| 	uint16_t port; | ||||
| }; | ||||
| 
 | ||||
| class ConnectRequestRecord | ||||
| { | ||||
| public: | ||||
| 	std::string address; | ||||
| 	uint16_t port; | ||||
| 
 | ||||
| 	std::string getRequestData(); | ||||
| }; | ||||
| 
 | ||||
| std::string ConnectRequestRecord::getRequestData() | ||||
| { | ||||
| 	std::string data = ""; | ||||
| 	data += 0x05; | ||||
| 	data += 0x01; | ||||
| 	data += char(0x00); | ||||
| 	data += 0x03; | ||||
| 	data += static_cast<unsigned char>(address.size()); | ||||
| 	data += address; | ||||
| 	data += port / 256; | ||||
| 	data += port % 256; | ||||
| 	return data; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| //typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket;
 | ||||
| 
 | ||||
| 
 | ||||
| class ProxySession : | ||||
| 	public std::enable_shared_from_this<ProxySession> | ||||
| { | ||||
| public: | ||||
| 	ProxySession(boost::asio::ip::tcp::socket socket, boost::asio::io_service& inIoService) | ||||
| 		: ioService(inIoService) | ||||
| 		, socket(std::move(socket)) | ||||
| 		, outsideConnectSocket(ioService) | ||||
| 	{ | ||||
| 		std::cout << "ProxySession Create" << std::endl; | ||||
| 	} | ||||
| 
 | ||||
| 	~ProxySession() | ||||
| 	{ | ||||
| 		std::cout << "ProxySession Destroy" << std::endl; | ||||
| 	} | ||||
| 
 | ||||
| 	void start() | ||||
| 	{ | ||||
| 		readClientVersion(); | ||||
| 	} | ||||
| 
 | ||||
| 
 | ||||
| private: | ||||
| 
 | ||||
| 
 | ||||
| 	std::array<unsigned char, 3> clientVersion; | ||||
| 	 | ||||
| 	void readClientVersion() | ||||
| 	{ | ||||
| 		auto self(shared_from_this()); | ||||
| 
 | ||||
| 		boost::asio::async_read(socket, | ||||
| 			boost::asio::buffer(clientVersion.data(), clientVersion.size()), | ||||
| 			[this, self](boost::system::error_code ec, std::size_t /*length*/) | ||||
| 		{ | ||||
| 			if (!ec) | ||||
| 			{ | ||||
| 				if (clientVersion[0] == 0x05 && clientVersion[1] == 0x01 && clientVersion[2] == 0x02) | ||||
| 				{ | ||||
| 					sendServerVersion(); | ||||
| 				} | ||||
| 				else | ||||
| 				{ | ||||
| 					socket.close(); | ||||
| 				} | ||||
| 			} | ||||
| 			else | ||||
| 			{ | ||||
| 				socket.close(); | ||||
| 			} | ||||
| 		}); | ||||
| 	} | ||||
| 
 | ||||
| 
 | ||||
| 	void sendServerVersion() | ||||
| 	{ | ||||
| 
 | ||||
| 		auto self(shared_from_this()); | ||||
| 	 | ||||
| 		std::array<char, 2> version = { 0x05, 0x02 }; | ||||
| 
 | ||||
| 		boost::asio::async_write(socket, | ||||
| 			boost::asio::buffer(version.data(), version.size()), | ||||
| 			[this, self](boost::system::error_code ec, std::size_t length) | ||||
| 		{ | ||||
| 			if (!ec) | ||||
| 			{ | ||||
| 				readLoginPassword(); | ||||
| 			} | ||||
| 			else | ||||
| 			{ | ||||
| 				socket.close(); | ||||
| 			} | ||||
| 		}); | ||||
| 	} | ||||
| 
 | ||||
| 
 | ||||
| 	void readLoginPassword() | ||||
| 	{ | ||||
| 		std::string login = "telegram-proxy-user"; | ||||
| 		std::string password = "telegram-telegram-999112"; | ||||
| 		std::string expectedData = ""; | ||||
| 		expectedData += 0x01; | ||||
| 		expectedData += static_cast<unsigned char>(login.size()); | ||||
| 		expectedData += login; | ||||
| 		expectedData += static_cast<unsigned char>(password.size()); | ||||
| 		expectedData += password; | ||||
| 
 | ||||
| 		std::shared_ptr<std::string> clientLoginPasswordPtr = std::make_shared<std::string>(); | ||||
| 
 | ||||
| 
 | ||||
| 		clientLoginPasswordPtr->resize(expectedData.size()); | ||||
| 
 | ||||
| 
 | ||||
| 		auto self(shared_from_this()); | ||||
| 
 | ||||
| 
 | ||||
| 		boost::asio::async_read(socket, | ||||
| 			boost::asio::buffer(&((*clientLoginPasswordPtr)[0]), clientLoginPasswordPtr->size()), | ||||
| 			[this, self, expectedData, clientLoginPasswordPtr](boost::system::error_code ec, std::size_t /*length*/) | ||||
| 		{ | ||||
| 			if (!ec) | ||||
| 			{ | ||||
| 				if (*clientLoginPasswordPtr == expectedData) | ||||
| 				{ | ||||
| 					sendAuthStatus(); | ||||
| 				} | ||||
| 				else | ||||
| 				{ | ||||
| 					socket.close(); | ||||
| 				} | ||||
| 			} | ||||
| 			else | ||||
| 			{ | ||||
| 				socket.close(); | ||||
| 			} | ||||
| 		}); | ||||
| 
 | ||||
| 	} | ||||
| 
 | ||||
| 	std::array<unsigned char, 2> authStatus = { 0x01, 0x00 }; | ||||
| 
 | ||||
| 	void sendAuthStatus() | ||||
| 	{ | ||||
| 		auto self(shared_from_this()); | ||||
| 
 | ||||
| 		 | ||||
| 
 | ||||
| 		boost::asio::async_write(socket, | ||||
| 			boost::asio::buffer(authStatus.data(), authStatus.size()), | ||||
| 			[this, self](boost::system::error_code ec, std::size_t length) | ||||
| 		{ | ||||
| 
 | ||||
| 			if (!ec) | ||||
| 			{ | ||||
| 				readConnectRequest(); | ||||
| 			} | ||||
| 			else | ||||
| 			{ | ||||
| 				socket.close(); | ||||
| 			} | ||||
| 		}); | ||||
| 	} | ||||
| 
 | ||||
| 	void readConnectRequest() | ||||
| 	{ | ||||
| 		std::shared_ptr<std::array<unsigned char, 5>> firstPartPtr = std::make_shared<std::array<unsigned char, 5>>(); | ||||
| 
 | ||||
| 
 | ||||
| 		auto self(shared_from_this()); | ||||
| 
 | ||||
| 
 | ||||
| 		boost::asio::async_read(socket, | ||||
| 			boost::asio::buffer(firstPartPtr->data(), firstPartPtr->size()), | ||||
| 			[this, self, firstPartPtr](boost::system::error_code ec, std::size_t /*length*/) | ||||
| 		{ | ||||
| 			if (!ec) | ||||
| 			{ | ||||
| 				if ((*firstPartPtr)[0] == 0x05 && (*firstPartPtr)[1] == 0x01 && (*firstPartPtr)[2] == 0x00 && (*firstPartPtr)[3] == 0x03) | ||||
| 				{ | ||||
| 					//unsigned int len = (*firstPartPtr)[4];
 | ||||
| 
 | ||||
| 					readConnectRequestPart2(firstPartPtr); | ||||
| 
 | ||||
| 
 | ||||
| 				} | ||||
| 				else | ||||
| 				{ | ||||
| 					socket.close(); | ||||
| 				} | ||||
| 			} | ||||
| 			else | ||||
| 			{ | ||||
| 				socket.close(); | ||||
| 			} | ||||
| 		}); | ||||
| 
 | ||||
| 	} | ||||
| 
 | ||||
| 
 | ||||
| 	void readConnectRequestPart2(std::shared_ptr<std::array<unsigned char, 5>> firstPartPtr) | ||||
| 	{ | ||||
| 		unsigned int len = (*firstPartPtr)[4]; | ||||
| 
 | ||||
| 		std::shared_ptr<std::vector<unsigned char>> secondPartPtr = std::make_shared<std::vector<unsigned char>>(); | ||||
| 
 | ||||
| 
 | ||||
| 		secondPartPtr->resize(len + 2); | ||||
| 
 | ||||
| 		auto self(shared_from_this()); | ||||
| 
 | ||||
| 
 | ||||
| 		boost::asio::async_read(socket, | ||||
| 			boost::asio::buffer(secondPartPtr->data(), secondPartPtr->size()), | ||||
| 			[this, self, firstPartPtr, secondPartPtr, len](boost::system::error_code ec, std::size_t /*length*/) | ||||
| 		{ | ||||
| 			if (!ec) | ||||
| 			{ | ||||
| 
 | ||||
| 				ConnectRequestRecord connectRequestRecord; | ||||
| 
 | ||||
| 				connectRequestRecord.address = std::string(&((*secondPartPtr)[0]), &((*secondPartPtr)[0]) + len); | ||||
| 				connectRequestRecord.port = (*secondPartPtr)[len] * 256 + (*secondPartPtr)[len + 1]; | ||||
| 
 | ||||
| 				boost::asio::ip::tcp::resolver resolver(this->ioService); | ||||
| 				auto endpointIterator = resolver.resolve({ connectRequestRecord.address, boost::lexical_cast<std::string>(connectRequestRecord.port) }); | ||||
| 
 | ||||
| 
 | ||||
| 				doConnectOutput(endpointIterator, connectRequestRecord); | ||||
| 
 | ||||
| 			} | ||||
| 			else | ||||
| 			{ | ||||
| 				socket.close(); | ||||
| 			} | ||||
| 		}); | ||||
| 
 | ||||
| 	} | ||||
| 
 | ||||
| 
 | ||||
| 	void doConnectOutput(boost::asio::ip::tcp::resolver::iterator endpointIterator, ConnectRequestRecord connectRequestRecord) | ||||
| 	{ | ||||
| 		auto self(shared_from_this()); | ||||
| 
 | ||||
| 		boost::asio::async_connect(outsideConnectSocket, endpointIterator, | ||||
| 			[this, self, connectRequestRecord](boost::system::error_code ec, boost::asio::ip::tcp::resolver::iterator) | ||||
| 		{ | ||||
| 			if (!ec) | ||||
| 			{ | ||||
| 				sendConnectResponse(connectRequestRecord); | ||||
| 			} | ||||
| 			else | ||||
| 			{ | ||||
| 				socket.close(); | ||||
| 				outsideConnectSocket.close(); | ||||
| 			} | ||||
| 		}); | ||||
| 	} | ||||
| 
 | ||||
| 	void sendConnectResponse(ConnectRequestRecord connectRequestRecord) | ||||
| 	{ | ||||
| 		auto self(shared_from_this()); | ||||
| 
 | ||||
| 		std::string connectResponse; | ||||
| 
 | ||||
| 		connectResponse += 0x05; | ||||
| 		connectResponse += static_cast<char>(0x00); | ||||
| 		connectResponse += static_cast<char>(0x00); | ||||
| 		connectResponse += 0x03; | ||||
| 
 | ||||
| 		connectResponse += static_cast<unsigned char>(connectRequestRecord.address.size()); | ||||
| 		connectResponse += connectRequestRecord.address; | ||||
| 		connectResponse += static_cast<unsigned char>(connectRequestRecord.port / 256); | ||||
| 		connectResponse += static_cast<unsigned char>(connectRequestRecord.port % 256); | ||||
| 
 | ||||
| 
 | ||||
| 		boost::asio::async_write(socket, | ||||
| 			boost::asio::buffer(connectResponse.data(), connectResponse.size()), | ||||
| 			[this, self](boost::system::error_code ec, std::size_t length) | ||||
| 		{ | ||||
| 
 | ||||
| 			if (!ec) | ||||
| 			{ | ||||
| 				transferDataForward(); | ||||
| 				transferDataBackward(); | ||||
| 			} | ||||
| 			else | ||||
| 			{ | ||||
| 				socket.close(); | ||||
| 				outsideConnectSocket.close(); | ||||
| 			} | ||||
| 		}); | ||||
| 
 | ||||
| 	} | ||||
| 
 | ||||
| 	unsigned char forwardChar; | ||||
| 	unsigned char backwardChar; | ||||
| 
 | ||||
| 
 | ||||
| 	void transferDataForward() | ||||
| 	{ | ||||
| 		auto self(shared_from_this()); | ||||
| 
 | ||||
| 		boost::asio::async_read(socket, | ||||
| 			boost::asio::buffer(&forwardChar, 1), | ||||
| 			[this, self](boost::system::error_code ec, std::size_t /*length*/) | ||||
| 		{ | ||||
| 			if (!ec) | ||||
| 			{ | ||||
| 				boost::asio::async_write(outsideConnectSocket, | ||||
| 					boost::asio::buffer(&forwardChar, 1), | ||||
| 					[this, self](boost::system::error_code ec, std::size_t length) | ||||
| 				{ | ||||
| 
 | ||||
| 					if (!ec) | ||||
| 					{ | ||||
| 						transferDataForward(); | ||||
| 					} | ||||
| 					else | ||||
| 					{ | ||||
| 						outsideConnectSocket.close(); | ||||
| 						socket.close(); | ||||
| 					} | ||||
| 				}); | ||||
| 			} | ||||
| 			else | ||||
| 			{ | ||||
| 				outsideConnectSocket.close(); | ||||
| 				socket.close(); | ||||
| 			} | ||||
| 		}); | ||||
| 	} | ||||
| 
 | ||||
| 	void transferDataBackward() | ||||
| 	{ | ||||
| 		auto self(shared_from_this()); | ||||
| 
 | ||||
| 		boost::asio::async_read(outsideConnectSocket, | ||||
| 			boost::asio::buffer(&backwardChar, 1), | ||||
| 			[this, self](boost::system::error_code ec, std::size_t /*length*/) | ||||
| 		{ | ||||
| 			if (!ec) | ||||
| 			{ | ||||
| 				boost::asio::async_write(socket, | ||||
| 					boost::asio::buffer(&backwardChar, 1), | ||||
| 					[this, self](boost::system::error_code ec, std::size_t length) | ||||
| 				{ | ||||
| 
 | ||||
| 					if (!ec) | ||||
| 					{ | ||||
| 						transferDataBackward(); | ||||
| 					} | ||||
| 					else | ||||
| 					{ | ||||
| 						outsideConnectSocket.close(); | ||||
| 						socket.close(); | ||||
| 					} | ||||
| 				}); | ||||
| 			} | ||||
| 			else | ||||
| 			{ | ||||
| 				outsideConnectSocket.close(); | ||||
| 				socket.close(); | ||||
| 			} | ||||
| 		}); | ||||
| 	} | ||||
| 
 | ||||
| 	boost::asio::io_service& ioService; | ||||
| 
 | ||||
| 	boost::asio::ip::tcp::socket socket; | ||||
| 
 | ||||
| 	boost::asio::ip::tcp::socket outsideConnectSocket; | ||||
| 
 | ||||
| 	 | ||||
| }; | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| class ProxyServer | ||||
| { | ||||
| public: | ||||
| 	ProxyServer(boost::asio::io_service& inIoService, | ||||
| 		const boost::asio::ip::tcp::endpoint& endpoint) | ||||
| 		: ioService(inIoService) | ||||
| 		, acceptor(inIoService, endpoint), | ||||
| 		socket(inIoService) | ||||
| 	{ | ||||
| 		doAccept(); | ||||
| 	} | ||||
| 
 | ||||
| private: | ||||
| 	void doAccept() | ||||
| 	{ | ||||
| 		acceptor.async_accept(socket, | ||||
| 			[this](boost::system::error_code ec) | ||||
| 		{ | ||||
| 			if (!ec) | ||||
| 			{ | ||||
| 				std::make_shared<ProxySession>(std::move(socket), ioService)->start(); | ||||
| 			} | ||||
| 
 | ||||
| 			counter++; | ||||
| 
 | ||||
| 			doAccept(); | ||||
| 		}); | ||||
| 	} | ||||
| 
 | ||||
| 	boost::asio::io_service& ioService; | ||||
| 
 | ||||
| 	boost::asio::ip::tcp::acceptor acceptor; | ||||
| 	boost::asio::ip::tcp::socket socket; | ||||
| 
 | ||||
| 	//std::map<size_t, ProxySession> proxySessionMap;
 | ||||
| 
 | ||||
| 	size_t counter = 0; | ||||
| }; | ||||
| 
 | ||||
| 
 | ||||
| int main() | ||||
| { | ||||
| 
 | ||||
| 	try | ||||
| 	{ | ||||
| 
 | ||||
| 		boost::asio::io_service ioService; | ||||
| 
 | ||||
| 		boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 8043); | ||||
| 
 | ||||
| 		ProxyServer proxyServer(ioService, endpoint); | ||||
| 
 | ||||
| 		ioService.run(); | ||||
| 
 | ||||
| 	} | ||||
| 	catch (std::exception& e) | ||||
| 	{ | ||||
| 		std::cerr << "Exception: " << e.what() << "\n"; | ||||
| 	} | ||||
| 
 | ||||
| 
 | ||||
| 	return 0; | ||||
| 
 | ||||
| } | ||||
							
								
								
									
										28
									
								
								proxyServerTest.sln
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										28
									
								
								proxyServerTest.sln
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,28 @@ | ||||
|  | ||||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||||
| # Visual Studio 14 | ||||
| VisualStudioVersion = 14.0.25420.1 | ||||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||||
| Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "proxyServerTest", "proxyServerTest.vcxproj", "{066072DA-FC40-49C7-A432-FA85F715B0F6}" | ||||
| EndProject | ||||
| Global | ||||
| 	GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||||
| 		Debug|x64 = Debug|x64 | ||||
| 		Debug|x86 = Debug|x86 | ||||
| 		Release|x64 = Release|x64 | ||||
| 		Release|x86 = Release|x86 | ||||
| 	EndGlobalSection | ||||
| 	GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||||
| 		{066072DA-FC40-49C7-A432-FA85F715B0F6}.Debug|x64.ActiveCfg = Debug|x64 | ||||
| 		{066072DA-FC40-49C7-A432-FA85F715B0F6}.Debug|x64.Build.0 = Debug|x64 | ||||
| 		{066072DA-FC40-49C7-A432-FA85F715B0F6}.Debug|x86.ActiveCfg = Debug|Win32 | ||||
| 		{066072DA-FC40-49C7-A432-FA85F715B0F6}.Debug|x86.Build.0 = Debug|Win32 | ||||
| 		{066072DA-FC40-49C7-A432-FA85F715B0F6}.Release|x64.ActiveCfg = Release|x64 | ||||
| 		{066072DA-FC40-49C7-A432-FA85F715B0F6}.Release|x64.Build.0 = Release|x64 | ||||
| 		{066072DA-FC40-49C7-A432-FA85F715B0F6}.Release|x86.ActiveCfg = Release|Win32 | ||||
| 		{066072DA-FC40-49C7-A432-FA85F715B0F6}.Release|x86.Build.0 = Release|Win32 | ||||
| 	EndGlobalSection | ||||
| 	GlobalSection(SolutionProperties) = preSolution | ||||
| 		HideSolutionNode = FALSE | ||||
| 	EndGlobalSection | ||||
| EndGlobal | ||||
							
								
								
									
										136
									
								
								proxyServerTest.vcxproj
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										136
									
								
								proxyServerTest.vcxproj
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,136 @@ | ||||
| <?xml version="1.0" encoding="utf-8"?> | ||||
| <Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||||
|   <ItemGroup Label="ProjectConfigurations"> | ||||
|     <ProjectConfiguration Include="Debug|Win32"> | ||||
|       <Configuration>Debug</Configuration> | ||||
|       <Platform>Win32</Platform> | ||||
|     </ProjectConfiguration> | ||||
|     <ProjectConfiguration Include="Release|Win32"> | ||||
|       <Configuration>Release</Configuration> | ||||
|       <Platform>Win32</Platform> | ||||
|     </ProjectConfiguration> | ||||
|     <ProjectConfiguration Include="Debug|x64"> | ||||
|       <Configuration>Debug</Configuration> | ||||
|       <Platform>x64</Platform> | ||||
|     </ProjectConfiguration> | ||||
|     <ProjectConfiguration Include="Release|x64"> | ||||
|       <Configuration>Release</Configuration> | ||||
|       <Platform>x64</Platform> | ||||
|     </ProjectConfiguration> | ||||
|   </ItemGroup> | ||||
|   <PropertyGroup Label="Globals"> | ||||
|     <ProjectGuid>{066072DA-FC40-49C7-A432-FA85F715B0F6}</ProjectGuid> | ||||
|     <RootNamespace>proxyServerTest</RootNamespace> | ||||
|     <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion> | ||||
|   </PropertyGroup> | ||||
|   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | ||||
|   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> | ||||
|     <ConfigurationType>Application</ConfigurationType> | ||||
|     <UseDebugLibraries>true</UseDebugLibraries> | ||||
|     <PlatformToolset>v140</PlatformToolset> | ||||
|     <CharacterSet>MultiByte</CharacterSet> | ||||
|   </PropertyGroup> | ||||
|   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | ||||
|     <ConfigurationType>Application</ConfigurationType> | ||||
|     <UseDebugLibraries>false</UseDebugLibraries> | ||||
|     <PlatformToolset>v140</PlatformToolset> | ||||
|     <WholeProgramOptimization>true</WholeProgramOptimization> | ||||
|     <CharacterSet>MultiByte</CharacterSet> | ||||
|   </PropertyGroup> | ||||
|   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> | ||||
|     <ConfigurationType>Application</ConfigurationType> | ||||
|     <UseDebugLibraries>true</UseDebugLibraries> | ||||
|     <PlatformToolset>v140</PlatformToolset> | ||||
|     <CharacterSet>MultiByte</CharacterSet> | ||||
|   </PropertyGroup> | ||||
|   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> | ||||
|     <ConfigurationType>Application</ConfigurationType> | ||||
|     <UseDebugLibraries>false</UseDebugLibraries> | ||||
|     <PlatformToolset>v140</PlatformToolset> | ||||
|     <WholeProgramOptimization>true</WholeProgramOptimization> | ||||
|     <CharacterSet>MultiByte</CharacterSet> | ||||
|   </PropertyGroup> | ||||
|   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | ||||
|   <ImportGroup Label="ExtensionSettings"> | ||||
|   </ImportGroup> | ||||
|   <ImportGroup Label="Shared"> | ||||
|   </ImportGroup> | ||||
|   <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||||
|     <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||||
|   </ImportGroup> | ||||
|   <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||||
|     <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||||
|   </ImportGroup> | ||||
|   <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||||
|     <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||||
|   </ImportGroup> | ||||
|   <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||||
|     <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||||
|   </ImportGroup> | ||||
|   <PropertyGroup Label="UserMacros" /> | ||||
|   <PropertyGroup /> | ||||
|   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||||
|     <ClCompile> | ||||
|       <WarningLevel>Level3</WarningLevel> | ||||
|       <Optimization>Disabled</Optimization> | ||||
|       <SDLCheck>true</SDLCheck> | ||||
|       <AdditionalIncludeDirectories>../boost_1_63_0;../../openssl-master;../../openssl-master/include;../../openssl-master/output/include</AdditionalIncludeDirectories> | ||||
|     </ClCompile> | ||||
|     <Link> | ||||
|       <GenerateDebugInformation>true</GenerateDebugInformation> | ||||
|       <AdditionalLibraryDirectories>../boost_1_63_0/stage/x86/lib/;../../openssl-master/output/lib</AdditionalLibraryDirectories> | ||||
|       <AdditionalDependencies>libcrypto.lib;libssl.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies> | ||||
|     </Link> | ||||
|   </ItemDefinitionGroup> | ||||
|   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||||
|     <ClCompile> | ||||
|       <WarningLevel>Level3</WarningLevel> | ||||
|       <Optimization>Disabled</Optimization> | ||||
|       <SDLCheck>true</SDLCheck> | ||||
|       <AdditionalIncludeDirectories>../boost_1_63_0</AdditionalIncludeDirectories> | ||||
|     </ClCompile> | ||||
|     <Link> | ||||
|       <GenerateDebugInformation>true</GenerateDebugInformation> | ||||
|       <AdditionalLibraryDirectories>../boost_1_63_0/stage/x64/lib/</AdditionalLibraryDirectories> | ||||
|     </Link> | ||||
|   </ItemDefinitionGroup> | ||||
|   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||||
|     <ClCompile> | ||||
|       <WarningLevel>Level3</WarningLevel> | ||||
|       <Optimization>MaxSpeed</Optimization> | ||||
|       <FunctionLevelLinking>true</FunctionLevelLinking> | ||||
|       <IntrinsicFunctions>true</IntrinsicFunctions> | ||||
|       <SDLCheck>true</SDLCheck> | ||||
|       <AdditionalIncludeDirectories>../boost_1_63_0;../../openssl-master;../../openssl-master/include;../../openssl-master/output/include</AdditionalIncludeDirectories> | ||||
|     </ClCompile> | ||||
|     <Link> | ||||
|       <GenerateDebugInformation>true</GenerateDebugInformation> | ||||
|       <EnableCOMDATFolding>true</EnableCOMDATFolding> | ||||
|       <OptimizeReferences>true</OptimizeReferences> | ||||
|       <AdditionalLibraryDirectories>../boost_1_63_0/stage/x86/lib/;../../openssl-master/output/lib</AdditionalLibraryDirectories> | ||||
|       <AdditionalDependencies>libcrypto.lib;libssl.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies> | ||||
|     </Link> | ||||
|   </ItemDefinitionGroup> | ||||
|   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||||
|     <ClCompile> | ||||
|       <WarningLevel>Level3</WarningLevel> | ||||
|       <Optimization>MaxSpeed</Optimization> | ||||
|       <FunctionLevelLinking>true</FunctionLevelLinking> | ||||
|       <IntrinsicFunctions>true</IntrinsicFunctions> | ||||
|       <SDLCheck>true</SDLCheck> | ||||
|       <AdditionalIncludeDirectories>../boost_1_63_0</AdditionalIncludeDirectories> | ||||
|     </ClCompile> | ||||
|     <Link> | ||||
|       <GenerateDebugInformation>true</GenerateDebugInformation> | ||||
|       <EnableCOMDATFolding>true</EnableCOMDATFolding> | ||||
|       <OptimizeReferences>true</OptimizeReferences> | ||||
|       <AdditionalLibraryDirectories>../boost_1_63_0/stage/x64/lib/</AdditionalLibraryDirectories> | ||||
|     </Link> | ||||
|   </ItemDefinitionGroup> | ||||
|   <ItemGroup> | ||||
|     <ClCompile Include="main.cpp" /> | ||||
|   </ItemGroup> | ||||
|   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||||
|   <ImportGroup Label="ExtensionTargets"> | ||||
|   </ImportGroup> | ||||
| </Project> | ||||
							
								
								
									
										22
									
								
								proxyServerTest.vcxproj.filters
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										22
									
								
								proxyServerTest.vcxproj.filters
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,22 @@ | ||||
| <?xml version="1.0" encoding="utf-8"?> | ||||
| <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||||
|   <ItemGroup> | ||||
|     <Filter Include="Source Files"> | ||||
|       <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> | ||||
|       <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions> | ||||
|     </Filter> | ||||
|     <Filter Include="Header Files"> | ||||
|       <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> | ||||
|       <Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions> | ||||
|     </Filter> | ||||
|     <Filter Include="Resource Files"> | ||||
|       <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> | ||||
|       <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> | ||||
|     </Filter> | ||||
|   </ItemGroup> | ||||
|   <ItemGroup> | ||||
|     <ClCompile Include="main.cpp"> | ||||
|       <Filter>Source Files</Filter> | ||||
|     </ClCompile> | ||||
|   </ItemGroup> | ||||
| </Project> | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user