public static void Stop() { reConnectThread?.Abort(); reConnectThread = null; heartThread?.Abort(); heartThread = null; NatClient?.Close(); NatClient = null; }
static void OnPackageReceived(object sender, NatRequestInfo natRequestInfo) { Task.Run(() => { switch (natRequestInfo.Body.Type) { case (byte)JsonType.NAT: NatClient.ProcessData(natRequestInfo); break; case (byte)JsonType.HTTP: HttpClientProxy.ProcessData(NatClient, natRequestInfo); break; case (byte)JsonType.TCP: { int waitTimes = 50; var tcpModel = natRequestInfo.Body.Data.FromJson <TcpModel>(); TcpClientProxy clientProxy = null; mark: clientProxy = TcpClientProxyList.Find(c => c.RemoteSession.SessionId == tcpModel.SessionId); if (natRequestInfo.Body.Action == (int)TcpAction.TransferData) { if ((clientProxy == null || !clientProxy.IsConnected) && waitTimes >= 0) { LogHelper.Warning($"----> {tcpModel.SessionId} 未连接 IsConnected={clientProxy?.IsConnected.ToString() ?? "NULL"} ProxyCount={TcpClientProxyList.Count}"); Thread.Sleep(100); waitTimes--; goto mark; } } if (clientProxy == null) { var arr = tcpModel.Local.Split(":"); var ip = arr[0]; int.TryParse(arr[1], out int port); clientProxy = new TcpClientProxy(new ClientOption() { Ip = ip, Port = port, NoDelay = true, ProtocolType = ProtocolType.Tcp }) { NatClient = NatClient }; } clientProxy.ProcessData(NatClient, natRequestInfo); break; } } }); }
static void OnClientConnected(object sender) { //发送注册包给服务端 var pack = PackHelper.CreatePack(new JsonData() { Type = (int)JsonType.NAT, Action = (int)NatAction.Connect, Data = Secret }); NatClient?.Send(pack); }
static async void ConnectNatServer() { try { if (!IsReConnect) { return; } isLock = true; LogHelper.Info($"正在连接服务器..."); //解析主机名 var serverIp = ServerUrl; var ipArr = ServerUrl.Split('.'); //先判断是不是IP,不是IP就尝试解析域名 if (ipArr.Where(c => int.TryParse(c, out int i) && i > 0 && i <= 255).Count() != 4) { IPHostEntry ipInfo = Dns.GetHostEntry(ServerUrl); serverIp = ipInfo.AddressList.Any() ? ipInfo.AddressList[0].ToString() : throw new Exception($"域名【{ServerUrl}】无法解析"); } NatClient = new NatClient(new ClientOption() { Ip = serverIp, Port = NatPort, NoDelay = true, ProtocolType = ProtocolType.Tcp, Security = SslProtocols.Tls12, SslClientAuthenticationOptions = new SslClientAuthenticationOptions { EnabledSslProtocols = SslProtocols.Tls12, TargetHost = serverIp, ClientCertificates = new X509CertificateCollection() { new X509Certificate(CertFile, CertPassword) } } }); NatClient.OnConnected += OnClientConnected; NatClient.OnReceived += OnPackageReceived; NatClient.OnClosed += OnClientClosed; await NatClient.ConnectAsync(); } catch (Exception ex) { LogHelper.Error($"连接服务器失败:{ex}"); } isLock = false; }
public void ProcessData(NatClient natClient, NatRequestInfo natRequestInfo) { try { var tcpModel = natRequestInfo.Body.Data.FromJson <TcpModel>(); switch (natRequestInfo.Body.Action) { case (int)TcpAction.Connect: { //tcp注册包 发起连接到内网服务器 RemoteSession = tcpModel; Map = natClient.Client.MapList.Find(c => c.remote_endpoint == tcpModel.Host); if (Map == null) { throw new Exception($"{Map.name} {Map.protocol} {Map.remote_endpoint} --> {Map.local_endpoint} 映射不存在"); } ConectLocalServerAsync(); } break; case (int)TcpAction.TransferData: { if (RemoteSession == null) { return; } //gzip解压 var request = DataHelper.Decompress(tcpModel.Content); //发送原始包 Send(request); LogHelper.Info($"{Map.name} {Map.protocol} {Map.remote_endpoint} --> {Map.local_endpoint} 发送报文{request.Length}字节"); } break; case (int)TcpAction.Close: { //tcp连接关闭包 ClientManager.TcpClientProxyList.Remove(this); Close(); } break; } } catch (Exception ex) { LogHelper.Error($"{Map.name} {Map.protocol} {Map.remote_endpoint} --> {Map.local_endpoint} 客户端处理TCP穿透业务异常,{ex}"); } }
static void SendHeart() { while (IsReConnect) { Thread.Sleep(50000); if (NatClient.IsConnected) { //发送心跳包给服务端 var pack = PackHelper.CreatePack(new JsonData() { Type = (int)JsonType.NAT, Action = (int)NatAction.Heart, Data = Secret }); NatClient?.Send(pack); } } }
public static async void ProcessData(NatClient natClient, NatRequestInfo natRequestInfo) { try { switch (natRequestInfo.Body.Action) { case (byte)HttpAction.Request: { var httpModel = natRequestInfo.Body.Data.FromJson <HttpModel>(); var map = natClient.Client.MapList.Find(c => c.remote_endpoint == httpModel.Host || (c.remote == httpModel.Host && c.remote_port == 80)); if (map == null) { LogHelper.Error($"映射不存在,外网访问地址:{httpModel.Host}"); return; } using HttpRequestMessage httpRequest = new HttpRequestMessage() { Method = new HttpMethod(httpModel.Method), RequestUri = new Uri($"{map.protocol}://{map.local_endpoint}{httpModel.Path}") }; LogHelper.Info($"{map.name} {httpModel.Method} {httpRequest.RequestUri.AbsoluteUri} {httpModel.Headers.ToJson()}{Environment.NewLine}"); string bodyStr = string.Empty; if (httpRequest.Method != HttpMethod.Get && httpModel.Content?.Length > 0) { var body = DataHelper.Decompress(httpModel.Content); //解压 bodyStr = body.ToUTF8String(); httpRequest.Content = httpModel.ContentType == null ? new StringContent(bodyStr, Encoding.UTF8) : new StringContent(bodyStr, Encoding.UTF8, httpModel.ContentType.Split(";")[0]); } LogHelper.Info($"{map.name} {httpModel.Method} {httpRequest.RequestUri.AbsoluteUri}{Environment.NewLine}【Header】{httpModel.Headers.ToJson()}{$"{Environment.NewLine}【Body】{bodyStr}".If(httpModel.Content?.Length < 1024)}{Environment.NewLine}"); using HttpClient _httpClient = new HttpClient(); //替换Host 不然400 Bad Request httpModel.Headers["Host"] = map.local_endpoint; foreach (var item in httpModel.Headers) { if (!item.Key.EqualsWhithNoCase("Content-Type")) { if (!httpRequest.Content?.Headers.TryAddWithoutValidation(item.Key, item.Value) ?? true) { _httpClient.DefaultRequestHeaders.TryAddWithoutValidation(item.Key, item.Value); } } } if (map.protocol == "https") { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; } var response = await _httpClient.SendAsync(httpRequest); //回传给服务器 httpModel.HttpVersion = $"{map.protocol.ToUpper()}/{response.Version}"; httpModel.StatusCode = (int)response.StatusCode; httpModel.StatusMessage = response.StatusCode.ToString(); httpModel.Local = map.local_endpoint; httpModel.Headers = response.Headers.ToDictionary(); httpModel.ResponseTime = DateTime.Now; foreach (var item in response.Content.Headers) { if (item.Key.EqualsWhithNoCase("Content-Type")) { httpModel.ContentType = string.Join(";", item.Value); } else { if (item.Key.EqualsWhithNoCase("Content-Length")) { continue; } httpModel.Headers.Add(item.Key, string.Join(";", item.Value)); } } httpModel.Headers.Remove("Transfer-Encoding"); //response收到的是完整的 这个响应头要去掉 不然浏览器解析出错 var returnContent = DataHelper.StreamToBytes(response.Content.ReadAsStreamAsync().Result); if (returnContent.Length > 0) { httpModel.Content = DataHelper.Compress(returnContent); } var pack = PackHelper.CreatePack(new JsonData() { Type = (int)JsonType.HTTP, Action = (int)HttpAction.Response, Data = httpModel.ToJson() }); natClient?.Send(pack); LogHelper.Info($"{map.name} {httpModel.Method} {httpRequest.RequestUri.AbsoluteUri}{$"{returnContent.ToUTF8String()}".If(returnContent.Length < 1024)} {httpModel.StatusCode} {httpModel.StatusMessage} {Math.Round(returnContent.Length * 1.00 / 1024, 1)}KB{Environment.NewLine}"); break; } } } catch (Exception ex) { LogHelper.Error($"处理请求异常:{ex}"); var pack = PackHelper.CreatePack(new JsonData() { Type = (int)JsonType.HTTP, Action = (int)HttpAction.Response, Data = new HttpModel() { StatusCode = (int)HttpStatusCode.BadRequest, ContentType = "text/html", Content = DataHelper.Compress(Encoding.UTF8.GetBytes($"server error")) }.ToJson() }); natClient?.Send(pack); } }