示例#1
0
        /// <summary>
        /// 浏览器tcp转发
        /// </summary>
        /// <param name="client"></param>
        private async Task TcpHandlerAsync(ConnectionContext browser)
        {
            try
            {
                //browser-->5,1,0
                var firstPack = (await browser.Transport.Input.ReadAsync()).Buffer;
                if (string.Join(",", firstPack.ToArray()) != "5,1,0")
                {
                    throw new Exception("proxy handshake faild");
                }

                browser.Transport.Input.AdvanceTo(firstPack.GetPosition(firstPack.Length));

                //server-->5,0
                //发5 0 回到浏览器
                await browser.Transport.Output.WriteAsync(new byte[] { 5, 0 });

                //browser-->address port
                var secondPack = (await browser.Transport.Input.ReadAsync()).Buffer;
                if (!Socket5Utility.TryParse(secondPack.ToArray(), out var socket5Result))
                {
                    throw new Exception("parse socket5 proxy infomation faild");
                }

                browser.Transport.Input.AdvanceTo(secondPack.GetPosition(secondPack.Length));

                await using SocketConnect target = new SocketConnect();
                await target.ConnectAsync(remoteAddress, remotePort, System.Text.Encoding.UTF8.GetString(socket5Result.Address), socket5Result.Port);

                logger.LogInformation($"连接到{System.Text.Encoding.UTF8.GetString(socket5Result.Address)}:{socket5Result.Port}");

                Task taskRecvBrowser = Task.Run(async() =>
                {
                    //获取浏览器数据
                    await foreach (var browserData in GetRecvDataAsync(browser))
                    {
                        //发送数据到服务器
                        await target.SendAsync(browserData);
                    }
                });

                Task taskRecvServer = Task.Run(async() =>
                {
                    //接收服务器数据
                    await foreach (var browserData in target.hubConnection.StreamAsync <byte[]>("GetTargetServerData"))
                    {
                        //发送数据到浏览器
                        await browser.Transport.Output.WriteAsync(browserData);
                    }
                });

                await Task.WhenAll(taskRecvBrowser, taskRecvServer);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"处理TcpHandlerAsync出现错误:{ex.InnerException?.Message ?? ex.Message}");
            }
        }
示例#2
0
        /// <summary>
        /// 处理浏览器
        /// </summary>
        /// <param name="browser"></param>
        /// <returns></returns>
        public static void ProcessBrowserAsync(ConnectionContext browser)
        {
            Task.Run(async() =>
            {
                //最终网站
                SocketConnect target = new SocketConnect();
                try
                {
                    while (true)
                    {
                        System.IO.Pipelines.ReadResult result = await browser.Transport.Input.ReadAsync();
                        if (!result.Buffer.IsEmpty)
                        {
                            var message = Message.ParsePack(result.Buffer.ToArray());
                            if (message.Item1 != null)
                            {
                                var data = message.Item1.Content;
                                //var data = Crypto.DecryptAES(message.Item1.Content);
                                Socket5Info socket5Info = new Socket5Info();
                                if (socket5Info.TryParse(data))
                                {
                                    await target.ConnectAsync(System.Text.Encoding.UTF8.GetString(socket5Info.Address), socket5Info.Port);
                                    //连接到服务器
                                    //var ipEndPoint = await GetIpEndPointAsync(System.Text.Encoding.UTF8.GetString(socket5Info.Address), socket5Info.Port);
                                    //if(ipEndPoint == null)
                                    //{
                                    //    break;
                                    //}

                                    //await target.ConnectAsync(ipEndPoint.Address,ipEndPoint.Port);

                                    byte[] sendData = new byte[] { 0x05, 0x00, 0x00, 0x01, 0x7f, 0x00, 0x00, 0x01, 0x1f, 0x40 };
                                    //发送确认到浏览器

                                    await browser.Transport.Output.WriteAsync(sendData);

                                    ProcessTargetServer(browser, target);
                                }
                                else
                                {
                                    //发送数据到目标服务器
                                    await target.TcpClient.Client.SendAsync(data, SocketFlags.None);
                                }

                                browser.Transport.Input.AdvanceTo(result.Buffer.GetPosition(message.Item2));
                            }
                            else
                            {
                                browser.Transport.Input.AdvanceTo(result.Buffer.GetPosition(0));
                            }
                        }
                        else
                        {
                            if (result.IsCompleted || result.IsCanceled)
                            {
                                break;
                            }
                        }
                    }
                    await browser.Transport.Input.CompleteAsync();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            });
        }
示例#3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            new Task(async() =>
            {
                //连接本地3389
                await local3389.ConnectAsync("127.0.0.1", 3389);
                await center.ConnectAsync("zwovo.xyz", 3390);


                new Task(async() =>
                {
                    while (true)
                    {
                        //收到本地中心服务器数据
                        var readResult = await center.PipeReader.ReadAsync();
                        if (readResult.Buffer.IsEmpty)
                        {
                            break;
                        }

                        SequencePosition position = readResult.Buffer.Start;
                        if (readResult.Buffer.TryGet(ref position, out var memory))
                        {
                            //发送到local3389
                            await center.SendAsync(memory);

                            local3389.PipeReader.AdvanceTo(readResult.Buffer.GetPosition(memory.Length));
                        }

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

                    await local3389.PipeReader.CompleteAsync();
                }).Start();



                while (true)
                {
                    //收到本地3389数据
                    var readResult = await local3389.PipeReader.ReadAsync();
                    if (readResult.Buffer.IsEmpty)
                    {
                        break;
                    }

                    SequencePosition position = readResult.Buffer.Start;
                    if (readResult.Buffer.TryGet(ref position, out var memory))
                    {
                        //发送到中心服务器
                        await center.SendAsync(memory);

                        local3389.PipeReader.AdvanceTo(readResult.Buffer.GetPosition(memory.Length));
                    }

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

                await local3389.PipeReader.CompleteAsync();
            }).Start();
        }