[~] Refactor
This commit is contained in:
parent
b10d546cd1
commit
4ad2863bf5
233
d1/f2.py
233
d1/f2.py
@ -13,81 +13,85 @@ import pprint
|
|||||||
sys.path.insert(0, os.path.dirname(__file__))
|
sys.path.insert(0, os.path.dirname(__file__))
|
||||||
|
|
||||||
|
|
||||||
def application(environ, start_response):
|
class Application:
|
||||||
def op1(data=None):
|
def __init__(self, environ, start_response):
|
||||||
|
self.environ = environ
|
||||||
|
self.start_response = start_response
|
||||||
|
|
||||||
|
def op1(self, data=None):
|
||||||
if data is None:
|
if data is None:
|
||||||
data = traceback.format_exc()
|
data = traceback.format_exc()
|
||||||
with io.open('log.txt', 'a') as f:
|
with io.open('log.txt', 'a') as f:
|
||||||
f.write(data)
|
f.write(data)
|
||||||
|
|
||||||
try:
|
def op2(self, rh):
|
||||||
t4 = int(environ.get('CONTENT_LENGTH', '0'))
|
|
||||||
t3 = environ['wsgi.input'].read(t4)
|
|
||||||
def op1(rh):
|
|
||||||
t5 = rh.split('_')
|
t5 = rh.split('_')
|
||||||
t6 = ['%s%s' % (o[0].upper(), o[1:].lower()) for o in t5]
|
t6 = ['%s%s' % (o[0].upper(), o[1:].lower()) for o in t5]
|
||||||
return '-'.join(t6)
|
return '-'.join(t6)
|
||||||
t2 = {
|
|
||||||
op1(k[5:]) : v
|
def op3(self,):
|
||||||
for k, v in environ.items()
|
for o in [self.input_dat, self.output_dat]:
|
||||||
if k.startswith('HTTP_')
|
|
||||||
}
|
|
||||||
for k, v in environ.items():
|
|
||||||
if k in [
|
|
||||||
'CONTENT_TYPE',
|
|
||||||
]:
|
|
||||||
t2[op1(k)] = v
|
|
||||||
t7 = dict(
|
|
||||||
uri=environ['REQUEST_URI'],
|
|
||||||
method=environ['REQUEST_METHOD'],
|
|
||||||
protocol=environ['SERVER_PROTOCOL'],
|
|
||||||
)
|
|
||||||
output_dat = None
|
|
||||||
input_dat = None
|
|
||||||
def op1():
|
|
||||||
for o in [input_dat, output_dat]:
|
|
||||||
if not o is None and os.path.exists(o):
|
if not o is None and os.path.exists(o):
|
||||||
os.unlink(o)
|
os.unlink(o)
|
||||||
try:
|
|
||||||
output_dat = tempfile.mktemp(suffix='.dat')
|
|
||||||
input_dat = tempfile.mktemp(suffix='.dat')
|
|
||||||
op1()
|
|
||||||
|
|
||||||
with io.open(input_dat, 'wb') as f:
|
def op4(self,):
|
||||||
f.write(t3)
|
self.output_dat = tempfile.mktemp(suffix='.dat')
|
||||||
|
self.input_dat = tempfile.mktemp(suffix='.dat')
|
||||||
|
|
||||||
try:
|
def op5(
|
||||||
with io.open(
|
status_code=None,
|
||||||
os.path.join(
|
status_text=None,
|
||||||
os.environ['HOME'],
|
content_type=None,
|
||||||
'proxy.json'
|
content=None,
|
||||||
),
|
headers_text=None,
|
||||||
'r'
|
):
|
||||||
) as f:
|
if not headers_text is None:
|
||||||
PROXY_URL = numpy.random.choice(json.load(f))
|
if not any([o.startswith('Content-Length') for o in headers_text]):
|
||||||
except:
|
headers_text = \
|
||||||
with io.open('log.txt', 'a') as f:
|
headers_text.replace(
|
||||||
f.write(traceback.format_exc())
|
'Transfer-Encoding: chunked\r\n',
|
||||||
|
''
|
||||||
|
)
|
||||||
|
headers_text += 'Content-Length: %d\r\n' % len(t10)
|
||||||
|
|
||||||
PROXY_URL = '127.0.0.1:9050'
|
t13 = headers_text.splitlines()[0]
|
||||||
|
t14 = t13.find(' ')
|
||||||
t17 = [
|
t15 = t13[t14 + 1:]
|
||||||
'curl',
|
status_suffix = t15
|
||||||
'http://%s%s' % (PROXY_URL, t7['uri']),
|
headers = [
|
||||||
*sum([
|
(o[:o.find(':')], o[o.find(':') + 1:])
|
||||||
['--header', '%s: %s' % (k, v)]
|
for o in headers_text.splitlines()[1:]
|
||||||
for k, v in t2.items()
|
|
||||||
], []),
|
|
||||||
'-X', t7['method'],
|
|
||||||
'--data-binary', '@%s' % input_dat,
|
|
||||||
'--max-filesize', '%d' % (60 * 1024 * 1024),
|
|
||||||
'-o', output_dat,
|
|
||||||
'-v',
|
|
||||||
'-q',
|
|
||||||
]
|
]
|
||||||
|
else:
|
||||||
|
if status_code is None:
|
||||||
|
status_code = 200
|
||||||
|
if status_text is None:
|
||||||
|
status_text = 'OK'
|
||||||
|
|
||||||
|
if content_type is None:
|
||||||
|
content_type = 'text/plain'
|
||||||
|
|
||||||
|
status_suffix = '%d %s' % (status_code, status_text)
|
||||||
|
headers = [('Content-Type', content_type)]
|
||||||
|
|
||||||
|
t1 = self.start_response(
|
||||||
|
status_suffix,
|
||||||
|
headers,
|
||||||
|
)
|
||||||
|
assert isinstance(content, bytes)
|
||||||
|
|
||||||
|
t1(content)
|
||||||
|
|
||||||
|
return []
|
||||||
|
|
||||||
|
def op6(self, cmd_args,):
|
||||||
|
self.op1(json.dumps(cmd_args))
|
||||||
|
|
||||||
with subprocess.Popen(
|
with subprocess.Popen(
|
||||||
t17, stderr=subprocess.PIPE, stdout=subprocess.PIPE, stdin=subprocess.PIPE
|
cmd_args,
|
||||||
|
stderr=subprocess.PIPE,
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stdin=subprocess.PIPE
|
||||||
) as p:
|
) as p:
|
||||||
try:
|
try:
|
||||||
p.wait(20)
|
p.wait(20)
|
||||||
@ -98,9 +102,9 @@ def application(environ, start_response):
|
|||||||
]
|
]
|
||||||
t9 = '\r\n'.join(response_headers)
|
t9 = '\r\n'.join(response_headers)
|
||||||
if not 'Content-Length: 0' in t9:
|
if not 'Content-Length: 0' in t9:
|
||||||
for k in range(3):
|
for k in range(100):
|
||||||
try:
|
try:
|
||||||
with io.open(output_dat, 'rb') as f:
|
with io.open(self.output_dat, 'rb') as f:
|
||||||
t10 = f.read()
|
t10 = f.read()
|
||||||
break
|
break
|
||||||
except:
|
except:
|
||||||
@ -109,30 +113,93 @@ def application(environ, start_response):
|
|||||||
t10 = b''
|
t10 = b''
|
||||||
finally:
|
finally:
|
||||||
p.terminate()
|
p.terminate()
|
||||||
|
|
||||||
|
return t10
|
||||||
|
|
||||||
|
def op7(self):
|
||||||
|
try:
|
||||||
|
with io.open(
|
||||||
|
os.path.join(
|
||||||
|
os.self.environ['HOME'],
|
||||||
|
'proxy.json'
|
||||||
|
),
|
||||||
|
'r'
|
||||||
|
) as f:
|
||||||
|
return numpy.random.choice(json.load(f))
|
||||||
except:
|
except:
|
||||||
t9 = 'FUCK SHIT\r\n'
|
with io.open('log.txt', 'a') as f:
|
||||||
t10 = traceback.format_exc().encode('utf-8')
|
f.write(traceback.format_exc())
|
||||||
|
|
||||||
|
return '127.0.0.1:9050'
|
||||||
|
|
||||||
|
def op8(self, input_content, headers, uri, method):
|
||||||
|
try:
|
||||||
|
self.op4()
|
||||||
|
|
||||||
|
with io.open(
|
||||||
|
self.input_dat,
|
||||||
|
'wb'
|
||||||
|
) as f:
|
||||||
|
f.write(input_content)
|
||||||
|
|
||||||
|
|
||||||
|
proxy_url = self.op7()
|
||||||
|
|
||||||
|
t17 = [
|
||||||
|
'curl',
|
||||||
|
'http://%s%s' % (proxy_url, uri),
|
||||||
|
*sum([
|
||||||
|
['--header', '%s: %s' % (k, v)]
|
||||||
|
for k, v in t2.items()
|
||||||
|
], []),
|
||||||
|
'-X', method,
|
||||||
|
'--data-binary', '@%s' % self.input_dat,
|
||||||
|
'--max-filesize', '%d' % (60 * 1024 * 1024),
|
||||||
|
'-o', self.output_dat,
|
||||||
|
'-v',
|
||||||
|
'-q',
|
||||||
|
]
|
||||||
|
|
||||||
|
t10 = self.op6(t17)
|
||||||
finally:
|
finally:
|
||||||
op1()
|
self.op3()
|
||||||
|
|
||||||
if not any([o.startswith('Content-Length') for o in t9]):
|
return t10
|
||||||
t9 = t9.replace('Transfer-Encoding: chunked\r\n', '')
|
|
||||||
t9 += 'Content-Length: %d\r\n' % len(t10)
|
def run(self):
|
||||||
t11 = t9.encode('utf-8') + t10
|
try:
|
||||||
t13 = t9.splitlines()[0]
|
t4 = int(self.environ.get('CONTENT_LENGTH', '0'))
|
||||||
t14 = t13.find(' ')
|
t3 = self.environ['wsgi.input'].read(t4)
|
||||||
t15 = t13[t14 + 1:]
|
t2 = {
|
||||||
t16 = [(o[:o.find(':')], o[o.find(':') + 1:]) for o in t9.splitlines()[1:]]
|
self.op2(k[5:]) : v
|
||||||
if False:
|
for k, v in self.environ.items()
|
||||||
t1 = start_response('200 OK', [('Content-Type', 'text/plain')])
|
if k.startswith('HTTP_')
|
||||||
t1(t15)
|
}
|
||||||
t1(json.dumps(t16))
|
for k, v in self.environ.items():
|
||||||
t1(json.dumps(t17))
|
if k in [
|
||||||
t1(t10)
|
'CONTENT_TYPE',
|
||||||
else:
|
]:
|
||||||
t1 = start_response(t15, t16)
|
t2[self.op2(k)] = v
|
||||||
t1(t10)
|
|
||||||
|
t7 = dict(
|
||||||
|
uri=self.environ['REQUEST_URI'],
|
||||||
|
method=self.environ['REQUEST_METHOD'],
|
||||||
|
protocol=self.environ['SERVER_PROTOCOL'],
|
||||||
|
)
|
||||||
|
|
||||||
|
return self.op5(
|
||||||
|
headers_text=t9,
|
||||||
|
content=t10,
|
||||||
|
)
|
||||||
except:
|
except:
|
||||||
op1()
|
self.op1()
|
||||||
|
return self.op5(
|
||||||
|
content='internal server error',
|
||||||
|
)
|
||||||
|
|
||||||
return []
|
|
||||||
|
def application(environ, start_response):
|
||||||
|
return Application(
|
||||||
|
environ=environ,
|
||||||
|
start_response=start_response,
|
||||||
|
).run()
|
||||||
|
Loading…
Reference in New Issue
Block a user