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