Skip to content

Commit d0e16dc

Browse files
committed
Add debug log in network transfer.
1 parent ca44b52 commit d0e16dc

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/.vs/
12
Backup/
23
bin/
34
obj/

nuget.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
12
<configuration>
23
<config>
34
<add key="repositoryPath" value="shadowsocks-csharp\3rd" />

shadowsocks-csharp/Controller/Service/TCPRelay.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ private void HandshakeReceive()
215215
response = new byte[] { 0, 91 };
216216
Console.WriteLine("socks 5 protocol error");
217217
}
218+
Logging.Debug($"======Send Local Port, size:" + response.Length);
218219
connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(HandshakeSendCallback), null);
219220
}
220221
else
@@ -246,6 +247,7 @@ private void HandshakeSendCallback(IAsyncResult ar)
246247
// +----+-----+-------+------+----------+----------+
247248
// Skip first 3 bytes
248249
// TODO validate
250+
Logging.Debug($"======Receive Local Port, size:" + 3);
249251
connection.BeginReceive(connetionRecvBuffer, 0, 3, 0,
250252
new AsyncCallback(handshakeReceive2Callback), null);
251253
}
@@ -272,6 +274,7 @@ private void handshakeReceive2Callback(IAsyncResult ar)
272274
if (command == 1)
273275
{
274276
byte[] response = { 5, 0, 0, 1, 0, 0, 0, 0, 0, 0 };
277+
Logging.Debug($"======Send Local Port, size:" + response.Length);
275278
connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(ResponseCallback), null);
276279
}
277280
else if (command == 3)
@@ -310,6 +313,7 @@ private void HandleUDPAssociate()
310313
address.CopyTo(response, 4);
311314
response[response.Length - 1] = (byte)(port & 0xFF);
312315
response[response.Length - 2] = (byte)((port >> 8) & 0xFF);
316+
Logging.Debug($"======Send Local Port, size:" + response.Length);
313317
connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(ReadAll), true);
314318
}
315319

@@ -324,6 +328,7 @@ private void ReadAll(IAsyncResult ar)
324328
if (ar.AsyncState != null)
325329
{
326330
connection.EndSend(ar);
331+
Logging.Debug($"======Receive Local Port, size:" + RecvSize);
327332
connection.BeginReceive(connetionRecvBuffer, 0, RecvSize, 0,
328333
new AsyncCallback(ReadAll), null);
329334
}
@@ -332,6 +337,7 @@ private void ReadAll(IAsyncResult ar)
332337
int bytesRead = connection.EndReceive(ar);
333338
if (bytesRead > 0)
334339
{
340+
Logging.Debug($"======Receive Local Port, size:" + RecvSize);
335341
connection.BeginReceive(connetionRecvBuffer, 0, RecvSize, 0,
336342
new AsyncCallback(ReadAll), null);
337343
}
@@ -402,6 +408,7 @@ private void StartConnect()
402408

403409
connected = false;
404410
// Connect to the remote endpoint.
411+
Logging.Debug($"++++++Connect Server Port");
405412
remote.BeginConnect(remoteEP,
406413
new AsyncCallback(ConnectCallback), connectTimer);
407414
}
@@ -501,8 +508,10 @@ private void StartPipe()
501508
}
502509
try
503510
{
511+
Logging.Debug($"++++++Receive Server Port, size:" + RecvSize);
504512
remote.BeginReceive(remoteRecvBuffer, 0, RecvSize, 0,
505513
new AsyncCallback(PipeRemoteReceiveCallback), null);
514+
Logging.Debug($"======Receive Local Port, size:"+ RecvSize);
506515
connection.BeginReceive(connetionRecvBuffer, 0, RecvSize, 0,
507516
new AsyncCallback(PipeConnectionReceiveCallback), null);
508517
}
@@ -536,6 +545,7 @@ private void PipeRemoteReceiveCallback(IAsyncResult ar)
536545
}
537546
encryptor.Decrypt(remoteRecvBuffer, bytesRead, remoteSendBuffer, out bytesToSend);
538547
}
548+
Logging.Debug($"======Send Local Port, size:" + bytesToSend);
539549
connection.BeginSend(remoteSendBuffer, 0, bytesToSend, 0, new AsyncCallback(PipeConnectionSendCallback), null);
540550

541551
IStrategy strategy = controller.GetCurrentStrategy();
@@ -588,6 +598,7 @@ private void PipeConnectionReceiveCallback(IAsyncResult ar)
588598
}
589599
encryptor.Encrypt(connetionRecvBuffer, bytesRead, connetionSendBuffer, out bytesToSend);
590600
}
601+
Logging.Debug($"++++++Send Server Port, size:" + bytesToSend);
591602
remote.BeginSend(connetionSendBuffer, 0, bytesToSend, 0, new AsyncCallback(PipeRemoteSendCallback), null);
592603

593604
IStrategy strategy = controller.GetCurrentStrategy();
@@ -619,6 +630,7 @@ private void PipeRemoteSendCallback(IAsyncResult ar)
619630
try
620631
{
621632
remote.EndSend(ar);
633+
Logging.Debug($"======Receive Local Port, size:" + RecvSize);
622634
connection.BeginReceive(this.connetionRecvBuffer, 0, RecvSize, 0,
623635
new AsyncCallback(PipeConnectionReceiveCallback), null);
624636
}
@@ -638,6 +650,7 @@ private void PipeConnectionSendCallback(IAsyncResult ar)
638650
try
639651
{
640652
connection.EndSend(ar);
653+
Logging.Debug($"++++++Receive Server Port, size:" + RecvSize);
641654
remote.BeginReceive(this.remoteRecvBuffer, 0, RecvSize, 0,
642655
new AsyncCallback(PipeRemoteReceiveCallback), null);
643656
}

shadowsocks-csharp/Controller/Service/UDPRelay.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ public void Send(byte[] data, int length)
8080
byte[] dataOut = new byte[length - 3 + 16 + IVEncryptor.ONETIMEAUTH_BYTES];
8181
int outlen;
8282
encryptor.Encrypt(dataIn, length - 3, dataOut, out outlen);
83+
Logging.Debug($"++++++Send Server Port, size:" + outlen);
8384
_remote.SendTo(dataOut, outlen, SocketFlags.None, _remoteEndPoint);
8485
}
8586
public void Receive()
8687
{
8788
EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
89+
Logging.Debug($"++++++Receive Server Port, size:" + _buffer.Length);
8890
_remote.BeginReceiveFrom(_buffer, 0, _buffer.Length, 0, ref remoteEndPoint, new AsyncCallback(RecvFromCallback), null);
8991
}
9092
public void RecvFromCallback(IAsyncResult ar)
@@ -103,6 +105,7 @@ public void RecvFromCallback(IAsyncResult ar)
103105
byte[] sendBuf = new byte[outlen + 3];
104106
Array.Copy(dataOut, 0, sendBuf, 3, outlen);
105107

108+
Logging.Debug($"======Send Local Port, size:" + (outlen + 3));
106109
_local.SendTo(sendBuf, outlen + 3, 0, _localEndPoint);
107110
Receive();
108111
}

0 commit comments

Comments
 (0)