This repository was archived by the owner on Nov 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
251 lines (228 loc) · 9.96 KB
/
app.py
File metadata and controls
251 lines (228 loc) · 9.96 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import threading
from typing import Optional
from flask import Flask
from errite.tools.mis import fileExists
import errite.da.daParser as dp
import psycopg2.pool
import psycopg2
import json
import time
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from errite.da.datools import getDLSUserInfo
import binascii
import uuid
import redis
import sentry_sdk
from flask import Flask
from sentry_sdk.integrations.flask import FlaskIntegration
app = Flask(__name__)
print("Deviant Logic Server V1.6.1")
print("Developed by Errite Games LLC")
file = open('private.key', 'r')
external_key = file.read()
other_file = open('private-other.key', 'r')
other_ext_key = other_file.read()
key = RSA.import_key(external_key)
other_key = RSA.import_key(other_ext_key)
redisData = None
profileRedisData = None
shardData = None
configData = None
clientData = None
profile_pool = None
with open("client.json") as clientJson:
clientData = json.load(clientJson)
if fileExists("db.json"):
database_active = True
with open("db.json", "r") as dbJson:
dbInfo = json.load(dbJson)
database_name = dbInfo["database-name"]
database_host = dbInfo["database-host"]
database_host2 = dbInfo["database-host2"]
database_host3 = dbInfo["database-host3"]
database_password = dbInfo["database-password"]
database_user = dbInfo["database-username"]
database_port = dbInfo["database-port"]
stop_duplicaterecovery = False
with open("config.json", "r") as configJson:
configData = json.load(configJson)
use_sentry = configData["sentry-enabled"]
sentry_url = configData["sentry-url"]
sentry_sdk.init(
dsn=sentry_url,
integrations=[FlaskIntegration()],
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
# We recommend adjusting this value in production.
traces_sample_rate=1.0
)
if database_host2 == "none":
connect_str = "dbname='" + database_name + "' user='" + database_user \
+ "'host='" + database_host + "' " + \
"'port='" + str(database_port) + "password='" + database_password + "'"
elif database_host3 == "none":
connect_str = "dbname='" + database_name + "' user='" + database_user \
+ "'host='" + database_host + "," + database_host2 + "' " + \
"'port='" + str(database_port) + "password='" + database_password + "'"
else:
connect_str = "dbname='" + database_name + "' user='" + database_user \
+ "'host='" + database_host + "," + database_host2 + "," + database_host3 + " " + \
"'port='" + str(database_port) + "'password='" + database_password + "'"
print("Connecting to database")
_connPool: Optional[psycopg2.pool.ThreadedConnectionPool] = None
_poolSemaphore = threading.Semaphore(40) # 10 is max no of connections in this case
_connPool = psycopg2.pool.ThreadedConnectionPool(1, 40, connect_str)
#db_connection = psycopg2.connect(connect_str)
print("Preparing to connect to Redis")
with open("redis.json", "r") as redisJson:
redisData = json.load(redisJson)
redisStr = redisData["url"]
redisPassword = redisData["password"]
print("Connecting to redis ")
rpool = redis.ConnectionPool.from_url(url=redisStr, password=redisPassword, db=0)
print("Connected to redis!")
redisJson.close()
with open("profile_redis.json", "r") as profileRedisJson:
profileRedisData = json.load(profileRedisJson)
profile_redisStr = profileRedisData["url"]
profile_redisPassword = profileRedisData["password"]
print("Connecting to profile redis ")
profile_pool = redis.ConnectionPool.from_url(url=profile_redisStr, password=profile_redisPassword, db=0)
print("Connected to profile redis!")
profileRedisJson.close()
print("Loading ShardData...")
with open("shard.json","r") as shardJson:
shardData = json.load(shardJson)
print("ShardData loaded!")
shardJson.close()
def getConnection():
_poolSemaphore.acquire(blocking=True)
print("Pool is delivering connection")
return _connPool.getconn()
def putConnectionBack(conn: psycopg2):
_connPool.putconn(conn, close=False)
_poolSemaphore.release()
print("Pool tool back a connection")
@app.route('/get_da_profile/<artist>/<token>/<da_token>')
def get_da_profile(artist=None, token=None, da_token=None):
ttconnection = redis.Redis(connection_pool=rpool)
token_result = ttconnection.get(token)
ttconnection.close()
if not token_result is None:
decryptor = PKCS1_OAEP.new(other_key)
obt_password_unhexed = binascii.unhexlify(da_token)
decrypted_token = decryptor.decrypt(obt_password_unhexed)
db_connection = getConnection()
pscursor = db_connection.cursor()
sql = """SELECT * FROM deviantcord.artist_info WHERE artist = %s"""
pscursor.execute(sql, (artist,))
obt_results = pscursor.fetchall()
if not len(obt_results) == 0:
jsonResponse = {}
jsonResponse["result"] = "pass"
jsonResponse["artist-name"] = obt_results [0][0]
jsonResponse["artist-icon"] = obt_results[0][1]
jsresponse = json.dumps(jsonResponse)
pscursor.close()
putConnectionBack(db_connection)
return jsresponse
else:
str_pass = decrypted_token.decode('utf-8')
obt_da_resp = dp.userInfoResponse(artist, str_pass, False)
validUser = True
if obt_da_resp["response"].status == 500 or obt_da_resp["response"].status == 400:
validUser = False
if validUser:
userInfo = getDLSUserInfo(obt_da_resp["data"])
redis_connection = redis.Redis(connection_pool=profile_pool)
redis_connection.set(artist.upper() + "-icon", userInfo["user_pic"])
redis_connection.expire(artist.upper() + "-icon", 259200)
sql = """INSERT INTO deviantcord.artist_info(artist, artist_picture_url) VALUES(%s, %s);"""
artist_cursor = db_connection.cursor()
artist_cursor.execute(sql, (artist, userInfo["user_pic"],))
db_connection.commit()
artist_cursor.close()
jsonResponse = {}
jsonResponse["result"] = "pass"
jsonResponse["artist-name"] = artist.upper()
jsonResponse["artist-icon"] = userInfo["user_pic"]
jsresponse = json.dumps(jsonResponse)
pscursor.close()
putConnectionBack(db_connection)
return jsresponse
else:
jsonResponse = {}
jsonResponse["result"] = "no-artist-change"
jsresponse = json.dumps(jsonResponse)
pscursor.close()
putConnectionBack(db_connection)
return jsresponse
else:
jsonResponse = {}
jsonResponse["result"] = "fail"
jsresponse = json.dumps(jsonResponse)
return jsresponse
@app.route('/get_token/<username>/<password>/<node_name>')
def get_token(username=None, password=None, node_name=None):
db_connection = getConnection()
login_cursor = db_connection.cursor()
login_cursor.execute("SELECT * FROM deviantcord.deviant_accounts WHERE username = %s",(username,))
results = login_cursor.fetchall()
obt_password = results[0][1]
obt_password_unhexed = binascii.unhexlify(obt_password)
decryptor = PKCS1_OAEP.new(key)
decrypted_given = decryptor.decrypt(obt_password_unhexed)
decrypted = decryptor.decrypt(binascii.unhexlify(password))
login_cursor.close()
putConnectionBack(db_connection)
if decrypted == decrypted_given:
login_token = str(uuid.uuid1())
rtokenconnection = redis.Redis(connection_pool=rpool, retry_on_timeout=True)
rtokenconnection.set(login_token, node_name)
rtokenconnection.expire(login_token,3600)
rtokenconnection.close()
jsonResponse = {}
jsonResponse["result"] = "pass"
jsonResponse["token"] = login_token
jsresponse = json.dumps(jsonResponse)
return jsresponse
else:
jsonResponse = {}
jsonResponse["result"] = "fail"
jsresponse = json.dumps(jsonResponse)
return jsresponse
login_cursor.close()
@app.route('/get_shard/<token>/<shard_type>')
def findNextShardID(token=None, shard_type=None):
ttconnection = redis.Redis(connection_pool=rpool)
token_result = ttconnection.get(token)
ttconnection.close()
if not token_result is None:
max = shardData[shard_type][shard_type + "-max"]
index = 0
chosenid = 0
lowest = 0
while not index == max:
index = index + 1
if index == 1:
lowest = shardData[shard_type][shard_type + "-1"]
chosenid = index
elif shardData[shard_type][shard_type + "-" + str(index)] < lowest:
lowest = shardData[shard_type][shard_type + "-" + str(index)]
chosenid = index
responseData = {}
responseData["results"] = "pass"
responseData["chosen-id"] = chosenid
response = json.dumps(responseData)
shardData[shard_type][shard_type + "-" + str(chosenid)] = shardData[shard_type][shard_type + "-" + str(chosenid)] + 1
saveFile = open("shard.json", "w+")
saveFile.write(json.dumps(shardData, indent=4, sort_keys=True))
saveFile.close()
else:
responseData = {}
responseData["results"] = "fail"
response = json.dumps(responseData)
return response
if __name__ == '__main__':
app.run(host="0.0.0.0")