Advertisement
bah99

proxy.cgi

Dec 4th, 2022
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | Source Code | 0 0
  1. #!C:/ms4w/Python/python.exe
  2. #coding : utf-8
  3. #enable debugging
  4. # Designed to prevent Open Proxy type stuff.
  5. import urllib
  6. import urllib.parse
  7. import urllib3
  8. import cgi
  9. import sys, os
  10.  
  11. """This is a blind proxy that we use to get around browser
  12. restrictions
  13.  
  14. that prevent the Javascript from loading pages not on the
  15. same server as the Javascript.  This
  16.  
  17. has several problems: it's less
  18. efficient, it might break some sites, and it's a security risk
  19.  
  20. because
  21. people can use this proxy to browse the web and possibly do bad stuff
  22. with it.  It only
  23.  
  24. loads pages via http and https, but it can load any
  25. content type. It supports GET and POST
  26.  
  27. requests."""
  28.  
  29.  
  30. allowedHosts = [ 'www.openlayers.org', 'http://localhost/tinyows/demo/tinyows.html',  'www.openstreetmap.org']
  31.  
  32. method = os.environ["REQUEST_METHOD"]
  33.  
  34. if method == "POST":
  35.     qs = os.environ["QUERY_STRING"]
  36.     d = urllib.parse.parse_qs(qs)
  37.     if "url" in d :
  38.         url = d["url"][0]
  39.     else:
  40.         url = "http://www.openlayers.org"
  41. else:
  42.     fs = cgi.FieldStorage()
  43.     url = fs.getvalue('url', "http://www.openlayers.org")
  44.  
  45. try:
  46.     host = url.split("/")[2]
  47.     if allowedHosts and not host in allowedHosts:
  48.         print ("Status: 502 Bad Gateway")
  49.         print ("Content-Type: text/plain")
  50.         print
  51.         print ("This proxy does not allow you to access that location (%s).") % (host,)
  52.         print
  53.         print (os.environ)
  54.  
  55.     elif url.startswith("http://") or url.startswith("https://"):
  56.    
  57.         if method == "POST":
  58.             length = int(os.environ["CONTENT_LENGTH"])
  59.             headers = {"Content-Type": os.environ["CONTENT_TYPE"]}
  60.             body = sys.stdin.read(length)    
  61.             r = urllib3.Request(url, body, headers)
  62.             y = urllib3.urlopen(r)
  63.         else:
  64.             y = urllib3.urlopen(url)
  65.             # print content type header
  66.             i = y.info()
  67.         if "Content-Type" in i:
  68.             print ("Content-Type: %s") % (i["Content-Type"])
  69.         else:
  70.             print ("Content-Type: text/plain")
  71.             print
  72.             print (y.read())
  73.             y.close()
  74.     else:
  75.         print ("Content-Type: text/plain")
  76.         print
  77.         print ("Illegal request.")
  78.  
  79. except Exception as e:
  80.     print ("Status: 500 Unexpected Error")
  81.     print ("Content-Type: text/plain")
  82.     print
  83.     print ("Some unexpected error occurred. Error text was:"), e
Tags: proxy.cgi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement