forked from argomaintainer/jsbbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
105 lines (83 loc) · 3.02 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# coding:utf-8
# Copyright 2011 litl, LLC. All Rights Reserved.
# the proxy code is from gist
import httplib
import re
import urllib
import urlparse
import os
import traceback
from flask import Flask, request, Response, url_for
from flask.helpers import send_from_directory
from werkzeug.datastructures import Headers
from werkzeug.exceptions import NotFound
app = Flask(__name__)
REALHOST = 'argo.sysu.edu.cn'
REALPORT = 80
STATIC_PATH = os.path.dirname(os.path.abspath(__file__))
def iterform(multidict):
for key in multidict.keys():
for value in multidict.getlist(key):
yield (key.encode("utf8"), value.encode("utf8"))
def parse_host_port(h):
"""Parses strings in the form host[:port]"""
host_port = h.split(":", 1)
if len(host_port) == 1:
return (h, 80)
else:
host_port[1] = int(host_port[1])
return host_port
@app.route('/n/<path:file>', methods=['GET', 'POST'])
def n_static(file=''):
return send_from_directory(STATIC_PATH, file)
def proxy_request(file):
hostname = REALHOST
port = REALPORT
# Whitelist a few headers to pass on
request_headers = {}
for h in ["Cookie", "Referer", "X-Csrf-Token", "X-Requested-With"]:
if h in request.headers:
request_headers[h] = request.headers[h]
request_headers['Cookie'] = 'PHPSESSID=22e697cf314742e2b5ef043f7a7a8643;';
# request_headers['Cookie'] = request_headers['Cookie'].split(';')[-1]+';'
if request.query_string:
path = "/%s?%s" % (file, request.query_string)
else:
path = "/" + file
if request.method == "POST":
form_data = list(iterform(request.form))
form_data = urllib.urlencode(form_data)
request_headers["Content-Length"] = len(form_data)
request_headers["Content-Type"] = "application/x-www-form-urlencoded"
else:
form_data = None
conn = httplib.HTTPConnection(hostname, port)
conn.request(request.method, path, body=form_data, headers=request_headers)
resp = conn.getresponse()
# Clean up response headers for forwarding
response_headers = Headers()
for key, value in resp.getheaders():
if key in ["content-length", "connection"]:
continue
if key == "set-cookie":
cookies = value.split(" ,")
print 'cookies: %s' % cookies
[response_headers.add(key, c) for c in cookies]
else:
response_headers.add(key, value)
contents = resp.read()
flask_response = Response(response=contents,
status=resp.status,
headers=response_headers,
content_type=resp.getheader('content-type'))
return flask_response
@app.route('/ajax/<path:file>', methods=["GET", "POST"])
def proxy_ajax(file) :
return proxy_request('ajax/'+file)
@app.route('/bbssignal/<path:file>', methods=["POST"])
def handler_bbssignal(file):
print file
print request.form
return ''
if __name__ == '__main__':
app.run(host="0.0.0.0", debug=True)