Skip to content

Commit cafde91

Browse files
committed
bug fixes
1 parent 8b7f3ee commit cafde91

File tree

10 files changed

+38
-38
lines changed

10 files changed

+38
-38
lines changed

AutoTunnel/AutoTunnel.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@
138138
</EmbeddedResource>
139139
</ItemGroup>
140140
<ItemGroup>
141-
<EmbeddedResource Include="..\tunnel_estabilishing.png">
142-
<Link>tunnel_estabilishing.png</Link>
141+
<EmbeddedResource Include="..\tunnel_establishing.png">
142+
<Link>tunnel_establishing.png</Link>
143143
</EmbeddedResource>
144144
</ItemGroup>
145145
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

AutoTunnel/BaseSender.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ private void StartInternal()
5555
int packetLen = 0;
5656
while (!_isExiting)
5757
{
58+
var oldHandle = _handle;
5859
if (!WinDivert.WinDivertRecv(_handle, packet, packet.Length, ref addr, ref packetLen))
5960
{
60-
// showing error only if handle is not removed
61-
if (_handle != IntPtr.Zero)
61+
// showing error only if handle is not removed and not changed
62+
if (_handle != IntPtr.Zero && oldHandle == _handle)
6263
{
6364
LogHelper.Log.WriteLine("Cannot receive network data: " + Marshal.GetLastWin32Error());
6465
Thread.Sleep(1000);

AutoTunnel/ClientSender.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using System.Net;
4+
using System.Net.NetworkInformation;
45
using System.Net.Sockets;
56
using System.Text;
67
using System.Threading;
@@ -119,7 +120,7 @@ private void InitInternal()
119120

120121
_connectEP = destEP;
121122

122-
Storage.IncrementEstabilishing();
123+
Storage.IncrementEstablishing();
123124
Storage.AddSession(new byte[16], destEP).IsClientSession = true;
124125

125126
if (proxyEP != null)
@@ -177,23 +178,23 @@ private void InitInternal()
177178
period = Math.Min(period + 2000, 1000 * 60);
178179

179180
LogHelper.Log.WriteLine("No response from server " + destEP);
180-
if (DateTime.UtcNow.Subtract(_lastInitRequest).TotalSeconds > 60)
181+
if (!_config.ConnectOnStart && DateTime.UtcNow.Subtract(_lastInitRequest).TotalSeconds > 60)
181182
{
182183
LogHelper.Log.WriteLine("Stopping connect atteptions to " + destEP + " until another request will occur");
183184
}
184185
}
185186

186187
if (recLength < 4 || _receiveBuffer[0] != (byte)StateFlags.ConnectAnswer)
187188
{
188-
Console.Error.WriteLine("Invalid server response");
189+
LogHelper.Log.WriteLine("Invalid server response");
189190
Storage.RemoveSession(destEP);
190191
return;
191192
}
192193

193194
var decLen = decryptHelper.Decrypt(_receiveBuffer, 4);
194195
if (decLen < 9)
195196
{
196-
Console.Error.WriteLine("Invalid server response");
197+
LogHelper.Log.WriteLine("Invalid server response");
197198
Storage.RemoveSession(destEP);
198199
return;
199200
}
@@ -206,11 +207,14 @@ private void InitInternal()
206207
LogHelper.Log.WriteLine("Initialized connection to " + destEP);
207208
_isInited = true;
208209
_initingEvent.Set();
210+
211+
// after connect - sending packet to estabilish backward connection
212+
using (var p = new Ping()) p.Send(DstAddr, 1);
209213
}
210214
finally
211215
{
212216
Interlocked.Exchange(ref _isIniting, 0);
213-
Storage.DecrementEstabilishing();
217+
Storage.DecrementEstablishing();
214218
}
215219
}
216220

AutoTunnel/Encryption/EncryptHelper.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,11 @@ public EncryptHelper(byte[] key)
3333
public int Encrypt(byte[] data, int len)
3434
{
3535
var hb = _headerBuf;
36-
// _random.GetBytes(hb);
36+
_random.GetBytes(hb);
3737
hb[0] = (byte)(len & 0xff);
3838
hb[1] = (byte)((len >> 8) & 0xff);
3939
hb[2] = (byte)((len >> 16) & 0xff);
4040
hb[3] = (byte)((len >> 24) & 0xff);
41-
// var c = Interlocked.Increment(ref _counter);
42-
/*_headerBuf[4] = (byte)(c & 0xff);
43-
_headerBuf[5] = (byte)((c >> 8) & 0xff);
44-
_headerBuf[6] = (byte)((c >> 16) & 0xff);
45-
_headerBuf[7] = (byte)((c >> 24) & 0xff);*/
4641
hb[4] = 0x1;
4742
hb[5] = 0x0;
4843
hb[6] = (byte)'A';

AutoTunnel/Listener.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private void StartInternal()
7878
{
7979
if (inBuf[0] == (byte)StateFlags.Connecting)
8080
{
81-
LogHelper.Log.WriteLine("Estabilishing connection from " + ep);
81+
LogHelper.Log.WriteLine("Establishing connection from " + ep);
8282
int dataLen = -1;
8383
DecryptHelper decryptHelper = null;
8484
EncryptHelper encryptHelper = null;
@@ -116,7 +116,7 @@ private void StartInternal()
116116

117117
s.SendTo(initBuf, initBuf.Length, SocketFlags.None, ep);
118118
var descr = selectedRemoteClient != null ? (!string.IsNullOrEmpty(selectedRemoteClient.Description) ? " as " + selectedRemoteClient.BinaryKey : string.Empty) : string.Empty;
119-
LogHelper.Log.WriteLine("Estabilished connection from " + ep + descr);
119+
LogHelper.Log.WriteLine("Established connection from " + ep + descr);
120120
continue;
121121
}
122122

@@ -167,7 +167,7 @@ private void StartInternal()
167167

168168
// var sourceIp = decBuf[12] + "." + decBuf[13] + "." + decBuf[14] + "." + decBuf[15];
169169
var sourceIp = new IPAddress(decBuf[12] | (decBuf[13] << 8) | (decBuf[14] << 16) | (decBuf[15] << 24));
170-
// if we already has option to estabilish connection to this ip, do not add additional sender
170+
// if we already has option to establish connection to this ip, do not add additional sender
171171
if (!_storage.OutgoingConnectionAdresses.Contains(sourceIp))
172172
{
173173
var sender = _storage.GetOrAddSender(sourceIp, () => new ReplySender(session, sourceIp, s, _storage));

AutoTunnel/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
3434
[assembly: AssemblyVersion("1.0.0.0")]
35-
[assembly: AssemblyFileVersion("1.0.0.0")]
35+
[assembly: AssemblyFileVersion("1.1.0.2")]

AutoTunnel/Service/ConsoleHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class ConsoleHelper
1212
private static extern IntPtr SendMessage(IntPtr hWnd, uint message, int lParam, IntPtr wParam);
1313

1414
private static IntPtr _iconActiveHandle = IntPtr.Zero;
15-
private static IntPtr _iconEstabilishingHandle = IntPtr.Zero;
15+
private static IntPtr _iconEstablishingHandle = IntPtr.Zero;
1616
private static IntPtr _iconParentHandle = IntPtr.Zero;
1717

1818
private static void RestoreOriginalIcon()
@@ -26,7 +26,7 @@ private static void RestoreOriginalIcon()
2626
public enum IconStatus
2727
{
2828
Active,
29-
Estabilishing,
29+
Establishing,
3030
Default
3131
}
3232

@@ -38,15 +38,15 @@ public static void SetActiveIcon(IconStatus status)
3838
{
3939
// ReSharper disable AssignNullToNotNullAttribute
4040
_iconActiveHandle = new Bitmap(typeof(ConsoleHelper).Assembly.GetManifestResourceStream("Force.AutoTunnel.tunnel_active.png")).GetHicon();
41-
_iconEstabilishingHandle = new Bitmap(typeof(ConsoleHelper).Assembly.GetManifestResourceStream("Force.AutoTunnel.tunnel_estabilishing.png")).GetHicon();
41+
_iconEstablishingHandle = new Bitmap(typeof(ConsoleHelper).Assembly.GetManifestResourceStream("Force.AutoTunnel.tunnel_establishing.png")).GetHicon();
4242
// ReSharper restore AssignNullToNotNullAttribute
4343

4444
_iconParentHandle = SendMessage(GetConsoleWindow(), 0x80, 0, _iconActiveHandle);
4545
}
4646

4747
var icon = _iconParentHandle;
4848
if (status == IconStatus.Active) icon = _iconActiveHandle;
49-
else if (status == IconStatus.Estabilishing) icon = _iconEstabilishingHandle;
49+
else if (status == IconStatus.Establishing) icon = _iconEstablishingHandle;
5050
SendMessage(GetConsoleWindow(), 0x80, 0, icon);
5151
SendMessage(GetConsoleWindow(), 0x80, 1, icon);
5252
}

AutoTunnel/TunnelStorage.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,25 @@ public bool HasSession(ulong key)
9393
return _sessions.ContainsKey(key);
9494
}
9595

96-
private int _estabilishingCount;
96+
private int _establishingCount;
9797

98-
public void IncrementEstabilishing()
98+
public void IncrementEstablishing()
9999
{
100-
Interlocked.Increment(ref _estabilishingCount);
100+
Interlocked.Increment(ref _establishingCount);
101101
SetIcon();
102102
}
103103

104-
public void DecrementEstabilishing()
104+
public void DecrementEstablishing()
105105
{
106-
Interlocked.Decrement(ref _estabilishingCount);
106+
Interlocked.Decrement(ref _establishingCount);
107107
SetIcon();
108108
}
109109

110110
private void SetIcon()
111111
{
112-
if (_estabilishingCount > 0)
112+
if (_establishingCount > 0)
113113
{
114-
ConsoleHelper.SetActiveIcon(ConsoleHelper.IconStatus.Estabilishing);
114+
ConsoleHelper.SetActiveIcon(ConsoleHelper.IconStatus.Establishing);
115115
}
116116
else
117117
{

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Secured network tunnel for connecting two computers. Main usage: connecting trav
2121
## Differences from IPSec
2222

2323
* No need to specify both endpoints to static addresses
24-
* Only one UDP port for usage (insted of ESP, AH protocols and 500 and 4500 UDP ports)
24+
* Only one UDP port for usage (instead of ESP, AH protocols and 500 and 4500 UDP ports)
2525
* Just some settings to make tunnel works, you no need to specify lot of parameters in different configs and phases
2626
* Currently, no IPv6 support
2727

@@ -34,7 +34,7 @@ Secured network tunnel for connecting two computers. Main usage: connecting trav
3434

3535
AutoTunnel uses [WinDivert](https://reqrypt.org/windivert.html) library to catch outgoing network packets, transfers it to another computer and puts them back, as normal packets.
3636
So, this packets looks like usual network packets, but you do not need to specify routing tables between computers. These computers are in virtual local network.
37-
After estabilishing connection with another computer, it begin to catch packets to source computer and transfers it back to it through estabilished tunnel.
37+
After establishing connection with another computer, it begins to catch packets to source computer and transfers it back to it through established tunnel.
3838

3939
## Encryption
4040
Tunnel is encrypted with AES256 with PFS support. Currently, for auth you can use only preshared keys (I think, it simple and good variant for authentication, because one key can be changed to another in some seconds, you do not need to regenerate certificates or private keys).
@@ -47,10 +47,10 @@ There are two types of computers:
4747
* Server - listens incoming connections
4848
* Client - connects to server
4949

50-
Any computer can be server for others computers and client for anothers. There are no need to use separate program.
50+
Any computer can be server for others computers and client for another. There is no need to use separate program.
5151
So, despite only two computers can use one tunnel, you can create a lot of tunnels between pairs of different computers.
5252

53-
Also, you can estabilish connection between two computers in client mode. Two tunnels will be created, but packets will be passed correctly.
53+
Also, you can establish connection between two computers in client mode. Two tunnels will be created, but packets will be passed correctly.
5454

5555

5656
# Installation and running
@@ -61,7 +61,7 @@ You need:
6161
* Enabled .NET Framework 4.0
6262
* [VC Redist 2012](https://www.microsoft.com/en-us/download/details.aspx?id=30679)
6363

64-
Program can be runned in console or work as a service. Service can be installed in next way:
64+
Program can be started in console or work as a service. Service can be installed in next way:
6565
```
6666
AutoTunnel.exe service install
6767
sc start AutoTunnel
@@ -109,18 +109,18 @@ remoteServers | null | Servers for connecting as client
109109
remoteServers.tunnelHost | null | IP address or host name of remote computer. If any packets will be send to this computer, it will be passed through tunnel
110110
remoteServers.connectHost | null | IP address or host with port of remote computer to connect. If skipped - tunnel host data can be used. You can specify it, it target computer has different IP addresses and you want to connect to one of them, but pass data for another
111111
remoteServers.proxyHost | null | IP address or host with port of proxy. Proxy can be used to connect to server which is not available from outer network
112-
remoteServers.key | null | Pre-shared key to estabilish connection with remote server
112+
remoteServers.key | null | Pre-shared key to establish connection with remote server
113113
remoteServers.keepAlive | false | Send special keep alive packets to keep connection alive. If you need permament connection, you can set it to true
114114
remoteServers.connectOnStart | false | Connect to another computer on application start or only when first packet will be sent to it
115115

116116

117117
# Proxy
118-
If target computer is unavailabe from outer network but you have separate computer which can transfer packets from local network to outer network, you can use special proxy program.
118+
If target computer is unavailable from outer network but you have separate computer which can transfer packets from local network to outer network, you can use special proxy program.
119119

120120
Full scheme of connection can be similar:
121121

122122
```
123-
Client computer (gray ip, NAT) <-> Router <-> (Internet) <-> Router <-> Proxy <-> Server (NAT)
123+
Client computer (gray ip, NAT) <-> Router <-> (Internet) <-> Router <-> Proxy <-> Server (NAT)
124124
192.168.1.42 <-> 192.168.1.1|91.10.20.42 <-> (Internet) <-> 82.40.10.1 <-> 82.40.10.54 <-> 10.0.1.15
125125
```
126126

File renamed without changes.

0 commit comments

Comments
 (0)