コード例 #1
0
        /// <summary>
        /// 浏览器tcp转发
        /// </summary>
        /// <param name="client"></param>
        private void TcpHandlerAsync(ConnectionContext browser)
        {
            Task.Run(async() =>
            {
                SocketConnect target = new SocketConnect();
                try
                {
                    await target.ConnectAsync(remoteAddress, remotePort);

                    ProcessTaget(browser, target);
                    while (true)
                    {
                        //浏览器普通接收
                        var result = await browser.Transport.Input.ReadAsync();

                        if (result.IsCompleted || result.IsCanceled)
                        {
                            break;
                        }

                        var buff = result.Buffer;


                        // 接收到浏览器数据
                        if (buff.Length == 3 && string.Join(",", buff.ToArray()) == "5,1,0")
                        {
                            //发5 0 回到浏览器
                            await browser.Transport.Output.WriteAsync(new byte[] { 5, 0 });
                        }
                        else
                        {
                            //var pack = Message.MakePack(new Message { Content = Crypto.EncryptAES(buff.ToArray()) });
                            var pack = Message.MakePack(new Message {
                                Content = buff.ToArray()
                            });

                            //发送数据到服务器
                            await target.TcpClient.Client.SendAsync(pack, SocketFlags.None);
                            //await target.Pipe.Output.WriteAsync(pack);
                        }

                        browser.Transport.Input.AdvanceTo(buff.GetPosition(buff.Length));
                    }

                    await browser.Transport.Input.CompleteAsync();
                }
                catch (Exception ex)
                {
                    logger.LogError(ex.Message);
                }
            });
        }
コード例 #2
0
        //处理目标服务器
        private Task ProcessTaget(ConnectionContext browser, SocketConnect target)
        {
            return(Task.Run(async() =>
            {
                try
                {
                    while (true)
                    {
                        var readResult = await target.PipeReader.ReadAsync();
                        if (!readResult.Buffer.IsEmpty)
                        {
                            SequencePosition position = readResult.Buffer.Start;
                            if (readResult.Buffer.TryGet(ref position, out var memory))
                            {
                                //发往浏览器
                                await browser.Transport.Output.WriteAsync(memory);
                                target.PipeReader.AdvanceTo(readResult.Buffer.GetPosition(memory.Length));
                            }
                        }
                        else
                        {
                            if (readResult.IsCanceled || readResult.IsCompleted)
                            {
                                break;
                            }
                        }
                    }

                    await target.PipeReader.CompleteAsync();
                }
                catch (Exception ex)
                {
                    logger.LogError(ex.Message);
                }
            }));
        }