-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
229 lines (210 loc) · 7.49 KB
/
Program.cs
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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections.Concurrent;
/***
*
* [ host (clojure socket repl) ] <---> [ local (PassThroughReplServer) ] <---> [ client (editor/user) ]
*
*/
namespace ReplProxyServer
{
class Server
{
const int BUFFER_SIZE = 0x1000;
TcpListener localListener;
string hostAddress;
int hostPort;
static IPAddress GetAddress(string host)
{
var addresses = Dns.GetHostAddresses(host);
if (addresses.Length > 0)
return addresses[0];
return null;
}
/// <summary>
/// Reads from input and writes to output. Blocks.
///
/// Modified from https://stackoverflow.com/questions/129305/how-to-write-the-content-of-one-stream-into-another-stream-in-net
/// </summary>
/// <returns>The number of bytes copied.</returns>
/// <param name="input">Input stream</param>
/// <param name="output">Output stream</param>
static int CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
bytesRead = input.Read(buffer, 0, buffer.Length);
output.Write(buffer, 0, bytesRead);
return bytesRead;
}
ConcurrentBag<Socket> sockets;
bool running = true;
public Server(int localPort, string localAddress = "127.0.0.1", int hostPort = 5555, string hostAddress = "127.0.0.1")
{
localListener = new TcpListener(GetAddress(localAddress), localPort);
this.hostAddress = hostAddress;
this.hostPort = hostPort;
this.sockets = new ConcurrentBag<Socket>();
}
/// <summary>
/// Spawn threads to copy bytes arriving from fromClient to toClient
/// </summary>
/// <param name="fromClient">Source client</param>
/// <param name="toClient">Destination client</param>
void PipeClients(TcpClient fromClient, TcpClient toClient)
{
new Thread(() =>
{
try
{
int b;
while (fromClient.Connected && toClient.Connected)
{
b = CopyStream(fromClient.GetStream(), toClient.GetStream());
if (b == 0)
break;
}
if (!fromClient.Connected)
Console.WriteLine(fromClient.Client.LocalEndPoint + " disconnected");
if (!toClient.Connected)
Console.WriteLine(toClient.Client.LocalEndPoint + " disconnected");
}
catch (IOException)
{
if (!fromClient.Connected)
Console.WriteLine(fromClient.Client.LocalEndPoint + " disconnected");
if (!toClient.Connected)
Console.WriteLine(toClient.Client.LocalEndPoint + " disconnected");
}
}).Start();
}
/// <summary>
/// Spawn worker threads for new connection.
///
/// Loop to reconnect to host if host goes down.
/// </summary>
/// <param name="clientConnection">New client connection</param>
public void AcceptConnection(TcpClient clientConnection)
{
sockets.Add(clientConnection.Client);
Console.WriteLine("New client " + clientConnection.Client.LocalEndPoint);
TcpClient hostClient = new TcpClient();
new Thread(() =>
{
while (running && clientConnection.Connected)
{
if (!hostClient.Connected)
{
try
{
Console.Write("Connecting " + clientConnection.Client.LocalEndPoint + " to " + hostAddress + ":" + hostPort + "... ");
hostClient = new TcpClient();
hostClient.Connect(GetAddress(hostAddress), hostPort);
Console.WriteLine("OK");
sockets.Add(hostClient.Client);
PipeClients(hostClient, clientConnection);
PipeClients(clientConnection, hostClient);
}
catch (SocketException ex)
{
Console.WriteLine(ex.Message);
Thread.Sleep(1000);
}
}
else
{
Thread.Sleep(1000);
}
}
}).Start();
}
/// <summary>
/// Listen for incoming connections and spawn worker threads for them
/// </summary>
public void Listen()
{
try
{
Console.Write("Listening on " + localListener.LocalEndpoint + "... ");
localListener.Start();
Console.WriteLine("OK");
Console.WriteLine("Press Q to quit gracefully");
while (running)
{
AcceptConnection(localListener.AcceptTcpClient());
}
}
catch (SocketException ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("Quitting");
Terminate();
Environment.Exit(1);
return;
}
}
public void Terminate()
{
running = false;
foreach (var socket in sockets)
{
var b = new byte[1];
if (socket.Connected)
{
socket.Shutdown(SocketShutdown.Both);
//while(socket.Receive(b) == 0) { Thread.Sleep(10); }
socket.Close();
}
}
}
}
class Program
{
public static void Usage()
{
Console.WriteLine("ReplProxyServer HOSTADDR HOSTPORT [LOCALHOST [LOCALPORT]]");
Environment.Exit(0);
}
public static void Main(string[] args)
{
string localAddress = "127.0.0.1";
int localPort = 5555;
string hostAddress = "";
int hostPort = 0;
switch (args.Length)
{
case 4:
localPort = int.Parse(args[3]);
goto case 3;
case 3:
localAddress = args[2];
goto case 2;
case 2:
hostPort = int.Parse(args[1]);
hostAddress = args[0];
break;
default:
Usage();
break;
}
var server = new Server(localPort, localAddress, hostPort, hostAddress);
new Thread(() =>
{
while (true)
{
if (Console.ReadKey().Key == ConsoleKey.Q)
{
server.Terminate();
Console.Clear();
Console.WriteLine("See You!");
Environment.Exit(0);
}
}
}).Start();
server.Listen();
}
}
}