-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServer.py
128 lines (94 loc) · 3.64 KB
/
Server.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from aiohttp import web
from hashlib import md5
import asyncio, logging, psycopg2, bcrypt, json, sys
# Setup logging for web requests.
logging.basicConfig(level = logging.INFO)
# Load our configuration.
data = open('credentials.json').read()
credentials = json.loads(data)
# Grab our database information.
host = credentials['Host']
database = credentials['Database']
username = credentials['Username']
password = credentials['Password']
# Connect to our PostgreSQL database.
conn = psycopg2.connect(host = host, database = database, user = username, password = password)
cur = conn.cursor()
class Crypto:
@staticmethod
def hash(undigested):
if type(undigested) == str:
undigested = undigested.encode('utf-8')
elif type(undigested) == int:
undigested = str(undigested).encode('utf-8')
return md5(undigested).hexdigest()
@staticmethod
def encryptPassword(password, digest = True):
if digest:
password = Crypto.hash(password)
swappedHash = password[16:32] + password[0:16]
return swappedHash
@staticmethod
def getLoginHash(password, rndk):
key = Crypto.encryptPassword(password, False)
key += rndk
key += 'Y(02.>\'H}t":E1'
loginHash = Crypto.encryptPassword(key)
return loginHash
class RequestTypes:
MAIN_SCREEN = 1
UPLOAD_COINS = 2
ONLINE_POLL = 3
POLL_SELECT = 4
NEWSLETTER = 5
MISSION_DOWNLOAD = 7
ACCOUNT_VALIDATION = 8
DOWNLOAD_NEWSLETTER = 9
async def handleSubmit(request):
args = await request.post()
requestId = int(args.get('RequestID'))
if requestId in (RequestTypes.ACCOUNT_VALIDATION, RequestTypes.UPLOAD_COINS):
username = args.get('UserName').decode().lower()
password = args.get('Password').decode()
if requestId == RequestTypes.UPLOAD_COINS:
amount = int(args.get('Amount'))
cur.execute("""SELECT coins FROM penguin WHERE username = %(username)s""", {'username': username})
curAmount = cur.fetchall()
if curAmount == []:
return web.Response()
else:
curAmount = curAmount[0][0]
curAmount += amount
cur.execute("""UPDATE penguin SET coins = %(curAmount)s WHERE username = %(username)s""", {'curAmount': curAmount, 'username': username})
conn.commit()
return web.Response(text = str(amount))
elif requestId == RequestTypes.ONLINE_POLL:
return web.Response(text = 'Test Poll')
elif requestId == RequestTypes.POLL_SELECT:
pollSelection = args.get('PollSelection').decode()
return web.Response(text = '25%')
elif requestId in (RequestTypes.NEWSLETTER, RequestTypes.DOWNLOAD_NEWSLETTER):
return web.Response(text = 'Test!')
elif requestId == RequestTypes.ACCOUNT_VALIDATION:
cur.execute("""SELECT password FROM penguin WHERE username = %(username)s""", {'username': username})
dbPassword = cur.fetchall()
if dbPassword == []:
return web.Response()
password = Crypto.hash(password).upper()
password = Crypto.getLoginHash(password, rndk = 'houdini')
if not bcrypt.checkpw(password.encode(), dbPassword[0][0].encode()):
return web.Response()
else:
return web.Response(text = '001')
return web.Response()
async def initializeService():
app = web.Application()
app.router.add_post('/submit_uk.php', handleSubmit)
return app
loop = asyncio.get_event_loop()
app = loop.run_until_complete(initializeService())
if '--nginx-proxy' in sys.argv:
port = 8080
else:
port = 80
web.run_app(app, host = '0.0.0.0', port = port)