2022-02-14 21:43:12 +00:00
|
|
|
global ssl
|
|
|
|
import ssl
|
|
|
|
|
|
|
|
global serverThread
|
|
|
|
class serverThread(threading.Thread):
|
2022-03-04 05:25:41 +00:00
|
|
|
def __init__(self,socket,isHttps):
|
2022-02-14 21:43:12 +00:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.socket = socket
|
2022-03-04 05:25:41 +00:00
|
|
|
self.isHttps = isHttps
|
2022-02-14 21:43:12 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
connection = False
|
|
|
|
address = False
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
connection,address = self.socket.accept()
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
2022-03-04 05:25:41 +00:00
|
|
|
if self.isHttps:
|
|
|
|
connection.settimeout(5)
|
|
|
|
connection.do_handshake()
|
2022-02-14 21:43:12 +00:00
|
|
|
connection.settimeout(timeout)
|
|
|
|
if not triggerEvent("onConnection",connection,address): raise excConnectionClosed
|
|
|
|
except Exception as e:
|
|
|
|
handleException(e)
|
|
|
|
try:
|
|
|
|
connection.close()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
global makeServer
|
|
|
|
def makeServer(host,port,https):
|
|
|
|
print("Opening " +str(host)+ ":" +str(port)+ " (" +str(https)+ ") ...")
|
|
|
|
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2022-07-05 13:28:39 +00:00
|
|
|
serverSocket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
|
2022-02-14 21:43:12 +00:00
|
|
|
serverSocket.bind((host,port))
|
|
|
|
if https:
|
2022-03-04 05:25:41 +00:00
|
|
|
proto = False
|
|
|
|
if sys.version_info >= (3,10):
|
|
|
|
proto = ssl.PROTOCOL_TLS_SERVER
|
|
|
|
else:
|
|
|
|
proto = ssl.PROTOCOL_TLS
|
|
|
|
|
|
|
|
ctx = ssl.SSLContext(proto)
|
|
|
|
ctx.check_hostname = False
|
|
|
|
ctx.load_cert_chain(https)
|
|
|
|
serverSocket = ctx.wrap_socket(
|
2022-02-14 21:43:12 +00:00
|
|
|
serverSocket,
|
|
|
|
server_side = True,
|
2022-03-04 05:25:41 +00:00
|
|
|
do_handshake_on_connect = False
|
2022-02-14 21:43:12 +00:00
|
|
|
)
|
|
|
|
serverSocket.listen(65535)
|
2022-03-04 05:25:41 +00:00
|
|
|
thread = serverThread(serverSocket,not (https == False))
|
2022-02-14 21:43:12 +00:00
|
|
|
serverThreads.append(thread)
|
|
|
|
thread.start()
|