Active TopicsActive Topics  Display List of Forum MembersMemberlist  HelpHelp   RegisterRegister  LoginLogin
ISAPI_Rewrite 2.x support forum
 Helicon Tech : ISAPI_Rewrite 2.x support forum
Subject Topic: asp proxy script Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
davidw53
Newbie
Newbie


Joined: 12 March 2007
Location: United Kingdom
Online Status: Offline
Posts: 7
Posted: 14 November 2007 at 4:43am | IP Logged Quote davidw53

Proxiing script which authenticates by looking for an asp session variable "Login". Also useful because it keeps the asp session alive while proxiing in case the application returns to an asp page.

The Script calls a second asp script "check-session.asp" that has enabelsessionstate set to true
-------- checksession.asp ------------------
<%@ Language=JScript EnableSessionState=True%>
<% if (Session("Login")) Response.Write ("1"); else Response.write ("0") %>
----------------------------------------

The proxiing script "rails-proxy.asp"
--------- rails-proxy.asp ----------------
<%@ Language=JScript EnableSessionState=False%>
<%
var HttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
HttpReq.Option( 6) = false //WinHttpRequestOption_EnableRedirects
var cookie =Request.ServerVariables( "HTTP_COOKIE")

HttpReq.open( "GET", "http://"+ Request.ServerVariables("HTTP_HOST") +"/stats/check-session.asp", true);
HttpReq.setRequestHeader( "Cookie", cookie); HttpReq.send

if(!HttpReq.WaitForResponse(5)){ HttpReq.Abort();
 Response.Redirect( Request.ServerVariables("HTTP_X_REWRITE_URL")) }
if( HttpReq.responseText !="1") Response.redirect( "/stats/userLogin.asp?logout=2")

HttpReq.open( Request.ServerVariables("REQUEST_METHOD"), "http://192.168.4.218:3000/rails/" + Request.QueryString, false);
HttpReq.setRequestHeader( "Cookie", cookie);
if( String( Request.ServerVariables( "CONTENT_TYPE"))) HttpReq.setRequestHeader( "Content-Type", Request.ServerVariables( "CONTENT_TYPE"))
HttpReq.setRequestHeader( "User-Agent", Request.ServerVariables( "HTTP_USER_AGENT"));
if( Request.ServerVariables( "HTTPS") =="on") HttpReq.setRequestHeader( "X-Forwarded-Proto", "https");
HttpReq.setRequestHeader( "X-Forwarded-Host", Request.ServerVariables( "HTTP_HOST"));

if( Request.TotalBytes) HttpReq.send( Request.BinaryRead( Request.TotalBytes)); else HttpReq.send;

Response.Status = "" + HttpReq.status + " " + HttpReq.statusText;

var contentType = HttpReq.getResponseHeader("Content-Type")
if (contentType) Response.ContentType = contentType

headers=String( HttpReq.getAllResponseHeaders()).split("\n");
for( var i=0; i<headers.length && headers; i++) {
 var header = headers.match(/([\w-\.]+):\s*([ \S]*)/);
 if( header &&( header[1] !="Content-Type")) Response.AddHeader( header[1], header[2]);
}

Response.BinaryWrite( HttpReq.responseBody);
%>
-----------------------------------------------

 

Back to Top View davidw53's Profile Search for other posts by davidw53 Visit davidw53's Homepage
 
davidw53
Newbie
Newbie


Joined: 12 March 2007
Location: United Kingdom
Online Status: Offline
Posts: 7
Posted: 23 November 2007 at 4:46am | IP Logged Quote davidw53

Updated script also handles "304 not modified" responses.

-------------------------------------------------------------
<%@ Language=JScript EnableSessionState=False %>
<%
var HttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
HttpReq.Option( 6) = false //WinHttpRequestOption_EnableRedirects

HttpReq.open( "GET", "http://"+ Request.ServerVariables("HTTP_HOST") +"/stats/check-session.asp", true);
HttpReq.setRequestHeader( "Cookie", Request.ServerVariables( "HTTP_COOKIE")); HttpReq.send

if(!HttpReq.WaitForResponse(1)){ HttpReq.Abort(); %>
 <form name=retry method="<%= Request.ServerVariables("REQUEST_METHOD") %>"
   action="<%= Request.ServerVariables("HTTP_X_REWRITE_URL") %>">
  <% for( var f =new Enumerator( Request.Form()); !f.atEnd(); f.moveNext()){
   var key = f.item(); %>
   <input type=hidden name="<%= key %>" value="<%= Request.Form( key) %>">
  <% } %>
 </form>
 <script>window.onload=function(){retry.submit()}</script>
 <% Response.end }
if( HttpReq.responseText !="1") Response.redirect( "/stats/userLogin.asp?logout=2")

HttpReq.open( Request.ServerVariables("REQUEST_METHOD"), "http://192.168.4.218:3000/rails/" + Request.QueryString, false);

//fs=new ActiveXObject("Scripting.FileSystemObject")
//Application("fno") =Application("fno") +1
//of=fs.OpenTextFile("C:\\Websites\\syntec\\stats\\proxy." +Application("fno") +".log", 8, true, 0)

var rheaders= String( Request.ServerVariables("ALL_RAW")).split("\n");
for(var i=0; i<rheaders.length && rheaders; i++){
 var rheader = rheaders.match(/([\w-\.]+):\s*([ \S]*)/);
// of.write( rheader[1] +"=" +rheader[2] +"\n")
 if(( rheader[1] !="Host")) HttpReq.setRequestHeader( rheader[1], rheader[2])
}

if( String( Request.ServerVariables( "CONTENT_TYPE"))) HttpReq.setRequestHeader( "Content-Type", Request.ServerVariables( "CONTENT_TYPE"))
if( Request.ServerVariables( "HTTPS") =="on") HttpReq.setRequestHeader( "X-Forwarded-Proto", "https")
HttpReq.setRequestHeader( "X-Forwarded-Host", Request.ServerVariables( "HTTP_HOST"))

if( Request.TotalBytes) HttpReq.send( Request.BinaryRead( Request.TotalBytes)); else HttpReq.send
Response.Status =HttpReq.status +" " +HttpReq.statusText

//of.write( "\n" +Request.QueryString +" - " +Request.ServerVariables("REQUEST_METHOD") +" " +Response.Status +"\n\n")

var headers= String( HttpReq.getAllResponseHeaders()).split("\n")
for( var i=0; i<headers.length && headers; i++) {
 var header = headers.match(/([\w-\.]+):\s*([ \S]*)/);
// if(header) of.write( "" +header[1] +"=" +header[2] +"\n")
 if( header) switch( header[1]){
  case "Cache-Control": Response.CacheControl =header[2]; break
  case "Content-Type": Response.ContentType =header[2]; break
//  case "Content-Length": break;
  case "Expires": Response.ExpiresAbsolute =header[2]; break
  case "Server": break
  default: Response.AddHeader( header[1], header[2]) }
}

//of.close()
if( HttpReq.status !=304) Response.BinaryWrite( HttpReq.responseBody)
%>

Back to Top View davidw53's Profile Search for other posts by davidw53 Visit davidw53's Homepage
 
davidw53
Newbie
Newbie


Joined: 12 March 2007
Location: United Kingdom
Online Status: Offline
Posts: 7
Posted: 06 January 2008 at 12:29am | IP Logged Quote davidw53

Latest version of this script is below.

Notes:
1.   The commented lines are for debug purposes
2.   Enablesessionstate must be false otherwise the script just hangs (I don't know why). If it could be true then the authentication part could be done in the body of the script instead of calling a separate asp page.
3.   Auto-redirect by the winhttp proxy must be disabled for most web applications to work effectively. If we did not need to do this we could use ServerXMLhttp which is more scaleable then winhttprequest.
4.   When the IIS server is restarted or the proxy script is edited, the first call to check-session.asp hangs - hence the redirect/retry code which fixes this problem with effectively a browser refresh.
5.   check-session.asp contains the authentication code which checks the requested url and session vars and returns -1 for session timeout, 1 if authenticated and 0 (or anything else) if not.

----------------------------------
<%@ Language=JScript EnableSessionState=False %>
<%
//Application( "fno") =Application( "fno") +1
//var fs =new ActiveXObject( "Scripting.FileSystemObject")
//var of =fs.OpenTextFile( "C:\\Websites\\syntec\\stats\\proxy." +Application("fno") +".log", 8, true, 0)

var HttpReq =new ActiveXObject( "WinHttp.WinHttpRequest.5.1");
HttpReq.Option( 6) =false //WinHttpRequestOption_EnableRedirects

HttpReq.open( "GET", "http://"+ Request.ServerVariables( "HTTP_HOST") +"/stats/check-session.asp?" +Request.QueryString, true);
HttpReq.setRequestHeader( "Cookie", Request.ServerVariables( "HTTP_COOKIE")); HttpReq.send

if( !HttpReq.WaitForResponse( 1)){ HttpReq.Abort();
// of.write( Request.QueryString +" - " +Request.ServerVariables( "REQUEST_METHOD") +" Retrying\n")
// of.write( "\nHTTP_X_REWRITE_URL=" +Request.ServerVariables( "HTTP_X_REWRITE_URL"))
 if( Request.ServerVariables( "REQUEST_METHOD") =="GET") Response.redirect( Request.ServerVariables( "HTTP_X_REWRITE_URL")) %>
 <form name=retry method="<%= Request.ServerVariables( "REQUEST_METHOD") %>"
   action="<%= Request.ServerVariables( "HTTP_X_REWRITE_URL") %>">
  <% for( var f =new Enumerator( Request.Form); !f.atEnd(); f.moveNext()){
   var key = f.item(); %>
   <input type=hidden name="<%= key %>" value="<%= Request.Form( key) %>">
  <% } %>
 </form>
 <script>window.onload=function(){retry.submit()}</script>
 <% Response.end }
if( HttpReq.responseText =="-1") Response.redirect( "/stats/userLogin.asp?logout=2")
if( HttpReq.responseText !="1"){ %>
 <p style=color:red;margin:50>Permission Denied
 <% Response.End }

HttpReq.open( Request.ServerVariables( "REQUEST_METHOD"), "http://" +Request.QueryString, false);

var rheaders= String( Request.ServerVariables( "ALL_RAW")).split("\n");
for(var i=0; i<rheaders.length && rheaders; i++){
 var rheader = rheaders.match(/([\w-\.]+):\s*([ \S]*)/);
// of.write( rheader[1] +"=" +rheader[2] +"\n")
 if(( rheader[1] !="Host")) HttpReq.setRequestHeader( rheader[1], rheader[2])
}

if( String( Request.ServerVariables( "CONTENT_TYPE"))) HttpReq.setRequestHeader( "Content-Type", Request.ServerVariables( "CONTENT_TYPE"))
if( Request.ServerVariables( "HTTPS") =="on") HttpReq.setRequestHeader( "X-Forwarded-Proto", "https")
HttpReq.setRequestHeader( "X-Forwarded-Host", Request.ServerVariables( "HTTP_HOST"))

if( Request.TotalBytes) HttpReq.send( Request.BinaryRead( Request.TotalBytes)); else HttpReq.send
Response.Status =HttpReq.status +" " +HttpReq.statusText

//of.write( "\n" +Request.QueryString +" - " +Request.ServerVariables( "REQUEST_METHOD") +" " +Response.Status +"\n\n")

var headers= String( HttpReq.getAllResponseHeaders()).split("\n")
for( var i=0; i<headers.length && headers; i++) {
 var header = headers.match(/([\w-\.]+):\s*([ \S]*)/);
// if(header) of.write( "" +header[1] +"=" +header[2] +"\n")
 if( header) switch( header[1]){
  case "Cache-Control": Response.CacheControl =header[2]; break
  case "Content-Type": Response.ContentType =header[2]; break
  case "Content-Length": var content= header[2] >0; break;
  case "Expires": Response.ExpiresAbsolute =header[2]; break
  case "Server": break
  default: Response.AddHeader( header[1], header[2]) }
}

if( content) Response.BinaryWrite( HttpReq.responseBody)
%>

Back to Top View davidw53's Profile Search for other posts by davidw53 Visit davidw53's Homepage
 
davidw53
Newbie
Newbie


Joined: 12 March 2007
Location: United Kingdom
Online Status: Offline
Posts: 7
Posted: 15 February 2008 at 3:24pm | IP Logged Quote davidw53

I forgot to mention. Need rewrie rule, eg.

RewriteRule /rails(/.*) /stats/reverse-proxy.asp\?192.168.4.218:3000$1 [L]

RewriteRule (/(?:search\?|smb/).*) /stats/reverse-proxy.asp\?129.178.88.72$1 [L]

to reverse-proxy a child domain (to some internal server) with the script.

Back to Top View davidw53's Profile Search for other posts by davidw53 Visit davidw53's Homepage
 
davidw53
Newbie
Newbie


Joined: 12 March 2007
Location: United Kingdom
Online Status: Offline
Posts: 7
Posted: 25 July 2008 at 11:32am | IP Logged Quote davidw53

The latest version is below.

Notes

  1. Allows proxiing based on the referer header. Sometimes you want to proxy all the images/calls from a page without identifying all possibilities.
  2. If enablesessionstate is true then the page will 'hang' if the user clicks elsewhere and the page has not fully loaded. Very noticable from a page with continuously running ajax calls that monitor something.
  3. No longer rely on correct 'content-lengh' header. Send response only if length>0. Fixes some problems.
  4. Logging now uses single file opened in global.asa. A number reference is used to identify the log lines of each call.

----------------------------------------------------------
Use in httpd.ini-

# FOR RAILS
RewriteCond URL /rails/note_attachment/upload\?(_SynBill_session_id=.*)(ASPSESSIONID.{8})%3D(.*)$
RewriteHeader Cookie: .* $1;$2=$3
RewriteRule /rails(/.*) /stats/reverse-proxy.asp\?192.168.4.218:3000$1 [L]

# FOR GOOGLE
RewriteRule (/(?:search\?|smb/).*) /stats/reverse-proxy.asp\?129.178.88.72$1 [L]

# QVIEW
RewriteRule (/qview\.htm\b.*) /stats/reverse-proxy.asp\?10.41.68.18:8081$1 [L]
RewriteRule (/stats/(?:check-session|userLogin)\.asp\b.*) $1 [L]
RewriteCond Cookie: .*qviewrdr=on.*
RewriteCond Referer: .*/qview\.htm\b.*
RewriteRule (.*) /stats/reverse-proxy.asp\?10.41.68.18:8081$1 [L]

----------------------------------------------------------

<%@ Language=JScript EnableSessionState=False %><%
//var lno= Application( "lno")= Application( "lno")+1
//function log( msg){
//    function pad( n){ return '00000'.slice( n.toString().length) +n }
// Application( "log").write( pad( lno) +': '+ msg +'\n') }

var HttpReq =new ActiveXObject( "WinHttp.WinHttpRequest.5.1");
HttpReq.Option( 6) =false //WinHttpRequestOption_EnableRedirects

HttpReq.open( "GET", "http://"+ Request.ServerVariables( "HTTP_HOST") +"/stats/check-session.asp?" +Request.QueryString, true);
HttpReq.setRequestHeader( "Referer", (Request.ServerVariables( "HTTP_REFERER").Item || 'x'))
HttpReq.setRequestHeader( "Cookie", Request.ServerVariables( "HTTP_COOKIE")); HttpReq.send

if( !HttpReq.WaitForResponse( 1)){ HttpReq.Abort();
// log( Request.QueryString +" - " +Request.ServerVariables( "REQUEST_METHOD") +" Retrying\n \
//  HTTP_X_REWRITE_URL=" +Request.ServerVariables( "HTTP_X_REWRITE_URL"))
 if( Request.ServerVariables( "REQUEST_METHOD") =="GET") Response.redirect( Request.ServerVariables( "HTTP_X_REWRITE_URL")) %>
 <form name=retry method="<%= Request.ServerVariables( "REQUEST_METHOD") %>"
   action="<%= Request.ServerVariables( "HTTP_X_REWRITE_URL") %>">
  <% for( var f =new Enumerator( Request.Form); !f.atEnd(); f.moveNext()){
   var key = f.item(); %>
   <input type=hidden name="<%= key %>" value="<%= Request.Form( key) %>">
  <% } %>
 </form>
 <script>window.onload=function(){ retry.submit() }</script>
 <% Response.end }

if( HttpReq.responseText =="-1"){
 if( Request.QueryString.Item == "10.41.68.18:8081/init"){
  Response.Cookies( "qviewrdr") ="off"; Response.Cookies( "qviewrdr").Path = "/"
  Response.write( '"RDR:/stats/userLogin.asp?logout=2"'); Response.end }
 Response.redirect( "/stats/userLogin.asp?logout=2") }
if( HttpReq.responseText !="1"){ %>
 <p style=color:red;margin:50>Permission Denied
 <% Response.End }

HttpReq.open( Request.ServerVariables( "REQUEST_METHOD"), "http://" +Request.QueryString, false)

var rheaders= Request.ServerVariables( "ALL_RAW").Item.split("\n");
for( var i=0; i<rheaders.length && rheaders; i++){
 var rheader= rheaders.match(/([\w-\.]+):\s*([ \S]*)/);
// log( rheader[1] +"=" +rheader[2])
 if( rheader[1].toLowerCase() !="host") HttpReq.setRequestHeader( rheader[1], rheader[2])
}

if( Request.ServerVariables( "HTTPS") =="on") HttpReq.setRequestHeader( "X-Forwarded-Proto", "https")
HttpReq.setRequestHeader( "X-Forwarded-Host", Request.ServerVariables( "HTTP_HOST"))

HttpReq.SetTimeouts( 0, 0, 0, 0)
HttpReq.send( Request.BinaryRead( Request.TotalBytes))

Response.Status =HttpReq.status +" " +HttpReq.statusText
//log( Request.QueryString +" - " +Request.ServerVariables( "REQUEST_METHOD") +" " +Response.Status)

var headers= HttpReq.getAllResponseHeaders().split("\n")
for( var i=0; i<headers.length && headers; i++) {
 var header = headers.match(/([\w-\.]+):\s*([ \S]*)/);
// if( header) log( "" +header[1] +"=" +header[2])
 if( header) switch( header[1].toLowerCase()){
  case "cache-control": Response.CacheControl= header[2]; break
  case "content-type": Response.ContentType= header[2]; break
  case "content-length": break; //case "date": break;
  case "expires": Response.ExpiresAbsolute =header[2]; break
  case "server": break;
  default: Response.AddHeader( header[1], header[2]) }}

//log( HttpReq.ResponseText)

if( Request.QueryString.Item.match( /10\.41\.68\.18:8081\/qview.htm\b.*/)){
 Response.Cookies( "qviewrdr") ="on"; Response.Cookies( "qviewrdr").Path = "/" }

if( Request.QueryString.Item == "10.41.68.18:8081/queues"
  && HttpReq.ResponseText.match( /RDR:http:\/\/www\.syntec\.co\.uk\b/)){
 Response.Cookies( "qviewrdr") ="off"; Response.Cookies( "qviewrdr").Path = "/"
 Response.write( '"RDR:' +HttpReq.ResponseText.slice( 28)); Response.end }

//if(HttpReq.status <300 || HttpReq.status >=400)
 if( HttpReq.ResponseText.length>0) Response.BinaryWrite( HttpReq.ResponseBody)
Response.Flush %>

Back to Top View davidw53's Profile Search for other posts by davidw53 Visit davidw53's Homepage
 

If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Post ReplyPost New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum