/// <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}"); } }
// 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(); }