Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

A cross-platform asynchronous HTTP(S) proxy server in C#.

License

NotificationsYou must be signed in to change notification settings

justcoding121/titanium-web-proxy

Repository files navigation

A lightweight HTTP(S) proxy server written in C#.

.NET CoreJoin the chat at https://gitter.im/Titanium-Web-Proxy/Lobby

Report bugs or raise issues here. For programming help useStackOverflow with the tag Titanium-Web-Proxy.

Features

  • Multithreaded and asynchronous proxy employing server connection pooling, certificate cache, and buffer pooling
  • View, modify, redirect and block requests or responses
  • Supports mutual SSL authentication, proxy authentication & automatic upstream proxy detection
  • Supports kerberos, NTLM authentication over HTTP protocols on windows domain controlled networks
  • SOCKS4/5 Proxy support

Installation

Install bynuget

For beta releases onbeta branch

Install-Package Titanium.Web.Proxy -Pre

For stable releases onstable branch

Install-Package Titanium.Web.Proxy

Supports

  • .NET Standard 2.0 or above
  • .NET Framework 4.5 or above

Note to contributors

Road map

  • Fixoutstanding bugs
  • Support reading request and response body as stream#823
  • Stop throwing new exceptions#634
  • Support HTTP 2.0

Collaborators

The owner of this project,justcoding121, is considered to be inactive from this project due to his busy work schedule. However, we have a collaborator listed below who time and again shows up to maintain this project. Please create pull requests prioritizing bug fixes for the attention of collaborators.

Development environment

Windows

  • Visual Studio Code as IDE for .NET
  • Visual Studio 2022 as IDE for .NET Framework/.NET

Mac OS

  • Visual Studio Code as IDE for .NET
  • Visual Studio 2022 as IDE for Mono

Linux

  • Visual Studio Code as IDE for .NET
  • Mono develop as IDE for Mono

Usage

Refer the HTTP Proxy Server library in your project and look up the test project to learn usage.

Setup HTTP proxy:

varproxyServer=newProxyServer();// locally trust root certificate used by this proxyproxyServer.CertificateManager.TrustRootCertificate(true);// optionally set the Certificate Engine// Under Mono only BouncyCastle will be supported//proxyServer.CertificateManager.CertificateEngine = Network.CertificateEngine.BouncyCastle;proxyServer.BeforeRequest+=OnRequest;proxyServer.BeforeResponse+=OnResponse;proxyServer.ServerCertificateValidationCallback+=OnCertificateValidation;proxyServer.ClientCertificateSelectionCallback+=OnCertificateSelection;varexplicitEndPoint=newExplicitProxyEndPoint(IPAddress.Any,8000,true){// Use self-issued generic certificate on all https requests// Optimizes performance by not creating a certificate for each https-enabled domain// Useful when certificate trust is not required by proxy clients//GenericCertificate = new X509Certificate2(Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "genericcert.pfx"), "password")};// Fired when a CONNECT request is receivedexplicitEndPoint.BeforeTunnelConnectRequest+=OnBeforeTunnelConnectRequest;// An explicit endpoint is where the client knows about the existence of a proxy// So client sends request in a proxy friendly mannerproxyServer.AddEndPoint(explicitEndPoint);proxyServer.Start();// Transparent endpoint is useful for reverse proxy (client is not aware of the existence of proxy)// A transparent endpoint usually requires a network router port forwarding HTTP(S) packets or DNS// to send data to this endPointvartransparentEndPoint=newTransparentProxyEndPoint(IPAddress.Any,8001,true){// Generic Certificate hostname to use// when SNI is disabled by clientGenericCertificateName="google.com"};proxyServer.AddEndPoint(transparentEndPoint);//proxyServer.UpStreamHttpProxy = new ExternalProxy() { HostName = "localhost", Port = 8888 };//proxyServer.UpStreamHttpsProxy = new ExternalProxy() { HostName = "localhost", Port = 8888 };foreach(varendPointinproxyServer.ProxyEndPoints)Console.WriteLine("Listening on '{0}' endpoint at Ip {1} and port: {2} ",endPoint.GetType().Name,endPoint.IpAddress,endPoint.Port);// Only explicit proxies can be set as system proxy!proxyServer.SetAsSystemHttpProxy(explicitEndPoint);proxyServer.SetAsSystemHttpsProxy(explicitEndPoint);// wait here (You can use something else as a wait function, I am using this as a demo)Console.Read();// Unsubscribe & QuitexplicitEndPoint.BeforeTunnelConnectRequest-=OnBeforeTunnelConnectRequest;proxyServer.BeforeRequest-=OnRequest;proxyServer.BeforeResponse-=OnResponse;proxyServer.ServerCertificateValidationCallback-=OnCertificateValidation;proxyServer.ClientCertificateSelectionCallback-=OnCertificateSelection;proxyServer.Stop();

Sample request and response event handlers

privateasyncTaskOnBeforeTunnelConnectRequest(objectsender,TunnelConnectSessionEventArgse){stringhostname=e.HttpClient.Request.RequestUri.Host;if(hostname.Contains("dropbox.com")){// Exclude Https addresses you don't want to proxy// Useful for clients that use certificate pinning// for example dropbox.come.DecryptSsl=false;}}publicasyncTaskOnRequest(objectsender,SessionEventArgse){Console.WriteLine(e.HttpClient.Request.Url);// read request headersvarrequestHeaders=e.HttpClient.Request.Headers;varmethod=e.HttpClient.Request.Method.ToUpper();if((method=="POST"||method=="PUT"||method=="PATCH")){// Get/Set request body bytesbyte[]bodyBytes=awaite.GetRequestBody();e.SetRequestBody(bodyBytes);// Get/Set request body as stringstringbodyString=awaite.GetRequestBodyAsString();e.SetRequestBodyString(bodyString);// store request// so that you can find it from response handlere.UserData=e.HttpClient.Request;}// To cancel a request with a custom HTML content// Filter URLif(e.HttpClient.Request.RequestUri.AbsoluteUri.Contains("google.com")){e.Ok("<!DOCTYPE html>"+"<html><body><h1>"+"Website Blocked"+"</h1>"+"<p>Blocked by titanium web proxy.</p>"+"</body>"+"</html>");}// Redirect exampleif(e.HttpClient.Request.RequestUri.AbsoluteUri.Contains("wikipedia.org")){e.Redirect("https://www.paypal.com");}}// Modify responsepublicasyncTaskOnResponse(objectsender,SessionEventArgse){// read response headersvarresponseHeaders=e.HttpClient.Response.Headers;//if (!e.ProxySession.Request.Host.Equals("medeczane.sgk.gov.tr")) return;if(e.HttpClient.Request.Method=="GET"||e.HttpClient.Request.Method=="POST"){if(e.HttpClient.Response.StatusCode==200){if(e.HttpClient.Response.ContentType!=null&&e.HttpClient.Response.ContentType.Trim().ToLower().Contains("text/html")){byte[]bodyBytes=awaite.GetResponseBody();e.SetResponseBody(bodyBytes);stringbody=awaite.GetResponseBodyAsString();e.SetResponseBodyString(body);}}}if(e.UserData!=null){// access request from UserData property where we stored it in RequestHandlervarrequest=(Request)e.UserData;}}// Allows overriding default certificate validation logicpublicTaskOnCertificateValidation(objectsender,CertificateValidationEventArgse){// set IsValid to true/false based on Certificate Errorsif(e.SslPolicyErrors==System.Net.Security.SslPolicyErrors.None)e.IsValid=true;returnTask.CompletedTask;}// Allows overriding default client certificate selection logic during mutual authenticationpublicTaskOnCertificateSelection(objectsender,CertificateSelectionEventArgse){// set e.clientCertificate to overridereturnTask.CompletedTask;}

Console example application screenshot

alt tag

GUI example application screenshot

alt tag

About

A cross-platform asynchronous HTTP(S) proxy server in C#.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp