Commit e0010612 authored by justcoding121's avatar justcoding121 Committed by justcoding121

Fix web socket issues

parent e7ea27b5
...@@ -60,22 +60,22 @@ namespace Titanium.Web.Proxy.Test ...@@ -60,22 +60,22 @@ namespace Titanium.Web.Proxy.Test
Console.WriteLine(e.RequestURL); Console.WriteLine(e.RequestURL);
if (e.RequestURL.Contains("somewebsite.com")) //if (e.RequestURL.Contains("somewebsite.com"))
if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT") && e.RequestContentLength > 0) // if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT") && e.RequestContentLength > 0)
{ // {
var m = e.GetRequestBody().Replace("a", "b"); // var m = e.GetRequestBody().Replace("a", "b");
e.SetRequestBody(m); // e.SetRequestBody(m);
} // }
//To cancel a request with a custom HTML content //To cancel a request with a custom HTML content
//Filter URL //Filter URL
if (e.RequestURL.Contains("somewebsite.com")) //if (e.RequestURL.Contains("somewebsite.com"))
{ //{
e.Ok("<!DOCTYPE html><html><body><h1>Blocked</h1><p>Website blocked.</p></body></html>"); // e.Ok("<!DOCTYPE html><html><body><h1>Blocked</h1><p>Website blocked.</p></body></html>");
} //}
} }
...@@ -85,22 +85,22 @@ namespace Titanium.Web.Proxy.Test ...@@ -85,22 +85,22 @@ namespace Titanium.Web.Proxy.Test
{ {
//To modify a response //To modify a response
if (e.RequestURL.Contains("somewebsite.com")) //if (e.RequestURL.Contains("somewebsite.com"))
if (e.ResponseStatusCode == HttpStatusCode.OK) //if (e.ResponseStatusCode == HttpStatusCode.OK)
{ //{
if (e.ResponseContentType.Trim().ToLower().Contains("text/html")) // if (e.ResponseContentType.Trim().ToLower().Contains("text/html"))
{ // {
//Get response body // //Get response body
string responseBody = e.GetResponseBody(); // string responseBody = e.GetResponseBody();
//Modify e.ServerResponse // //Modify e.ServerResponse
Regex rex = new Regex("</body>", RegexOptions.RightToLeft | RegexOptions.IgnoreCase | RegexOptions.Multiline); // 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); // string modified = rex.Replace(responseBody, "<script type =\"text/javascript\">alert('Response was modified by this script!');</script></body>", 1);
//Set modifed response Html Body // //Set modifed response Html Body
e.SetResponseBody(modified); // e.SetResponseBody(modified);
} // }
} //}
} }
......
...@@ -4,19 +4,20 @@ using System.Linq; ...@@ -4,19 +4,20 @@ using System.Linq;
using System.Text; using System.Text;
using System.IO; using System.IO;
namespace Titanium.Web.Proxy.Helpers namespace Titanium.Web.Proxy.Extensions
{ {
public static class StreamHelper public static class StreamHelper
{ {
private const int DEFAULT_BUFFER_SIZE = 8192; // +32767 private const int DEFAULT_BUFFER_SIZE = 8192; // +32767
public static void CopyTo(string initialData, Stream input, Stream output, int bufferSize) public static void CopyToAsync(this Stream input, string initialData, Stream output, int bufferSize)
{ {
var bytes = Encoding.ASCII.GetBytes(initialData); var bytes = Encoding.ASCII.GetBytes(initialData);
output.Write(bytes,0, bytes.Length); output.Write(bytes,0, bytes.Length);
CopyTo(input, output, bufferSize); CopyToAsync(input, output, bufferSize);
} }
public static void CopyTo(Stream input, Stream output, int bufferSize) //http://stackoverflow.com/questions/1540658/net-asynchronous-stream-read-write
public static void CopyToAsync(this Stream input, Stream output, int bufferSize)
{ {
try try
{ {
......
...@@ -7,16 +7,16 @@ using System.Diagnostics; ...@@ -7,16 +7,16 @@ using System.Diagnostics;
namespace Titanium.Web.Proxy.Helpers namespace Titanium.Web.Proxy.Helpers
{ {
public class CustomBinaryReader : BinaryReader internal class CustomBinaryReader : BinaryReader
{ {
public CustomBinaryReader(Stream stream, Encoding encoding) internal CustomBinaryReader(Stream stream, Encoding encoding)
: base(stream, encoding) : base(stream, encoding)
{ {
} }
public string ReadLine() internal string ReadLine()
{ {
char[] buf = new char[1]; char[] buf = new char[1];
StringBuilder readBuffer = new StringBuilder(); StringBuilder readBuffer = new StringBuilder();
...@@ -54,5 +54,16 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -54,5 +54,16 @@ namespace Titanium.Web.Proxy.Helpers
} }
internal List<string> ReadAllLines()
{
string tmpLine = null;
List<string> requestLines = new List<string>();
while (!String.IsNullOrEmpty(tmpLine = ReadLine()))
{
requestLines.Add(tmpLine);
}
return requestLines;
}
} }
} }
...@@ -7,6 +7,7 @@ using System.IO; ...@@ -7,6 +7,7 @@ using System.IO;
using System.Net; using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Net.Sockets; using System.Net.Sockets;
using Titanium.Web.Proxy.Extensions;
namespace Titanium.Web.Proxy.Helpers namespace Titanium.Web.Proxy.Helpers
{ {
...@@ -14,85 +15,30 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -14,85 +15,30 @@ namespace Titanium.Web.Proxy.Helpers
{ {
private static readonly int BUFFER_SIZE = 8192; private static readonly int BUFFER_SIZE = 8192;
private static readonly String[] colonSpaceSplit = new string[] { ": " }; private static readonly String[] colonSpaceSplit = new string[] { ": " };
public static void SendRaw(string hostname, int tunnelPort, System.IO.Stream clientStream)
public static void SendRaw(Stream clientStream, string httpCmd, List<string> requestLines, string hostName, int tunnelPort, bool isHttps)
{ {
StringBuilder sb = new StringBuilder();
System.Net.Sockets.TcpClient tunnelClient = null; if (httpCmd != null)
NetworkStream tunnelStream = null;
try
{
tunnelClient = new System.Net.Sockets.TcpClient(hostname, tunnelPort);
tunnelStream = tunnelClient.GetStream();
var tunnelReadBuffer = new byte[BUFFER_SIZE];
Task sendRelay = Task.Factory.StartNew(() => StreamHelper.CopyTo(clientStream, tunnelStream, BUFFER_SIZE));
Task receiveRelay = Task.Factory.StartNew(() => StreamHelper.CopyTo(tunnelStream, clientStream, BUFFER_SIZE));
Task.WaitAll(sendRelay, receiveRelay);
}
catch
{ {
if (tunnelStream != null) sb.Append(httpCmd);
{ sb.Append(Environment.NewLine);
tunnelStream.Close();
tunnelStream.Dispose();
}
if (tunnelClient != null)
tunnelClient.Close();
throw;
} }
} for (int i = 0; i < requestLines.Count; i++)
public static void SendRaw(string httpCmd, string secureHostName, List<string> requestLines, bool isHttps, Stream clientStream)
{
StringBuilder sb = new StringBuilder();
sb.Append(httpCmd);
sb.Append(Environment.NewLine);
string hostname = secureHostName;
for (int i = 1; i < requestLines.Count; i++)
{ {
var header = requestLines[i]; var header = requestLines[i];
if (secureHostName == null)
{
String[] headerParsed = httpCmd.Split(colonSpaceSplit, 2, StringSplitOptions.None);
switch (headerParsed[0].ToLower())
{
case "host":
var hostdetail = headerParsed[1];
if (hostdetail.Contains(":"))
hostname = hostdetail.Split(':')[0].Trim();
else
hostname = hostdetail.Trim();
break;
default:
break;
}
}
sb.Append(header); sb.Append(header);
sb.Append(Environment.NewLine); sb.Append(Environment.NewLine);
} }
sb.Append(Environment.NewLine); sb.Append(Environment.NewLine);
int tunnelPort = 80;
if (isHttps)
{
tunnelPort = 443;
}
System.Net.Sockets.TcpClient tunnelClient = null; System.Net.Sockets.TcpClient tunnelClient = null;
Stream tunnelStream = null; Stream tunnelStream = null;
try try
{ {
tunnelClient = new System.Net.Sockets.TcpClient(hostname, tunnelPort); tunnelClient = new System.Net.Sockets.TcpClient(hostName, tunnelPort);
tunnelStream = tunnelClient.GetStream() as Stream; tunnelStream = tunnelClient.GetStream() as Stream;
if (isHttps) if (isHttps)
...@@ -101,7 +47,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -101,7 +47,7 @@ namespace Titanium.Web.Proxy.Helpers
try try
{ {
sslStream = new SslStream(tunnelStream); sslStream = new SslStream(tunnelStream);
sslStream.AuthenticateAsClient(hostname); sslStream.AuthenticateAsClient(hostName);
tunnelStream = sslStream; tunnelStream = sslStream;
} }
catch catch
...@@ -112,8 +58,8 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -112,8 +58,8 @@ namespace Titanium.Web.Proxy.Helpers
} }
var sendRelay = Task.Factory.StartNew(() => StreamHelper.CopyTo(sb.ToString(), clientStream, tunnelStream, BUFFER_SIZE)); var sendRelay = Task.Factory.StartNew(() => clientStream.CopyToAsync(sb.ToString(), tunnelStream, BUFFER_SIZE));
var receiveRelay = Task.Factory.StartNew(() => StreamHelper.CopyTo(tunnelStream, clientStream, BUFFER_SIZE)); var receiveRelay = Task.Factory.StartNew(() => tunnelStream.CopyToAsync(clientStream, BUFFER_SIZE));
Task.WaitAll(sendRelay, receiveRelay); Task.WaitAll(sendRelay, receiveRelay);
} }
...@@ -131,5 +77,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -131,5 +77,7 @@ namespace Titanium.Web.Proxy.Helpers
throw; throw;
} }
} }
} }
} }
\ No newline at end of file
...@@ -19,8 +19,7 @@ namespace Titanium.Web.Proxy.Models ...@@ -19,8 +19,7 @@ namespace Titanium.Web.Proxy.Models
internal CustomBinaryReader clientStreamReader { get; set; } internal CustomBinaryReader clientStreamReader { get; set; }
internal StreamWriter clientStreamWriter { get; set; } internal StreamWriter clientStreamWriter { get; set; }
internal string httpsHostName { get; set; }
internal string httpsDecoratedHostName { get; set; }
internal int requestContentLength { get; set; } internal int requestContentLength { get; set; }
internal Encoding requestEncoding { get; set; } internal Encoding requestEncoding { get; set; }
internal Version requestHttpVersion { get; set; } internal Version requestHttpVersion { get; set; }
......
This diff is collapsed.
...@@ -90,7 +90,7 @@ ...@@ -90,7 +90,7 @@
<Compile Include="ProxyServer.cs" /> <Compile Include="ProxyServer.cs" />
<Compile Include="Models\SessionEventArgs.cs" /> <Compile Include="Models\SessionEventArgs.cs" />
<Compile Include="Helpers\Tcp.cs" /> <Compile Include="Helpers\Tcp.cs" />
<Compile Include="Helpers\Stream.cs" /> <Compile Include="Extensions\StreamExtensions.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup /> <ItemGroup />
<ItemGroup> <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