Commit bc76f768 authored by titanium007's avatar titanium007

Add validation for function calls

parent 0818cb20
......@@ -60,28 +60,28 @@ namespace Titanium.Web.Proxy.Test
Console.WriteLine(e.RequestURL);
////modify request headers
//var requestHeaders = e.RequestHeaders;
//modify request headers
var requestHeaders = e.RequestHeaders;
//if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT") && e.RequestContentLength > 0)
//{
// //Get/Set request body bytes
// byte[] bodyBytes = e.GetRequestBody();
// e.SetRequestBody(bodyBytes);
if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT") && e.RequestContentLength > 0)
{
//Get/Set request body bytes
byte[] bodyBytes = e.GetRequestBody();
e.SetRequestBody(bodyBytes);
// //Get/Set request body as string
// string bodyString = e.GetRequestBodyAsString();
// e.SetRequestBodyString(bodyString);
//Get/Set request body as string
string bodyString = e.GetRequestBodyAsString();
e.SetRequestBodyString(bodyString);
//}
}
////To cancel a request with a custom HTML content
////Filter URL
//To cancel a request with a custom HTML content
//Filter URL
//if (e.RequestURL.Contains("google.com"))
//{
// e.Ok("<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>");
//}
if (e.RequestURL.Contains("google.com"))
{
e.Ok("<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>");
}
}
......@@ -90,27 +90,28 @@ namespace Titanium.Web.Proxy.Test
public void OnResponse(object sender, SessionEventArgs e)
{
//modify response headers
//var responseHeaders = e.ResponseHeaders;
//if (e.ResponseStatusCode == HttpStatusCode.OK)
//{
// if (e.ResponseContentType.Trim().ToLower().Contains("text/html"))
// {
// //Get/Set response body bytes
// byte[] responseBodyBytes = e.GetResponseBody();
// e.SetResponseBody(responseBodyBytes);
// //Get response body as string
// string responseBody = e.GetResponseBodyAsString();
// //Modify e.ServerResponse
// Regex rex = new Regex("</body>", RegexOptions.RightToLeft | RegexOptions.IgnoreCase | RegexOptions.Multiline);
// string modified = rex.Replace(responseBody, "<script type =\"text/javascript\">alert('Response was modified by this script!');</script></body>", 1);
// //Set modifed response Html Body
// e.SetResponseBodyString(modified);
// }
//}
var responseHeaders = e.ResponseHeaders;
if (e.ResponseStatusCode == HttpStatusCode.OK)
{
if (e.ResponseContentType.Trim().ToLower().Contains("text/html"))
{
//Get/Set response body bytes
byte[] responseBodyBytes = e.GetResponseBody();
e.SetResponseBody(responseBodyBytes);
//Get response body as string
string responseBody = e.GetResponseBodyAsString();
//Modify e.ServerResponse
Regex rex = new Regex("</body>", RegexOptions.RightToLeft | RegexOptions.IgnoreCase | RegexOptions.Multiline);
string modified = rex.Replace(responseBody, "<script type =\"text/javascript\">alert('Response was modified by this script!');</script></body>", 1);
//Set modifed response Html Body
e.SetResponseBodyString(modified);
}
}
}
......
......@@ -35,7 +35,10 @@ namespace Titanium.Web.Proxy.EventArguments
internal string responseBodyString { get; set; }
internal bool responseBodyRead { get; set; }
internal bool RequestLocked { get; set; }
internal HttpWebRequest proxyRequest { get; set; }
internal bool ResponseLocked { get; set; }
internal HttpWebResponse serverResponse { get; set; }
internal bool isHttps { get; set; }
......@@ -132,11 +135,15 @@ namespace Titanium.Web.Proxy.EventArguments
public Encoding GetRequestBodyEncoding()
{
if (RequestLocked) throw new Exception("You cannot call this function after request is made to server.");
return requestEncoding;
}
public byte[] GetRequestBody()
{
if (RequestLocked) throw new Exception("You cannot call this function after request is made to server.");
if ((proxyRequest.Method.ToUpper() == "POST" || proxyRequest.Method.ToUpper() == "PUT") && RequestContentLength > 0)
{
readRequestBody();
......@@ -150,6 +157,8 @@ namespace Titanium.Web.Proxy.EventArguments
}
public string GetRequestBodyAsString()
{
if (RequestLocked) throw new Exception("You cannot call this function after request is made to server.");
if ((proxyRequest.Method.ToUpper() == "POST" || proxyRequest.Method.ToUpper() == "PUT") && RequestContentLength > 0)
{
readRequestBody();
......@@ -168,6 +177,8 @@ namespace Titanium.Web.Proxy.EventArguments
public void SetRequestBody(byte[] body)
{
if (RequestLocked) throw new Exception("You cannot call this function after request is made to server.");
if (!requestBodyRead)
{
readRequestBody();
......@@ -179,6 +190,8 @@ namespace Titanium.Web.Proxy.EventArguments
public void SetRequestBodyString(string body)
{
if (RequestLocked) throw new Exception("Youcannot call this function after request is made to server.");
if (!requestBodyRead)
{
readRequestBody();
......@@ -190,17 +203,24 @@ namespace Titanium.Web.Proxy.EventArguments
public Encoding GetResponseBodyEncoding()
{
if (!RequestLocked) throw new Exception("You cannot call this function before request is made to server.");
return responseEncoding;
}
public byte[] GetResponseBody()
{
if (!RequestLocked) throw new Exception("You cannot call this function before request is made to server.");
readResponseBody();
return responseBody;
}
public string GetResponseBodyAsString()
{
if (!RequestLocked) throw new Exception("You cannot call this function before request is made to server.");
GetResponseBody();
if (responseBodyString == null)
{
responseBodyString = responseEncoding.GetString(responseBody);
......@@ -209,6 +229,8 @@ namespace Titanium.Web.Proxy.EventArguments
}
public void SetResponseBody(byte[] body)
{
if (!RequestLocked) throw new Exception("You cannot call this function before request is made to server.");
if (responseBody == null)
{
GetResponseBody();
......@@ -220,6 +242,8 @@ namespace Titanium.Web.Proxy.EventArguments
}
public void SetResponseBodyString(string body)
{
if (!RequestLocked) throw new Exception("You cannot call this function before request is made to server.");
if (responseBody == null)
{
GetResponseBody();
......@@ -232,6 +256,7 @@ namespace Titanium.Web.Proxy.EventArguments
public void Ok(string html)
{
if (RequestLocked) throw new Exception("You cannot call this function after request is made to server.");
if (html == null)
html = string.Empty;
......
......@@ -60,7 +60,7 @@ namespace Titanium.Web.Proxy
httpRemoteUri = new Uri("https://" + httpCmdSplit[1]);
clientStreamReader.ReadAllLines();
WriteConnectedResponse(clientStreamWriter, httpVersion);
WriteConnectResponse(clientStreamWriter, httpVersion);
var certificate = ProxyServer.CertManager.CreateCertificate(httpRemoteUri.Host);
......@@ -94,7 +94,7 @@ namespace Titanium.Web.Proxy
else if (httpVerb.ToUpper() == "CONNECT")
{
clientStreamReader.ReadAllLines();
WriteConnectedResponse(clientStreamWriter, httpVersion);
WriteConnectResponse(clientStreamWriter, httpVersion);
TcpHelper.SendRaw(clientStreamReader.BaseStream, null, null, httpRemoteUri.Host, httpRemoteUri.Port, false);
Dispose(client, clientStream, clientStreamReader, clientStreamWriter, null);
return;
......@@ -112,14 +112,7 @@ namespace Titanium.Web.Proxy
}
private static void WriteConnectedResponse(StreamWriter clientStreamWriter, string httpVersion)
{
clientStreamWriter.WriteLine(httpVersion + " 200 Connection established");
clientStreamWriter.WriteLine(String.Format("Timestamp: {0}", DateTime.Now.ToString()));
clientStreamWriter.WriteLine(String.Format("connection:close"));
clientStreamWriter.WriteLine();
clientStreamWriter.Flush();
}
private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, Stream clientStream, CustomBinaryReader clientStreamReader, StreamWriter clientStreamWriter, string secureTunnelHostName)
{
......@@ -203,14 +196,16 @@ namespace Titanium.Web.Proxy
args.proxyRequest.ConnectionGroupName = args.requestHostname;
args.proxyRequest.AllowWriteStreamBuffering = true;
//If requested interception
if (BeforeRequest != null)
{
args.requestEncoding = args.proxyRequest.GetEncoding();
BeforeRequest(null, args);
}
args.RequestLocked = true;
if (args.cancelRequest)
{
Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args);
......@@ -257,6 +252,15 @@ namespace Titanium.Web.Proxy
}
private static void WriteConnectResponse(StreamWriter clientStreamWriter, string httpVersion)
{
clientStreamWriter.WriteLine(httpVersion + " 200 Connection established");
clientStreamWriter.WriteLine(String.Format("Timestamp: {0}", DateTime.Now.ToString()));
clientStreamWriter.WriteLine(String.Format("connection:close"));
clientStreamWriter.WriteLine();
clientStreamWriter.Flush();
}
private static void SetRequestHeaders(List<HttpHeader> requestHeaders, HttpWebRequest webRequest)
{
......
......@@ -41,12 +41,15 @@ namespace Titanium.Web.Proxy
args.ResponseHeaders = ReadResponseHeaders(args.serverResponse);
args.responseStream = args.serverResponse.GetResponseStream();
if (BeforeResponse != null)
{
args.responseEncoding = args.serverResponse.GetEncoding();
BeforeResponse(null, args);
}
args.ResponseLocked = true;
if (args.responseBodyRead)
{
bool isChunked = args.ResponseHeaders.Any(x => x.Name.ToLower() == "transfer-encoding") == false ? false : args.ResponseHeaders.First(x => x.Name.ToLower() == "transfer-encoding").Value.ToLower() == "chunked" ? true : false;
......
......@@ -90,7 +90,7 @@
<Compile Include="Helpers\NetFramework.cs" />
<Compile Include="Helpers\Compression.cs" />
<Compile Include="ProxyServer.cs" />
<Compile Include="EventArgs\SessionEventArgs.cs" />
<Compile Include="EventArguments\SessionEventArgs.cs" />
<Compile Include="Helpers\Tcp.cs" />
<Compile Include="Extensions\StreamExtensions.cs" />
</ItemGroup>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment