FeedProxy: Introduced a config file to define the allowed hostnames. Also added a package action to allow hostnames.

This commit is contained in:
leekelleher
2012-04-06 11:06:05 -01:00
parent 82a8383df3
commit c6891dd971
7 changed files with 154 additions and 18 deletions

View File

@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<feedProxy>
<!-- To enable the FeedProxy to access other domains, please add the hostname to an 'allow' element. -->
<!-- <allow host="domain.com" /> -->
<!-- The following hostnames are used by on the dashboards, please do not delete these. -->
<allow host="umbraco.com" />
<allow host="umbraco.org" />
</feedProxy>

View File

@@ -38,6 +38,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "config", "config", "{05329D
config templates\config\Dashboard.config = config templates\config\Dashboard.config
config templates\config\ExamineIndex.config = config templates\config\ExamineIndex.config
config templates\config\ExamineSettings.config = config templates\config\ExamineSettings.config
config templates\config\feedProxy.config = config templates\config\feedProxy.config
config templates\config\formHandlers.config = config templates\config\formHandlers.config
config templates\config\metablogConfig.config = config templates\config\metablogConfig.config
config templates\config\restExtensions.config = config templates\config\restExtensions.config

View File

@@ -84,6 +84,14 @@ namespace umbraco.IO
}
}
public static string FeedProxyConfig
{
get
{
return string.Concat(SystemDirectories.Config, "/feedProxy.config");
}
}
public static string ContentCacheXml
{
get

View File

@@ -520,6 +520,98 @@ namespace umbraco.cms.businesslogic.packager.standardPackageActions
}
public class addProxyFeedHost : umbraco.interfaces.IPackageAction
{
#region IPackageAction Members
public bool Execute(string packageName, XmlNode xmlData)
{
var hostname = xmlData.Attributes["host"].Value;
if (string.IsNullOrEmpty(hostname))
return false;
var xdoc = xmlHelper.OpenAsXmlDocument(SystemFiles.FeedProxyConfig);
xdoc.PreserveWhitespace = true;
var xn = xdoc.SelectSingleNode("//feedProxy");
if (xn != null)
{
var insert = true;
if (xn.HasChildNodes)
{
foreach (XmlNode node in xn.SelectNodes("//allow"))
{
if (node.Attributes["host"] != null && node.Attributes["host"].Value == hostname)
insert = false;
}
}
if (insert)
{
var newHostname = xmlHelper.addTextNode(xdoc, "allow", string.Empty);
newHostname.Attributes.Append(xmlHelper.addAttribute(xdoc, "host", hostname));
xn.AppendChild(newHostname);
xdoc.Save(IOHelper.MapPath(SystemFiles.FeedProxyConfig));
return true;
}
}
return false;
}
public string Alias()
{
return "addProxyFeedHost";
}
public bool Undo(string packageName, XmlNode xmlData)
{
var hostname = xmlData.Attributes["host"].Value;
if (string.IsNullOrEmpty(hostname))
return false;
var xdoc = xmlHelper.OpenAsXmlDocument(SystemFiles.FeedProxyConfig);
xdoc.PreserveWhitespace = true;
var xn = xdoc.SelectSingleNode("//feedProxy");
if (xn != null)
{
bool inserted = false;
if (xn.HasChildNodes)
{
foreach (XmlNode node in xn.SelectNodes("//allow"))
{
if (node.Attributes["host"] != null && node.Attributes["host"].Value == hostname)
{
xn.RemoveChild(node);
inserted = true;
}
}
}
if (inserted)
{
xdoc.Save(IOHelper.MapPath(SystemFiles.FeedProxyConfig));
return true;
}
}
return false;
}
#endregion
public XmlNode SampleXml()
{
string sample = "<Action runat=\"install\" undo=\"true\" alias=\"addProxyFeedHost\" host=\"umbraco.com\"/>";
return helper.parseStringToXmlNode(sample);
}
}
/// <summary>
/// This class implements the IPackageAction Interface, used to execute code when packages are installed.
/// All IPackageActions only takes a PackageName and a XmlNode as input, and executes based on the data in the xmlnode.

View File

@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<feedProxy>
<!-- To enable the FeedProxy to access other domains, please add the hostname to an 'allow' element. -->
<!-- <allow host="domain.com" /> -->
<!-- The following hostnames are used by on the dashboards, please do not delete these. -->
<allow host="umbraco.com" />
<allow host="umbraco.org" />
</feedProxy>

View File

@@ -2572,6 +2572,7 @@
<Content Include="config\ExamineIndex.config" />
<Content Include="config\scripting.config" />
<Content Include="config\Skinning.config" />
<Content Include="config\feedProxy.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings1.Designer.cs</LastGenOutput>

View File

@@ -1,37 +1,53 @@
namespace dashboardUtilities
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Net.Mime;
using System.Xml;
using umbraco.BasePages;
using umbraco.BusinessLogic;
using umbraco.IO;
using umbraco;
public partial class FeedProxy : UmbracoEnsuredPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request["url"] != null)
try
{
var requestUri = new Uri(Request["url"]);
if (requestUri != null)
if (Request.QueryString.AllKeys.Contains("url") && Request.QueryString["url"] != null)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
request.Method = WebRequestMethods.Http.Get;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string tmp = reader.ReadToEnd();
response.Close();
var url = Request.QueryString["url"];
if (!string.IsNullOrWhiteSpace(url) && !url.StartsWith("/"))
{
Uri requestUri;
if (Uri.TryCreate(url, UriKind.Absolute, out requestUri))
{
var feedProxyXml = xmlHelper.OpenAsXmlDocument(IOHelper.MapPath(SystemFiles.FeedProxyConfig));
if (feedProxyXml != null && feedProxyXml.SelectSingleNode(string.Concat("//access[@host = '", requestUri.Host, "']")) == null)
{
using (var client = new WebClient())
{
var response = client.DownloadString(requestUri);
Response.Clear();
Response.ContentType = "text/xml";
Response.Write(tmp);
if (!string.IsNullOrEmpty(response))
{
Response.Clear();
Response.ContentType = Request.QueryString["type"] ?? MediaTypeNames.Text.Xml;
Response.Write(response);
Response.End();
}
}
}
}
}
}
}
catch (Exception ex)
{
Log.Add(LogTypes.Error, -1, ex.ToString());
}
}
}
}