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