Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 2eed8cb

Browse files
committed
space before the commetns
1 parent 67c1e26 commit 2eed8cb

File tree

20 files changed

+174
-171
lines changed

20 files changed

+174
-171
lines changed

README.md

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ For stable releases on [stable branch](https://github.com/justcoding121/Titanium
3030
Supports
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
5656
var proxyServer = new ProxyServer();
5757

58-
//locally trust root certificate used by this proxy
58+
// locally trust root certificate used by this proxy
5959
proxyServer.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
6565
proxyServer.BeforeRequest += OnRequest;
@@ -70,28 +70,28 @@ proxyServer.ClientCertificateSelectionCallback += OnCertificateSelection;
7070

7171
var 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
8080
explicitEndPoint.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
8484
proxyServer.AddEndPoint(explicitEndPoint);
8585
proxyServer.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
9090
var 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

9797
proxyServer.AddEndPoint(transparentEndPoint);
@@ -103,36 +103,36 @@ foreach (var endPoint in proxyServer.ProxyEndPoints)
103103
Console.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!
107107
proxyServer.SetAsSystemHttpProxy(explicitEndPoint);
108108
proxyServer.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)
111111
Console.Read();
112112

113-
//Unsubscribe & Quit
113+
// Unsubscribe & Quit
114114
explicitEndPoint.BeforeTunnelConnect -= OnBeforeTunnelConnect;
115115
proxyServer.BeforeRequest -= OnRequest;
116116
proxyServer.BeforeResponse -= OnResponse;
117117
proxyServer.ServerCertificateValidationCallback -= OnCertificateValidation;
118118
proxyServer.ClientCertificateSelectionCallback -= OnCertificateSelection;
119119

120120
proxyServer.Stop();
121-
121+
122122
```
123123
Sample request and response event handlers
124124

125-
```csharp
125+
```csharp
126126

127127
private 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
183184
public 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
213213
public 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
223223
public 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
```

examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ private async Task onResponse(object sender, SessionEventArgs e)
193193

194194
string ext = System.IO.Path.GetExtension(e.HttpClient.Request.RequestUri.AbsolutePath);
195195

196-
//access user data set in request to do something with it
196+
// access user data set in request to do something with it
197197
//var userData = e.HttpClient.UserData as CustomUserData;
198198

199199
//if (ext == ".gif" || ext == ".png" || ext == ".jpg")

src/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -615,36 +615,36 @@ public void Redirect(string url, bool closeServerConnection = false)
615615
/// <param name="closeServerConnection">Close the server connection used by request if any?</param>
616616
public void Respond(Response response, bool closeServerConnection = false)
617617
{
618-
//request already send/ready to be sent.
618+
// request already send/ready to be sent.
619619
if (HttpClient.Request.Locked)
620620
{
621-
//response already received from server and ready to be sent to client.
621+
// response already received from server and ready to be sent to client.
622622
if (HttpClient.Response.Locked)
623623
{
624624
throw new Exception("You cannot call this function after response is sent to the client.");
625625
}
626626

627-
//cleanup original response.
627+
// cleanup original response.
628628
if (closeServerConnection)
629629
{
630-
//no need to cleanup original connection.
631-
//it will be closed any way.
630+
// no need to cleanup original connection.
631+
// it will be closed any way.
632632
TerminateServerConnection();
633633
}
634634

635635
response.SetOriginalHeaders(HttpClient.Response);
636636

637-
//response already received from server but not yet ready to sent to client.
637+
// response already received from server but not yet ready to sent to client.
638638
HttpClient.Response = response;
639639
HttpClient.Response.Locked = true;
640640
}
641-
//request not yet sent/not yet ready to be sent.
641+
// request not yet sent/not yet ready to be sent.
642642
else
643643
{
644644
HttpClient.Request.Locked = true;
645645
HttpClient.Request.CancelRequest = true;
646646

647-
//set new response.
647+
// set new response.
648648
HttpClient.Response = response;
649649
HttpClient.Response.Locked = true;
650650
}

src/Titanium.Web.Proxy/ExplicitClientHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ await clientStreamWriter.WriteResponseAsync(connectArgs.HttpClient.Response,
152152

153153
http2Supported = connection.NegotiatedApplicationProtocol ==
154154
SslApplicationProtocol.Http2;
155-
//release connection back to pool instead of closing when connection pool is enabled.
155+
156+
// release connection back to pool instead of closing when connection pool is enabled.
156157
await tcpConnectionFactory.Release(connection, true);
157158
}
158159
catch (Exception)

src/Titanium.Web.Proxy/Extensions/TcpExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ internal static bool IsGoodConnection(this TcpClient client)
5050

5151
socket.Blocking = false;
5252
socket.Send(tmp, 0, 0);
53-
//Connected.
53+
// Connected.
5454
}
5555
catch
5656
{
57-
//Should we let 10035 == WSAEWOULDBLOCK as valid connection?
57+
// Should we let 10035 == WSAEWOULDBLOCK as valid connection?
5858
return false;
5959
}
6060
finally

src/Titanium.Web.Proxy/Helpers/Network.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal static bool IsLocalIpAddress(string hostName)
4141
return true;
4242
}
4343

44-
//if hostname matches local host name
44+
// if hostname matches local host name
4545
if (hostName.Equals(localhostName, StringComparison.OrdinalIgnoreCase))
4646
{
4747
return true;

src/Titanium.Web.Proxy/Helpers/RunTime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static class RunTime
4242

4343
public static bool IsMac => isRunningOnMac;
4444

45-
//https://github.com/qmatteoq/DesktopBridgeHelpers/blob/master/DesktopBridge.Helpers/Helpers.cs
45+
// https://github.com/qmatteoq/DesktopBridgeHelpers/blob/master/DesktopBridge.Helpers/Helpers.cs
4646
private class UwpHelper
4747
{
4848
const long APPMODEL_ERROR_NO_PACKAGE = 15700L;

src/Titanium.Web.Proxy/Network/RetryPolicy.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ internal async Task<RetryResult> ExecuteAsync(Func<TcpServerConnection, Task<boo
3737
{
3838
try
3939
{
40-
//setup connection
40+
// setup connection
4141
currentConnection = currentConnection as TcpServerConnection ??
4242
await generator();
43-
//try
43+
// try
4444
@continue = await action(currentConnection);
4545

4646
}
@@ -65,12 +65,12 @@ internal async Task<RetryResult> ExecuteAsync(Func<TcpServerConnection, Task<boo
6565
return new RetryResult(currentConnection, exception, @continue);
6666
}
6767

68-
//before retry clear connection
68+
// before retry clear connection
6969
private async Task disposeConnection()
7070
{
7171
if (currentConnection != null)
7272
{
73-
//close connection on error
73+
// close connection on error
7474
await tcpConnectionFactory.Release(currentConnection, true);
7575
currentConnection = null;
7676
}

src/Titanium.Web.Proxy/Network/Tcp/TcpClientConnection.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ public void Dispose()
8282
{
8383
Task.Run(async () =>
8484
{
85-
//delay calling tcp connection close()
86-
//so that client have enough time to call close first.
87-
//This way we can push tcp Time_Wait to client side when possible.
85+
// delay calling tcp connection close()
86+
// so that client have enough time to call close first.
87+
// This way we can push tcp Time_Wait to client side when possible.
8888
await Task.Delay(1000);
8989
proxyServer.UpdateClientConnectionCount(false);
9090
tcpClient.CloseSocket();

0 commit comments

Comments
 (0)