-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPipe.cs
More file actions
71 lines (62 loc) · 2.04 KB
/
TCPipe.cs
File metadata and controls
71 lines (62 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
class TcpProxy
{
static void Main(string[] args)
{
if (args.Length != 3)
{
Console.WriteLine("Usage: TCPipe.exe <listenPort> <remoteHost> <remotePort>");
return;
}
int listenPort = int.Parse(args[0]);
string remoteHost = args[1];
int remotePort = int.Parse(args[2]);
TcpListener listener = new TcpListener(IPAddress.Any, listenPort);
listener.Start();
Console.WriteLine($"Listening on 0.0.0.0:{listenPort} -> {remoteHost}:{remotePort}");
while (true)
{
TcpClient client = listener.AcceptTcpClient();
Console.WriteLine($"Client connected: {client.Client.RemoteEndPoint}");
Task.Run(() => HandleClient(client, remoteHost, remotePort));
}
}
static void HandleClient(TcpClient client, string remoteHost, int remotePort)
{
try
{
TcpClient remote = new TcpClient(remoteHost, remotePort);
NetworkStream clientStream = client.GetStream();
NetworkStream remoteStream = remote.GetStream();
// Bidirectional forwarding
Task t1 = Task.Run(() => CopyStream(clientStream, remoteStream));
Task t2 = Task.Run(() => CopyStream(remoteStream, clientStream));
Task.WaitAll(t1, t2);
client.Close();
remote.Close();
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
try { client.Close(); } catch { }
}
}
static void CopyStream(NetworkStream from, NetworkStream to)
{
byte[] buffer = new byte[8192];
try
{
int bytesRead;
while ((bytesRead = from.Read(buffer, 0, buffer.Length)) > 0)
{
to.Write(buffer, 0, bytesRead);
to.Flush();
}
}
catch { } // Ignore errors (closed stream, etc.)
}
}