forked from xan1242/NFSC_MasterServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInit.py
More file actions
executable file
·138 lines (122 loc) · 7.57 KB
/
Copy pathInit.py
File metadata and controls
executable file
·138 lines (122 loc) · 7.57 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
#! python3
#import stdin
import sys
import os
import threading
import configparser
import sqlite3
import logging
import socket
from Logger import *
from Database import *
from Network import *
try:
from twisted.internet import ssl, reactor
from twisted.internet.protocol import Factory, Protocol
from twisted.web.server import Site
from OpenSSL import SSL
Log("Init", "\033[37m").new_message("Starting nu-EAN, an EA Nation 1.0 server emulator...\n")
def mainapp():
try:
ssl_key = readFromConfig("SSL", "priv_key_path")
ssl_cert = readFromConfig("SSL", "cert_file_path")
plasma_client_port = int(readFromConfig("connection", "plasma_client_port"))
plasma_server_port = int(readFromConfig("connection", "plasma_server_port"))
theater_client_port = int(readFromConfig("connection", "theater_client_port"))
theater_server_port = int(readFromConfig("connection", "theater_server_port"))
messenger_server_port = int(readFromConfig("connection", "messenger_server_port"))
http_server_port = int(readFromConfig("connection", "http_server_port"))
except Exception as ConfigError:
logger.error("Fatal Error!\n"
"Failed to load certain values in the config.ini, be sure that EVERY \n"
"option has a valid value and try it again.\n\n"
"Additional error info:\n" + str(ConfigError))
sys.exit(2)
try:
Database(True)
except Exception as DatabaseError:
Log("Database", "\033[37;1;41m").new_message("Fatal Error! Cannot initialize database!\n\n"
"Additional error info:\n" + str(DatabaseError), 0)
sys.exit(3)
try:
SSLContext = ssl.DefaultOpenSSLContextFactory(ssl_key, ssl_cert)
Log("Init", "\033[37m").new_message("Successfully created SSL Context!", 2)
except Exception as SSLErr:
Log("Init", "\033[37;41m").new_message("Fatal Error!\n"
"Failed to create SSL Context!\n"
"Make sure that you installed all required modules using\n"
"`pip install -r requirements.txt`\n"
"Also check if you specified correct SSL Cert and/or key in "
"`config.ini`\n "
"Additional error info:\n" + str(SSLErr), 0)
sys.exit(4)
try:
factory = Factory()
factory.protocol = PlasmaClient.HANDLER
reactor.listenSSL(plasma_client_port, factory, SSLContext)
Log("PlasmaClient", "\033[33;1m").new_message("Created TCP Socket (now listening on port " + str(plasma_client_port) + ")", 1)
except Exception as BindError:
Log("Init", "\033[33;1;41m").new_message("Fatal Error! Cannot bind socket to port: " + str(plasma_client_port) + "\n"
"Make sure that this port isn't used by another program!\n\n"
"Additional error info:\n" + str(BindError), 0)
sys.exit(5)
try:
factory = Factory()
factory.protocol = PlasmaServer.HANDLER
reactor.listenSSL(plasma_server_port, factory, SSLContext)
Log("PlasmaServer", "\033[32;1m").new_message("Created TCP Socket (now listening on port " + str(plasma_server_port) + ")", 1)
except Exception as BindError:
Log("Init", "\033[33;1;41m").new_message("Fatal Error! Cannot bind socket to port: " + str(plasma_server_port) + "\n"
"Make sure that this port isn't used by another program!\n\n"
"Additional error info:\n" + str(BindError), 0)
sys.exit(5)
try:
factoryTCP = Factory()
factoryTCP.protocol = TheaterClient.TCPHandler
reactor.listenTCP(theater_client_port, factoryTCP)
Log("TheaterClient", "\033[35;1m").new_message("Created TCP Socket (now listening on port " + str(theater_client_port) + ")", 1)
reactor.listenUDP(theater_client_port, TheaterClient.UDPHandler())
Log("TheaterClient", "\033[35;1m").new_message("Created UDP Socket (now listening on port " + str(theater_client_port) + ")", 1)
except Exception as BindError:
Log("Init", "\033[35;1;41m").new_message("Fatal Error! Cannot bind socket to port: " + str(theater_client_port) + "\n"
"Make sure that this port isn't used by another program!\n\n"
"Additional error info:\n" + str(BindError), 0)
sys.exit(5)
try:
factoryTCP = Factory()
factoryTCP.protocol = TheaterServer.TCPHandler
reactor.listenTCP(theater_server_port, factoryTCP)
Log("TheaterServer", "\033[36;21m").new_message("Created TCP Socket (now listening on port " + str(theater_server_port) + ")", 1)
reactor.listenUDP(theater_server_port, TheaterServer.UDPHandler())
Log("TheaterServer", "\033[36;21m").new_message("Created UDP Socket (now listening on port " + str(theater_server_port) + ")", 1)
except Exception as BindError:
Log("Init", "\033[35;1;41m").new_message("Fatal Error! Cannot bind socket to port: " + str(theater_server_port) + "\n"
"Make sure that this port isn't used by another program!\n\n"
"Additional error info:\n" + str(BindError), 0)
sys.exit(5)
try:
factoryTCP = Factory()
factoryTCP.protocol = MessengerServer.TCPHandler
reactor.listenTCP(messenger_server_port, factoryTCP)
Log("MessengerServer", "\033[36;1m").new_message("Created TCP Socket (now listening on port " + str(messenger_server_port) + ")", 1)
reactor.listenUDP(messenger_server_port, MessengerServer.UDPHandler())
Log("MessengerServer", "\033[36;1m").new_message("Created UDP Socket (now listening on port " + str(messenger_server_port) + ")", 1)
except Exception as BindError:
Log("Init", "\033[35;1;41m").new_message("Fatal Error! Cannot bind socket to port: " + str(messenger_server_port) + "\n"
"Make sure that this port isn't used by another program!\n\n"
"Additional error info:\n" + str(BindError), 0)
sys.exit(5)
try:
site = Site(WebServer.Handler())
reactor.listenTCP(http_server_port, site)
Log("WebServer", "\033[36m").new_message("Created TCP Socket (now listening on port " + str(http_server_port) + ")", 1)
except Exception as BindError:
Log("Init", "\033[35;1;41m").new_message("Fatal Error! Cannot bind socket to port: " + str(http_server_port) + "\n"
"Make sure that this port isn't used by another program!\n\n"
"Additional error info:\n" + str(BindError), 0)
sys.exit(5)
Log("Init", "\033[37m").new_message("Finished initialization! Ready for receiving incoming connections...", 0)
reactor.run()
finally:
if __name__ == '__main__':
mainapp()