├── .gitignore ├── README.md ├── TcpUdpForwarder.sln └── TcpUdpForwarder ├── Controller ├── ForwarderController.cs ├── Logging.cs ├── Management.cs ├── MgmtListener.cs ├── MgmtPipe.cs ├── TcpForwarder.cs ├── TcpPipe.cs ├── UdpForwarder.cs └── UdpPipe.cs ├── Model ├── Config.cs └── ServerInfo.cs ├── Program.cs ├── Properties ├── AssemblyInfo.cs ├── Resources.Designer.cs └── Resources.resx ├── Resources ├── f128.png ├── f16.png ├── f20.png └── f24.png ├── Services ├── ProjectInstaller.Designer.cs ├── ProjectInstaller.cs ├── ProjectInstaller.resx ├── TcpUdpForwarder.Designer.cs └── TcpUdpForwarder.cs ├── TcpUdpForwarder.csproj ├── Utils └── Utils.cs ├── View ├── ConfigForm.cs ├── ConfigForm.designer.cs ├── ConfigForm.resx └── SSForwardView.cs ├── config.xml ├── install.bat └── uninstall.bat /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | build/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studo 2015 cache/options directory 26 | .vs/ 27 | 28 | # MSTest test Results 29 | [Tt]est[Rr]esult*/ 30 | [Bb]uild[Ll]og.* 31 | 32 | # NUNIT 33 | *.VisualState.xml 34 | TestResult.xml 35 | 36 | # Build Results of an ATL Project 37 | [Dd]ebugPS/ 38 | [Rr]eleasePS/ 39 | dlldata.c 40 | 41 | *_i.c 42 | *_p.c 43 | *_i.h 44 | *.ilk 45 | *.meta 46 | *.obj 47 | *.pch 48 | *.pdb 49 | *.pgc 50 | *.pgd 51 | *.rsp 52 | *.sbr 53 | *.tlb 54 | *.tli 55 | *.tlh 56 | *.tmp 57 | *.tmp_proj 58 | *.log 59 | *.vspscc 60 | *.vssscc 61 | .builds 62 | *.pidb 63 | *.svclog 64 | *.scc 65 | 66 | # Chutzpah Test files 67 | _Chutzpah* 68 | 69 | # Visual C++ cache files 70 | ipch/ 71 | *.aps 72 | *.ncb 73 | *.opensdf 74 | *.sdf 75 | *.cachefile 76 | 77 | # Visual Studio profiler 78 | *.psess 79 | *.vsp 80 | *.vspx 81 | 82 | # TFS 2012 Local Workspace 83 | $tf/ 84 | 85 | # Guidance Automation Toolkit 86 | *.gpState 87 | 88 | # ReSharper is a .NET coding add-in 89 | _ReSharper*/ 90 | *.[Rr]e[Ss]harper 91 | *.DotSettings.user 92 | 93 | # JustCode is a .NET coding addin-in 94 | .JustCode 95 | 96 | # TeamCity is a build add-in 97 | _TeamCity* 98 | 99 | # DotCover is a Code Coverage Tool 100 | *.dotCover 101 | 102 | # NCrunch 103 | _NCrunch_* 104 | .*crunch*.local.xml 105 | 106 | # MightyMoose 107 | *.mm.* 108 | AutoTest.Net/ 109 | 110 | # Web workbench (sass) 111 | .sass-cache/ 112 | 113 | # Installshield output folder 114 | [Ee]xpress/ 115 | 116 | # DocProject is a documentation generator add-in 117 | DocProject/buildhelp/ 118 | DocProject/Help/*.HxT 119 | DocProject/Help/*.HxC 120 | DocProject/Help/*.hhc 121 | DocProject/Help/*.hhk 122 | DocProject/Help/*.hhp 123 | DocProject/Help/Html2 124 | DocProject/Help/html 125 | 126 | # Click-Once directory 127 | publish/ 128 | 129 | # Publish Web Output 130 | *.[Pp]ublish.xml 131 | *.azurePubxml 132 | # TODO: Comment the next line if you want to checkin your web deploy settings 133 | # but database connection strings (with potential passwords) will be unencrypted 134 | *.pubxml 135 | *.publishproj 136 | 137 | # NuGet Packages 138 | *.nupkg 139 | # The packages folder can be ignored because of Package Restore 140 | **/packages/* 141 | # except build/, which is used as an MSBuild target. 142 | !**/packages/build/ 143 | # Uncomment if necessary however generally it will be regenerated when needed 144 | #!**/packages/repositories.config 145 | 146 | # Windows Azure Build Output 147 | csx/ 148 | *.build.csdef 149 | 150 | # Windows Store app package directory 151 | AppPackages/ 152 | 153 | # Others 154 | *.[Cc]ache 155 | ClientBin/ 156 | [Ss]tyle[Cc]op.* 157 | ~$* 158 | *~ 159 | *.dbmdl 160 | *.dbproj.schemaview 161 | *.pfx 162 | *.publishsettings 163 | node_modules/ 164 | bower_components/ 165 | 166 | # RIA/Silverlight projects 167 | Generated_Code/ 168 | 169 | # Backup & report files from converting an old project file 170 | # to a newer Visual Studio version. Backup files are not needed, 171 | # because we have git ;-) 172 | _UpgradeReport_Files/ 173 | Backup*/ 174 | UpgradeLog*.XML 175 | UpgradeLog*.htm 176 | 177 | # SQL Server files 178 | *.mdf 179 | *.ldf 180 | 181 | # Business Intelligence projects 182 | *.rdl.data 183 | *.bim.layout 184 | *.bim_*.settings 185 | 186 | # Microsoft Fakes 187 | FakesAssemblies/ 188 | 189 | # Node.js Tools for Visual Studio 190 | .ntvs_analysis.dat 191 | 192 | # Visual Studio 6 build log 193 | *.plg 194 | 195 | # Visual Studio 6 workspace options file 196 | *.opt 197 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # The project dead, try https://github.com/GangZhuo/TrafficStatistics. 4 | 5 | 6 | 7 | 8 | 9 | 10 | TCP (UDP) forwarder 11 | ======================= 12 | 13 | 14 | #### Features 15 | 16 | Forward TCP and UDP to remote server 17 | 18 | 19 | #### Build 20 | 21 | Develop on Microsoft Visual Studio Community 2013 with .Net 2.0 . 22 | 23 | 24 | #### Usage 25 | 26 | 1. Find icon in the notification tray 27 | 2. You can add multiple servers in servers menu 28 | 3. Select Enable to start up forwarder service 29 | 30 | 31 | #### License 32 | 33 | GPLv3 34 | -------------------------------------------------------------------------------- /TcpUdpForwarder.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.31101.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TcpUdpForwarder", "TcpUdpForwarder\TcpUdpForwarder.csproj", "{1EF4C8DC-F5AB-4D95-B9BB-C3EF38823F2B}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {1EF4C8DC-F5AB-4D95-B9BB-C3EF38823F2B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {1EF4C8DC-F5AB-4D95-B9BB-C3EF38823F2B}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {1EF4C8DC-F5AB-4D95-B9BB-C3EF38823F2B}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {1EF4C8DC-F5AB-4D95-B9BB-C3EF38823F2B}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /TcpUdpForwarder/Controller/ForwarderController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Collections.Generic; 4 | using System.Net.Sockets; 5 | 6 | using TcpUdpForwarder.Model; 7 | 8 | namespace TcpUdpForwarder.Controller 9 | { 10 | public class ForwarderController 11 | { 12 | public const string Version = "1.0.0"; 13 | 14 | Config _config; 15 | MgmtListener _mgmtServer; 16 | TcpForwarder _tcpForwarder; 17 | UdpForwarder _udpForwarder; 18 | bool _issvc = false; 19 | Management _mgmt; 20 | 21 | public event EventHandler EnableStatusChanged; 22 | public event EventHandler ConfigChanged; 23 | public event ErrorEventHandler Errored; 24 | 25 | public ForwarderController(bool issvc) 26 | { 27 | _issvc = issvc; 28 | _config = Config.Load(); 29 | } 30 | 31 | public void Start() 32 | { 33 | Reload(); 34 | } 35 | 36 | public void Stop() 37 | { 38 | if (_mgmtServer != null) 39 | { 40 | _mgmtServer.Stop(); 41 | _mgmtServer = null; 42 | } 43 | if (_tcpForwarder != null) 44 | { 45 | _tcpForwarder.Stop(); 46 | _tcpForwarder = null; 47 | } 48 | if (_udpForwarder != null) 49 | { 50 | _udpForwarder.Stop(); 51 | _udpForwarder = null; 52 | } 53 | if (_mgmt != null) 54 | { 55 | _mgmt.OnClose -= _mgmt_OnClose; 56 | _mgmt.Close(); 57 | _mgmt = null; 58 | } 59 | } 60 | 61 | public void Reload() 62 | { 63 | MgmtListener oldMgmt = null; 64 | try 65 | { 66 | _config = Config.Load(); 67 | if (_issvc) 68 | { 69 | if (_tcpForwarder != null) 70 | _tcpForwarder.Stop(); 71 | if (_udpForwarder != null) 72 | _udpForwarder.Stop(); 73 | if (_mgmtServer != null && _mgmtServer.mgmtPort != _config.mgmtPort) 74 | { 75 | oldMgmt = _mgmtServer; 76 | _mgmtServer = null; 77 | } 78 | if (_mgmtServer == null) 79 | { 80 | _mgmtServer = new MgmtListener(this, _config.mgmtPort); 81 | _mgmtServer.Start(); 82 | } 83 | if (!_config.enabled) 84 | return; 85 | ServerInfo server = GetCurrentServer(); 86 | _tcpForwarder = new TcpForwarder(server); 87 | _tcpForwarder.Start(); 88 | _udpForwarder = new UdpForwarder(server); 89 | _udpForwarder.Start(); 90 | } 91 | else 92 | { 93 | if (_mgmt == null) 94 | { 95 | ReConnectMgmtPort(); 96 | } 97 | else 98 | { 99 | _mgmt.ReloadService(); 100 | } 101 | } 102 | } 103 | catch (Exception e) 104 | { 105 | // translate Microsoft language into human language 106 | // i.e. An attempt was made to access a socket in a way forbidden by its access permissions => Port already in use 107 | if (e is SocketException) 108 | { 109 | SocketException se = (SocketException)e; 110 | if (se.SocketErrorCode == SocketError.AccessDenied) 111 | { 112 | e = new Exception("Port already in use", e); 113 | } 114 | } 115 | Logging.LogUsefulException(e); 116 | ReportError(e); 117 | } 118 | finally 119 | { 120 | if (oldMgmt != null) 121 | { 122 | oldMgmt.Stop(); 123 | } 124 | } 125 | } 126 | 127 | private void _mgmt_OnStart(object sender, EventArgs e) 128 | { 129 | ReportConfigChanged(); 130 | } 131 | 132 | private void _mgmt_OnClose(object sender, EventArgs e) 133 | { 134 | ReportError(new Exception("Can't connet to service management port (127.0.0.1:" + _config.mgmtPort + ")")); 135 | ReConnectMgmtPort(); 136 | } 137 | 138 | private void ReConnectMgmtPort() 139 | { 140 | if (_mgmt != null) 141 | { 142 | _mgmt.OnStart -= _mgmt_OnStart; 143 | _mgmt.OnClose -= _mgmt_OnClose; 144 | _mgmt.Close(); 145 | _mgmt = null; 146 | } 147 | _mgmt = new Management(this, _config.mgmtPort); 148 | _mgmt.OnStart += _mgmt_OnStart; 149 | _mgmt.OnClose += _mgmt_OnClose; 150 | _mgmt.Start(); 151 | Console.WriteLine("Connet to service management port (127.0.0.1:" + _config.mgmtPort + ")"); 152 | } 153 | 154 | public void ReportError(Exception e) 155 | { 156 | if (_issvc) 157 | { 158 | if (_mgmtServer != null) 159 | _mgmtServer.Pipes.ReportError(e); 160 | } 161 | else if (Errored != null) 162 | { 163 | Errored(this, new ErrorEventArgs(e)); 164 | } 165 | } 166 | 167 | public void ReportConfigChanged() 168 | { 169 | if (_issvc) 170 | { 171 | if (_mgmtServer != null) 172 | _mgmtServer.Pipes.ReportConfigChanged(); 173 | } 174 | else if (ConfigChanged != null) 175 | { 176 | ConfigChanged(this, new EventArgs()); 177 | } 178 | } 179 | 180 | public void ReportEnableStatusChanged() 181 | { 182 | if (_issvc) 183 | { 184 | if (_mgmtServer != null) 185 | _mgmtServer.Pipes.ReportEnableStatusChanged(); 186 | } 187 | else if (EnableStatusChanged != null) 188 | { 189 | EnableStatusChanged(this, new EventArgs()); 190 | } 191 | } 192 | 193 | public ServerInfo GetCurrentServer() 194 | { 195 | return _config.GetCurrentServer(); 196 | } 197 | 198 | // always return copy 199 | public Config GetConfiguration() 200 | { 201 | return Config.Load(); 202 | } 203 | 204 | public void SelectServerIndex(int index) 205 | { 206 | _config.index = index; 207 | SaveConfig(_config); 208 | } 209 | 210 | public void ToggleEnable(bool enabled) 211 | { 212 | _config.enabled = enabled; 213 | SaveConfig(_config); 214 | ReportEnableStatusChanged(); 215 | } 216 | 217 | public void SaveServers(List servers) 218 | { 219 | _config.servers = servers; 220 | SaveConfig(_config); 221 | } 222 | 223 | protected void SaveConfig(Config newConfig) 224 | { 225 | Config.Save(newConfig); 226 | Reload(); 227 | } 228 | 229 | } 230 | } 231 | -------------------------------------------------------------------------------- /TcpUdpForwarder/Controller/Logging.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Net.Sockets; 4 | 5 | namespace TcpUdpForwarder.Controller 6 | { 7 | public class Logging 8 | { 9 | public static string LogFile; 10 | 11 | public static bool OpenLogFile(bool is_svc) 12 | { 13 | try 14 | { 15 | string temppath = Path.GetTempPath(); 16 | LogFile = Path.Combine(temppath, "TcpUdpForwarder.log"); 17 | FileStream fs = new FileStream(LogFile, FileMode.Append); 18 | StreamWriterWithTimestamp sw = new StreamWriterWithTimestamp(fs); 19 | sw.AutoFlush = true; 20 | Console.SetOut(sw); 21 | Console.SetError(sw); 22 | Console.WriteLine("Log to " + LogFile); 23 | return true; 24 | } 25 | catch (IOException e) 26 | { 27 | Console.WriteLine(e.ToString()); 28 | return false; 29 | } 30 | } 31 | 32 | public static void LogUsefulException(Exception e) 33 | { 34 | // just log useful exceptions, not all of them 35 | if (e is SocketException) 36 | { 37 | SocketException se = (SocketException)e; 38 | if (se.SocketErrorCode == SocketError.ConnectionAborted) 39 | { 40 | // closed by browser when sending 41 | // normally happens when download is canceled or a tab is closed before page is loaded 42 | } 43 | else if (se.SocketErrorCode == SocketError.ConnectionReset) 44 | { 45 | // received rst 46 | } 47 | else if (se.SocketErrorCode == SocketError.NotConnected) 48 | { 49 | // close when not connected 50 | } 51 | else 52 | { 53 | Console.WriteLine(e.Message + "\r\n" + e.StackTrace); 54 | } 55 | } 56 | else 57 | { 58 | Console.WriteLine(e.Message + "\r\n" + e.StackTrace); 59 | } 60 | } 61 | 62 | } 63 | 64 | // Simply extended System.IO.StreamWriter for adding timestamp workaround 65 | public class StreamWriterWithTimestamp : StreamWriter 66 | { 67 | public StreamWriterWithTimestamp(Stream stream) 68 | : base(stream) 69 | { 70 | } 71 | 72 | private string GetTimestamp() 73 | { 74 | return "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] "; 75 | } 76 | 77 | public override void WriteLine(string value) 78 | { 79 | base.WriteLine(GetTimestamp() + value); 80 | } 81 | 82 | public override void Write(string value) 83 | { 84 | base.Write(GetTimestamp() + value); 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /TcpUdpForwarder/Controller/Management.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Net; 3 | using System.Net.Sockets; 4 | using System.IO; 5 | using System.Text; 6 | 7 | namespace TcpUdpForwarder.Controller 8 | { 9 | public class Management 10 | { 11 | ForwarderController controller; 12 | private int _mgmtPort; 13 | private Socket _remote; 14 | private bool _closed = false; 15 | public const int RecvSize = 16384; 16 | // remote receive buffer 17 | private byte[] recvBuffer = new byte[RecvSize]; 18 | 19 | public event EventHandler OnStart; 20 | public event EventHandler OnClose; 21 | 22 | public Management(ForwarderController controller, int mgmtPort) 23 | { 24 | this.controller = controller; 25 | this._mgmtPort = mgmtPort; 26 | } 27 | 28 | public void Start() 29 | { 30 | try 31 | { 32 | // TODO async resolving 33 | IPEndPoint remoteEP = new IPEndPoint(IPAddress.Loopback, _mgmtPort); 34 | 35 | _remote = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 36 | _remote.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); 37 | 38 | // Connect to the remote endpoint. 39 | _remote.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), null); 40 | } 41 | catch (Exception e) 42 | { 43 | Logging.LogUsefulException(e); 44 | this.Close(); 45 | } 46 | } 47 | 48 | private void ConnectCallback(IAsyncResult ar) 49 | { 50 | if (_closed) 51 | { 52 | return; 53 | } 54 | try 55 | { 56 | _remote.EndConnect(ar); 57 | _remote.BeginReceive(recvBuffer, 0, RecvSize, 0, new AsyncCallback(receiveCallback), null); 58 | if (OnStart != null) 59 | OnStart(this, new EventArgs()); 60 | } 61 | catch (Exception e) 62 | { 63 | Logging.LogUsefulException(e); 64 | this.Close(); 65 | } 66 | } 67 | 68 | private void receiveCallback(IAsyncResult ar) 69 | { 70 | if (_closed) 71 | { 72 | return; 73 | } 74 | try 75 | { 76 | int bytesRead = _remote.EndReceive(ar); 77 | 78 | if (bytesRead > 0) 79 | { 80 | string str = Encoding.UTF8.GetString(recvBuffer, 0, bytesRead); 81 | int i = str.IndexOf(' '); 82 | string key, value; 83 | if (i > 0) 84 | { 85 | key = str.Substring(0, i).Trim(); 86 | value = str.Substring(i).Trim(); 87 | } 88 | else 89 | { 90 | key = str.Trim(); 91 | value = string.Empty; 92 | } 93 | switch (key) 94 | { 95 | case "Error": 96 | controller.ReportError(new Exception(value)); 97 | break; 98 | case "ConfigChanged": 99 | controller.ReportConfigChanged(); 100 | break; 101 | case "EnableStatusChanged": 102 | controller.ReportEnableStatusChanged(); 103 | break; 104 | default: 105 | Console.WriteLine(str); 106 | break; 107 | } 108 | _remote.BeginReceive(this.recvBuffer, 0, RecvSize, 0, 109 | new AsyncCallback(receiveCallback), null); 110 | } 111 | else 112 | { 113 | this.Close(); 114 | } 115 | } 116 | catch (Exception e) 117 | { 118 | Logging.LogUsefulException(e); 119 | this.Close(); 120 | } 121 | } 122 | 123 | public void Send(string s) 124 | { 125 | byte[] bytes = Encoding.UTF8.GetBytes(s); 126 | Send(bytes, 0, bytes.Length); 127 | } 128 | 129 | public void Send(byte[] bytes, int offset, int length) 130 | { 131 | try 132 | { 133 | _remote.BeginSend(bytes, offset, length, 0, new AsyncCallback(sendCallback), null); 134 | } 135 | catch (Exception e) 136 | { 137 | Logging.LogUsefulException(e); 138 | this.Close(); 139 | } 140 | } 141 | 142 | private void sendCallback(IAsyncResult ar) 143 | { 144 | if (_closed) 145 | { 146 | return; 147 | } 148 | try 149 | { 150 | _remote.EndSend(ar); 151 | } 152 | catch (Exception e) 153 | { 154 | Logging.LogUsefulException(e); 155 | this.Close(); 156 | } 157 | } 158 | 159 | public void StartService() 160 | { 161 | Send("Start"); 162 | } 163 | 164 | public void StopService() 165 | { 166 | Send("Stop"); 167 | } 168 | 169 | public void ReloadService() 170 | { 171 | Send("Reload"); 172 | } 173 | 174 | public bool IsClosed() 175 | { 176 | lock (this) 177 | { 178 | return _closed; 179 | } 180 | } 181 | 182 | public void Close() 183 | { 184 | lock (this) 185 | { 186 | if (_closed) 187 | { 188 | return; 189 | } 190 | _closed = true; 191 | } 192 | if (_remote != null) 193 | { 194 | try 195 | { 196 | _remote.Shutdown(SocketShutdown.Both); 197 | _remote.Close(); 198 | } 199 | catch (SocketException e) 200 | { 201 | Logging.LogUsefulException(e); 202 | } 203 | } 204 | if (OnClose != null) 205 | { 206 | OnClose(this, new EventArgs()); 207 | OnClose = null; 208 | } 209 | if(OnStart != null) 210 | { 211 | OnStart = null; 212 | } 213 | } 214 | } 215 | } 216 | -------------------------------------------------------------------------------- /TcpUdpForwarder/Controller/MgmtListener.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Net; 4 | using System.Net.Sockets; 5 | 6 | namespace TcpUdpForwarder.Controller 7 | { 8 | public class MgmtListener 9 | { 10 | ForwarderController controller; 11 | Socket _socket; 12 | public int mgmtPort { get; private set; } 13 | MgmtPipe _pipe; 14 | 15 | public MgmtPipe Pipes 16 | { 17 | get { return _pipe; } 18 | } 19 | 20 | public MgmtListener(ForwarderController controller, int mgmtPort) 21 | { 22 | this.controller = controller; 23 | this.mgmtPort = mgmtPort; 24 | _pipe = new MgmtPipe(controller); 25 | } 26 | 27 | public void Start() 28 | { 29 | if (Utils.Utils.CheckIfTcpPortInUse(mgmtPort)) 30 | throw new Exception("Port already in use"); 31 | 32 | try 33 | { 34 | // Create a TCP/IP socket. 35 | _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 36 | _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 37 | IPEndPoint localEndPoint = null; 38 | localEndPoint = new IPEndPoint(IPAddress.Loopback, mgmtPort); 39 | 40 | // Bind the socket to the local endpoint and listen for incoming connections. 41 | _socket.Bind(localEndPoint); 42 | _socket.Listen(1024); 43 | 44 | 45 | // Start an asynchronous socket to listen for connections. 46 | Console.WriteLine("Management listen on " + localEndPoint.ToString()); 47 | _socket.BeginAccept(new AsyncCallback(AcceptCallback), _socket); 48 | } 49 | catch (SocketException) 50 | { 51 | _socket.Close(); 52 | throw; 53 | } 54 | } 55 | 56 | public void Stop() 57 | { 58 | if (_socket != null) 59 | { 60 | _socket.Close(); 61 | _socket = null; 62 | } 63 | } 64 | 65 | public void AcceptCallback(IAsyncResult ar) 66 | { 67 | Socket listener = (Socket)ar.AsyncState; 68 | try 69 | { 70 | Socket conn = listener.EndAccept(ar); 71 | if (_pipe.CreatePipe(conn)) 72 | return; 73 | // do nothing 74 | } 75 | catch (ObjectDisposedException) 76 | { 77 | } 78 | catch (Exception e) 79 | { 80 | Logging.LogUsefulException(e); 81 | } 82 | finally 83 | { 84 | try 85 | { 86 | listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); 87 | } 88 | catch (ObjectDisposedException) 89 | { 90 | // do nothing 91 | } 92 | catch (Exception e) 93 | { 94 | Logging.LogUsefulException(e); 95 | } 96 | } 97 | } 98 | 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /TcpUdpForwarder/Controller/MgmtPipe.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Net; 4 | using System.Net.Sockets; 5 | using System.Text; 6 | 7 | using TcpUdpForwarder.Model; 8 | 9 | namespace TcpUdpForwarder.Controller 10 | { 11 | public class MgmtPipe 12 | { 13 | ForwarderController controller; 14 | List handlers = new List(); 15 | 16 | public MgmtPipe(ForwarderController controller) 17 | { 18 | this.controller = controller; 19 | } 20 | 21 | public bool CreatePipe(Socket socket) 22 | { 23 | Handler handler = new Handler(controller); 24 | handler.OnClose += handler_OnClose; 25 | handler.Start(socket); 26 | return true; 27 | } 28 | 29 | public void ReportError(Exception e) 30 | { 31 | lock (handlers) 32 | { 33 | foreach(Handler h in handlers) 34 | { 35 | h.Send("Error " + e.Message); 36 | } 37 | } 38 | } 39 | 40 | public void ReportConfigChanged() 41 | { 42 | lock (handlers) 43 | { 44 | foreach (Handler h in handlers) 45 | { 46 | h.Send("ConfigChanged"); 47 | } 48 | } 49 | } 50 | 51 | public void ReportEnableStatusChanged() 52 | { 53 | lock (handlers) 54 | { 55 | foreach (Handler h in handlers) 56 | { 57 | h.Send("EnableStatusChanged"); 58 | } 59 | } 60 | } 61 | 62 | private void handler_OnClose(object sender, EventArgs e) 63 | { 64 | lock (handlers) 65 | { 66 | handlers.Remove((Handler)sender); 67 | } 68 | } 69 | 70 | private void UpdateHandlerList(Handler handler) 71 | { 72 | lock (handlers) 73 | { 74 | handlers.Insert(0, handler); 75 | } 76 | } 77 | 78 | class Handler 79 | { 80 | ForwarderController controller; 81 | private Socket _local; 82 | private bool _closed = false; 83 | public const int RecvSize = 16384; 84 | // receive buffer 85 | private byte[] recvBuffer = new byte[RecvSize]; 86 | 87 | public event EventHandler OnClose; 88 | 89 | public Handler(ForwarderController controller) 90 | { 91 | this.controller = controller; 92 | } 93 | 94 | public void Start(Socket socket) 95 | { 96 | this._local = socket; 97 | StartPipe(); 98 | } 99 | 100 | private void StartPipe() 101 | { 102 | if (_closed) 103 | { 104 | return; 105 | } 106 | try 107 | { 108 | _local.BeginReceive(recvBuffer, 0, RecvSize, 0, new AsyncCallback(receiveCallback), null); 109 | Send(Logging.LogFile); 110 | } 111 | catch (Exception e) 112 | { 113 | Logging.LogUsefulException(e); 114 | this.Close(); 115 | } 116 | } 117 | 118 | private void receiveCallback(IAsyncResult ar) 119 | { 120 | if (_closed) 121 | { 122 | return; 123 | } 124 | try 125 | { 126 | int bytesRead = _local.EndReceive(ar); 127 | 128 | if (bytesRead > 0) 129 | { 130 | string str = Encoding.UTF8.GetString(recvBuffer, 0, bytesRead); 131 | int i = str.IndexOf(' '); 132 | string key, value; 133 | if (i > 0) 134 | { 135 | key = str.Substring(0, i).Trim(); 136 | value = str.Substring(i).Trim(); 137 | } 138 | else 139 | { 140 | key = str.Trim(); 141 | value = string.Empty; 142 | } 143 | switch (key) 144 | { 145 | case "Start": 146 | controller.Start(); 147 | break; 148 | case "Stop": 149 | controller.Stop(); 150 | break; 151 | case "Reload": 152 | controller.Reload(); 153 | break; 154 | } 155 | _local.BeginReceive(this.recvBuffer, 0, RecvSize, 0, 156 | new AsyncCallback(receiveCallback), null); 157 | } 158 | else 159 | { 160 | this.Close(); 161 | } 162 | } 163 | catch (Exception e) 164 | { 165 | Logging.LogUsefulException(e); 166 | this.Close(); 167 | } 168 | } 169 | 170 | public void Send(string s) 171 | { 172 | byte[] bytes = Encoding.UTF8.GetBytes(s); 173 | Send(bytes, 0, bytes.Length); 174 | } 175 | 176 | public void Send(byte[] bytes, int offset, int length) 177 | { 178 | try 179 | { 180 | _local.BeginSend(bytes, offset, length, 0, new AsyncCallback(sendCallback), null); 181 | } 182 | catch (Exception e) 183 | { 184 | Logging.LogUsefulException(e); 185 | this.Close(); 186 | } 187 | } 188 | 189 | private void sendCallback(IAsyncResult ar) 190 | { 191 | if (_closed) 192 | { 193 | return; 194 | } 195 | try 196 | { 197 | _local.EndSend(ar); 198 | } 199 | catch (Exception e) 200 | { 201 | Logging.LogUsefulException(e); 202 | this.Close(); 203 | } 204 | } 205 | 206 | public void Close() 207 | { 208 | lock (this) 209 | { 210 | if (_closed) 211 | { 212 | return; 213 | } 214 | _closed = true; 215 | } 216 | if (_local != null) 217 | { 218 | try 219 | { 220 | _local.Shutdown(SocketShutdown.Both); 221 | _local.Close(); 222 | } 223 | catch (Exception e) 224 | { 225 | Logging.LogUsefulException(e); 226 | } 227 | } 228 | if (OnClose != null) 229 | OnClose(this, new EventArgs()); 230 | } 231 | } 232 | } 233 | } 234 | -------------------------------------------------------------------------------- /TcpUdpForwarder/Controller/TcpForwarder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Net; 4 | using System.Net.Sockets; 5 | 6 | using TcpUdpForwarder.Model; 7 | 8 | namespace TcpUdpForwarder.Controller 9 | { 10 | public class TcpForwarder 11 | { 12 | Socket _socket; 13 | TcpPipe _pipe; 14 | ServerInfo _server; 15 | 16 | public TcpForwarder(ServerInfo server) 17 | { 18 | _server = server; 19 | this._pipe = new TcpPipe(_server); 20 | } 21 | 22 | public void Start() 23 | { 24 | if (Utils.Utils.CheckIfTcpPortInUse(_server.localPort)) 25 | throw new Exception("Port already in use"); 26 | 27 | try 28 | { 29 | // Create a TCP/IP socket. 30 | _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 31 | _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 32 | IPEndPoint localEndPoint = null; 33 | localEndPoint = new IPEndPoint(IPAddress.Any, _server.localPort); 34 | 35 | // Bind the socket to the local endpoint and listen for incoming connections. 36 | _socket.Bind(localEndPoint); 37 | _socket.Listen(1024); 38 | 39 | 40 | // Start an asynchronous socket to listen for connections. 41 | Console.WriteLine("TCP listen on " + localEndPoint.ToString()); 42 | _socket.BeginAccept(new AsyncCallback(AcceptCallback), _socket); 43 | } 44 | catch (SocketException) 45 | { 46 | _socket.Close(); 47 | throw; 48 | } 49 | } 50 | 51 | public void Stop() 52 | { 53 | if (_socket != null) 54 | { 55 | _socket.Close(); 56 | _socket = null; 57 | } 58 | } 59 | 60 | public void AcceptCallback(IAsyncResult ar) 61 | { 62 | Socket listener = (Socket)ar.AsyncState; 63 | try 64 | { 65 | Socket conn = listener.EndAccept(ar); 66 | if (_pipe.CreatePipe(conn)) 67 | return; 68 | // do nothing 69 | } 70 | catch (ObjectDisposedException) 71 | { 72 | } 73 | catch (Exception e) 74 | { 75 | Logging.LogUsefulException(e); 76 | } 77 | finally 78 | { 79 | try 80 | { 81 | listener.BeginAccept( new AsyncCallback(AcceptCallback), listener); 82 | } 83 | catch (ObjectDisposedException) 84 | { 85 | // do nothing 86 | } 87 | catch (Exception e) 88 | { 89 | Logging.LogUsefulException(e); 90 | } 91 | } 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /TcpUdpForwarder/Controller/TcpPipe.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Net; 3 | using System.Net.Sockets; 4 | 5 | using TcpUdpForwarder.Model; 6 | 7 | namespace TcpUdpForwarder.Controller 8 | { 9 | public class TcpPipe 10 | { 11 | ServerInfo _server; 12 | 13 | public TcpPipe(ServerInfo server) 14 | { 15 | this._server = server; 16 | } 17 | 18 | public bool CreatePipe(Socket socket) 19 | { 20 | new Handler(_server).Start(socket); 21 | return true; 22 | } 23 | 24 | class Handler 25 | { 26 | ServerInfo _server; 27 | 28 | private Socket _local; 29 | private Socket _remote; 30 | private bool _closed = false; 31 | private bool _localShutdown = false; 32 | private bool _remoteShutdown = false; 33 | public const int RecvSize = 16384; 34 | // remote receive buffer 35 | private byte[] remoteRecvBuffer = new byte[RecvSize]; 36 | // connection receive buffer 37 | private byte[] connetionRecvBuffer = new byte[RecvSize]; 38 | 39 | public Handler(ServerInfo server) 40 | { 41 | this._server = server; 42 | } 43 | 44 | public void Start(Socket socket) 45 | { 46 | this._local = socket; 47 | IPAddress ipAddress = null; 48 | bool parsed = IPAddress.TryParse(_server.server, out ipAddress); 49 | if (!parsed) 50 | ipAddress = LookupIp(_server.server); 51 | if (ipAddress == null) 52 | throw new Exception("Wrong target server"); 53 | try 54 | { 55 | // TODO async resolving 56 | IPEndPoint remoteEP = new IPEndPoint(ipAddress, _server.serverPort); 57 | 58 | _remote = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); 59 | _remote.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); 60 | 61 | // Connect to the remote endpoint. 62 | _remote.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), null); 63 | } 64 | catch (Exception e) 65 | { 66 | Logging.LogUsefulException(e); 67 | this.Close(); 68 | } 69 | } 70 | 71 | private IPAddress LookupIp(string hostNameOrAddress) 72 | { 73 | IPAddress ipAddress = null; 74 | IPHostEntry hostinfo = Dns.GetHostEntry(hostNameOrAddress); 75 | IPAddress[] aryIP = hostinfo.AddressList; 76 | if (aryIP != null) 77 | { 78 | foreach (IPAddress ip in aryIP) 79 | { 80 | if (ip.AddressFamily == AddressFamily.InterNetwork) 81 | { 82 | ipAddress = ip; 83 | break; 84 | } 85 | } 86 | } 87 | return ipAddress; 88 | } 89 | 90 | private void ConnectCallback(IAsyncResult ar) 91 | { 92 | if (_closed) 93 | { 94 | return; 95 | } 96 | try 97 | { 98 | _remote.EndConnect(ar); 99 | StartPipe(); 100 | } 101 | catch (Exception e) 102 | { 103 | Logging.LogUsefulException(e); 104 | this.Close(); 105 | } 106 | } 107 | 108 | private void StartPipe() 109 | { 110 | if (_closed) 111 | { 112 | return; 113 | } 114 | try 115 | { 116 | _remote.BeginReceive(remoteRecvBuffer, 0, RecvSize, 0, new AsyncCallback(PipeRemoteReceiveCallback), null); 117 | _local.BeginReceive(connetionRecvBuffer, 0, RecvSize, 0, new AsyncCallback(PipeConnectionReceiveCallback), null); 118 | } 119 | catch (Exception e) 120 | { 121 | Logging.LogUsefulException(e); 122 | this.Close(); 123 | } 124 | } 125 | 126 | private void PipeRemoteReceiveCallback(IAsyncResult ar) 127 | { 128 | if (_closed) 129 | { 130 | return; 131 | } 132 | try 133 | { 134 | int bytesRead = _remote.EndReceive(ar); 135 | 136 | if (bytesRead > 0) 137 | { 138 | _local.BeginSend(remoteRecvBuffer, 0, bytesRead, 0, new AsyncCallback(PipeConnectionSendCallback), null); 139 | } 140 | else 141 | { 142 | //Console.WriteLine("bytesRead: " + bytesRead.ToString()); 143 | _local.Shutdown(SocketShutdown.Send); 144 | _localShutdown = true; 145 | CheckClose(); 146 | } 147 | } 148 | catch (Exception e) 149 | { 150 | Logging.LogUsefulException(e); 151 | this.Close(); 152 | } 153 | } 154 | 155 | private void PipeConnectionReceiveCallback(IAsyncResult ar) 156 | { 157 | if (_closed) 158 | { 159 | return; 160 | } 161 | try 162 | { 163 | int bytesRead = _local.EndReceive(ar); 164 | 165 | if (bytesRead > 0) 166 | { 167 | _remote.BeginSend(connetionRecvBuffer, 0, bytesRead, 0, new AsyncCallback(PipeRemoteSendCallback), null); 168 | } 169 | else 170 | { 171 | _remote.Shutdown(SocketShutdown.Send); 172 | _remoteShutdown = true; 173 | CheckClose(); 174 | } 175 | } 176 | catch (Exception e) 177 | { 178 | Logging.LogUsefulException(e); 179 | this.Close(); 180 | } 181 | } 182 | 183 | private void PipeRemoteSendCallback(IAsyncResult ar) 184 | { 185 | if (_closed) 186 | { 187 | return; 188 | } 189 | try 190 | { 191 | _remote.EndSend(ar); 192 | _local.BeginReceive(this.connetionRecvBuffer, 0, RecvSize, 0, 193 | new AsyncCallback(PipeConnectionReceiveCallback), null); 194 | } 195 | catch (Exception e) 196 | { 197 | Logging.LogUsefulException(e); 198 | this.Close(); 199 | } 200 | } 201 | 202 | private void PipeConnectionSendCallback(IAsyncResult ar) 203 | { 204 | if (_closed) 205 | { 206 | return; 207 | } 208 | try 209 | { 210 | _local.EndSend(ar); 211 | _remote.BeginReceive(this.remoteRecvBuffer, 0, RecvSize, 0, 212 | new AsyncCallback(PipeRemoteReceiveCallback), null); 213 | } 214 | catch (Exception e) 215 | { 216 | Logging.LogUsefulException(e); 217 | this.Close(); 218 | } 219 | } 220 | 221 | private void CheckClose() 222 | { 223 | if (_localShutdown && _remoteShutdown) 224 | { 225 | this.Close(); 226 | } 227 | } 228 | 229 | public void Close() 230 | { 231 | lock (this) 232 | { 233 | if (_closed) 234 | { 235 | return; 236 | } 237 | _closed = true; 238 | } 239 | if (_local != null) 240 | { 241 | try 242 | { 243 | _local.Shutdown(SocketShutdown.Both); 244 | _local.Close(); 245 | _local = null; 246 | connetionRecvBuffer = null; 247 | } 248 | catch (Exception e) 249 | { 250 | Logging.LogUsefulException(e); 251 | } 252 | } 253 | if (_remote != null) 254 | { 255 | try 256 | { 257 | _remote.Shutdown(SocketShutdown.Both); 258 | _remote.Close(); 259 | _remote = null; 260 | remoteRecvBuffer = null; 261 | } 262 | catch (SocketException e) 263 | { 264 | Logging.LogUsefulException(e); 265 | } 266 | } 267 | } 268 | } 269 | } 270 | } 271 | -------------------------------------------------------------------------------- /TcpUdpForwarder/Controller/UdpForwarder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Net; 4 | using System.Net.Sockets; 5 | 6 | using TcpUdpForwarder.Model; 7 | 8 | namespace TcpUdpForwarder.Controller 9 | { 10 | public class UdpForwarder 11 | { 12 | Socket _socket; 13 | ServerInfo _server; 14 | UdpPipe _pipe; 15 | 16 | public UdpForwarder(ServerInfo server) 17 | { 18 | _server = server; 19 | this._pipe = new UdpPipe(_server); 20 | } 21 | 22 | public void Start() 23 | { 24 | if (Utils.Utils.CheckIfUdpPortInUse(_server.localPort)) 25 | throw new Exception("Port already in use"); 26 | 27 | try 28 | { 29 | // Create a TCP/IP socket. 30 | _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 31 | _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 32 | IPEndPoint localEndPoint = null; 33 | localEndPoint = new IPEndPoint(IPAddress.Any, _server.localPort); 34 | 35 | // Bind the socket to the local endpoint and listen for incoming connections. 36 | _socket.Bind(localEndPoint); 37 | 38 | // Start an asynchronous socket to listen for connections. 39 | Console.WriteLine("UDP listen on " + localEndPoint.ToString()); 40 | EndPoint remoteEP = (EndPoint)(new IPEndPoint(IPAddress.Any, 0)); 41 | byte[] buf = new byte[4096]; 42 | object[] state = new object[] { 43 | _socket, 44 | buf 45 | }; 46 | _socket.BeginReceiveFrom(buf, 0, buf.Length, SocketFlags.None, ref remoteEP, new AsyncCallback(receiveFrom), state); 47 | } 48 | catch (SocketException) 49 | { 50 | _socket.Close(); 51 | throw; 52 | } 53 | } 54 | 55 | public void Stop() 56 | { 57 | if (_socket != null) 58 | { 59 | _socket.Close(); 60 | _socket = null; 61 | } 62 | } 63 | 64 | private void receiveFrom(IAsyncResult ar) 65 | { 66 | object[] state = (object[])ar.AsyncState; 67 | Socket socket = (Socket)state[0]; 68 | byte[] buf = (byte[])state[1]; 69 | EndPoint remoteEP = (EndPoint)(new IPEndPoint(IPAddress.Any, 0)); 70 | try 71 | { 72 | int bytesRead = socket.EndReceiveFrom(ar, ref remoteEP); 73 | if (_pipe.CreatePipe(buf, bytesRead, socket, remoteEP)) 74 | return; 75 | // do nothing 76 | } 77 | catch (ObjectDisposedException) 78 | { 79 | } 80 | catch (Exception e) 81 | { 82 | Logging.LogUsefulException(e); 83 | } 84 | finally 85 | { 86 | try 87 | { 88 | remoteEP = (EndPoint)(new IPEndPoint(IPAddress.Any, 0)); 89 | socket.BeginReceiveFrom(buf, 0, buf.Length, SocketFlags.None, ref remoteEP, new AsyncCallback(receiveFrom), state); 90 | } 91 | catch (ObjectDisposedException) 92 | { 93 | // do nothing 94 | } 95 | catch (Exception e) 96 | { 97 | Logging.LogUsefulException(e); 98 | } 99 | } 100 | } 101 | 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /TcpUdpForwarder/Controller/UdpPipe.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Net; 5 | using System.Net.Sockets; 6 | 7 | using TcpUdpForwarder.Model; 8 | 9 | namespace TcpUdpForwarder.Controller 10 | { 11 | public class UdpPipe 12 | { 13 | ServerInfo _server; 14 | EndPoint _serverEP; 15 | 16 | Hashtable _handlers = Hashtable.Synchronized(new Hashtable()); 17 | 18 | System.Timers.Timer _timer = new System.Timers.Timer(10000); 19 | 20 | public UdpPipe(ServerInfo server) 21 | { 22 | _server = server; 23 | IPAddress ipAddress = null; 24 | bool parsed = IPAddress.TryParse(_server.server, out ipAddress); 25 | if (!parsed) 26 | ipAddress = LookupIp(_server.server); 27 | if (ipAddress == null) 28 | throw new Exception("Wrong target server"); 29 | _serverEP = new IPEndPoint(ipAddress, _server.serverPort); 30 | _timer.AutoReset = true; 31 | _timer.Enabled = true; 32 | _timer.Elapsed += _timer_Elapsed; 33 | _timer.Start(); 34 | } 35 | 36 | ~UdpPipe() 37 | { 38 | _timer.Stop(); 39 | _handlers.Clear(); 40 | } 41 | 42 | public bool CreatePipe(byte[] firstPacket, int length, Socket fromSocket, EndPoint fromEP) 43 | { 44 | Handler handler = getHandler(fromEP, fromSocket); 45 | handler.Handle(firstPacket, length); 46 | return true; 47 | } 48 | 49 | Handler getHandler(EndPoint fromEP, Socket fromSocket) 50 | { 51 | string key = fromEP.ToString(); 52 | lock(this) 53 | { 54 | if (_handlers.ContainsKey(key)) 55 | { 56 | #if DEBUG 57 | try { Console.WriteLine("reuse udp handler: " + key); } 58 | catch { } 59 | #endif 60 | 61 | return (Handler)_handlers[key]; 62 | } 63 | Handler handler = new Handler(); 64 | _handlers.Add(key, handler); 65 | handler._local = fromSocket; 66 | handler._localEP = fromEP; 67 | handler._remoteEP = _serverEP; 68 | handler.OnClose += handler_OnClose; 69 | handler.Start(); 70 | #if DEBUG 71 | try { Console.WriteLine("create udp handler: " + key); } 72 | catch { } 73 | #endif 74 | return handler; 75 | } 76 | } 77 | 78 | private void handler_OnClose(object sender, EventArgs e) 79 | { 80 | Handler handler = (Handler)sender; 81 | string key = handler._localEP.ToString(); 82 | lock (this) 83 | { 84 | if (_handlers.ContainsKey(key)) 85 | { 86 | #if DEBUG 87 | try { Console.WriteLine("remove udp handler: " + key); } 88 | catch { } 89 | #endif 90 | _handlers.Remove(key); 91 | } 92 | } 93 | } 94 | 95 | private IPAddress LookupIp(string hostNameOrAddress) 96 | { 97 | IPAddress ipAddress = null; 98 | IPHostEntry hostinfo = Dns.GetHostEntry(hostNameOrAddress); 99 | IPAddress[] aryIP = hostinfo.AddressList; 100 | if (aryIP != null) 101 | { 102 | foreach (IPAddress ip in aryIP) 103 | { 104 | if (ip.AddressFamily == AddressFamily.InterNetwork) 105 | { 106 | ipAddress = ip; 107 | break; 108 | } 109 | } 110 | } 111 | return ipAddress; 112 | } 113 | 114 | private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 115 | { 116 | try 117 | { 118 | _timer.Stop(); 119 | List keys = new List(_handlers.Count); 120 | lock (this) 121 | { 122 | foreach (string key in _handlers.Keys) 123 | { 124 | Handler handler = (Handler)_handlers[key]; 125 | if (handler.IsExpire()) 126 | { 127 | keys.Add(handler); 128 | } 129 | } 130 | foreach (Handler handler in keys) 131 | { 132 | #if DEBUG 133 | try { Console.WriteLine("remove expired udp handler: " + handler._remote.LocalEndPoint.ToString()); } 134 | catch { } 135 | #endif 136 | string key = handler._localEP.ToString(); 137 | handler.Close(false); 138 | _handlers.Remove(key); 139 | } 140 | } 141 | } 142 | catch (Exception ex) 143 | { 144 | Logging.LogUsefulException(ex); 145 | } 146 | finally 147 | { 148 | _timer.Start(); 149 | } 150 | } 151 | 152 | class Handler 153 | { 154 | public event EventHandler OnClose; 155 | 156 | private DateTime _expires; 157 | 158 | public Socket _local; 159 | public EndPoint _localEP; 160 | public EndPoint _remoteEP; 161 | public Socket _remote; 162 | 163 | private bool _closed = false; 164 | public const int RecvSize = 16384; 165 | // remote receive buffer 166 | private byte[] remoteRecvBuffer = new byte[RecvSize]; 167 | 168 | private List _packages = new List(1024); 169 | private bool _connected = false; 170 | private bool _sending = false; 171 | 172 | public bool IsExpire() 173 | { 174 | lock(this) 175 | { 176 | return _expires <= DateTime.Now; 177 | } 178 | } 179 | 180 | public void Start() 181 | { 182 | try 183 | { 184 | _remote = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 185 | _remote.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.NoDelay, true); 186 | _remote.BeginConnect(_remoteEP, new AsyncCallback(RemoteConnectCallback), null); 187 | Delay(); 188 | } 189 | catch (Exception e) 190 | { 191 | Logging.LogUsefulException(e); 192 | this.Close(); 193 | } 194 | } 195 | 196 | public void Handle(byte[] buffer, int length) 197 | { 198 | if (_closed) 199 | { 200 | return; 201 | } 202 | try 203 | { 204 | if (length > 0) 205 | { 206 | lock (_packages) 207 | { 208 | byte[] bytes = new byte[length]; 209 | Buffer.BlockCopy(buffer, 0, bytes, 0, length); 210 | _packages.Add(bytes); 211 | } 212 | StartSend(); 213 | } 214 | else 215 | { 216 | this.Close(); 217 | } 218 | } 219 | catch (Exception e) 220 | { 221 | Logging.LogUsefulException(e); 222 | this.Close(); 223 | } 224 | } 225 | 226 | private void RemoteConnectCallback(IAsyncResult ar) 227 | { 228 | if (_closed) 229 | { 230 | return; 231 | } 232 | try 233 | { 234 | _remote.EndConnect(ar); 235 | lock (_packages) 236 | _connected = true; 237 | StartPipe(); 238 | StartSend(); 239 | } 240 | catch (Exception e) 241 | { 242 | Logging.LogUsefulException(e); 243 | this.Close(); 244 | } 245 | } 246 | 247 | private void StartPipe() 248 | { 249 | if (_closed) 250 | { 251 | return; 252 | } 253 | try 254 | { 255 | EndPoint remoteEP = (EndPoint)(new IPEndPoint(((IPEndPoint)_remoteEP).Address, ((IPEndPoint)_remoteEP).Port)); 256 | _remote.BeginReceiveFrom(this.remoteRecvBuffer, 0, RecvSize, 0, ref remoteEP, 257 | new AsyncCallback(PipeRemoteReceiveCallback), null); 258 | StartSend(); 259 | Delay(); 260 | } 261 | catch (Exception e) 262 | { 263 | Logging.LogUsefulException(e); 264 | this.Close(); 265 | } 266 | } 267 | 268 | private void StartSend() 269 | { 270 | if (_closed) 271 | { 272 | return; 273 | } 274 | try 275 | { 276 | lock (_packages) 277 | { 278 | if (_sending || !_connected) 279 | return; 280 | if (_packages.Count > 0) 281 | { 282 | _sending = true; 283 | byte[] bytes = _packages[0]; 284 | _packages.RemoveAt(0); 285 | _remote.BeginSendTo(bytes, 0, bytes.Length, 0, _remoteEP, new AsyncCallback(PipeRemoteSendCallback), null); 286 | } 287 | else 288 | { 289 | _sending = false; 290 | } 291 | } 292 | } 293 | catch (Exception e) 294 | { 295 | Logging.LogUsefulException(e); 296 | this.Close(); 297 | } 298 | } 299 | 300 | private void PipeRemoteSendCallback(IAsyncResult ar) 301 | { 302 | if (_closed) 303 | { 304 | return; 305 | } 306 | try 307 | { 308 | _remote.EndSendTo(ar); 309 | lock (_packages) 310 | _sending = false; 311 | StartSend(); 312 | Delay(); 313 | } 314 | catch (Exception e) 315 | { 316 | Logging.LogUsefulException(e); 317 | this.Close(); 318 | } 319 | } 320 | 321 | private void PipeRemoteReceiveCallback(IAsyncResult ar) 322 | { 323 | if (_closed) 324 | { 325 | return; 326 | } 327 | try 328 | { 329 | int bytesRead = _remote.EndReceive(ar); 330 | if (bytesRead > 0) 331 | { 332 | _local.BeginSendTo(remoteRecvBuffer, 0, bytesRead, 0, _localEP, new AsyncCallback(PipeConnectionSendCallback), null); 333 | } 334 | else 335 | { 336 | this.Close(); 337 | } 338 | Delay(); 339 | } 340 | catch (Exception e) 341 | { 342 | Logging.LogUsefulException(e); 343 | this.Close(); 344 | } 345 | } 346 | 347 | private void PipeConnectionSendCallback(IAsyncResult ar) 348 | { 349 | if (_closed) 350 | { 351 | return; 352 | } 353 | try 354 | { 355 | _local.EndSendTo(ar); 356 | EndPoint remoteEP = (EndPoint)(new IPEndPoint(((IPEndPoint)_remoteEP).Address, ((IPEndPoint)_remoteEP).Port)); 357 | _remote.BeginReceiveFrom(this.remoteRecvBuffer, 0, RecvSize, 0, ref remoteEP, 358 | new AsyncCallback(PipeRemoteReceiveCallback), null); 359 | Delay(); 360 | } 361 | catch (Exception e) 362 | { 363 | Logging.LogUsefulException(e); 364 | this.Close(); 365 | } 366 | } 367 | 368 | private void Delay() 369 | { 370 | lock (this) 371 | { 372 | _expires = DateTime.Now.AddMinutes(1); 373 | } 374 | } 375 | 376 | public void Close(bool reportClose = true) 377 | { 378 | lock (this) 379 | { 380 | if (_closed) 381 | { 382 | return; 383 | } 384 | _closed = true; 385 | } 386 | if (_remote != null) 387 | { 388 | try 389 | { 390 | _remote.Shutdown(SocketShutdown.Both); 391 | _remote.Close(); 392 | _remote = null; 393 | remoteRecvBuffer = null; 394 | _packages = null; 395 | } 396 | catch (SocketException e) 397 | { 398 | Logging.LogUsefulException(e); 399 | } 400 | } 401 | if (reportClose && OnClose != null) 402 | { 403 | OnClose(this, new EventArgs()); 404 | OnClose = null; 405 | } 406 | } 407 | } 408 | } 409 | } 410 | -------------------------------------------------------------------------------- /TcpUdpForwarder/Model/Config.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Xml; 4 | using System.IO; 5 | using System.Text; 6 | 7 | namespace TcpUdpForwarder.Model 8 | { 9 | public class Config 10 | { 11 | private static string CONFIG_FILE = "config.xml"; 12 | 13 | public bool isDefault; 14 | public bool enabled; 15 | public List servers; 16 | public int index; 17 | public int mgmtPort; 18 | 19 | public Config() 20 | { 21 | isDefault = true; 22 | enabled = false; 23 | servers = new List(); 24 | servers.Add(GetDefaultServer()); 25 | index = 0; 26 | mgmtPort = 4434; 27 | } 28 | 29 | public ServerInfo GetCurrentServer() 30 | { 31 | if (servers != null && index >= 0 && index < servers.Count) 32 | return servers[index]; 33 | return null; 34 | } 35 | 36 | public static Config Load() 37 | { 38 | Config config = new Config(); 39 | 40 | if (File.Exists(CONFIG_FILE)) 41 | { 42 | config.isDefault = false; 43 | XmlDocument xmldoc = new XmlDocument(); 44 | xmldoc.Load(CONFIG_FILE); 45 | XmlNode n; 46 | 47 | n = xmldoc.SelectSingleNode("/config/enabled"); 48 | if (n != null) config.enabled = Convert.ToBoolean(n.Attributes["value"].Value); 49 | 50 | n = xmldoc.SelectSingleNode("/config/index"); 51 | if (n != null) config.index = Convert.ToInt32(n.Attributes["value"].Value); 52 | 53 | n = xmldoc.SelectSingleNode("/config/mgmt_port"); 54 | if (n != null) config.mgmtPort = Convert.ToInt32(n.Attributes["value"].Value); 55 | 56 | config.servers.Clear(); 57 | XmlNodeList ns = xmldoc.SelectNodes("/config/server"); 58 | foreach (XmlNode sn in ns) 59 | { 60 | ServerInfo s = GetDefaultServer(); 61 | XmlAttribute attr; 62 | 63 | attr = sn.Attributes["server"]; 64 | if (attr !=null) s.server = attr.Value; 65 | 66 | attr = sn.Attributes["server_port"]; 67 | if (attr != null) s.serverPort = Convert.ToInt32(attr.Value); 68 | 69 | attr = sn.Attributes["bind_port"]; 70 | if (attr != null) s.localPort = Convert.ToInt32(attr.Value); 71 | 72 | config.servers.Add(s); 73 | } 74 | } 75 | return config; 76 | } 77 | 78 | public static void Save(Config config) 79 | { 80 | XmlWriterSettings settings = new XmlWriterSettings(); 81 | settings.Encoding = Encoding.UTF8; 82 | settings.Indent = true; 83 | using (FileStream fs = new FileStream(CONFIG_FILE, FileMode.Create, FileAccess.Write)) 84 | { 85 | using (XmlWriter writer = XmlWriter.Create(fs, settings)) 86 | { 87 | writer.WriteStartDocument(); 88 | writer.WriteStartElement("config"); 89 | 90 | #region 91 | 92 | writer.WriteStartElement("enabled"); 93 | writer.WriteStartAttribute("value"); 94 | writer.WriteString(config.enabled.ToString().ToLower()); 95 | writer.WriteEndAttribute(); 96 | writer.WriteEndElement(); 97 | 98 | writer.WriteStartElement("index"); 99 | writer.WriteStartAttribute("value"); 100 | writer.WriteString(config.index.ToString()); 101 | writer.WriteEndAttribute(); 102 | writer.WriteEndElement(); 103 | 104 | writer.WriteStartElement("mgmt_port"); 105 | writer.WriteStartAttribute("value"); 106 | writer.WriteString(config.mgmtPort.ToString()); 107 | writer.WriteEndAttribute(); 108 | writer.WriteEndElement(); 109 | 110 | foreach (ServerInfo s in config.servers) 111 | { 112 | writer.WriteStartElement("server"); 113 | writer.WriteStartAttribute("server"); 114 | writer.WriteString(s.server); 115 | writer.WriteEndAttribute(); 116 | writer.WriteStartAttribute("server_port"); 117 | writer.WriteString(s.serverPort.ToString()); 118 | writer.WriteEndAttribute(); 119 | writer.WriteStartAttribute("bind_port"); 120 | writer.WriteString(s.localPort.ToString()); 121 | writer.WriteEndAttribute(); 122 | writer.WriteEndElement(); 123 | } 124 | 125 | #endregion 126 | 127 | writer.WriteEndElement(); 128 | writer.WriteEndDocument(); 129 | } 130 | } 131 | config.isDefault = false; 132 | } 133 | 134 | public static ServerInfo GetDefaultServer() 135 | { 136 | return new ServerInfo() 137 | { 138 | server = "104.224.129.11", 139 | serverPort = 443, 140 | localPort = 443, 141 | }; 142 | } 143 | 144 | public static void CheckServer(ServerInfo server) 145 | { 146 | CheckPort(server.serverPort); 147 | CheckPort(server.localPort); 148 | CheckServer(server.server); 149 | } 150 | 151 | public static void CheckPort(int port) 152 | { 153 | if (port <= 0 || port > 65535) 154 | { 155 | throw new ArgumentException("Port out of range"); 156 | } 157 | } 158 | 159 | private static void CheckServer(string server) 160 | { 161 | if (string.IsNullOrEmpty(server)) 162 | { 163 | throw new ArgumentException("Server IP can not be blank"); 164 | } 165 | } 166 | 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /TcpUdpForwarder/Model/ServerInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TcpUdpForwarder.Model 4 | { 5 | public class ServerInfo 6 | { 7 | /// 8 | /// forward to this server 9 | /// 10 | public string server; 11 | 12 | /// 13 | /// forward to this server port 14 | /// 15 | public int serverPort; 16 | 17 | /// 18 | /// bind on this port 19 | /// 20 | public int localPort; 21 | 22 | public string FriendlyName() 23 | { 24 | if (string.IsNullOrEmpty(server)) 25 | return "New server"; 26 | return server + ":" + serverPort + " (bind " + localPort + ")"; 27 | } 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /TcpUdpForwarder/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Windows.Forms; 3 | using System.IO; 4 | using System.Reflection; 5 | using System.ServiceProcess; 6 | 7 | using TcpUdpForwarder.Model; 8 | using TcpUdpForwarder.Controller; 9 | using TcpUdpForwarder.View; 10 | 11 | namespace TcpUdpForwarder 12 | { 13 | class Program 14 | { 15 | static bool is_svc = false; 16 | 17 | static void Main(string[] args) 18 | { 19 | if (args != null && args.Length > 0) 20 | { 21 | foreach (string s in args) 22 | { 23 | if (s == "--svc") 24 | { 25 | is_svc = true; 26 | break; 27 | } 28 | } 29 | } 30 | //Directory.SetCurrentDirectory(Application.StartupPath); 31 | Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)); 32 | 33 | Logging.OpenLogFile(is_svc); 34 | ForwarderController controller = null; 35 | if (is_svc) 36 | { 37 | ServiceBase[] servicesToRun; 38 | servicesToRun = new ServiceBase[] 39 | { 40 | new Services.TcpUdpForwarder() 41 | }; 42 | ServiceBase.Run(servicesToRun); 43 | } 44 | else 45 | { 46 | controller = new ForwarderController(is_svc); 47 | SSForwardView viewController = new SSForwardView(controller); 48 | controller.Start(); 49 | Application.Run(); 50 | } 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /TcpUdpForwarder/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("TCP(UDP) Forwarder")] 9 | [assembly: AssemblyDescription("Forward tcp(udp) package to a remote server")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("TCP(UDP) Forwarder")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("98af69d1-0f0c-4a73-ad26-5d83e88f6d26")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /TcpUdpForwarder/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.34209 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace TcpUdpForwarder.Properties { 12 | using System; 13 | 14 | 15 | /// 16 | /// A strongly-typed resource class, for looking up localized strings, etc. 17 | /// 18 | // This class was auto-generated by the StronglyTypedResourceBuilder 19 | // class via a tool like ResGen or Visual Studio. 20 | // To add or remove a member, edit your .ResX file then rerun ResGen 21 | // with the /str option, or rebuild your VS project. 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources { 26 | 27 | private static global::System.Resources.ResourceManager resourceMan; 28 | 29 | private static global::System.Globalization.CultureInfo resourceCulture; 30 | 31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 32 | internal Resources() { 33 | } 34 | 35 | /// 36 | /// Returns the cached ResourceManager instance used by this class. 37 | /// 38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 39 | internal static global::System.Resources.ResourceManager ResourceManager { 40 | get { 41 | if (object.ReferenceEquals(resourceMan, null)) { 42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("TcpUdpForwarder.Properties.Resources", typeof(Resources).Assembly); 43 | resourceMan = temp; 44 | } 45 | return resourceMan; 46 | } 47 | } 48 | 49 | /// 50 | /// Overrides the current thread's CurrentUICulture property for all 51 | /// resource lookups using this strongly typed resource class. 52 | /// 53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 54 | internal static global::System.Globalization.CultureInfo Culture { 55 | get { 56 | return resourceCulture; 57 | } 58 | set { 59 | resourceCulture = value; 60 | } 61 | } 62 | 63 | /// 64 | /// Looks up a localized resource of type System.Drawing.Bitmap. 65 | /// 66 | internal static System.Drawing.Bitmap f128 { 67 | get { 68 | object obj = ResourceManager.GetObject("f128", resourceCulture); 69 | return ((System.Drawing.Bitmap)(obj)); 70 | } 71 | } 72 | 73 | /// 74 | /// Looks up a localized resource of type System.Drawing.Bitmap. 75 | /// 76 | internal static System.Drawing.Bitmap f16 { 77 | get { 78 | object obj = ResourceManager.GetObject("f16", resourceCulture); 79 | return ((System.Drawing.Bitmap)(obj)); 80 | } 81 | } 82 | 83 | /// 84 | /// Looks up a localized resource of type System.Drawing.Bitmap. 85 | /// 86 | internal static System.Drawing.Bitmap f20 { 87 | get { 88 | object obj = ResourceManager.GetObject("f20", resourceCulture); 89 | return ((System.Drawing.Bitmap)(obj)); 90 | } 91 | } 92 | 93 | /// 94 | /// Looks up a localized resource of type System.Drawing.Bitmap. 95 | /// 96 | internal static System.Drawing.Bitmap f24 { 97 | get { 98 | object obj = ResourceManager.GetObject("f24", resourceCulture); 99 | return ((System.Drawing.Bitmap)(obj)); 100 | } 101 | } 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /TcpUdpForwarder/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | 121 | 122 | ..\Resources\f128.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 123 | 124 | 125 | ..\Resources\f16.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 126 | 127 | 128 | ..\Resources\f20.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 129 | 130 | 131 | ..\Resources\f24.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 132 | 133 | -------------------------------------------------------------------------------- /TcpUdpForwarder/Resources/f128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GangZhuo/TcpUdpForwarder/1b3ce2fa841280c9334be8fc2c7ab80b924d9386/TcpUdpForwarder/Resources/f128.png -------------------------------------------------------------------------------- /TcpUdpForwarder/Resources/f16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GangZhuo/TcpUdpForwarder/1b3ce2fa841280c9334be8fc2c7ab80b924d9386/TcpUdpForwarder/Resources/f16.png -------------------------------------------------------------------------------- /TcpUdpForwarder/Resources/f20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GangZhuo/TcpUdpForwarder/1b3ce2fa841280c9334be8fc2c7ab80b924d9386/TcpUdpForwarder/Resources/f20.png -------------------------------------------------------------------------------- /TcpUdpForwarder/Resources/f24.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GangZhuo/TcpUdpForwarder/1b3ce2fa841280c9334be8fc2c7ab80b924d9386/TcpUdpForwarder/Resources/f24.png -------------------------------------------------------------------------------- /TcpUdpForwarder/Services/ProjectInstaller.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace TcpUdpForwarder 2 | { 3 | partial class ProjectInstaller 4 | { 5 | /// 6 | /// Required designer variable. 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// Clean up any resources being used. 12 | /// 13 | /// true if managed resources should be disposed; otherwise, false. 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region Component Designer generated code 24 | 25 | /// 26 | /// Required method for Designer support - do not modify 27 | /// the contents of this method with the code editor. 28 | /// 29 | private void InitializeComponent() 30 | { 31 | this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller(); 32 | this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller(); 33 | // 34 | // serviceProcessInstaller1 35 | // 36 | this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.NetworkService; 37 | this.serviceProcessInstaller1.Password = null; 38 | this.serviceProcessInstaller1.Username = null; 39 | // 40 | // serviceInstaller1 41 | // 42 | this.serviceInstaller1.Description = "Forward the tcp (udp)"; 43 | this.serviceInstaller1.DisplayName = "TCP (UDP) Forwarder"; 44 | this.serviceInstaller1.ServiceName = "TcpUdpForwarder"; 45 | // 46 | // ProjectInstaller 47 | // 48 | this.Installers.AddRange(new System.Configuration.Install.Installer[] { 49 | this.serviceProcessInstaller1, 50 | this.serviceInstaller1}); 51 | 52 | } 53 | 54 | #endregion 55 | 56 | private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1; 57 | private System.ServiceProcess.ServiceInstaller serviceInstaller1; 58 | } 59 | } -------------------------------------------------------------------------------- /TcpUdpForwarder/Services/ProjectInstaller.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.ComponentModel; 5 | using System.Configuration.Install; 6 | using System.Text; 7 | 8 | namespace TcpUdpForwarder 9 | { 10 | [RunInstaller(true)] 11 | public partial class ProjectInstaller : System.Configuration.Install.Installer 12 | { 13 | public ProjectInstaller() 14 | { 15 | InitializeComponent(); 16 | } 17 | 18 | protected override void OnBeforeInstall(IDictionary savedState) 19 | { 20 | string parameter = "--svc"; 21 | var assemblyPath = Context.Parameters["assemblypath"]; 22 | assemblyPath += "\" \"" + parameter; 23 | Context.Parameters["assemblypath"] = assemblyPath; 24 | base.OnBeforeInstall(savedState); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /TcpUdpForwarder/Services/ProjectInstaller.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | 121 | 17, 56 122 | 123 | 124 | 196, 17 125 | 126 | 127 | False 128 | 129 | -------------------------------------------------------------------------------- /TcpUdpForwarder/Services/TcpUdpForwarder.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace TcpUdpForwarder.Services 2 | { 3 | partial class TcpUdpForwarder 4 | { 5 | /// 6 | /// Required designer variable. 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// Clean up any resources being used. 12 | /// 13 | /// true if managed resources should be disposed; otherwise, false. 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region Component Designer generated code 24 | 25 | /// 26 | /// Required method for Designer support - do not modify 27 | /// the contents of this method with the code editor. 28 | /// 29 | private void InitializeComponent() 30 | { 31 | components = new System.ComponentModel.Container(); 32 | this.ServiceName = "TcpUdpForwarder"; 33 | } 34 | 35 | #endregion 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /TcpUdpForwarder/Services/TcpUdpForwarder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Data; 5 | using System.Diagnostics; 6 | using System.ServiceProcess; 7 | using System.Text; 8 | 9 | using TcpUdpForwarder.Controller; 10 | 11 | namespace TcpUdpForwarder.Services 12 | { 13 | partial class TcpUdpForwarder : ServiceBase 14 | { 15 | ForwarderController controller; 16 | 17 | public TcpUdpForwarder() 18 | { 19 | InitializeComponent(); 20 | } 21 | 22 | protected override void OnStart(string[] args) 23 | { 24 | controller = new ForwarderController(true); 25 | controller.Start(); 26 | } 27 | 28 | protected override void OnStop() 29 | { 30 | controller.Stop(); 31 | controller = null; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /TcpUdpForwarder/TcpUdpForwarder.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {1EF4C8DC-F5AB-4D95-B9BB-C3EF38823F2B} 8 | WinExe 9 | Properties 10 | TcpUdpForwarder 11 | TcpUdpForwarder 12 | v2.0 13 | 512 14 | 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | Component 61 | 62 | 63 | ProjectInstaller.cs 64 | 65 | 66 | 67 | True 68 | True 69 | Resources.resx 70 | 71 | 72 | 73 | 74 | Component 75 | 76 | 77 | TcpUdpForwarder.cs 78 | 79 | 80 | 81 | Form 82 | 83 | 84 | ConfigForm.cs 85 | 86 | 87 | 88 | 89 | 90 | ProjectInstaller.cs 91 | 92 | 93 | ResXFileCodeGenerator 94 | Resources.Designer.cs 95 | 96 | 97 | ConfigForm.cs 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | PreserveNewest 110 | 111 | 112 | PreserveNewest 113 | 114 | 115 | 116 | 123 | -------------------------------------------------------------------------------- /TcpUdpForwarder/Utils/Utils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Net; 3 | using System.Net.NetworkInformation; 4 | using System.Diagnostics; 5 | using System.Runtime.InteropServices; 6 | 7 | namespace TcpUdpForwarder.Utils 8 | { 9 | public class Utils 10 | { 11 | public static void ReleaseMemory() 12 | { 13 | // release any unused pages 14 | // making the numbers look good in task manager 15 | // this is totally nonsense in programming 16 | // but good for those users who care 17 | // making them happier with their everyday life 18 | // which is part of user experience 19 | GC.Collect(GC.MaxGeneration); 20 | GC.WaitForPendingFinalizers(); 21 | SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, 22 | (UIntPtr)0xFFFFFFFF, (UIntPtr)0xFFFFFFFF); 23 | } 24 | 25 | public static bool CheckIfTcpPortInUse(int port) 26 | { 27 | IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties(); 28 | IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners(); 29 | 30 | foreach (IPEndPoint endPoint in ipEndPoints) 31 | { 32 | if (endPoint.Port == port) 33 | { 34 | return true; 35 | } 36 | } 37 | return false; 38 | } 39 | 40 | public static bool CheckIfUdpPortInUse(int port) 41 | { 42 | IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties(); 43 | IPEndPoint[] ipEndPoints = ipProperties.GetActiveUdpListeners(); 44 | 45 | foreach (IPEndPoint endPoint in ipEndPoints) 46 | { 47 | if (endPoint.Port == port) 48 | { 49 | return true; 50 | } 51 | } 52 | return false; 53 | } 54 | 55 | [DllImport("kernel32.dll")] 56 | [return: MarshalAs(UnmanagedType.Bool)] 57 | private static extern bool SetProcessWorkingSetSize(IntPtr process, 58 | UIntPtr minimumWorkingSetSize, UIntPtr maximumWorkingSetSize); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /TcpUdpForwarder/View/ConfigForm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Drawing; 5 | using System.Text; 6 | using System.Windows.Forms; 7 | using System.Diagnostics; 8 | using Microsoft.Win32; 9 | 10 | using TcpUdpForwarder.Model; 11 | using TcpUdpForwarder.Controller; 12 | using TcpUdpForwarder.Properties; 13 | 14 | namespace TcpUdpForwarder.View 15 | { 16 | public partial class ConfigForm : Form 17 | { 18 | private ForwarderController controller; 19 | 20 | // this is a copy of configuration that we are working on 21 | private Config _modifiedConfiguration; 22 | private int _oldSelectedIndex = -1; 23 | 24 | public ConfigForm(ForwarderController controller) 25 | { 26 | this.Font = System.Drawing.SystemFonts.MessageBoxFont; 27 | InitializeComponent(); 28 | 29 | // a dirty hack 30 | this.ServersListBox.Dock = System.Windows.Forms.DockStyle.Fill; 31 | this.PerformLayout(); 32 | 33 | this.Icon = Icon.FromHandle(Resources.f128.GetHicon()); 34 | 35 | this.controller = controller; 36 | controller.ConfigChanged += controller_ConfigChanged; 37 | 38 | LoadCurrentConfiguration(); 39 | } 40 | 41 | private void controller_ConfigChanged(object sender, EventArgs e) 42 | { 43 | LoadCurrentConfiguration(); 44 | } 45 | 46 | private void ShowWindow() 47 | { 48 | this.Opacity = 1; 49 | this.Show(); 50 | IPTextBox.Focus(); 51 | } 52 | 53 | private bool SaveOldSelectedServer() 54 | { 55 | try 56 | { 57 | if (_oldSelectedIndex == -1 || _oldSelectedIndex >= _modifiedConfiguration.servers.Count) 58 | { 59 | return true; 60 | } 61 | ServerInfo server = new ServerInfo 62 | { 63 | server = IPTextBox.Text, 64 | serverPort = int.Parse(ServerPortTextBox.Text), 65 | localPort = int.Parse(BindPortTextBox.Text), 66 | }; 67 | Config.CheckServer(server); 68 | _modifiedConfiguration.servers[_oldSelectedIndex] = server; 69 | 70 | return true; 71 | } 72 | catch (FormatException) 73 | { 74 | MessageBox.Show("Illegal port number format"); 75 | } 76 | catch (Exception ex) 77 | { 78 | MessageBox.Show(ex.Message); 79 | } 80 | return false; 81 | } 82 | 83 | private void LoadSelectedServer() 84 | { 85 | if (ServersListBox.SelectedIndex >= 0 && ServersListBox.SelectedIndex < _modifiedConfiguration.servers.Count) 86 | { 87 | ServerInfo server = _modifiedConfiguration.servers[ServersListBox.SelectedIndex]; 88 | 89 | IPTextBox.Text = server.server; 90 | ServerPortTextBox.Text = server.serverPort.ToString(); 91 | BindPortTextBox.Text = server.localPort.ToString(); 92 | ServerGroupBox.Visible = true; 93 | } 94 | else 95 | { 96 | ServerGroupBox.Visible = false; 97 | } 98 | } 99 | 100 | private void LoadConfiguration(Config configuration) 101 | { 102 | ServersListBox.Items.Clear(); 103 | foreach (ServerInfo server in _modifiedConfiguration.servers) 104 | { 105 | ServersListBox.Items.Add(server.FriendlyName()); 106 | } 107 | } 108 | 109 | private void LoadCurrentConfiguration() 110 | { 111 | _modifiedConfiguration = controller.GetConfiguration(); 112 | LoadConfiguration(_modifiedConfiguration); 113 | _oldSelectedIndex = _modifiedConfiguration.index; 114 | ServersListBox.SelectedIndex = _modifiedConfiguration.index; 115 | LoadSelectedServer(); 116 | } 117 | 118 | private void ConfigForm_Load(object sender, EventArgs e) 119 | { 120 | 121 | } 122 | 123 | private void ServersListBox_SelectedIndexChanged(object sender, EventArgs e) 124 | { 125 | if (_oldSelectedIndex == ServersListBox.SelectedIndex) 126 | { 127 | // we are moving back to oldSelectedIndex or doing a force move 128 | return; 129 | } 130 | if (!SaveOldSelectedServer()) 131 | { 132 | // why this won't cause stack overflow? 133 | ServersListBox.SelectedIndex = _oldSelectedIndex; 134 | return; 135 | } 136 | LoadSelectedServer(); 137 | _oldSelectedIndex = ServersListBox.SelectedIndex; 138 | } 139 | 140 | private void AddButton_Click(object sender, EventArgs e) 141 | { 142 | if (!SaveOldSelectedServer()) 143 | { 144 | return; 145 | } 146 | ServerInfo server = Config.GetDefaultServer(); 147 | _modifiedConfiguration.servers.Add(server); 148 | LoadConfiguration(_modifiedConfiguration); 149 | ServersListBox.SelectedIndex = _modifiedConfiguration.servers.Count - 1; 150 | _oldSelectedIndex = ServersListBox.SelectedIndex; 151 | } 152 | 153 | private void DeleteButton_Click(object sender, EventArgs e) 154 | { 155 | _oldSelectedIndex = ServersListBox.SelectedIndex; 156 | if (_oldSelectedIndex >= 0 && _oldSelectedIndex < _modifiedConfiguration.servers.Count) 157 | { 158 | _modifiedConfiguration.servers.RemoveAt(_oldSelectedIndex); 159 | } 160 | if (_oldSelectedIndex >= _modifiedConfiguration.servers.Count) 161 | { 162 | // can be -1 163 | _oldSelectedIndex = _modifiedConfiguration.servers.Count - 1; 164 | } 165 | ServersListBox.SelectedIndex = _oldSelectedIndex; 166 | LoadConfiguration(_modifiedConfiguration); 167 | ServersListBox.SelectedIndex = _oldSelectedIndex; 168 | LoadSelectedServer(); 169 | } 170 | 171 | private void OKButton_Click(object sender, EventArgs e) 172 | { 173 | if (!SaveOldSelectedServer()) 174 | { 175 | return; 176 | } 177 | if (_modifiedConfiguration.servers.Count == 0) 178 | { 179 | MessageBox.Show("Please add at least one server"); 180 | return; 181 | } 182 | controller.SaveServers(_modifiedConfiguration.servers); 183 | this.Close(); 184 | } 185 | 186 | private void CancelButton_Click(object sender, EventArgs e) 187 | { 188 | this.Close(); 189 | } 190 | 191 | private void ConfigForm_Shown(object sender, EventArgs e) 192 | { 193 | IPTextBox.Focus(); 194 | } 195 | 196 | private void ConfigForm_FormClosed(object sender, FormClosedEventArgs e) 197 | { 198 | controller.ConfigChanged -= controller_ConfigChanged; 199 | } 200 | 201 | } 202 | } 203 | -------------------------------------------------------------------------------- /TcpUdpForwarder/View/ConfigForm.designer.cs: -------------------------------------------------------------------------------- 1 | namespace TcpUdpForwarder.View 2 | { 3 | partial class ConfigForm 4 | { 5 | /// 6 | /// 必需的设计器变量。 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// 清理所有正在使用的资源。 12 | /// 13 | /// 如果应释放托管资源,为 true;否则为 false。 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region Windows 窗体设计器生成的代码 24 | 25 | /// 26 | /// 设计器支持所需的方法 - 不要 27 | /// 使用代码编辑器修改此方法的内容。 28 | /// 29 | private void InitializeComponent() 30 | { 31 | this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); 32 | this.BindPortLabel = new System.Windows.Forms.Label(); 33 | this.IPLabel = new System.Windows.Forms.Label(); 34 | this.ServerPortLabel = new System.Windows.Forms.Label(); 35 | this.IPTextBox = new System.Windows.Forms.TextBox(); 36 | this.ServerPortTextBox = new System.Windows.Forms.TextBox(); 37 | this.BindPortTextBox = new System.Windows.Forms.TextBox(); 38 | this.panel2 = new System.Windows.Forms.Panel(); 39 | this.OKButton = new System.Windows.Forms.Button(); 40 | this.MyCancelButton = new System.Windows.Forms.Button(); 41 | this.DeleteButton = new System.Windows.Forms.Button(); 42 | this.AddButton = new System.Windows.Forms.Button(); 43 | this.ServerGroupBox = new System.Windows.Forms.GroupBox(); 44 | this.ServersListBox = new System.Windows.Forms.ListBox(); 45 | this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel(); 46 | this.tableLayoutPanel3 = new System.Windows.Forms.TableLayoutPanel(); 47 | this.tableLayoutPanel4 = new System.Windows.Forms.TableLayoutPanel(); 48 | this.tableLayoutPanel1.SuspendLayout(); 49 | this.ServerGroupBox.SuspendLayout(); 50 | this.tableLayoutPanel2.SuspendLayout(); 51 | this.tableLayoutPanel3.SuspendLayout(); 52 | this.tableLayoutPanel4.SuspendLayout(); 53 | this.SuspendLayout(); 54 | // 55 | // tableLayoutPanel1 56 | // 57 | this.tableLayoutPanel1.AutoSize = true; 58 | this.tableLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; 59 | this.tableLayoutPanel1.ColumnCount = 2; 60 | this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); 61 | this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); 62 | this.tableLayoutPanel1.Controls.Add(this.BindPortLabel, 0, 3); 63 | this.tableLayoutPanel1.Controls.Add(this.IPLabel, 0, 0); 64 | this.tableLayoutPanel1.Controls.Add(this.ServerPortLabel, 0, 1); 65 | this.tableLayoutPanel1.Controls.Add(this.IPTextBox, 1, 0); 66 | this.tableLayoutPanel1.Controls.Add(this.ServerPortTextBox, 1, 1); 67 | this.tableLayoutPanel1.Controls.Add(this.BindPortTextBox, 1, 3); 68 | this.tableLayoutPanel1.Location = new System.Drawing.Point(8, 21); 69 | this.tableLayoutPanel1.Margin = new System.Windows.Forms.Padding(0); 70 | this.tableLayoutPanel1.Name = "tableLayoutPanel1"; 71 | this.tableLayoutPanel1.Padding = new System.Windows.Forms.Padding(3); 72 | this.tableLayoutPanel1.RowCount = 4; 73 | this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); 74 | this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); 75 | this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); 76 | this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); 77 | this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F)); 78 | this.tableLayoutPanel1.Size = new System.Drawing.Size(238, 84); 79 | this.tableLayoutPanel1.TabIndex = 0; 80 | // 81 | // BindPortLabel 82 | // 83 | this.BindPortLabel.Anchor = System.Windows.Forms.AnchorStyles.Right; 84 | this.BindPortLabel.AutoSize = true; 85 | this.BindPortLabel.Location = new System.Drawing.Point(16, 61); 86 | this.BindPortLabel.Name = "BindPortLabel"; 87 | this.BindPortLabel.Size = new System.Drawing.Size(50, 13); 88 | this.BindPortLabel.TabIndex = 9; 89 | this.BindPortLabel.Text = "Bind Port"; 90 | // 91 | // IPLabel 92 | // 93 | this.IPLabel.Anchor = System.Windows.Forms.AnchorStyles.Right; 94 | this.IPLabel.AutoSize = true; 95 | this.IPLabel.Location = new System.Drawing.Point(15, 9); 96 | this.IPLabel.Name = "IPLabel"; 97 | this.IPLabel.Size = new System.Drawing.Size(51, 13); 98 | this.IPLabel.TabIndex = 0; 99 | this.IPLabel.Text = "Server IP"; 100 | // 101 | // ServerPortLabel 102 | // 103 | this.ServerPortLabel.Anchor = System.Windows.Forms.AnchorStyles.Right; 104 | this.ServerPortLabel.AutoSize = true; 105 | this.ServerPortLabel.Location = new System.Drawing.Point(6, 35); 106 | this.ServerPortLabel.Name = "ServerPortLabel"; 107 | this.ServerPortLabel.Size = new System.Drawing.Size(60, 13); 108 | this.ServerPortLabel.TabIndex = 1; 109 | this.ServerPortLabel.Text = "Server Port"; 110 | // 111 | // IPTextBox 112 | // 113 | this.IPTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); 114 | this.IPTextBox.Location = new System.Drawing.Point(72, 6); 115 | this.IPTextBox.MaxLength = 512; 116 | this.IPTextBox.Name = "IPTextBox"; 117 | this.IPTextBox.Size = new System.Drawing.Size(160, 20); 118 | this.IPTextBox.TabIndex = 0; 119 | this.IPTextBox.WordWrap = false; 120 | // 121 | // ServerPortTextBox 122 | // 123 | this.ServerPortTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); 124 | this.ServerPortTextBox.Location = new System.Drawing.Point(72, 32); 125 | this.ServerPortTextBox.MaxLength = 10; 126 | this.ServerPortTextBox.Name = "ServerPortTextBox"; 127 | this.ServerPortTextBox.Size = new System.Drawing.Size(160, 20); 128 | this.ServerPortTextBox.TabIndex = 1; 129 | this.ServerPortTextBox.WordWrap = false; 130 | // 131 | // BindPortTextBox 132 | // 133 | this.BindPortTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); 134 | this.BindPortTextBox.Location = new System.Drawing.Point(72, 58); 135 | this.BindPortTextBox.MaxLength = 32; 136 | this.BindPortTextBox.Name = "BindPortTextBox"; 137 | this.BindPortTextBox.Size = new System.Drawing.Size(160, 20); 138 | this.BindPortTextBox.TabIndex = 10; 139 | this.BindPortTextBox.WordWrap = false; 140 | // 141 | // panel2 142 | // 143 | this.panel2.Anchor = System.Windows.Forms.AnchorStyles.Top; 144 | this.panel2.AutoSize = true; 145 | this.panel2.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; 146 | this.panel2.Location = new System.Drawing.Point(207, 187); 147 | this.panel2.Name = "panel2"; 148 | this.panel2.Size = new System.Drawing.Size(0, 0); 149 | this.panel2.TabIndex = 1; 150 | // 151 | // OKButton 152 | // 153 | this.OKButton.DialogResult = System.Windows.Forms.DialogResult.OK; 154 | this.OKButton.Dock = System.Windows.Forms.DockStyle.Right; 155 | this.OKButton.Location = new System.Drawing.Point(3, 3); 156 | this.OKButton.Margin = new System.Windows.Forms.Padding(3, 3, 3, 0); 157 | this.OKButton.Name = "OKButton"; 158 | this.OKButton.Size = new System.Drawing.Size(75, 23); 159 | this.OKButton.TabIndex = 8; 160 | this.OKButton.Text = "OK"; 161 | this.OKButton.UseVisualStyleBackColor = true; 162 | this.OKButton.Click += new System.EventHandler(this.OKButton_Click); 163 | // 164 | // MyCancelButton 165 | // 166 | this.MyCancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel; 167 | this.MyCancelButton.Dock = System.Windows.Forms.DockStyle.Right; 168 | this.MyCancelButton.Location = new System.Drawing.Point(84, 3); 169 | this.MyCancelButton.Margin = new System.Windows.Forms.Padding(3, 3, 0, 0); 170 | this.MyCancelButton.Name = "MyCancelButton"; 171 | this.MyCancelButton.Size = new System.Drawing.Size(75, 23); 172 | this.MyCancelButton.TabIndex = 9; 173 | this.MyCancelButton.Text = "Cancel"; 174 | this.MyCancelButton.UseVisualStyleBackColor = true; 175 | this.MyCancelButton.Click += new System.EventHandler(this.CancelButton_Click); 176 | // 177 | // DeleteButton 178 | // 179 | this.DeleteButton.Dock = System.Windows.Forms.DockStyle.Right; 180 | this.DeleteButton.Location = new System.Drawing.Point(86, 6); 181 | this.DeleteButton.Margin = new System.Windows.Forms.Padding(3, 6, 0, 3); 182 | this.DeleteButton.Name = "DeleteButton"; 183 | this.DeleteButton.Size = new System.Drawing.Size(80, 23); 184 | this.DeleteButton.TabIndex = 7; 185 | this.DeleteButton.Text = "&Delete"; 186 | this.DeleteButton.UseVisualStyleBackColor = true; 187 | this.DeleteButton.Click += new System.EventHandler(this.DeleteButton_Click); 188 | // 189 | // AddButton 190 | // 191 | this.AddButton.Dock = System.Windows.Forms.DockStyle.Left; 192 | this.AddButton.Location = new System.Drawing.Point(0, 6); 193 | this.AddButton.Margin = new System.Windows.Forms.Padding(0, 6, 3, 3); 194 | this.AddButton.Name = "AddButton"; 195 | this.AddButton.Size = new System.Drawing.Size(80, 23); 196 | this.AddButton.TabIndex = 6; 197 | this.AddButton.Text = "&Add"; 198 | this.AddButton.UseVisualStyleBackColor = true; 199 | this.AddButton.Click += new System.EventHandler(this.AddButton_Click); 200 | // 201 | // ServerGroupBox 202 | // 203 | this.ServerGroupBox.AutoSize = true; 204 | this.ServerGroupBox.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; 205 | this.ServerGroupBox.Controls.Add(this.tableLayoutPanel1); 206 | this.ServerGroupBox.Location = new System.Drawing.Point(178, 0); 207 | this.ServerGroupBox.Margin = new System.Windows.Forms.Padding(12, 0, 0, 0); 208 | this.ServerGroupBox.Name = "ServerGroupBox"; 209 | this.ServerGroupBox.Size = new System.Drawing.Size(249, 121); 210 | this.ServerGroupBox.TabIndex = 6; 211 | this.ServerGroupBox.TabStop = false; 212 | this.ServerGroupBox.Text = "Server"; 213 | // 214 | // ServersListBox 215 | // 216 | this.ServersListBox.FormattingEnabled = true; 217 | this.ServersListBox.IntegralHeight = false; 218 | this.ServersListBox.Location = new System.Drawing.Point(0, 0); 219 | this.ServersListBox.Margin = new System.Windows.Forms.Padding(0); 220 | this.ServersListBox.Name = "ServersListBox"; 221 | this.ServersListBox.Size = new System.Drawing.Size(166, 148); 222 | this.ServersListBox.TabIndex = 5; 223 | this.ServersListBox.SelectedIndexChanged += new System.EventHandler(this.ServersListBox_SelectedIndexChanged); 224 | // 225 | // tableLayoutPanel2 226 | // 227 | this.tableLayoutPanel2.AutoSize = true; 228 | this.tableLayoutPanel2.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; 229 | this.tableLayoutPanel2.ColumnCount = 2; 230 | this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); 231 | this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); 232 | this.tableLayoutPanel2.Controls.Add(this.tableLayoutPanel3, 1, 2); 233 | this.tableLayoutPanel2.Controls.Add(this.ServersListBox, 0, 0); 234 | this.tableLayoutPanel2.Controls.Add(this.ServerGroupBox, 1, 0); 235 | this.tableLayoutPanel2.Controls.Add(this.tableLayoutPanel4, 0, 1); 236 | this.tableLayoutPanel2.Location = new System.Drawing.Point(12, 12); 237 | this.tableLayoutPanel2.Margin = new System.Windows.Forms.Padding(0); 238 | this.tableLayoutPanel2.Name = "tableLayoutPanel2"; 239 | this.tableLayoutPanel2.RowCount = 3; 240 | this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle()); 241 | this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle()); 242 | this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle()); 243 | this.tableLayoutPanel2.Size = new System.Drawing.Size(427, 212); 244 | this.tableLayoutPanel2.TabIndex = 7; 245 | // 246 | // tableLayoutPanel3 247 | // 248 | this.tableLayoutPanel3.AutoSize = true; 249 | this.tableLayoutPanel3.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; 250 | this.tableLayoutPanel3.ColumnCount = 2; 251 | this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); 252 | this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); 253 | this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F)); 254 | this.tableLayoutPanel3.Controls.Add(this.MyCancelButton, 1, 0); 255 | this.tableLayoutPanel3.Controls.Add(this.OKButton, 0, 0); 256 | this.tableLayoutPanel3.Dock = System.Windows.Forms.DockStyle.Right; 257 | this.tableLayoutPanel3.Location = new System.Drawing.Point(268, 183); 258 | this.tableLayoutPanel3.Margin = new System.Windows.Forms.Padding(3, 3, 0, 3); 259 | this.tableLayoutPanel3.Name = "tableLayoutPanel3"; 260 | this.tableLayoutPanel3.RowCount = 1; 261 | this.tableLayoutPanel3.RowStyles.Add(new System.Windows.Forms.RowStyle()); 262 | this.tableLayoutPanel3.Size = new System.Drawing.Size(159, 26); 263 | this.tableLayoutPanel3.TabIndex = 8; 264 | // 265 | // tableLayoutPanel4 266 | // 267 | this.tableLayoutPanel4.AutoSize = true; 268 | this.tableLayoutPanel4.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; 269 | this.tableLayoutPanel4.ColumnCount = 2; 270 | this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); 271 | this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); 272 | this.tableLayoutPanel4.Controls.Add(this.DeleteButton, 1, 0); 273 | this.tableLayoutPanel4.Controls.Add(this.AddButton, 0, 0); 274 | this.tableLayoutPanel4.Dock = System.Windows.Forms.DockStyle.Top; 275 | this.tableLayoutPanel4.Location = new System.Drawing.Point(0, 148); 276 | this.tableLayoutPanel4.Margin = new System.Windows.Forms.Padding(0); 277 | this.tableLayoutPanel4.Name = "tableLayoutPanel4"; 278 | this.tableLayoutPanel4.RowCount = 1; 279 | this.tableLayoutPanel4.RowStyles.Add(new System.Windows.Forms.RowStyle()); 280 | this.tableLayoutPanel4.Size = new System.Drawing.Size(166, 32); 281 | this.tableLayoutPanel4.TabIndex = 8; 282 | // 283 | // ConfigForm 284 | // 285 | this.AcceptButton = this.OKButton; 286 | this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); 287 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; 288 | this.AutoSize = true; 289 | this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; 290 | this.CancelButton = this.MyCancelButton; 291 | this.ClientSize = new System.Drawing.Size(574, 367); 292 | this.Controls.Add(this.tableLayoutPanel2); 293 | this.Controls.Add(this.panel2); 294 | this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; 295 | this.MaximizeBox = false; 296 | this.MinimizeBox = false; 297 | this.Name = "ConfigForm"; 298 | this.Padding = new System.Windows.Forms.Padding(12, 12, 12, 9); 299 | this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 300 | this.Text = "Edit Servers"; 301 | this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.ConfigForm_FormClosed); 302 | this.Load += new System.EventHandler(this.ConfigForm_Load); 303 | this.Shown += new System.EventHandler(this.ConfigForm_Shown); 304 | this.tableLayoutPanel1.ResumeLayout(false); 305 | this.tableLayoutPanel1.PerformLayout(); 306 | this.ServerGroupBox.ResumeLayout(false); 307 | this.ServerGroupBox.PerformLayout(); 308 | this.tableLayoutPanel2.ResumeLayout(false); 309 | this.tableLayoutPanel2.PerformLayout(); 310 | this.tableLayoutPanel3.ResumeLayout(false); 311 | this.tableLayoutPanel4.ResumeLayout(false); 312 | this.ResumeLayout(false); 313 | this.PerformLayout(); 314 | 315 | } 316 | 317 | #endregion 318 | 319 | private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; 320 | private System.Windows.Forms.Label IPLabel; 321 | private System.Windows.Forms.Label ServerPortLabel; 322 | private System.Windows.Forms.TextBox IPTextBox; 323 | private System.Windows.Forms.TextBox ServerPortTextBox; 324 | private System.Windows.Forms.Panel panel2; 325 | private System.Windows.Forms.Button OKButton; 326 | private System.Windows.Forms.Button MyCancelButton; 327 | private System.Windows.Forms.Button DeleteButton; 328 | private System.Windows.Forms.Button AddButton; 329 | private System.Windows.Forms.GroupBox ServerGroupBox; 330 | private System.Windows.Forms.ListBox ServersListBox; 331 | private System.Windows.Forms.TextBox BindPortTextBox; 332 | private System.Windows.Forms.Label BindPortLabel; 333 | private System.Windows.Forms.TableLayoutPanel tableLayoutPanel2; 334 | private System.Windows.Forms.TableLayoutPanel tableLayoutPanel3; 335 | private System.Windows.Forms.TableLayoutPanel tableLayoutPanel4; 336 | } 337 | } 338 | 339 | -------------------------------------------------------------------------------- /TcpUdpForwarder/View/ConfigForm.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | -------------------------------------------------------------------------------- /TcpUdpForwarder/View/SSForwardView.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Drawing; 4 | using System.Windows.Forms; 5 | 6 | using TcpUdpForwarder.Model; 7 | using TcpUdpForwarder.Controller; 8 | using TcpUdpForwarder.Properties; 9 | 10 | namespace TcpUdpForwarder.View 11 | { 12 | public class SSForwardView 13 | { 14 | private ForwarderController _controller; 15 | private NotifyIcon _notifyIcon; 16 | private ContextMenu _contextMenu; 17 | private MenuItem _enableItem; 18 | private MenuItem _serversItem; 19 | private MenuItem _serversGroupSeperatorItem; 20 | private MenuItem _editServersItem; 21 | private ConfigForm _configForm; 22 | private bool _isFirstRun; 23 | 24 | public SSForwardView(ForwarderController controller) 25 | { 26 | _controller = controller; 27 | LoadMenu(); 28 | 29 | _controller.EnableStatusChanged += _controller_EnableStatusChanged; 30 | _controller.ConfigChanged += _controller_ConfigChanged; 31 | _controller.Errored += _controller_Errored; 32 | 33 | _notifyIcon = new NotifyIcon(); 34 | UpdateTrayIcon(); 35 | _notifyIcon.Visible = true; 36 | _notifyIcon.ContextMenu = _contextMenu; 37 | 38 | LoadCurrentConfiguration(); 39 | if (controller.GetConfiguration().isDefault) 40 | { 41 | _isFirstRun = true; 42 | ShowConfigForm(); 43 | } 44 | } 45 | 46 | private void _controller_EnableStatusChanged(object sender, EventArgs e) 47 | { 48 | _enableItem.Checked = _controller.GetConfiguration().enabled; 49 | } 50 | 51 | private void _controller_ConfigChanged(object sender, EventArgs e) 52 | { 53 | LoadCurrentConfiguration(); 54 | UpdateTrayIcon(); 55 | } 56 | 57 | private void _controller_Errored(object sender, System.IO.ErrorEventArgs e) 58 | { 59 | ShowBalloon("TCP(UDP) forwarder error", e.GetException().Message, ToolTipIcon.Error); 60 | //MessageBox.Show(e.GetException().ToString(), String.Format("TCP(UDP) forwarder Error: {0}", e.GetException().Message)); 61 | } 62 | 63 | private void ShowFirstTimeBalloon() 64 | { 65 | if (_isFirstRun) 66 | { 67 | ShowBalloon("TCP(UDP) forwarder is here", "You can turn on/off in the context menu", ToolTipIcon.Info); 68 | _isFirstRun = false; 69 | } 70 | } 71 | 72 | private void ShowBalloon(string title, string text, ToolTipIcon icon = ToolTipIcon.Info) 73 | { 74 | _notifyIcon.BalloonTipTitle = title; 75 | _notifyIcon.BalloonTipText = text; 76 | _notifyIcon.BalloonTipIcon = icon; 77 | _notifyIcon.ShowBalloonTip(0); 78 | } 79 | 80 | private void LoadCurrentConfiguration() 81 | { 82 | Config config = _controller.GetConfiguration(); 83 | UpdateServersMenu(); 84 | _enableItem.Checked = config.enabled; 85 | } 86 | 87 | private void UpdateServersMenu() 88 | { 89 | var items = _serversItem.MenuItems; 90 | while (items[0] != _serversGroupSeperatorItem) 91 | { 92 | items.RemoveAt(0); 93 | } 94 | 95 | Config configuration = _controller.GetConfiguration(); 96 | for (int i = 0; i < configuration.servers.Count; i++) 97 | { 98 | ServerInfo server = configuration.servers[i]; 99 | MenuItem item = new MenuItem(server.FriendlyName()); 100 | item.Tag = i; 101 | item.Click += AServerItem_Click; 102 | items.Add(i, item); 103 | } 104 | 105 | if (configuration.index >= 0 && configuration.index < configuration.servers.Count) 106 | { 107 | items[configuration.index].Checked = true; 108 | } 109 | } 110 | 111 | private void ShowConfigForm() 112 | { 113 | if (_configForm != null) 114 | { 115 | _configForm.Activate(); 116 | } 117 | else 118 | { 119 | _configForm = new ConfigForm(_controller); 120 | _configForm.Show(); 121 | _configForm.FormClosed += _configForm_FormClosed; 122 | } 123 | } 124 | 125 | private void _configForm_FormClosed(object sender, FormClosedEventArgs e) 126 | { 127 | _configForm = null; 128 | ShowFirstTimeBalloon(); 129 | } 130 | 131 | private void UpdateTrayIcon() 132 | { 133 | int dpi; 134 | Graphics graphics = Graphics.FromHwnd(IntPtr.Zero); 135 | dpi = (int)graphics.DpiX; 136 | graphics.Dispose(); 137 | Bitmap icon = null; 138 | if (dpi < 97) 139 | { 140 | // dpi = 96; 141 | icon = Resources.f16; 142 | } 143 | else if (dpi < 121) 144 | { 145 | // dpi = 120; 146 | icon = Resources.f20; 147 | } 148 | else 149 | { 150 | icon = Resources.f24; 151 | } 152 | Config config = _controller.GetConfiguration(); 153 | bool enabled = config.enabled; 154 | if (!enabled) 155 | { 156 | Bitmap iconCopy = new Bitmap(icon); 157 | for (int x = 0; x < iconCopy.Width; x++) 158 | { 159 | for (int y = 0; y < iconCopy.Height; y++) 160 | { 161 | Color color = icon.GetPixel(x, y); 162 | iconCopy.SetPixel(x, y, Color.FromArgb((byte)(color.A / 1.25), color.R, color.G, color.B)); 163 | } 164 | } 165 | icon = iconCopy; 166 | } 167 | _notifyIcon.Icon = Icon.FromHandle(icon.GetHicon()); 168 | 169 | // we want to show more details but notify icon title is limited to 63 characters 170 | string text = "TCP(UDP) forwarder" + " " + ForwarderController.Version 171 | + "\n" + config.GetCurrentServer().FriendlyName(); 172 | _notifyIcon.Text = text.Substring(0, Math.Min(63, text.Length)); 173 | } 174 | 175 | private void LoadMenu() 176 | { 177 | _contextMenu = new ContextMenu(new MenuItem[] { 178 | _enableItem = new MenuItem("Enable", new EventHandler(this.EnableItem_Click)), 179 | new MenuItem("-"), 180 | _serversItem = new MenuItem("Servers", new MenuItem[] { 181 | this._serversGroupSeperatorItem = new MenuItem("-"), 182 | this._editServersItem = new MenuItem("Edit Servers...", new EventHandler(this.EditServersItem_Click)), 183 | }), 184 | new MenuItem("-"), 185 | new MenuItem("Show Logs...", new EventHandler(this.ShowLogItem_Click)), 186 | new MenuItem("About...", new EventHandler(this.AboutItem_Click)), 187 | new MenuItem("-"), 188 | new MenuItem("Quit", new EventHandler(this.Quit_Click)) 189 | }); 190 | } 191 | 192 | private void EnableItem_Click(object sender, EventArgs e) 193 | { 194 | _controller.ToggleEnable(!_enableItem.Checked); 195 | } 196 | 197 | private void AServerItem_Click(object sender, EventArgs e) 198 | { 199 | MenuItem item = (MenuItem)sender; 200 | _controller.SelectServerIndex((int)item.Tag); 201 | } 202 | 203 | private void EditServersItem_Click(object sender, EventArgs e) 204 | { 205 | ShowConfigForm(); 206 | } 207 | 208 | private void ShowLogItem_Click(object sender, EventArgs e) 209 | { 210 | string argument = Logging.LogFile; 211 | System.Diagnostics.Process.Start("notepad.exe", argument); 212 | } 213 | 214 | private void AboutItem_Click(object sender, EventArgs e) 215 | { 216 | try 217 | { 218 | System.Diagnostics.Process.Start("https://github.com/GangZhuo/TcpUdpForwarder"); 219 | } 220 | catch { } 221 | } 222 | 223 | private void Quit_Click(object sender, EventArgs e) 224 | { 225 | _controller.Stop(); 226 | _notifyIcon.Visible = false; 227 | Application.Exit(); 228 | } 229 | 230 | } 231 | } 232 | -------------------------------------------------------------------------------- /TcpUdpForwarder/config.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /TcpUdpForwarder/install.bat: -------------------------------------------------------------------------------- 1 | "C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe" "TcpUdpForwarder.exe" -------------------------------------------------------------------------------- /TcpUdpForwarder/uninstall.bat: -------------------------------------------------------------------------------- 1 | "C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe" "TcpUdpForwarder.exe" "/u" --------------------------------------------------------------------------------