@@ -30,7 +30,7 @@ For stable releases on [stable branch](https://github.com/justcoding121/Titanium
30
30
Supports
31
31
32
32
* .Net Standard 2.0 or above
33
- * .Net Framework 4.5 or above
33
+ * .Net Framework 4.6.1 or above
34
34
35
35
### Development environment
36
36
@@ -55,11 +55,11 @@ Setup HTTP proxy:
55
55
``` csharp
56
56
var proxyServer = new ProxyServer ();
57
57
58
- // locally trust root certificate used by this proxy
58
+ // locally trust root certificate used by this proxy
59
59
proxyServer .CertificateManager .TrustRootCertificate = true ;
60
60
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
63
63
// proxyServer.CertificateManager.CertificateEngine = Network.CertificateEngine.BouncyCastle;
64
64
65
65
proxyServer .BeforeRequest += OnRequest ;
@@ -70,28 +70,28 @@ proxyServer.ClientCertificateSelectionCallback += OnCertificateSelection;
70
70
71
71
var explicitEndPoint = new ExplicitProxyEndPoint (IPAddress .Any , 8000 , true )
72
72
{
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")
77
77
};
78
78
79
- // Fired when a CONNECT request is received
79
+ // Fired when a CONNECT request is received
80
80
explicitEndPoint .BeforeTunnelConnect += OnBeforeTunnelConnect ;
81
81
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
84
84
proxyServer .AddEndPoint (explicitEndPoint );
85
85
proxyServer .Start ();
86
86
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
90
90
var transparentEndPoint = new TransparentProxyEndPoint (IPAddress .Any , 8001 , true )
91
91
{
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"
95
95
};
96
96
97
97
proxyServer .AddEndPoint (transparentEndPoint );
@@ -103,36 +103,36 @@ foreach (var endPoint in proxyServer.ProxyEndPoints)
103
103
Console .WriteLine (" Listening on '{0}' endpoint at Ip {1} and port: {2} " ,
104
104
endPoint .GetType ().Name , endPoint .IpAddress , endPoint .Port );
105
105
106
- // Only explicit proxies can be set as system proxy!
106
+ // Only explicit proxies can be set as system proxy!
107
107
proxyServer .SetAsSystemHttpProxy (explicitEndPoint );
108
108
proxyServer .SetAsSystemHttpsProxy (explicitEndPoint );
109
109
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)
111
111
Console .Read ();
112
112
113
- // Unsubscribe & Quit
113
+ // Unsubscribe & Quit
114
114
explicitEndPoint .BeforeTunnelConnect -= OnBeforeTunnelConnect ;
115
115
proxyServer .BeforeRequest -= OnRequest ;
116
116
proxyServer .BeforeResponse -= OnResponse ;
117
117
proxyServer .ServerCertificateValidationCallback -= OnCertificateValidation ;
118
118
proxyServer .ClientCertificateSelectionCallback -= OnCertificateSelection ;
119
119
120
120
proxyServer .Stop ();
121
-
121
+
122
122
```
123
123
Sample request and response event handlers
124
124
125
- ``` csharp
125
+ ``` csharp
126
126
127
127
private async Task OnBeforeTunnelConnectRequest (object sender , TunnelConnectSessionEventArgs e )
128
128
{
129
129
string hostname = e .HttpClient .Request .RequestUri .Host ;
130
130
131
131
if (hostname .Contains (" dropbox.com" ))
132
132
{
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
136
136
e .DecryptSsl = false ;
137
137
}
138
138
}
@@ -141,88 +141,88 @@ public async Task OnRequest(object sender, SessionEventArgs e)
141
141
{
142
142
Console .WriteLine (e .HttpClient .Request .Url );
143
143
144
- // // read request headers
144
+ // read request headers
145
145
var requestHeaders = e .HttpClient .Request .RequestHeaders ;
146
146
147
147
var method = e .HttpClient .Request .Method .ToUpper ();
148
148
if ((method == " POST" || method == " PUT" || method == " PATCH" ))
149
149
{
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 ;
161
161
}
162
162
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
165
165
if (e .HttpClient .Request .RequestUri .AbsoluteUri .Contains (" google.com" ))
166
166
{
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>" );
174
174
}
175
- // Redirect example
175
+
176
+ // Redirect example
176
177
if (e .HttpClient .Request .RequestUri .AbsoluteUri .Contains (" wikipedia.org" ))
177
178
{
178
- e .Redirect (" https://www.paypal.com" );
179
+ e .Redirect (" https://www.paypal.com" );
179
180
}
180
181
}
181
182
182
- // Modify response
183
+ // Modify response
183
184
public async Task OnResponse (object sender , SessionEventArgs e )
184
185
{
185
- // read response headers
186
+ // read response headers
186
187
var responseHeaders = e .HttpClient .Response .ResponseHeaders ;
187
188
188
189
// if (!e.ProxySession.Request.Host.Equals("medeczane.sgk.gov.tr")) return;
189
190
if (e .HttpClient .Request .Method == " GET" || e .HttpClient .Request .Method == " POST" )
190
191
{
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
+ }
202
203
}
203
204
204
- if (e .UserData != null )
205
+ if (e .UserData != null )
205
206
{
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 ;
208
209
}
209
-
210
210
}
211
211
212
- /// Allows overriding default certificate validation logic
212
+ // Allows overriding default certificate validation logic
213
213
public Task OnCertificateValidation (object sender , CertificateValidationEventArgs e )
214
214
{
215
- // set IsValid to true/false based on Certificate Errors
215
+ // set IsValid to true/false based on Certificate Errors
216
216
if (e .SslPolicyErrors == System .Net .Security .SslPolicyErrors .None )
217
- e .IsValid = true ;
217
+ e .IsValid = true ;
218
218
219
219
return Task .FromResult (0 );
220
220
}
221
221
222
- /// Allows overriding default client certificate selection logic during mutual authentication
222
+ // Allows overriding default client certificate selection logic during mutual authentication
223
223
public Task OnCertificateSelection (object sender , CertificateSelectionEventArgs e )
224
224
{
225
- // set e.clientCertificate to override
225
+ // set e.clientCertificate to override
226
226
return Task .FromResult (0 );
227
227
}
228
228
```
0 commit comments