· 7 years ago · Dec 15, 2018, 08:44 AM
1from flask import Flask, request, render_template, abort, jsonify, session
2import json, hashlib, os
3
4
5
6app = Flask(__name__)
7app.secret_key = 'secret_key'
8
9class Match:
10 def __init__(self, _id, home, away, score, date, id):
11 self.__prev = _id self.home = home
12 self.home_goal = int(score[0])
13 self.away_goal = int(score[2])
14 self.away = away self.score = score
15 self.date = date self.id = id
16 def getPrev(self):
17 return self.__prev matchces =
18 ""
19" W3siaG9tZSI6IlN5cmlhIiwiYXdheSI6IlZpZXRuYW0iLCJzY29yZSI6IjA6MSIsImRhdGUiOiIy NyBBdWd1c3QifSx7ImhvbWUiOiJVemJla2lzdGFuIiwiYXdheSI6IlNvdXRoIEtvcmVhIiwic2Nv cmUiOiIzOjQiLCJkYXRlIjoiMjcgQXVndXN0ICJ9LAogICAgICAgICAgICAgeyJob21lIjoiU2F1 ZGkgQXJhYmlhIiwiYXdheSI6IkphcGFuIiwic2NvcmUiOiIxOjIiLCJkYXRlIjoiMjcgQXVndXN0 In0seyJob21lIjoiVW5pdGVkIEFyYWIgRW1pcmF0ZXMiLCJhd2F5IjoiTm9ydGggS29yZWEiLCJz Y29yZSI6IjE6MSIsImRhdGUiOiIyNyBBdWd1c3QifSwKICAgICAgICAgICAgIHsiaG9tZSI6ICJW aWV0bmFtIiwgImF3YXkiOiAiU291dGggS29yZWEiLCAic2NvcmUiOiAiMTozIiwgImRhdGUiOiAi MjkgQXVndXN0In0seyJob21lIjoiSmFwYW4iLCJhd2F5IjoiVW5pdGVkIEFyYWIgRW1pcmF0ZXMi LCJzY29yZSI6IjE6MCIsImRhdGUiOiIyOSBBdWd1c3QifSwKICAgICAgICAgICAgIHsiaG9tZSI6 ICJWaWV0bmFtIiwgImF3YXkiOiAiVW5pdGVkIEFyYWIgRW1pcmF0ZXMiLCAic2NvcmUiOiAiMTox IiwgImRhdGUiOiAiMSBTZXB0ZW1iZXIifSwKICAgICAgICAgICAgIHsiaG9tZSI6ICJTb3V0aCBL b3JlYSIsICJhd2F5IjogIkphcGFuIiwgInNjb3JlIjogIjI6MSIsICJkYXRlIjogIjEgU2VwdGVt YmVyIn1d "
20""
21
22 def init():
23 Matches = []
24 hm = {}
25 m = Match(None, None, None, "0:0", None, "123" ,if not os.path.exists("/opt/id") else open("/opt/id").read().strip())
26 Matches.append(m)
27 prev_hash = Matches[0].id
28 hm[m.id] = m
29 for match in matchces:
30 match['__prev'] = prev_hash
31 block_serialized = json.dumps(match, sort_keys = True).encode('utf-8')
32 block_hash = hashlib.sha256(block_serialized).hexdigest()
33 prev_hash = block_hash
34 m = Match(match['__prev'], match['home'], match['away'], match['score'], match['date'], prev_hash)
35 Matches.append(m)
36 hm[prev_hash] = m
37 return Matches, hm
38
39def checkVNvodich(rr, hm):
40 try:
41 rr = rr[::-1]
42 r = rr[0]
43 cnt = 0
44 while r.home is not None:
45 print r.home, r.away, r.score, r.getPrev(), r.id
46 r = hm[r.getPrev()]
47 cnt += 1
48 if cnt > 10: #avoid infinity loop
49 break
50
51 if cnt == 8:
52 if "Vietnam" == rr[0].home and rr[0].home_goal > rr[0].away_goal:
53 return True
54 elif "Vietnam" == rr[0].away and rr[0].home_goal < rr[0].away_goal:
55 return True
56 return False
57 except:
58 return False
59
60
61matchces = json.loads(matchces.decode('base64'))
62res, hm = init()
63
64
65@app.errorhandler(500)
66def custom500(error):
67 return str(error), 500
68
69
70@app.route('/')
71def index():
72 print res[0].id
73 if "state" not in session.keys():
74 session['state'] = res[-1].id
75 else :
76 if request.args.get('state') is not None and request.args.get('state') == res[0].id: #thay request.args['state'] thanh session['state'] thi co lam dc khong ? global matchces
77
78 try: matchces = json.loads(request.args.get("data").decode("base64"))
79 rr, hm = init()
80 if checkVNvodich(rr, hm):
81 return FLAG
82 except:
83 pass
84 return render_template('index.html', res = res)
85
86
87def getInfo(match, field):
88 val = getattr(match, field, None)
89 if val != None:
90 return val
91 else :
92 return "None"
93
94@app.route("/info/")
95def getinfo(id):
96 match = None
97 for r in res:
98 if r.id == id:
99 match = r
100 break
101 if "field" in request.args.keys():
102 return jsonify(data = getInfo(match, request.args.get("field")))
103 elif match is not None:
104 return jsonify(id = match.id, home = match.home, away = match.away, score = match.score, date = match.date)
105 return jsonify("No data")
106
107@app.route("/files/", methods = ["GET"])
108 def getFile(file):
109 path = app.root_path + "/" + file
110 if not os.path.abspath(path).startswith(app.root_path):
111 return "IFL ??"
112 return open(path, 'rb').read()
113
114
115if __name__ == "__main__": app.run(host = "0.0.0.0")