-
Notifications
You must be signed in to change notification settings - Fork 1
/
Client.cs
283 lines (260 loc) · 11 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Sentry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Margatroid.Alice.Native;
using static Margatroid.Alice.MessageHelper;
using static Margatroid.Alice.Native.IO;
namespace Margatroid.Alice
{
class Client : IHostedService, IDisposable
{
private const int BufferSize = 8192;
private bool DisposedValue;
private readonly CancellationTokenSource CancelSource;
private readonly ILogger<Server> _logger;
private readonly IHub _hub;
private readonly IConfiguration _configuration;
private Guid SessionId;
private readonly Dictionary<IPAddress, IPEndPoint> Tunnels;
private readonly HashSet<IPAddress> QueriedVirtualAddresses;
private readonly IPEndPoint ServerEndPoint;
private readonly IPEndPoint ListenEndPoint;
private readonly Queue<TunFrame> TunnelSendQueue;
private readonly Queue<TunFrame> TunnelReceiveQueue;
private readonly Tun Tun;
private readonly UdpSocket UdpSocket;
public Client(IHub hub, IConfiguration configuration, ILogger<Server> logger, IHostApplicationLifetime appLifetime)
{
_hub = hub;
_configuration = configuration;
_logger = logger;
CancelSource = new CancellationTokenSource();
QueriedVirtualAddresses = new HashSet<IPAddress>();
Tunnels = new Dictionary<IPAddress, IPEndPoint>();
Tun = new Tun("alicetun");
var serverAddress = IPAddress.Parse(_configuration["Client:ServerAddress"]);
var serverPort = int.Parse(_configuration["Client:ServerPort"]);
ServerEndPoint = new IPEndPoint(serverAddress, serverPort);
var listenAddress = IPAddress.Parse(_configuration["Client:ListenAddress"]);
var listenPort = int.Parse(_configuration["Client:ListenPort"]);
ListenEndPoint = new IPEndPoint(listenAddress, listenPort);
TunnelReceiveQueue = new Queue<TunFrame>();
TunnelSendQueue = new Queue<TunFrame>();
UdpSocket = new UdpSocket();
UdpSocket.Bind(ListenEndPoint);
}
public Task StartAsync(CancellationToken cancellationToken)
{
var mainTask = Task.Run(() =>
{
try
{
MainLoop();
}
catch (Exception e)
{
_hub.CaptureException(e);
_logger.LogError(e.ToString());
CancelSource.Cancel();
}
}, CancelSource.Token);
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
CancelSource.Cancel();
return Task.CompletedTask;
}
public void MainLoop()
{
// Init socket
// var socketFileDescriptorFlags = Check(Fcntl(UdpSocket, F_GETFL, 0));
// Check(Fcntl(socketFileDescriptor, F_SETFL, socketFileDescriptorFlags | O_NONBLOCK));
_logger.LogInformation("Start listening on {bindAddress}:{bindPort}", ListenEndPoint.Address, ListenEndPoint.Port);
// Register to server
Register();
// Create epoll
const int maxEvents = 64;
using var epollFileDescriptor = EpollCreate();
// Add tun to epoll
var tunEpollEvent = new EpollEvent();
tunEpollEvent.events = EpollEvents.EPOLLIN | EpollEvents.EPOLLOUT;
tunEpollEvent.fd = (int)Tun.TunFileDescriptor.DangerousGetHandle().ToInt64();
Check(EpollCtl(epollFileDescriptor, EPOLL_CTL_ADD, Tun.TunFileDescriptor, ref tunEpollEvent));
// Add UDP socket to epoll
var socketEpollEvent = new EpollEvent();
socketEpollEvent.events = EpollEvents.EPOLLIN | EpollEvents.EPOLLOUT;
tunEpollEvent.fd = (int)UdpSocket.SocketFileDescriptor.DangerousGetHandle().ToInt64();
Check(EpollCtl(epollFileDescriptor, EPOLL_CTL_ADD, UdpSocket.SocketFileDescriptor, ref socketEpollEvent));
// main loop
Message message;
IPEndPoint remote;
TunFrame tunFrame;
var epollReceivedEvents = new EpollEvent[maxEvents];
while (true)
{
var epollWaitRet = Check(EpollWait(epollFileDescriptor, epollReceivedEvents, maxEvents, -1));
for (var i = 0; i < epollWaitRet; i++)
{
var fd = epollReceivedEvents[i].fd;
if (epollReceivedEvents[i].events == EpollEvents.EPOLLIN)
{
if (fd == Tun.TunFileDescriptor)
{
var data = Tun.Read(BufferSize);
TunnelSendQueue.Enqueue(new TunFrame(data));
}
else if (fd == UdpSocket.SocketFileDescriptor)
{
var data = UdpSocket.Recv(BufferSize, out remote);
message = DeserializeMessage(data);
ListenDispatch(message, remote);
}
else
{
throw new AliceException("Unexpected file descriptor");
}
}
else if (epollReceivedEvents[i].events == EpollEvents.EPOLLOUT)
{
if (fd == Tun.TunFileDescriptor)
{
if (TunnelReceiveQueue.TryDequeue(out tunFrame))
{
Tun.Write(tunFrame.FrameData);
}
}
else if (fd == UdpSocket.SocketFileDescriptor)
{
if (TunnelSendQueue.TryDequeue(out tunFrame))
{
SendTunnel(tunFrame);
}
}
}
}
}
}
public void Register()
{
var registerMessage = new Message
{
Type = MessageType.Register,
LocalAddress = InterfaceHelper.GetLocalAddress().GetAddressBytes()
};
_logger.LogInformation($"Registering to {ServerEndPoint.Address}");
UdpSocket.SendMessageTo(ServerEndPoint, registerMessage);
IPEndPoint remote;
var message = UdpSocket.RecvMessage(out remote);
var virtualIPAddress = new IPAddress(message.VirtualAddress);
_logger.LogInformation("MessageType={type}, VA={virtualAddress}", message.Type, virtualIPAddress);
if (message.Type != MessageType.RegisterSuccess)
{
throw new AliceException("Cannot register on server");
}
SessionId = message.SessionId;
Tun.SetIP(virtualIPAddress);
Tun.SetMask("255.255.255.0");
}
public void ListenDispatch(Message message, IPEndPoint remote)
{
switch (message.Type)
{
case MessageType.HeartBeat:
HeartBeat();
return;
case MessageType.Tunnel:
TunnelReceiveQueue.Enqueue(new TunFrame(message.Payload));
return;
case MessageType.SessionBroadcast:
return;
case MessageType.SessionExists:
var virtualAddress = new IPAddress(message.VirtualAddress);
var tunnelAddress = new IPAddress(message.TunnelAddress);
var tunnelEndPoint = new IPEndPoint(tunnelAddress, message.TunnelPort);
_logger.LogInformation($"Session exists. VA={new IPAddress(message.VirtualAddress)} "
+ $"TA={tunnelAddress} "
+ $"TP={message.TunnelPort}");
Tunnels.Add(virtualAddress, tunnelEndPoint);
return;
case MessageType.SessionNotFound:
_logger.LogInformation($"Session not found. VA={new IPAddress(message.VirtualAddress)}");
return;
case MessageType.SessionExpired:
Register();
QueriedVirtualAddresses.Clear();
Tunnels.Clear();
TunnelSendQueue.Clear();
TunnelReceiveQueue.Clear();
return;
}
}
public void HeartBeat()
{
var message = new Message
{
Type = MessageType.HeartBeat,
SessionId = SessionId
};
UdpSocket.SendMessageTo(ServerEndPoint, message);
}
public void SendTunnel(TunFrame tunFrame)
{
IPEndPoint remote;
if (Tunnels.TryGetValue(tunFrame.DestinationAddress, out remote))
{
var message = new Message
{
Type = MessageType.Tunnel,
Payload = tunFrame.FrameData
};
UdpSocket.SendMessageTo(remote, message);
} else if (!QueriedVirtualAddresses.Contains(tunFrame.DestinationAddress))
{
var message = new Message
{
SessionId = SessionId,
Type = MessageType.SessionQuery,
VirtualAddress = tunFrame.DestinationAddress.GetAddressBytes()
};
UdpSocket.SendMessageTo(ServerEndPoint, message);
}
}
protected virtual void Dispose(bool disposing)
{
if (!DisposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects)
}
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
UdpSocket.Dispose();
Tun.Dispose();
DisposedValue = true;
}
}
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~Client()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}