-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.cs
242 lines (218 loc) · 4.81 KB
/
Client.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
230
231
232
233
234
235
236
237
238
239
240
241
242
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace TCPRedirect
{
public class Client
{
private const int UNTHROTTLED_BUFFER = 20480;
private const int THROTTLED_BUFFER = 712;
private const int REPORT_INTERVAL = 102400;
private const int THROTTLE = 20;
private TcpClient _Connection;
private string _Destination;
private int _Port;
private bool _Log = false;
private string _LogPath;
private bool _ShowDirectionTags = true;
private bool _Throttle = false;
private int BufferSize = 0;
bool Clean = false;
private bool logLastIn = false;
private TcpClient Forwarder;
NetworkStream sin;
NetworkStream sout;
Thread tIn;
Thread tOut;
#region Property Acessors
public bool Throttle
{
get { return _Throttle; }
set
{
_Throttle = value;
}
}
public TcpClient Connection
{
get { return _Connection; }
set { _Connection = value; }
}
public string Destination
{
get { return _Destination; }
set { _Destination = value; }
}
public int Port
{
get { return _Port; }
set { _Port = value; }
}
public bool Log
{
get { return _Log; }
set { _Log = value; }
}
public string LogPath
{
get { return _LogPath; }
set { _LogPath = value; }
}
public bool ShowDirectionTags
{
get { return _ShowDirectionTags; }
set { _ShowDirectionTags = value; }
}
#endregion
public delegate void DataEventHandler(string Data);
public event DataEventHandler DataIn;
public event DataEventHandler DataOut;
public Client(TcpClient connection, string dest, int port, string logPath, bool throttle)
{
_Connection = connection;
_Destination = dest;
_Port = port;
_Throttle = throttle;
if (logPath != "")
{
_Log = true;
_LogPath = logPath;
}
BufferSize = _Throttle ? THROTTLED_BUFFER : UNTHROTTLED_BUFFER;
}
public void Start()
{
try
{
Forwarder = new TcpClient();
Forwarder.Connect(IPAddress.Parse(_Destination), _Port);
while (true)
{
sin = _Connection.GetStream();
sout = Forwarder.GetStream();
tIn = new Thread(new ThreadStart(InStream));
tOut = new Thread(new ThreadStart(OutStream));
tIn.Start();
tOut.Start();
while(tIn.IsAlive || tOut.IsAlive){ Thread.Sleep(1); }
CleanUp();
Console.WriteLine("Connection Finished");
return;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message + " - " + "Connection Finished");
CleanUp();
}
}
private void InStream()
{
long LastReported = 0;
long Bytes = 0;
byte[] buffer = new byte[BufferSize];
try
{
int Read = 0;
do
{
if (_Throttle) { Thread.Sleep(THROTTLE); }
Read = sin.Read(buffer,0,buffer.Length);
if (Read != 0)
{
sout.Write(buffer,0,Read);
if(_Log){ WriteLog(buffer, Read, _LogPath, true); }
Bytes += Read;
if ((Bytes - LastReported) > REPORT_INTERVAL)
{
Console.WriteLine((Bytes/1024).ToString() + " Kb In");
LastReported = Bytes;
}
OnDataIn(buffer);
}
}
while(Read != 0);
}
catch (Exception e) { }
Console.WriteLine("Client dropped");
}
private void OutStream()
{
long LastReported = 0;
long Bytes = 0;
byte[] buffer = new byte[BufferSize];
try
{
int Read =0;
do
{
if (_Throttle) { Thread.Sleep(THROTTLE); }
Read = sout.Read(buffer,0,buffer.Length);
sin.Write(buffer,0,Read);
if(_Log){ WriteLog(buffer, Read, _LogPath, false); }
Bytes += Read;
if ((Bytes - LastReported) > REPORT_INTERVAL)
{
Console.WriteLine((Bytes/1024).ToString() + " Kb Out");
LastReported = Bytes;
}
OnDataOut(buffer);
}
while(Read != 0);
}
catch{}
Console.WriteLine("Server dropped");
}
public virtual void OnDataIn(byte[] data)
{
if (DataIn != null)
{
string Data = System.Text.Encoding.ASCII.GetString(data);
DataIn(Data);
}
}
public virtual void OnDataOut(byte[] data)
{
if (DataOut != null)
{
string Data = System.Text.Encoding.ASCII.GetString(data);
DataOut(Data);
}
}
private void WriteLog(byte[] Data, int Length, string Path, bool In)
{
try
{
FileStream s = new FileStream(Path, FileMode.Append);
if (In != logLastIn && _ShowDirectionTags)
{
string Tag = In ? "\r\n<<< IN >>>\r\n" : "\r\n<<< OUT >>>\r\n";
s.Write(System.Text.Encoding.ASCII.GetBytes(Tag),0,Tag.Length);
logLastIn = In;
}
s.Write(Data, 0, Length);
s.Flush();
s.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
private void CleanUp()
{
if (!Clean)
{
tIn.Abort();
tOut.Abort();
sin.Close();
sout.Close();
Forwarder.Close();
_Connection.Close();
Clean = true;
}
}
}
}