Ejemplo n.º 1
0
        //data
        private void RunDataReceiver(ClientWebSocket socket, DataTunnelInfo dInfo, Func <Guid, byte[], Task> dataReceiver)
        {
            Task.Run(async() =>
            {
                await ReceiveAsync(socket, MessageHandler, CloseHandler);
                if (socket.State == WebSocketState.Open)
                {
                    await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
                }

                log.LogInformation($"Дисконект DT");
            });

            async Task MessageHandler(byte[] encryptedData)
            {
                var data = await dEncoder.Decrypt(dInfo.Key, encryptedData);

                await using var ms = new MemoryStream(data);
                using var br       = new BinaryReader(ms);
                var guidb       = br.ReadBytes(16);
                var payloadSize = br.ReadInt32();
                var payload     = br.ReadBytes(payloadSize);

                await dataReceiver(new Guid(guidb), payload);
            }

            async Task CloseHandler()
            {
                await RemoveConnection(dInfo.TunnelId);
            }
        }
Ejemplo n.º 2
0
 private async Task SendData(DataTunnelInfo tunnelInfo, byte[] data)
 {
     if (tunnelInfo.Socket.State != WebSocketState.Open)
     {
         await RemoveConnection(tunnelInfo.TunnelId);
     }
     try
     {
         await tunnelInfo.Socket.SendAsync(new ArraySegment <byte>(data), WebSocketMessageType.Binary, true, CancellationToken.None);
     }
     catch (Exception)
     {
         await RemoveConnection(tunnelInfo.TunnelId);
     }
 }
Ejemplo n.º 3
0
        public async Task <Guid> OpenDataTunnel(Func <Guid, byte[], Task> dataReceiver, Func <Guid, Task> disconnectTunnel)
        {
            var res = await SendDataTunnelRequest();

            if (res.Status != ResStatus.Ok)
            {
                log.LogError($"Ошибка открытия дата тунеля: {res.Error.Code}");
                throw new Exception($"Ошибка открытия дата тунеля: {res.Error.Code}");
            }

            var ws    = new ClientWebSocket();
            var wsUri = new Uri($"{serverUri}/{res.DTUri}");

            log.LogInformation($"Подключение к {wsUri}");


            try
            {
                if (!string.IsNullOrWhiteSpace(config.Proxy?.Server))
                {
                    var proxy = new WebProxy(config.Proxy.Server, config.Proxy.Port);

                    proxy.UseDefaultCredentials = config.Proxy.UseDefaultCredentials;
                    if (!string.IsNullOrWhiteSpace(config.Proxy.Login))
                    {
                        proxy.Credentials = new NetworkCredential(config.Proxy.Login, config.Proxy.Passwd);
                    }

                    managerWebSocket.Options.Proxy = proxy;
                }

                await ws.ConnectAsync(wsUri, CancellationToken.None);
            }
            catch (Exception ex)
            {
                log.LogError(ex, $"Ошибка соединения :{ex.Message}");
                throw;
            }

            var dtInfo = new DataTunnelInfo
            {
                TunnelId          = Guid.NewGuid(),
                Uri               = wsUri.ToString(),
                BlockSize         = res.DTBS,
                Socket            = ws,
                DisconectCallback = disconnectTunnel
            };

            try
            {
                var aes = Aes.Create();
                aes.Key    = res.DTKey;
                aes.IV     = res.DTIV;
                dtInfo.Key = aes;
            }
            catch (Exception ex)
            {
                log.LogError(ex, $"Ошибка ключа :{ex.Message}");
                throw;
            }

            dataTunnelInfos.TryAdd(dtInfo.TunnelId, dtInfo);

            RunDataReceiver(ws, dtInfo, dataReceiver);
            log.LogInformation($"Дата тунель открыт");

            return(dtInfo.TunnelId);
        }