@@ -30,7 +30,7 @@ For stable releases on [stable branch](https://github.com/justcoding121/Titanium
3030Supports
3131
3232 * .Net Standard 2.0 or above
33- * .Net Framework 4.5 or above
33+ * .Net Framework 4.6.1 or above
3434
3535### Development environment
3636
@@ -55,11 +55,11 @@ Setup HTTP proxy:
5555``` csharp
5656var proxyServer = new ProxyServer ();
5757
58- // locally trust root certificate used by this proxy
58+ // locally trust root certificate used by this proxy
5959proxyServer .CertificateManager .TrustRootCertificate = true ;
6060
61- // optionally set the Certificate Engine
62- // Under Mono only BouncyCastle will be supported
61+ // optionally set the Certificate Engine
62+ // Under Mono only BouncyCastle will be supported
6363// proxyServer.CertificateManager.CertificateEngine = Network.CertificateEngine.BouncyCastle;
6464
6565proxyServer .BeforeRequest += OnRequest ;
@@ -70,28 +70,28 @@ proxyServer.ClientCertificateSelectionCallback += OnCertificateSelection;
7070
7171var explicitEndPoint = new ExplicitProxyEndPoint (IPAddress .Any , 8000 , true )
7272{
73- // Use self-issued generic certificate on all https requests
74- // Optimizes performance by not creating a certificate for each https-enabled domain
75- // Useful when certificate trust is not required by proxy clients
76- // GenericCertificate = new X509Certificate2(Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "genericcert.pfx"), "password")
73+ // Use self-issued generic certificate on all https requests
74+ // Optimizes performance by not creating a certificate for each https-enabled domain
75+ // Useful when certificate trust is not required by proxy clients
76+ // GenericCertificate = new X509Certificate2(Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "genericcert.pfx"), "password")
7777 };
7878
79- // Fired when a CONNECT request is received
79+ // Fired when a CONNECT request is received
8080explicitEndPoint .BeforeTunnelConnect += OnBeforeTunnelConnect ;
8181
82- // An explicit endpoint is where the client knows about the existence of a proxy
83- // So client sends request in a proxy friendly manner
82+ // An explicit endpoint is where the client knows about the existence of a proxy
83+ // So client sends request in a proxy friendly manner
8484proxyServer .AddEndPoint (explicitEndPoint );
8585proxyServer .Start ();
8686
87- // Transparent endpoint is useful for reverse proxy (client is not aware of the existence of proxy)
88- // A transparent endpoint usually requires a network router port forwarding HTTP(S) packets or DNS
89- // to send data to this endPoint
87+ // Transparent endpoint is useful for reverse proxy (client is not aware of the existence of proxy)
88+ // A transparent endpoint usually requires a network router port forwarding HTTP(S) packets or DNS
89+ // to send data to this endPoint
9090var transparentEndPoint = new TransparentProxyEndPoint (IPAddress .Any , 8001 , true )
9191{
92- // Generic Certificate hostname to use
93- // when SNI is disabled by client
94- GenericCertificateName = " google.com"
92+ // Generic Certificate hostname to use
93+ // when SNI is disabled by client
94+ GenericCertificateName = " google.com"
9595};
9696
9797proxyServer .AddEndPoint (transparentEndPoint );
@@ -103,36 +103,36 @@ foreach (var endPoint in proxyServer.ProxyEndPoints)
103103Console .WriteLine (" Listening on '{0}' endpoint at Ip {1} and port: {2} " ,
104104 endPoint .GetType ().Name , endPoint .IpAddress , endPoint .Port );
105105
106- // Only explicit proxies can be set as system proxy!
106+ // Only explicit proxies can be set as system proxy!
107107proxyServer .SetAsSystemHttpProxy (explicitEndPoint );
108108proxyServer .SetAsSystemHttpsProxy (explicitEndPoint );
109109
110- // wait here (You can use something else as a wait function, I am using this as a demo)
110+ // wait here (You can use something else as a wait function, I am using this as a demo)
111111Console .Read ();
112112
113- // Unsubscribe & Quit
113+ // Unsubscribe & Quit
114114explicitEndPoint .BeforeTunnelConnect -= OnBeforeTunnelConnect ;
115115proxyServer .BeforeRequest -= OnRequest ;
116116proxyServer .BeforeResponse -= OnResponse ;
117117proxyServer .ServerCertificateValidationCallback -= OnCertificateValidation ;
118118proxyServer .ClientCertificateSelectionCallback -= OnCertificateSelection ;
119119
120120proxyServer .Stop ();
121-
121+
122122```
123123Sample request and response event handlers
124124
125- ``` csharp
125+ ``` csharp
126126
127127private async Task OnBeforeTunnelConnectRequest (object sender , TunnelConnectSessionEventArgs e )
128128{
129129 string hostname = e .HttpClient .Request .RequestUri .Host ;
130130
131131 if (hostname .Contains (" dropbox.com" ))
132132 {
133- // Exclude Https addresses you don't want to proxy
134- // Useful for clients that use certificate pinning
135- // for example dropbox.com
133+ // Exclude Https addresses you don't want to proxy
134+ // Useful for clients that use certificate pinning
135+ // for example dropbox.com
136136 e .DecryptSsl = false ;
137137 }
138138}
@@ -141,88 +141,88 @@ public async Task OnRequest(object sender, SessionEventArgs e)
141141{
142142 Console .WriteLine (e .HttpClient .Request .Url );
143143
144- // // read request headers
144+ // read request headers
145145 var requestHeaders = e .HttpClient .Request .RequestHeaders ;
146146
147147 var method = e .HttpClient .Request .Method .ToUpper ();
148148 if ((method == " POST" || method == " PUT" || method == " PATCH" ))
149149 {
150- // Get/Set request body bytes
151- byte [] bodyBytes = await e .GetRequestBody ();
152- await e .SetRequestBody (bodyBytes );
153-
154- // Get/Set request body as string
155- string bodyString = await e .GetRequestBodyAsString ();
156- await e .SetRequestBodyString (bodyString );
157-
158- // store request
159- // so that you can find it from response handler
160- e .UserData = e .HttpClient .Request ;
150+ // Get/Set request body bytes
151+ byte [] bodyBytes = await e .GetRequestBody ();
152+ await e .SetRequestBody (bodyBytes );
153+
154+ // Get/Set request body as string
155+ string bodyString = await e .GetRequestBodyAsString ();
156+ await e .SetRequestBodyString (bodyString );
157+
158+ // store request
159+ // so that you can find it from response handler
160+ e .UserData = e .HttpClient .Request ;
161161 }
162162
163- // To cancel a request with a custom HTML content
164- // Filter URL
163+ // To cancel a request with a custom HTML content
164+ // Filter URL
165165 if (e .HttpClient .Request .RequestUri .AbsoluteUri .Contains (" google.com" ))
166166 {
167- e .Ok (" <!DOCTYPE html>" +
168- " <html><body><h1>" +
169- " Website Blocked" +
170- " </h1>" +
171- " <p>Blocked by titanium web proxy.</p>" +
172- " </body>" +
173- " </html>" );
167+ e .Ok (" <!DOCTYPE html>" +
168+ " <html><body><h1>" +
169+ " Website Blocked" +
170+ " </h1>" +
171+ " <p>Blocked by titanium web proxy.</p>" +
172+ " </body>" +
173+ " </html>" );
174174 }
175- // Redirect example
175+
176+ // Redirect example
176177 if (e .HttpClient .Request .RequestUri .AbsoluteUri .Contains (" wikipedia.org" ))
177178 {
178- e .Redirect (" https://www.paypal.com" );
179+ e .Redirect (" https://www.paypal.com" );
179180 }
180181}
181182
182- // Modify response
183+ // Modify response
183184public async Task OnResponse (object sender , SessionEventArgs e )
184185{
185- // read response headers
186+ // read response headers
186187 var responseHeaders = e .HttpClient .Response .ResponseHeaders ;
187188
188189 // if (!e.ProxySession.Request.Host.Equals("medeczane.sgk.gov.tr")) return;
189190 if (e .HttpClient .Request .Method == " GET" || e .HttpClient .Request .Method == " POST" )
190191 {
191- if (e .HttpClient .Response .ResponseStatusCode == " 200" )
192- {
193- if (e .HttpClient .Response .ContentType != null && e .HttpClient .Response .ContentType .Trim ().ToLower ().Contains (" text/html" ))
194- {
195- byte [] bodyBytes = await e .GetResponseBody ();
196- await e .SetResponseBody (bodyBytes );
197-
198- string body = await e .GetResponseBodyAsString ();
199- await e .SetResponseBodyString (body );
200- }
201- }
192+ if (e .HttpClient .Response .ResponseStatusCode == " 200" )
193+ {
194+ if (e .HttpClient .Response .ContentType != null && e .HttpClient .Response .ContentType .Trim ().ToLower ().Contains (" text/html" ))
195+ {
196+ byte [] bodyBytes = await e .GetResponseBody ();
197+ await e .SetResponseBody (bodyBytes );
198+
199+ string body = await e .GetResponseBodyAsString ();
200+ await e .SetResponseBodyString (body );
201+ }
202+ }
202203 }
203204
204- if (e .UserData != null )
205+ if (e .UserData != null )
205206 {
206- // access request from UserData property where we stored it in RequestHandler
207- var request = (Request )e .UserData ;
207+ // access request from UserData property where we stored it in RequestHandler
208+ var request = (Request )e .UserData ;
208209 }
209-
210210}
211211
212- /// Allows overriding default certificate validation logic
212+ // Allows overriding default certificate validation logic
213213public Task OnCertificateValidation (object sender , CertificateValidationEventArgs e )
214214{
215- // set IsValid to true/false based on Certificate Errors
215+ // set IsValid to true/false based on Certificate Errors
216216 if (e .SslPolicyErrors == System .Net .Security .SslPolicyErrors .None )
217- e .IsValid = true ;
217+ e .IsValid = true ;
218218
219219 return Task .FromResult (0 );
220220}
221221
222- /// Allows overriding default client certificate selection logic during mutual authentication
222+ // Allows overriding default client certificate selection logic during mutual authentication
223223public Task OnCertificateSelection (object sender , CertificateSelectionEventArgs e )
224224{
225- // set e.clientCertificate to override
225+ // set e.clientCertificate to override
226226 return Task .FromResult (0 );
227227}
228228```
0 commit comments