Skip to content

Instantly share code, notes, and snippets.

@orellabac
Last active September 9, 2019 00:54
Show Gist options
  • Save orellabac/25a6edaf7f02bfde9cecdeb98896a828 to your computer and use it in GitHub Desktop.
Save orellabac/25a6edaf7f02bfde9cecdeb98896a828 to your computer and use it in GitHub Desktop.
ProxyController
using HtmlAgilityPack;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Web.Mvc;
namespace WebApplication9.Controllers
{
public class ProxyController : Controller
{
/// <summary>
/// Adjust html node if there are relative. Action attributes require thjat we keep the document base
/// </summary>
public void fixNodes(HtmlDocument doc, string pageSourceURL, string tagName, string tagAttribute, string documentRoot = null)
{
foreach (HtmlNode node in doc.DocumentNode.SelectNodes(string.Format("//{0}/@{1}",tagName,tagAttribute)))
{
//Is it a relative path
var srcValue = node.Attributes[tagAttribute].Value;
if (!srcValue.StartsWith("http"))
{
if (documentRoot!=null)
node.Attributes[tagAttribute].Value = "/Proxy/" + Base64Encode(documentRoot + srcValue) + "/" + Base64Encode(pageSourceURL) + "/";
else
node.Attributes[tagAttribute].Value = pageSourceURL + srcValue;
}
}
}
public static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
public static string Base64Decode(string base64EncodedData)
{
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
public ActionResult Index(string url,string root)
{
string proxyURL = string.Empty;
try
{
proxyURL = Base64Decode(url);
if (string.IsNullOrWhiteSpace(root)) //No root was sent
root = null;
else
root = Base64Decode(root);
}
catch { }
if (proxyURL != string.Empty)
{
HttpWebRequest request;
if (Request.QueryString.Count==0)
request = (HttpWebRequest)WebRequest.Create(proxyURL);
else
{
request = (HttpWebRequest)WebRequest.Create(proxyURL + "?" + Request.QueryString);
}
//Use same method
request.Method = Request.HttpMethod;
//Transfer Headers
CopyHeaders(Request.Headers,request.Headers);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//Transfer Headers
CopyHeaders(response.Headers, Response.Headers);
if (response.StatusCode.ToString().ToLower() == "ok")
{
string contentType = response.ContentType;
Stream content = response.GetResponseStream();
//StreamReader contentReader = new StreamReader(content);
//var text = contentReader.ReadToEnd();
var doc = new HtmlDocument();
doc.Load(content);
root = root ?? proxyURL;
fixNodes(doc, root, "img", "src");
fixNodes(doc, root, "form", "action",root);
Response.ContentType = contentType;
doc.Save(Response.OutputStream);
}
}
return Content("");
}
/// <summary>
/// Quick function to copy header values between two collections
/// </summary>
static void CopyHeaders(NameValueCollection to, NameValueCollection from)
{
foreach (string header in from.AllKeys)
{
try
{to.Add(header, from[header]);}
catch
{}
}
}
}
}
@divz1010
Copy link

divz1010 commented Sep 6, 2019

i am trying to run the solution but its throwing error please let us know if we can get complete solution.Thanks in advance

@orellabac
Copy link
Author

I'm sorry. I don't think I have all the code for this sample. But if you let me know what errors do you have I can certainly help your fix them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment