PDA

View Full Version : سوال: مشکل در مقدار دهی httpContext برای ساخت پروکسی



DotNet_King
شنبه 25 خرداد 1387, 13:10 عصر
با سلام
کد زیر مربوط به یه پروکسی ساده با استفاده از HttpHandler است
مشکله من در نسبت دادن مقدار به یک شئی HttpContext است.به عبارتی به چه صورت باید httpContext رو مقدار دهی کنم که به آدرسی دلخواه بره.
کسی کی دونه به چه صورت می تو نم با استفاده از این کد یه سایت راه دور رو به صورت کد اچ تی ام ال تو سایتم دریافت کنم. اگه راهی دیگری هم برای این کار ، سراغ دارید لطفا اینجا ارائه بدید

تنظیمات وب کانفیگ :

کد برنامه :



using System;using System.Configuration;using System.Web;using System.Net;using System.Text;using System.IO; namespace ReverseProxy{ ///<summary> /// Handler that intercept Client's request and deliver the web site ///</summary> public class ReverseProxy: IHttpHandler { ///<summary> /// Method calls when client request the server ///</summary> /// &;lt;param name="context">HTTP context for client</param> public void ProcessRequest(HttpContext context) { //read values from configuration fileint proxyMode = Convert.ToInt32(ConfigurationSettings.AppSettings["ProxyMode"]); string remoteWebSite = ConfigurationSettings.AppSettings["RemoteWebSite"]; string remoteUrl; if (proxyMode==0) remoteUrl= ParseURL(context.Request.Url.AbsoluteUri); //all site acceptedelseremoteUrl= context.Request.Url.AbsoluteUri.Replace("http://"+ context.Request.Url.Host+ context.Request.ApplicationPath,remoteWebSite); //only one site accepted //create the web request to get the remote stream HttpWebRequest request = (HttpWebRequest)WebRequest.Create(remoteUrl); //TODO : you can add your own credentials system //request.Credentials = CredentialCache.DefaultCredentials; HttpWebResponse response; try { response = (HttpWebResponse)request.GetResponse (); } catch(System.Net.WebException we) { //remote url not found, send 404 to client context.Response.StatusCode = 404; context.Response.StatusDescription = "Not Found"; context.Response.Write("<h2>Page not found</h2>"); context.Response.End(); return; } Stream receiveStream = response.GetResponseStream(); if ((response.ContentType.ToLower().IndexOf("html")>=0) ||(response.ContentType.ToLower().IndexOf("javascript")>=0)) { //this response is HTML Content, so we must parse it StreamReader readStream = new StreamReader (receiveStream, Encoding.Default); Uri test = new Uri(remoteUrl); string content; if (proxyMode==0) content= ParseHtmlResponse(readStream.ReadToEnd(), context.Request.ApplicationPath+"/http//"+test.Host); else content= ParseHtmlResponse(readStream.ReadToEnd(), context.Request.ApplicationPath); //write the updated HTML to the client context.Response.Write(content); //close streamsreadStream.Close(); response.Close(); context.Response.End(); } else { //the response is not HTML Contentbyte[] buff = new byte[1024]; int bytes = 0; while( ( bytes = receiveStream.Read( buff, 0, 1024 ) ) > 0 ) { //Write the stream directly to the client context.Response.OutputStream.Write (buff, 0, bytes ); } //close streams response.Close(); context.Response.End(); } } ///<summary> /// Get the remote URL to call ///</summary> ///<paramname="url">URL get by client</param> ///<returns>Remote URL to return to the client</returns> public string ParseURL(string url) { if (url.IndexOf("http/")>=0) { string externalUrl=url.Substring(url.IndexOf("http/")); return externalUrl.Replace("http/","http://") ; } else return url; } ///<summary> /// Parse HTML response for update links and images sources ///</summary> ///<paramname="html">HTML response</param> ///<paramname="appPath">Path of application for replacement</param> ///<returns>HTML updated</returns> public string ParseHtmlResponse(string html,string appPath) { html=html.Replace("\"/","\""+appPath+"/"); html=html.Replace("'/","'"+appPath+"/"); html=html.Replace("=/","="+appPath+"/"); return html; } /// /// Specifies whether this instance is reusable by other Http requests /// public bool IsReusable { get { return true; } } }}








<httpHandlers>
<add verb="*" path="*" type="ReverseProxy.ReverseProxy, ReverseProxy" />
</httpHandlers>

<appSettings>
<!-- PROXY Mode
0 : all web site can be requested by a client with
an url like http://www.codeproject.com/KB/web-security/"http://reverseproxyurl/http//www.site.com/" (http://www.codeproject.com/KB/web-security/"http://reverseproxyurl/http//www.site.com/")
1 : Only on web site can be resuested by clients, \
In this case Web Application uses RemoteWebSite variable
to deliver the content of the web site.
-->
<add key="ProxyMode" value="1" />
<add key="RemoteWebSite" value="http://www.codeproject.com/">http://www.codeproject.com/ (http://www.codeproject.com/KB/web-security/"</span)" />
</appSettings>

DotNet_King
شنبه 25 خرداد 1387, 13:23 عصر
using System;using System.Configuration;using System.Web;using System.Net;using System.Text;using System.IO; namespace ReverseProxy{ /// <summary> /// Handler that intercept Client's request and deliver the web site /// </summary> public class ReverseProxy: IHttpHandler { /// <summary> /// Method calls when client request the server /// </summary> /// &;lt;param name="context">HTTP context for client</param> public void ProcessRequest(HttpContext context) { //read values from configuration fileint proxyMode = Convert.ToInt32(ConfigurationSettings.AppSettings["ProxyMode"]); string remoteWebSite = ConfigurationSettings.AppSettings["RemoteWebSite"]; string remoteUrl; if (proxyMode==0) remoteUrl= ParseURL(context.Request.Url.AbsoluteUri); //all site acceptedelseremoteUrl= context.Request.Url.AbsoluteUri.Replace("http://"+ context.Request.Url.Host+ context.Request.ApplicationPath,remoteWebSite); //only one site accepted //create the web request to get the remote stream HttpWebRequest request = (HttpWebRequest)WebRequest.Create(remoteUrl); //TODO : you can add your own credentials system //request.Credentials = CredentialCache.DefaultCredentials; HttpWebResponse response; try { response = (HttpWebResponse)request.GetResponse (); } catch(System.Net.WebException we) { //remote url not found, send 404 to client context.Response.StatusCode = 404; context.Response.StatusDescription = "Not Found"; context.Response.Write("<h2>Page not found</h2>"); context.Response.End(); return; } Stream receiveStream = response.GetResponseStream(); if ((response.ContentType.ToLower().IndexOf("html")>=0) ||(response.ContentType.ToLower().IndexOf("javascript")>=0)) { //this response is HTML Content, so we must parse it StreamReader readStream = new StreamReader (receiveStream, Encoding.Default); Uri test = new Uri(remoteUrl); string content; if (proxyMode==0) content= ParseHtmlResponse(readStream.ReadToEnd(), context.Request.ApplicationPath+"/http//"+test.Host); else content= ParseHtmlResponse(readStream.ReadToEnd(), context.Request.ApplicationPath); //write the updated HTML to the client context.Response.Write(content); //close streamsreadStream.Close(); response.Close(); context.Response.End(); } else { //the response is not HTML Contentbyte[] buff = new byte[1024]; int bytes = 0; while( ( bytes = receiveStream.Read( buff, 0, 1024 ) ) > 0 ) { //Write the stream directly to the client context.Response.OutputStream.Write (buff, 0, bytes ); } //close streams response.Close(); context.Response.End(); } } /// <summary> /// Get the remote URL to call /// </summary> /// <param name="url">URL get by client</param> /// <returns>Remote URL to return to the client</returns> public string ParseURL(string url) { if (url.IndexOf("http/")>=0) { string externalUrl=url.Substring(url.IndexOf("http/")); return externalUrl.Replace("http/","http://") ; } else return url; } /// <summary> /// Parse HTML response for update links and images sources /// </summary> /// <param name="html">HTML response</param> /// <param name="appPath">Path of application for replacement</param> /// <returns>HTML updated</returns> public string ParseHtmlResponse(string html,string appPath) { html=html.Replace("\"/","\""+appPath+"/"); html=html.Replace("'/","'"+appPath+"/"); html=html.Replace("=/","="+appPath+"/"); return html; } /// /// Specifies whether this instance is reusable by other Http requests /// public bool IsReusable { get { return true; } } }}

DotNet_King
شنبه 25 خرداد 1387, 13:33 عصر
این سورس کد
نمی دونم چرا خوب نمایشش نمی ده! فقط یه سطر بیشتر نداره وقتی تو تگ کد می زارمش

DotNet_King
یک شنبه 26 خرداد 1387, 16:55 عصر
خارج از این کد اگه راه دیگری به نظرتون می رسه واسه ساخت یه چیزی شبیه فیاتر شکن لطفا بگید

KavoshGar_ir
دوشنبه 27 خرداد 1387, 14:52 عصر
کسی کی دونه به چه صورت می تو نم با استفاده از این کد یه سایت راه دور رو به صورت کد اچ تی ام ال تو سایتم دریافت کنم. اگه راهی دیگری هم برای این کار ، سراغ دارید لطفا اینجا ارائه بدید

[/code]
برای گرفتن سورس یک سایت از HttpWebRequest استفاده کنید اگر در این تالار جستجو کنید کدش کامل نوشته شده ...


خارج از این کد اگه راه دیگری به نظرتون می رسه واسه ساخت یه چیزی شبیه فیاتر شکن لطفا بگید
فیلترشکن هم قبلا یکی از بچه های همین سایت به نام SalarSoft نوشته از این لینک (http://sourceforge.net/project/showfiles.php?group_id=155329) می تونی دانلودش کنی!