/// <summary> /// Initializes a new instance of the <see cref="Proxy" /> class. /// </summary> /// <param name="proxyServer">The proxy server.</param> /// <param name="geolocation">The geolocation.</param> /// <param name="pingMs">The ping (in ms).</param> /// <param name="speedKbps">The speed (in kB/s).</param> public Proxy(ProxyServer proxyServer, AddressGeolocation geolocation, int pingMs, int speedKbps) { ProxyServer = proxyServer; Geolocation = geolocation; PingMs = pingMs; SpeedKbps = speedKbps; }
/// <summary> /// Creates a new instance of the ProxyConnection class with the remote socket of the client /// and the ProxyServer this connection should belong to. /// </summary> /// <param name="networkSocket"> The network socket of the network client </param> /// <param name="server"> The proxy server this connection belongs to </param> public ProxyConnection(Socket networkSocket, ProxyServer server) { _logger = LogManager.GetLogger("Proxy Connection"); _networkSocket = networkSocket; _server = server; _random = new Random (); }
public void LiteralWindowsProxyServerTest() { var p = new ProxyServer("http=1.2.3.4:8080"); Assert.AreEqual(ProxyProtocol.HttpConnect, p.Protocol); Assert.AreEqual("1.2.3.4", p.Host); Assert.AreEqual(8080, p.Port); }
public void LiteralUriProxyServerTest() { var p = new ProxyServer("socks://[::1]:1080"); Assert.AreEqual(ProxyProtocol.Socks4A, p.Protocol); // WTF? there is a full expansion of IPv6 addresses in URI parsing. Assert.AreEqual("[0000:0000:0000:0000:0000:0000:0000:0001]", p.Host); Assert.AreEqual(1080, p.Port); }
/// <summary> /// Creates a TCP connection to server /// </summary> /// <param name="remoteHostName">The remote hostname.</param> /// <param name="remotePort">The remote port.</param> /// <param name="httpVersion">The http version to use.</param> /// <param name="isHttps">Is this a HTTPS request.</param> /// <param name="applicationProtocols">The list of HTTPS application level protocol to negotiate if needed.</param> /// <param name="isConnect">Is this a CONNECT request.</param> /// <param name="proxyServer">The current ProxyServer instance.</param> /// <param name="session">The http session.</param> /// <param name="upStreamEndPoint">The local upstream endpoint to make request via.</param> /// <param name="externalProxy">The external proxy to make request via.</param> /// <param name="cancellationToken">The cancellation token for this async task.</param> /// <returns></returns> private async Task <TcpServerConnection> createServerConnection(string remoteHostName, int remotePort, Version httpVersion, bool isHttps, List <SslApplicationProtocol> applicationProtocols, bool isConnect, ProxyServer proxyServer, SessionEventArgsBase session, IPEndPoint upStreamEndPoint, ExternalProxy externalProxy, CancellationToken cancellationToken) { //deny connection to proxy end points to avoid infinite connection loop. if (Server.ProxyEndPoints.Any(x => x.Port == remotePort) && NetworkHelper.IsLocalIpAddress(remoteHostName)) { throw new Exception($"A client is making HTTP request to one of the listening ports of this proxy {remoteHostName}:{remotePort}"); } if (externalProxy != null) { if (Server.ProxyEndPoints.Any(x => x.Port == externalProxy.Port) && NetworkHelper.IsLocalIpAddress(externalProxy.HostName)) { throw new Exception($"A client is making HTTP request via external proxy to one of the listening ports of this proxy {remoteHostName}:{remotePort}"); } } bool useUpstreamProxy = false; // check if external proxy is set for HTTP/HTTPS if (externalProxy != null && !(externalProxy.HostName == remoteHostName && externalProxy.Port == remotePort)) { useUpstreamProxy = true; // check if we need to ByPass if (externalProxy.BypassLocalhost && NetworkHelper.IsLocalIpAddress(remoteHostName)) { useUpstreamProxy = false; } } TcpClient tcpClient = null; CustomBufferedStream stream = null; SslApplicationProtocol negotiatedApplicationProtocol = default; try { tcpClient = new TcpClient(upStreamEndPoint) { NoDelay = proxyServer.NoDelay, ReceiveTimeout = proxyServer.ConnectionTimeOutSeconds * 1000, SendTimeout = proxyServer.ConnectionTimeOutSeconds * 1000, LingerState = new LingerOption(true, proxyServer.TcpTimeWaitSeconds) }; //linux has a bug with socket reuse in .net core. if (proxyServer.ReuseSocket && RunTime.IsWindows) { tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); } var hostname = useUpstreamProxy ? externalProxy.HostName : remoteHostName; var port = useUpstreamProxy ? externalProxy.Port : remotePort; var ipAddresses = await Dns.GetHostAddressesAsync(hostname); if (ipAddresses == null || ipAddresses.Length == 0) { throw new Exception($"Could not resolve the hostname {hostname}"); } if (session != null) { session.TimeLine["Dns Resolved"] = DateTime.Now; } for (int i = 0; i < ipAddresses.Length; i++) { try { await tcpClient.ConnectAsync(ipAddresses[i], port); break; } catch (Exception e) { if (i == ipAddresses.Length - 1) { throw new Exception($"Could not establish connection to {hostname}", e); } } } if (session != null) { session.TimeLine["Connection Established"] = DateTime.Now; } await proxyServer.InvokeConnectionCreateEvent(tcpClient, false); stream = new CustomBufferedStream(tcpClient.GetStream(), proxyServer.BufferPool, proxyServer.BufferSize); if (useUpstreamProxy && (isConnect || isHttps)) { var writer = new HttpRequestWriter(stream, proxyServer.BufferPool, proxyServer.BufferSize); var connectRequest = new ConnectRequest { OriginalUrl = $"{remoteHostName}:{remotePort}", HttpVersion = httpVersion }; connectRequest.Headers.AddHeader(KnownHeaders.Connection, KnownHeaders.ConnectionKeepAlive); if (!string.IsNullOrEmpty(externalProxy.UserName) && externalProxy.Password != null) { connectRequest.Headers.AddHeader(HttpHeader.ProxyConnectionKeepAlive); connectRequest.Headers.AddHeader( HttpHeader.GetProxyAuthorizationHeader(externalProxy.UserName, externalProxy.Password)); } await writer.WriteRequestAsync(connectRequest, cancellationToken : cancellationToken); string httpStatus = await stream.ReadLineAsync(cancellationToken); Response.ParseResponseLine(httpStatus, out _, out int statusCode, out string statusDescription); if (statusCode != 200 && !statusDescription.EqualsIgnoreCase("OK") && !statusDescription.EqualsIgnoreCase("Connection Established")) { throw new Exception("Upstream proxy failed to create a secure tunnel"); } await stream.ReadAndIgnoreAllLinesAsync(cancellationToken); } if (isHttps) { var sslStream = new SslStream(stream, false, proxyServer.ValidateServerCertificate, proxyServer.SelectClientCertificate); stream = new CustomBufferedStream(sslStream, proxyServer.BufferPool, proxyServer.BufferSize); var options = new SslClientAuthenticationOptions { ApplicationProtocols = applicationProtocols, TargetHost = remoteHostName, ClientCertificates = null, EnabledSslProtocols = proxyServer.SupportedSslProtocols, CertificateRevocationCheckMode = proxyServer.CheckCertificateRevocation }; await sslStream.AuthenticateAsClientAsync(options, cancellationToken); #if NETCOREAPP2_1 negotiatedApplicationProtocol = sslStream.NegotiatedApplicationProtocol; #endif if (session != null) { session.TimeLine["HTTPS Established"] = DateTime.Now; } } } catch (Exception) { stream?.Dispose(); tcpClient?.Close(); throw; } return(new TcpServerConnection(proxyServer, tcpClient) { UpStreamProxy = externalProxy, UpStreamEndPoint = upStreamEndPoint, HostName = remoteHostName, Port = remotePort, IsHttps = isHttps, NegotiatedApplicationProtocol = negotiatedApplicationProtocol, UseUpstreamProxy = useUpstreamProxy, StreamWriter = new HttpRequestWriter(stream, proxyServer.BufferPool, proxyServer.BufferSize), Stream = stream, Version = httpVersion }); }
internal TcpConnectionFactory(ProxyServer server) { this.Server = server; Task.Run(async() => await clearOutdatedConnections()); }
/// <summary> /// Creates a TCP connection to server /// </summary> /// <param name="remoteHostName">The remote hostname.</param> /// <param name="remotePort">The remote port.</param> /// <param name="httpVersion">The http version to use.</param> /// <param name="isHttps">Is this a HTTPS request.</param> /// <param name="sslProtocol">The SSL protocol.</param> /// <param name="applicationProtocols">The list of HTTPS application level protocol to negotiate if needed.</param> /// <param name="isConnect">Is this a CONNECT request.</param> /// <param name="proxyServer">The current ProxyServer instance.</param> /// <param name="sessionArgs">The http session.</param> /// <param name="upStreamEndPoint">The local upstream endpoint to make request via.</param> /// <param name="externalProxy">The external proxy to make request via.</param> /// <param name="cacheKey">The connection cache key</param> /// <param name="prefetch">if set to <c>true</c> [prefetch].</param> /// <param name="cancellationToken">The cancellation token for this async task.</param> /// <returns></returns> private async Task <TcpServerConnection?> createServerConnection(string remoteHostName, int remotePort, Version httpVersion, bool isHttps, SslProtocols sslProtocol, List <SslApplicationProtocol>?applicationProtocols, bool isConnect, ProxyServer proxyServer, SessionEventArgsBase sessionArgs, IPEndPoint?upStreamEndPoint, IExternalProxy?externalProxy, string cacheKey, bool prefetch, CancellationToken cancellationToken) { if (upStreamEndPoint == null) { // deny connection to proxy end points to avoid infinite connection loop. if (Server.ProxyEndPoints.Any(x => x.Port == remotePort) && NetworkHelper.IsLocalIpAddress(remoteHostName)) { throw new Exception($"A client is making HTTP request to one of the listening ports of this proxy {remoteHostName}:{remotePort}"); } if (externalProxy != null) { if (Server.ProxyEndPoints.Any(x => x.Port == externalProxy.Port) && NetworkHelper.IsLocalIpAddress(externalProxy.HostName)) { throw new Exception($"A client is making HTTP request via external proxy to one of the listening ports of this proxy {remoteHostName}:{remotePort}"); } } } else if (IPAddress.IsLoopback(upStreamEndPoint.Address)) { throw new Exception($"A client is making HTTP request to one of the listening ports of this proxy {upStreamEndPoint.Address}:{upStreamEndPoint.Port}"); } if (isHttps && sslProtocol == SslProtocols.None) { sslProtocol = proxyServer.SupportedSslProtocols; } bool useUpstreamProxy1 = false; // check if external proxy is set for HTTP/HTTPS if (externalProxy != null && !(externalProxy.HostName == remoteHostName && externalProxy.Port == remotePort)) { useUpstreamProxy1 = true; // check if we need to ByPass if (externalProxy.BypassLocalhost && NetworkHelper.IsLocalIpAddress(remoteHostName, externalProxy.ProxyDnsRequests)) { useUpstreamProxy1 = false; } } if (!useUpstreamProxy1) { externalProxy = null; } Socket? tcpServerSocket = null; HttpServerStream?stream = null; SslApplicationProtocol negotiatedApplicationProtocol = default; bool retry = true; var enabledSslProtocols = sslProtocol; retry: try { bool socks = externalProxy != null && externalProxy.ProxyType != ExternalProxyType.Http; string hostname = remoteHostName; int port = remotePort; if (externalProxy != null) { hostname = externalProxy.HostName; port = externalProxy.Port; } var ipAddresses = await Dns.GetHostAddressesAsync(hostname); if (ipAddresses == null || ipAddresses.Length == 0) { if (prefetch) { return(null); } throw new Exception($"Could not resolve the hostname {hostname}"); } if (sessionArgs != null) { sessionArgs.TimeLine["Dns Resolved"] = DateTime.UtcNow; } Array.Sort(ipAddresses, (x, y) => x.AddressFamily.CompareTo(y.AddressFamily)); Exception?lastException = null; for (int i = 0; i < ipAddresses.Length; i++) { try { var ipAddress = ipAddresses[i]; var addressFamily = upStreamEndPoint?.AddressFamily ?? ipAddress.AddressFamily; if (socks) { var proxySocket = new ProxySocket.ProxySocket(addressFamily, SocketType.Stream, ProtocolType.Tcp); proxySocket.ProxyType = externalProxy !.ProxyType == ExternalProxyType.Socks4 ? ProxyTypes.Socks4 : ProxyTypes.Socks5; proxySocket.ProxyEndPoint = new IPEndPoint(ipAddress, port); if (!string.IsNullOrEmpty(externalProxy.UserName) && externalProxy.Password != null) { proxySocket.ProxyUser = externalProxy.UserName; proxySocket.ProxyPass = externalProxy.Password; } tcpServerSocket = proxySocket; } else { if (upStreamEndPoint != null) { ipAddress = upStreamEndPoint.Address; tcpServerSocket = new Socket(upStreamEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); //tcpServerSocket.Bind(upStreamEndPoint); } else { tcpServerSocket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp); } } tcpServerSocket.NoDelay = proxyServer.NoDelay; tcpServerSocket.ReceiveTimeout = proxyServer.ConnectionTimeOutSeconds * 1000; tcpServerSocket.SendTimeout = proxyServer.ConnectionTimeOutSeconds * 1000; tcpServerSocket.LingerState = new LingerOption(true, proxyServer.TcpTimeWaitSeconds); if (proxyServer.ReuseSocket && RunTime.IsSocketReuseAvailable) { tcpServerSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); } Task connectTask; if (socks) { if (externalProxy !.ProxyDnsRequests) { connectTask = ProxySocketConnectionTaskFactory.CreateTask((ProxySocket.ProxySocket)tcpServerSocket, remoteHostName, remotePort); } else { // todo: resolve only once when the SOCKS proxy has multiple addresses (and the first address fails) var remoteIpAddresses = await Dns.GetHostAddressesAsync(remoteHostName); if (remoteIpAddresses == null || remoteIpAddresses.Length == 0) { throw new Exception($"Could not resolve the SOCKS remote hostname {remoteHostName}"); } // todo: use the 2nd, 3rd... remote addresses when first fails connectTask = ProxySocketConnectionTaskFactory.CreateTask((ProxySocket.ProxySocket)tcpServerSocket, remoteIpAddresses[0], remotePort); } } else { connectTask = SocketConnectionTaskFactory.CreateTask(tcpServerSocket, ipAddress, port); } await Task.WhenAny(connectTask, Task.Delay(proxyServer.ConnectTimeOutSeconds * 1000, cancellationToken)); if (!connectTask.IsCompleted || !tcpServerSocket.Connected) { // here we can just do some cleanup and let the loop continue since // we will either get a connection or wind up with a null tcpClient // which will throw try { connectTask.Dispose(); } catch { // ignore } try { #if NET451 tcpServerSocket?.Close(); #else tcpServerSocket?.Dispose(); #endif tcpServerSocket = null; } catch { // ignore } continue; } break; } catch (Exception e) { // dispose the current TcpClient and try the next address lastException = e; #if NET451 tcpServerSocket?.Close(); #else tcpServerSocket?.Dispose(); #endif tcpServerSocket = null; } }
public HttpClient GetClient(ProxyServer proxyServer, bool enableBasicProxyAuthorization = false) { return(TestHelper.GetHttpClient(proxyServer.ProxyEndPoints[0].Port, enableBasicProxyAuthorization)); }
internal HttpServerStream(ProxyServer server, Stream stream, IBufferPool bufferPool, CancellationToken cancellationToken) : base(server, stream, bufferPool, cancellationToken) { }
private static void DoWork(object data) { var checkThread = (CheckThread)data; var fm = (AnonymityForm)Application.OpenForms["AnonymityForm"]; ProxyServer proxy = null; string level = "未知"; try { while (Checker.CheckingQueue.Count > 0) { try { #region 验证匿名度 Monitor.Enter(Checker.CheckingQueue); proxy = Checker.CheckingQueue.Dequeue(); Monitor.Exit(Checker.CheckingQueue); string html = checkThread._checker.App.GetHtml(checkThread._checker.CheckUrl, proxy.proxy + ":" + proxy.port); if (html.Contains("ProxyJudge")) { level = checkThread._checker.App.GetMidString(html, "AnonyLevel :", "<BR>"); level = checkThread._checker.App.GetMidString(level, "<FONT color=\"yellow\">", "</FONT>"); switch (level) { case "1": level = "高匿名代理"; break; case "2": level = "匿名代理"; break; case "3": level = "普通匿名代理"; break; case "3?": level = "透明代理"; break; case "4?": level = "透明代理"; break; case "5?": level = "透明代理"; break; case "4": level = "透明代理"; break; case "5": level = "透明代理"; break; } } checkThread.Status = "Completed"; #endregion } catch { // 线程被放弃 checkThread.Status = "Completed"; level = "未知"; } finally { if (null != proxy) { List <ProxyServer> drs = (from p in checkThread._checker.App.ProxyList where p.proxy == proxy.proxy && p.port == proxy.port select p).ToList <ProxyServer>(); foreach (ProxyServer dr in drs) { dr.anonymity = level; } } if (null != fm) { var sb = new StringBuilder(); if (proxy != null) { sb.Append(proxy.proxy); } sb.Append(":"); if (proxy != null) { sb.Append(proxy.port); } sb.Append(","); sb.Append(level); fm.Refresh(sb.ToString()); sb = new StringBuilder(); sb.Append(checkThread._checker.TotalCheckedCount); sb.Append("/"); sb.Append(checkThread._checker.App.ProxyList.Count); string info = string.Format("正在验证匿名度{0},请稍后...", sb); checkThread._checker.App.SetStatusText(info); } } } } catch { } finally { checkThread.OnCompleted(); } }
private void FillDataGrid(ProxyServer.BusinessLayer.GroupTaskResponse response) { if (response.ErrorCode != 0) { throw new ApplicationException("Nie udało się zrealizować żądania - serwer zwrócił błąd: " + response.ErrorCode.ToString()); } if (response.Tasks != null && response.Tasks.Length > 0) { TaskView[] list = new TaskView[response.Tasks.Length]; for (int i = 0; i < response.Tasks.Length; i++) { TaskView view = new TaskView(); view.ID = response.Tasks[i].ID; view.Name = response.Tasks[i].Name; view.State = response.Tasks[i].State; list[i] = view; } dlSearchResults.DataSource = list; dlSearchResults.DataBind(); dlSearchResults.Visible = true; dlSearchResults.VirtualItemCount = response.TotalCount; } else { dlSearchResults.Visible = false; AddMessageOK("Nie znaleziono żadnych rekordów."); } SetSaveEnabled(false); }
public static void GetHtmlString(DownloadThread downloader) { string result = ""; if (string.IsNullOrEmpty(downloader.Url)) { return; } var sw = new Stopwatch(); sw.Start(); var r = new Random(); string url = downloader.url; var webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url).ToString()); try { downloader.Description = "设置完成,开始访问网页..."; webRequest.Method = "Get"; webRequest.Accept = "*/*"; webRequest.UserAgent = "Mozilla/4.0 (MSIE 6.0; Windows NT " + r.Next(99999) + ")"; webRequest.Headers["Accept-Language"] = "zh-cn"; webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.Referer = "http://www.loamen.com/404.htm?name=" + Application.ProductName + "&url=" + url; webRequest.Timeout = downloader.timeOut * 1000; webRequest.ReadWriteTimeout = downloader.timeOut * 1000; webRequest.ServicePoint.ConnectionLimit = 100; WebResponse response = webRequest.GetResponse(); result = GetHtmlString(response, Encoding.Default); response.Close(); string line = result; var regex = new Regex(Download.RegexProxy); MatchCollection matches = regex.Matches(line); int count = 0; for (int i = 0; i < matches.Count; i++) { string ip = matches[i].Groups["Proxy"].Value; string port = matches[i].Groups["Port"].Value; var proxy = new ProxyServer(); proxy.proxy = matches[i].Groups["Proxy"].Value; proxy.port = Convert.ToInt32(matches[i].Groups["Port"].Value); proxy.type = "HTTP"; proxy.status = -1; var regexProxy = new Regex( @"(?<Proxy>(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]))"); if (regexProxy.Matches(proxy.proxy).Count != 1 || proxy.port == 0) { return; } lock (downloader.app.ProxyList) { if (downloader.app.ProxyList.Count(p => p.proxy == proxy.proxy && p.port == proxy.port) == 0) { downloader.app.ProxyList.Add(proxy); } count++; } } sw.Stop(); var message = "采集完毕,耗时:" + sw.ElapsedMilliseconds + "毫秒,共" + count + "条"; downloader.Description = url + message; downloader.app.SetStatusText(message); } catch (Exception ex) { sw.Stop(); var message = "出错啦:" + ex.Message + sw.ElapsedMilliseconds; downloader.Description = url + message; downloader.app.SetStatusText(message); } finally { webRequest.Abort(); } }
/// <summary> /// Creates a TCP connection to server /// </summary> /// <param name="server"></param> /// <param name="remoteHostName"></param> /// <param name="remotePort"></param> /// <param name="httpVersion"></param> /// <param name="isHttps"></param> /// <param name="isConnect"></param> /// <param name="upStreamEndPoint"></param> /// <param name="externalHttpProxy"></param> /// <param name="externalHttpsProxy"></param> /// <returns></returns> internal async Task <TcpConnection> CreateClient(ProxyServer server, string remoteHostName, int remotePort, Version httpVersion, bool isHttps, bool isConnect, IPEndPoint upStreamEndPoint, ExternalProxy externalHttpProxy, ExternalProxy externalHttpsProxy) { bool useUpstreamProxy = false; var externalProxy = isHttps ? externalHttpsProxy : externalHttpProxy; //check if external proxy is set for HTTP/HTTPS if (externalProxy != null && !(externalProxy.HostName == remoteHostName && externalProxy.Port == remotePort)) { useUpstreamProxy = true; //check if we need to ByPass if (externalProxy.BypassLocalhost && NetworkHelper.IsLocalIpAddress(remoteHostName)) { useUpstreamProxy = false; } } TcpClient client = null; CustomBufferedStream stream = null; try { #if NET45 client = new TcpClient(upStreamEndPoint); #else client = new TcpClient(); client.Client.Bind(upStreamEndPoint); #endif //If this proxy uses another external proxy then create a tunnel request for HTTP/HTTPS connections if (useUpstreamProxy) { await client.ConnectAsync(externalProxy.HostName, externalProxy.Port); } else { await client.ConnectAsync(remoteHostName, remotePort); } stream = new CustomBufferedStream(client.GetStream(), server.BufferSize); if (useUpstreamProxy && (isConnect || isHttps)) { using (var writer = new HttpRequestWriter(stream, server.BufferSize)) { await writer.WriteLineAsync($"CONNECT {remoteHostName}:{remotePort} HTTP/{httpVersion}"); await writer.WriteLineAsync($"Host: {remoteHostName}:{remotePort}"); await writer.WriteLineAsync("Connection: Keep-Alive"); if (!string.IsNullOrEmpty(externalProxy.UserName) && externalProxy.Password != null) { await HttpHeader.ProxyConnectionKeepAlive.WriteToStreamAsync(writer); await writer.WriteLineAsync("Proxy-Authorization" + ": Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes( externalProxy.UserName + ":" + externalProxy.Password))); } await writer.WriteLineAsync(); await writer.FlushAsync(); } using (var reader = new CustomBinaryReader(stream, server.BufferSize)) { string result = await reader.ReadLineAsync(); if (!new[] { "200 OK", "connection established" }.Any(s => result.ContainsIgnoreCase(s))) { throw new Exception("Upstream proxy failed to create a secure tunnel"); } await reader.ReadAndIgnoreAllLinesAsync(); } } if (isHttps) { var sslStream = new SslStream(stream, false, server.ValidateServerCertificate, server.SelectClientCertificate); stream = new CustomBufferedStream(sslStream, server.BufferSize); await sslStream.AuthenticateAsClientAsync(remoteHostName, null, server.SupportedSslProtocols, server.CheckCertificateRevocation); } client.ReceiveTimeout = server.ConnectionTimeOutSeconds * 1000; client.SendTimeout = server.ConnectionTimeOutSeconds * 1000; } catch (Exception) { stream?.Dispose(); client?.Dispose(); throw; } server.UpdateServerConnectionCount(true); return(new TcpConnection { UpStreamHttpProxy = externalHttpProxy, UpStreamHttpsProxy = externalHttpsProxy, UpStreamEndPoint = upStreamEndPoint, HostName = remoteHostName, Port = remotePort, IsHttps = isHttps, UseUpstreamProxy = useUpstreamProxy, TcpClient = client, StreamReader = new CustomBinaryReader(stream, server.BufferSize), Stream = stream, Version = httpVersion }); }
internal TunnelConnectSessionEventArgs(ProxyServer server, ProxyEndPoint endPoint, ConnectRequest connectRequest, CancellationTokenSource cancellationTokenSource) : base(server, endPoint, cancellationTokenSource, connectRequest) { HttpClient.ConnectRequest = connectRequest; }
public async Task CanHandleAttachmentExceptionsGracefully_Smuggler() { var backupPath = NewDataPath("BackupFolder"); var server = GetNewServer(); var allowDownload = false; var forwarder = new ProxyServer(8070, 8079) { VetoTransfer = (totalRead, buffer) => { var payload = System.Text.Encoding.UTF8.GetString(buffer.Array, buffer.Offset, buffer.Count); return(payload.Contains("GET /static/users/678 ") && allowDownload == false); } }; try { string databaseName; using (var store = new DocumentStore { Url = "http://localhost:8079" }) { databaseName = store.DefaultDatabase; store.Initialize(); InsertAttachments(store, 2000); } var options = new SmugglerOptions { Limit = 1500, Incremental = true }; var dumper = new SmugglerApi(); var allAttachments = new List <RavenJObject>(); ExportDataResult exportResult = null; try { exportResult = dumper.ExportData(new SmugglerExportOptions { ToFile = backupPath, From = new RavenConnectionStringOptions { Url = "http://localhost:8070", DefaultDatabase = databaseName, } }, options).Result; Assert.False(true, "Previous op should throw."); } catch (AggregateException e) { var inner = (SmugglerExportException)e.ExtractSingleInnerException(); exportResult = new ExportDataResult { FilePath = inner.File }; } allowDownload = true; using (var fileStream = new FileStream(exportResult.FilePath, FileMode.Open)) using (var stream = new GZipStream(fileStream, CompressionMode.Decompress)) { var chunk1 = RavenJToken.TryLoad(stream) as RavenJObject; var att1 = chunk1["Attachments"] as RavenJArray; allAttachments.AddRange(att1.Values <RavenJObject>()); } exportResult = await dumper.ExportData(new SmugglerExportOptions { ToFile = backupPath, From = new RavenConnectionStringOptions { Url = "http://localhost:8070", DefaultDatabase = databaseName, } }, options); using (var fileStream = new FileStream(exportResult.FilePath, FileMode.Open)) using (var stream = new GZipStream(fileStream, CompressionMode.Decompress)) { var chunk2 = RavenJToken.TryLoad(stream) as RavenJObject; var attr2 = chunk2["Attachments"] as RavenJArray; allAttachments.AddRange(attr2.Values <RavenJObject>()); } Assert.Equal(2000, allAttachments.Count()); IOExtensions.DeleteDirectory(backupPath); } finally { forwarder.Dispose(); server.Dispose(); } }
public async Task CanHandleDocumentExceptionsGracefully_Smuggler() { var backupPath = NewDataPath("BackupFolder"); var server = GetNewServer(databaseName: Constants.SystemDatabase); var alreadyReset = false; var forwarder = new ProxyServer(8070, 8079) { VetoTransfer = (totalRead, buffer) => { if (alreadyReset == false && totalRead > 25000) { alreadyReset = true; return(true); } return(false); } }; try { string databaseName; using (var store = new DocumentStore { Url = "http://localhost:8079" }) { databaseName = store.DefaultDatabase; store.Initialize(); InsertUsers(store, 0, 2000); } var options = new SmugglerOptions { Limit = 1900, Incremental = true }; var dumper = new SmugglerApi(); var allDocs = new List <RavenJObject>(); ExportDataResult exportResult = null; try { exportResult = await dumper.ExportData(new SmugglerExportOptions { ToFile = backupPath, From = new RavenConnectionStringOptions { Url = "http://localhost:8070", DefaultDatabase = databaseName, } }, options); Assert.False(true, "Previous op should throw."); } catch (SmugglerExportException e) { exportResult = new ExportDataResult { FilePath = e.File }; } using (var fileSteam = new FileStream(exportResult.FilePath, FileMode.Open)) using (var stream = new GZipStream(fileSteam, CompressionMode.Decompress)) { var chunk1 = RavenJToken.TryLoad(stream) as RavenJObject; var doc1 = chunk1["Docs"] as RavenJArray; allDocs.AddRange(doc1.Values <RavenJObject>()); } exportResult = await dumper.ExportData(new SmugglerExportOptions { ToFile = backupPath, From = new RavenConnectionStringOptions { Url = "http://localhost:8070", DefaultDatabase = databaseName, } }, options); using (var fileStream = new FileStream(exportResult.FilePath, FileMode.Open)) using (var stream = new GZipStream(fileStream, CompressionMode.Decompress)) { var chunk2 = RavenJToken.TryLoad(stream) as RavenJObject; var doc2 = chunk2["Docs"] as RavenJArray; allDocs.AddRange(doc2.Values <RavenJObject>()); } Assert.Equal(2000, allDocs.Count(d => (d.Value <string>("Name") ?? String.Empty).StartsWith("User"))); } finally { forwarder.Dispose(); server.Dispose(); IOExtensions.DeleteDirectory(backupPath); } }
internal TcpClientConnection(ProxyServer proxyServer, Socket tcpClientSocket) { this.tcpClientSocket = tcpClientSocket; this.proxyServer = proxyServer; this.proxyServer.UpdateClientConnectionCount(true); }
/// <summary> /// Initializes a new instance of the <see cref="ProxyRouteException"/> class. /// </summary> /// <param name="proxy">The proxy.</param> public ProxyRouteException(ProxyServer proxy) { Proxy = proxy; }
/// <summary> /// Initializes a new instance of the <see cref="SessionEventArgsBase" /> class. /// </summary> private SessionEventArgsBase(ProxyServer server) { BufferPool = server.BufferPool; ExceptionFunc = server.ExceptionFunc; TimeLine["Session Created"] = DateTime.Now; }
public ProxyTestController() { proxyServer = new ProxyServer(); }
public MainWindow() { proxyServer = new ProxyServer(); //proxyServer.EnableHttp2 = true; //proxyServer.CertificateManager.CertificateEngine = CertificateEngine.DefaultWindows; ////Set a password for the .pfx file //proxyServer.CertificateManager.PfxPassword = "******"; ////Set Name(path) of the Root certificate file //proxyServer.CertificateManager.PfxFilePath = @"C:\NameFolder\rootCert.pfx"; ////do you want Replace an existing Root certificate file(.pfx) if password is incorrect(RootCertificate=null)? yes====>true //proxyServer.CertificateManager.OverwritePfxFile = true; ////save all fake certificates in folder "crts"(will be created in proxy dll directory) ////if create new Root certificate file(.pfx) ====> delete folder "crts" //proxyServer.CertificateManager.SaveFakeCertificates = true; proxyServer.ForwardToUpstreamGateway = true; //increase the ThreadPool (for server prod) //proxyServer.ThreadPoolWorkerThread = Environment.ProcessorCount * 6; ////if you need Load or Create Certificate now. ////// "true" if you need Enable===> Trust the RootCertificate used by this proxy server //proxyServer.CertificateManager.EnsureRootCertificate(true); ////or load directly certificate(As Administrator if need this) ////and At the same time chose path and password ////if password is incorrect and (overwriteRootCert=true)(RootCertificate=null) ====> replace an existing .pfx file ////note : load now (if existed) //proxyServer.CertificateManager.LoadRootCertificate(@"C:\NameFolder\rootCert.pfx", "PfxPassword"); var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 8000, true); proxyServer.AddEndPoint(explicitEndPoint); //proxyServer.UpStreamHttpProxy = new ExternalProxy //{ // HostName = "158.69.115.45", // Port = 3128, // UserName = "******", // Password = "******", //}; //var socksEndPoint = new SocksProxyEndPoint(IPAddress.Any, 1080, true) //{ // // Generic Certificate hostname to use // // When SNI is disabled by client // //GenericCertificateName = "google.com" //}; //proxyServer.AddEndPoint(socksEndPoint); proxyServer.BeforeRequest += ProxyServer_BeforeRequest; proxyServer.BeforeResponse += ProxyServer_BeforeResponse; proxyServer.AfterResponse += ProxyServer_AfterResponse; explicitEndPoint.BeforeTunnelConnectRequest += ProxyServer_BeforeTunnelConnectRequest; explicitEndPoint.BeforeTunnelConnectResponse += ProxyServer_BeforeTunnelConnectResponse; proxyServer.ClientConnectionCountChanged += delegate { Dispatcher.Invoke(() => { ClientConnectionCount = proxyServer.ClientConnectionCount; }); }; proxyServer.ServerConnectionCountChanged += delegate { Dispatcher.Invoke(() => { ServerConnectionCount = proxyServer.ServerConnectionCount; }); }; proxyServer.Start(); proxyServer.SetAsSystemProxy(explicitEndPoint, ProxyProtocolType.AllHttp); InitializeComponent(); }
internal TunnelConnectSessionEventArgs(ProxyServer server, ProxyEndPoint endPoint, ConnectRequest connectRequest, HttpClientStream clientStream, CancellationTokenSource cancellationTokenSource) : base(server, endPoint, clientStream, connectRequest, connectRequest, cancellationTokenSource) { }