This repository has been archived by the owner on Oct 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.py
56 lines (46 loc) · 1.59 KB
/
app.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
from geventhttpclient import HTTPClient, URL
from flask import Flask, request, redirect, abort
import settings
app = Flask(__name__)
def get_client_addr():
if not request.headers.getlist("X-Real-IP"):
ip = request.remote_addr
else:
ip = request.headers.getlist("X-Real-IP")[0]
return ip
def check_recaptcha(secret, resp, ip):
try:
url = URL('https://www.google.com/recaptcha/api/siteverify?secret=%s&response=%s&ip=%s' % (secret, resp, ip))
http = HTTPClient.from_url(url)
response = http.get(url.request_uri)
if response.status_code == 200:
raw_res = response.read()
res = json.loads(raw_res)
if res.get('success'):
return True
except:
pass
return False
@app.route('/', methods=['POST'])
def handler():
domain = request.headers.get('Testcookie-Domain', '')
nexturl = request.headers.get('Testcookie-Nexturl', '/')
cookie_name = request.headers.get('Testcookie-Name')
cookie_val = request.headers.get('Testcookie-Value')
secret = settings.RE_SECRETS.get(domain)
if not cookie_name or not cookie_val or not secret:
abort(500)
ip = get_client_addr()
if check_recaptcha(secret, request.form['g-recaptcha-response'], ip):
resp = redirect(nexturl)
resp.set_cookie(cookie_name, cookie_val)
return resp
return redirect(nexturl)
if __name__ == '__main__':
import logging
logging.basicConfig(level=logging.DEBUG)
app.debug = True
app.run('localhost', 10101)