forked from pesupy-chat/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_main.py
225 lines (172 loc) · 9.17 KB
/
server_main.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
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
import os
import sys
import asyncio
import websockets
from server_modules import firstrun
from server_modules import encryption as en
from server_modules import db_handler as db
from server_modules import packet_handler as p
import i18n
from yaml import safe_load as loadyaml
from yaml import dump as dumpyaml
def execute_firstrun():
firstrun.main()
print(i18n.firstrun.security)
db.decrypt_creds(en.fernet_gen(firstrun.working_dir.workingdir), firstrun.working_dir.workingdir)
print(i18n.firstrun.initialize_db)
db.initialize_schemas()
db.close()
print(i18n.firstrun.exit)
sys.exit()
# def check_missing_config(f, yamlc, config):
# try:
# if yamlc[config] is None:
# print(i18n.firstrun.setting_not_found.format(config))
# if config == 'working_directory':
# print(i18n.firstrun.ft_question)
# while True:
# choice = input("(Y / N) > ")
# if choice.lower() == 'y':
# print(i18n.firstrun.exec)
# f.close()
# os.remove(f'{rootdir}/config.yml')
# firstrun.main()
# print(i18n.firstrun.exit)
# sys.exit()
# elif choice.lower() == 'n':
# fill_missing_config(f, yamlc, 'working_directory')
# break
# else:
# fill_missing_config(f, yamlc, config)
# except KeyError:
# print(i18n.firstrun.setting_not_found.format(config))
# fill_missing_config(f, yamlc, config)
def check_missing_config(file, yaml_config, config_key):
if yaml_config.get(config_key) is not None:
return
print(i18n.firstrun.setting_not_found.format(config_key))
if config_key != 'working_directory':
fill_missing_config(file, yaml_config, config_key)
return
print(i18n.firstrun.ft_question)
while True:
choice = input("(Y / N) > ").lower()
if choice == 'y':
print(i18n.firstrun.exec)
file.close()
os.remove(f'{rootdir}/config.yml')
firstrun.main()
print(i18n.firstrun.exit)
sys.exit()
elif choice == 'n':
fill_missing_config(file, yaml_config, 'working_directory')
break
def fill_missing_config(f, yamlc, config):
print(i18n.firstrun.fix_missing.format(config))
yamlc[config] = input('\n> ')
if config in ('listen_port', 'any_other_int_type_config'):
yamlc[config] = int(yamlc[config])
f.seek(0)
f.write(dumpyaml(yamlc))
async def catch(websocket):
# Handle incoming packets
try:
while True:
result = await p.handle(SESSIONS, SERVER_CREDS, await websocket.recv(), websocket)
if result in ('CONN_CLOSED',):
pass
elif result:
await websocket.send(result)
# Handle disconnection due to any exception
except Exception as err3:
client = await p.identify_client(websocket, SESSIONS)
print(i18n.log.tags.info + i18n.log.conn.disconnected.format(client, err3))
del SESSIONS[client]
return None
async def main(chost, cport):
async with websockets.serve(
catch, host=chost, port=cport,
ping_interval=120, ping_timeout=None, close_timeout=None,
max_size=1048576
):
await asyncio.Future() # run forever
if __name__ == "__main__":
SESSIONS = {}
SERVER_CREDS = {}
try:
rootdir = os.path.dirname(os.path.abspath(__file__))
print('\n' + i18n.log.tags.info + i18n.log.server_start.format(rootdir))
f = open(f'{rootdir}/config.yml', 'r+')
yaml = loadyaml(f.read())
if not yaml:
f.close()
os.remove(f'{rootdir}/config.yml')
print(i18n.firstrun.empty_config, i18n.firstrun.exit, end='\n')
sys.exit()
check_missing_config(f, yaml, 'working_directory')
check_missing_config(f, yaml, 'listen_address')
check_missing_config(f, yaml, 'listen_port')
if not os.path.isfile(f"{yaml['working_directory']}/creds/db"):
print(i18n.database.creds_not_found)
execute_firstrun()
f.close()
except FileNotFoundError as err:
print(i18n.firstrun.config_not_found, i18n.firstrun.exec)
execute_firstrun()
workingdir = yaml['working_directory']
host, port = yaml['listen_address'], yaml['listen_port']
try:
fkey = en.fernet_gen(workingdir)
db.decrypt_creds(fkey, workingdir)
except Exception as w:
print(i18n.database.de_cred_fail.format(w))
print(i18n.firstrun.exit)
sys.exit()
server_eprkey, server_epbkey = en.create_rsa_key_pair()
SERVER_CREDS['server_eprkey'] = server_eprkey
SERVER_CREDS['server_epbkey'] = en.ser_key_pem(server_epbkey, 'public')
print('\n' + i18n.log.tags.info + i18n.log.server_online)
try:
asyncio.run(main(host, port))
except KeyboardInterrupt:
print('\n' + i18n.log.tags.info + i18n.log.server_exit)
db.close()
sys.exit()
"""
this server is powered by this cat -- https://rory.cat
,,,,...,,,***//////////(/////****,,,,,,***,,,,,,,,,,****//////////((#####(((////////////////////////////////
........,,****/////((//////////*****,,,,,,,,,,,,,,******//////////(######(((////////////////////////////////
...... ..,***///(//(((((((/////////**********,********///////////(((######((((///******///***///////////////
...... .,*%((##%#((((/(/(((((((((((//////*****/////////////////(((#########((//***,,*******///////////////
,,.........,((((#%%#(%#(((((((((((##(((///////////////**/////////((####%%%####(//**,,,,,,**/////////////////
,,,,..,,,,,*((///((####(#(((((((######(((////////////*********////((########(((//********/&#(((////////////(
,,,,,,,,,,,**#(/(#####%##(/((//((####(((/////////////******///////((((####((((//****/(#%((((/(/*/////(((((((
,,,,,,,,,,****#(((#########((/((/((((((///*///////////////((((((((((((((((((////((((%(((((//(***////((((((((
,,,,,,,,*,,,***#((((#((######(/*,/#(##/****///(((((((((#####%%%%##((((###((((//*/#((((((#(#(/**////(((((((((
,,,*************#(/(((#######((/*,*,*(/****/**/*****,**,,**//**/((((//****,*,*#(//(/(((((#(((//////(((((((((
*****************((((/((((((#(//(*,*,**/***,,****,***,*,,*,****,***/*/**/(/,,,*///((((###%###(((/////(((((((
************,******(((///*//((##/**,**/**,,,,,*(/%/***,,,,,,,,,,,,***,*,,,,,,***////((#%%%%%###(((////(((((#
********,,,,,,,,,**/(((////(((///*****,/***,/((%#&/**,,,***,,,,,,*..,,,.,,,,,***///(/(#%%%%%%###(((//(((((##
,,,,,,,,,,,,,,,,,,,*(#(**,/////******/**(//#&%#@&&#**,***,/*,*,,*,,.,,,****(/((////**(%%&%%%%%##(((///((((##
,,,,..............,,*%(//**/*/*//(//#(#(#((%&&&@@&%*,/,,,*/,,,.**,***,,,,,,***///***/#&&&%%%%%##((/////(((##
,,,..................,(/*/******(//*(%%(##%&@@@@@&%((/,,*//,***/(#/*****,,,,,*,*,,,,*%&&&&&%%%##((/////((((#
*,,,,,,..............,(/*,,**((((//*/(#(//&&&&&&&&&%#//**//*,,*((****/////,,,,,,,,**#%&&&&&%%%%##((///(((((#
***,,,,,,,,,.......,*/**///*//////////****(#%%%%&%%%%#//****,*///*****(((*/*,,,,,,*//#%&&&&%%%%##(((//(((((#
******,,,,,,,.....,,*******,*,. ,(. . .*/#%%%%%&%%%%#(*****,,.. ...,******//*,,*/(%%&&&%%%%###((//(((((#
********,,,,,,,,,,,/**,,*//(//* *,. .,.,.#%%%%&%%%%%%%#(/, ... .,,.,***,,*******/%%%%%%%%%%##((///((((#
*********,,,,,,,,,***,,////***//* ,*,,,,...#&&&&&&%%%%%%%/..,,/*, ,,..//*///*,,,,***/(%%%%%%%%%##((///(((((
**************************,,,,***/((((/(#,,%&&&&&&&&&&&%(,.... .,**,,.,******,,,,,,**(%%%%%%%###(((//(((((
*****************//*///****,,,,,/(#%&&&&&&&&&&&&&&&&&%&&%/,/(#(/**/*,..,,,,,,**/*/*,,,,*/(########(((///((((
****************//(((#((///,,,,*((%&&&&&&&&&&&&@@&&&&&&%%&&&&&&%%%#/*,,,**,*///*/((/*,,*/###%%#((((##%######
*****************//(((##(////***#%&&&&&&&&&&&&&&&&&&&%%%%%&&&&%%%%%(*,*//((((///*****,,*(((((#%#(///**//(###
*******************/###((((((###%%%%%%&&&&&%&%%%%%%%%%#%&&%%%%%%%%####(/((##((//**,*,**(#((/*//((/******//(#
********************/(#########%%%%&&&&%&&&#/*#(/(/*,,(#%&&%%%%%%%%%%##(//(((/****/**/((///*****/*********//
/////////************/(%%######%%%%%%%%%%%%%%%//**./####%##%#%%%%%%%%####((((#(((((##%%%(////***//*,,,*,,,*/
/////////**/*********//(%%####%##%%%%####%%%####//(######%%%%%###%#%%%%%%%######%%%%%%###/**,,,,****,,,,,,**
////***************///(%&&%%%%%%###%%#########(/,,*(((############%%%%%%%%%%%%%%%%%%%##(/*,,*/*******,,,,,**
*********,,*******//((%&@&&&%%%%%%###%%%##(#((#((((//**/////(##########%%%%%%%&&&&&&%%%(,,,***,*,,***,,,,**/
*****************///(%&&&&&%%%%%%%%######%%%###((((################%%%%%%%&&&&&&&&&%#(**/*..***,,,,,,,.,,***
******************/(#&&&&&%%%%%%%%%%%%%######%%%#############%%%%%%%%%%&&&&&&&&%%%%##%/,*,*/,,.,,,,,,,,,**//
*************,,***/(%&&&&&%%%%%%%%%%%%%%%%%%%%##%%#%%###%%%#%%%%%%%%%%%%%%%%%%%%(((//**/(*,,**,...,,,,,,*//(
,,********,,,,,,**/(%&&&&%%%%%%%%%%%%%%%%%%%%%%%%%#%%%%%%%%%%%%%%%%%%%%%%%#%#(/(*,/**,,,,,*,,.,*,.,,,,***///
"""