2021-04-13 20:06:16 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
|
|
|
|
oldexcepthook = sys.excepthook
|
|
|
|
def newexcepthook(type,value,traceback):
|
|
|
|
oldexcepthook(type,value,traceback)
|
2021-04-14 01:12:13 +00:00
|
|
|
#input("Press ENTER to quit.")
|
2021-04-13 20:06:16 +00:00
|
|
|
sys.excepthook = newexcepthook
|
|
|
|
|
|
|
|
import os
|
|
|
|
p = os.path.join
|
|
|
|
pUp = os.path.dirname
|
|
|
|
s = False
|
|
|
|
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
|
|
|
|
s = os.path.realpath(sys.executable)
|
|
|
|
else:
|
|
|
|
s = os.path.realpath(__file__)
|
|
|
|
sp = pUp(s)
|
|
|
|
|
|
|
|
# script start
|
2021-04-13 18:33:16 +00:00
|
|
|
import threading
|
|
|
|
import queue
|
|
|
|
import socket
|
|
|
|
import subprocess
|
2021-04-14 01:12:13 +00:00
|
|
|
import configparser
|
2021-04-13 18:33:16 +00:00
|
|
|
|
2021-04-13 23:09:34 +00:00
|
|
|
outThreads = {}
|
|
|
|
inThreads = {}
|
2021-04-13 18:33:16 +00:00
|
|
|
threadId = 0
|
|
|
|
threadsLock = threading.Lock()
|
2021-04-14 01:12:13 +00:00
|
|
|
fileLock = threading.Lock()
|
2021-04-13 18:33:16 +00:00
|
|
|
|
|
|
|
serverAddr = ("127.0.0.1",12000)
|
|
|
|
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
|
|
|
|
bufferSize = 1000 # buffer size in bytes
|
|
|
|
|
2021-04-13 23:09:34 +00:00
|
|
|
def commandToList(cmd):
|
|
|
|
args = []
|
|
|
|
cArg = ""
|
|
|
|
escape = False
|
|
|
|
quoted = False
|
|
|
|
for letter in cmd:
|
|
|
|
if escape == True:
|
|
|
|
cArg += letter
|
|
|
|
escape = False
|
|
|
|
continue
|
|
|
|
|
|
|
|
if letter == "\\":
|
|
|
|
escape = True
|
|
|
|
continue
|
|
|
|
|
|
|
|
#if quoted == False and letter == ",":
|
|
|
|
if letter == ",":
|
|
|
|
if cArg == "": continue
|
|
|
|
args.append(cArg)
|
|
|
|
cArg = ""
|
|
|
|
continue
|
|
|
|
|
|
|
|
#if letter == '"':
|
|
|
|
# quoted = not quoted
|
|
|
|
# continue
|
|
|
|
|
|
|
|
cArg += letter
|
|
|
|
|
|
|
|
args.append(cArg)
|
|
|
|
|
|
|
|
return args
|
2021-04-13 18:33:16 +00:00
|
|
|
|
|
|
|
class outThread(threading.Thread):
|
2021-04-13 23:09:34 +00:00
|
|
|
def __init__(self,threadId,connection,address,user):
|
2021-04-13 18:33:16 +00:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.threadId = threadId
|
|
|
|
self.queue = queue.Queue()
|
|
|
|
self.connection = connection
|
|
|
|
self.address = address
|
2021-04-13 23:09:34 +00:00
|
|
|
self.user = user
|
|
|
|
|
|
|
|
def closeThread(self):
|
|
|
|
with threadsLock:
|
2021-04-14 01:12:13 +00:00
|
|
|
try:
|
|
|
|
self.connection.close()
|
|
|
|
except Exception as e:
|
|
|
|
print("closing a connection failed: " +str(e))
|
2021-04-13 23:09:34 +00:00
|
|
|
del outThreads[str(self.threadId)]
|
2021-04-13 18:33:16 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
data = self.queue.get()
|
|
|
|
self.connection.sendall(data)
|
2021-04-13 23:09:34 +00:00
|
|
|
self.closeThread()
|
2021-04-13 18:33:16 +00:00
|
|
|
except:
|
2021-04-13 23:09:34 +00:00
|
|
|
self.closeThread()
|
2021-04-13 18:33:16 +00:00
|
|
|
raise
|
|
|
|
|
2021-04-13 23:09:34 +00:00
|
|
|
class inThread(threading.Thread):
|
|
|
|
def __init__(self,threadId,connection,address):
|
2021-04-13 18:33:16 +00:00
|
|
|
threading.Thread.__init__(self)
|
2021-04-13 23:09:34 +00:00
|
|
|
self.threadId = threadId
|
|
|
|
self.connection = connection
|
|
|
|
self.address = address
|
|
|
|
|
|
|
|
def closeThread(self):
|
|
|
|
with threadsLock:
|
2021-04-14 01:12:13 +00:00
|
|
|
for thread in outThreads:
|
|
|
|
thread = outThreads[thread]
|
|
|
|
if thread.user == self.user:
|
|
|
|
try:
|
|
|
|
thread.connection.close()
|
|
|
|
except Exception as e:
|
|
|
|
print("closing a connection failed: " +str(e))
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.connection.close()
|
|
|
|
except Exception as e:
|
|
|
|
print("closing a connection failed: " +str(e))
|
|
|
|
pass
|
2021-04-13 23:09:34 +00:00
|
|
|
del inThreads[str(self.threadId)]
|
2021-04-13 18:33:16 +00:00
|
|
|
|
|
|
|
def run(self):
|
2021-04-13 23:09:34 +00:00
|
|
|
try:
|
|
|
|
global threadId
|
|
|
|
data = self.connection.recv(1000).decode("utf-8")
|
|
|
|
while data[-1] == " ": data = data[:-1]
|
|
|
|
args = commandToList(data)
|
|
|
|
cmd = args.pop(0)
|
|
|
|
|
|
|
|
user = args[0]
|
|
|
|
self.user = user
|
2021-04-14 01:12:13 +00:00
|
|
|
password = False
|
|
|
|
if len(args) > 1:
|
|
|
|
password = args[1]
|
|
|
|
|
|
|
|
userPath = p(sp,"users",user+ ".ini")
|
|
|
|
with fileLock:
|
|
|
|
if not os.path.isfile(userPath):
|
|
|
|
self.closeThread()
|
|
|
|
return
|
|
|
|
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read(userPath)
|
2021-04-13 23:09:34 +00:00
|
|
|
|
|
|
|
if cmd == "watch":
|
2021-04-14 01:12:13 +00:00
|
|
|
if cmd in config and "pass" in config[cmd] and config[cmd]["pass"] != "" and config[cmd]["pass"] != password:
|
|
|
|
self.closeThread()
|
|
|
|
return
|
|
|
|
|
2021-04-13 23:09:34 +00:00
|
|
|
with threadsLock:
|
|
|
|
thread = outThread(self.threadId,self.connection,self.address,self.user)
|
|
|
|
outThreads[str(threadId)] = thread
|
|
|
|
thread.start()
|
|
|
|
return
|
|
|
|
|
|
|
|
if cmd == "broadcast":
|
2021-04-14 01:12:13 +00:00
|
|
|
if cmd in config and "pass" in config[cmd] and config[cmd]["pass"] != "" and config[cmd]["pass"] != password:
|
|
|
|
self.closeThread()
|
|
|
|
return
|
|
|
|
|
2021-04-13 23:09:34 +00:00
|
|
|
while True:
|
|
|
|
data = self.connection.recv(bufferSize)
|
|
|
|
with threadsLock:
|
|
|
|
for thread in outThreads:
|
|
|
|
thread = outThreads[thread]
|
|
|
|
if thread.user == user:
|
|
|
|
thread.queue.put(data)
|
|
|
|
|
|
|
|
self.closeThread()
|
2021-04-14 01:12:13 +00:00
|
|
|
return
|
2021-04-13 23:09:34 +00:00
|
|
|
except:
|
|
|
|
self.closeThread()
|
|
|
|
raise
|
2021-04-13 18:33:16 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
global threadId
|
2021-04-13 20:06:16 +00:00
|
|
|
serverSocket.bind(serverAddr)
|
|
|
|
serverSocket.listen(1024)
|
|
|
|
|
2021-04-13 18:33:16 +00:00
|
|
|
while True:
|
|
|
|
connection, address = serverSocket.accept()
|
2021-04-13 23:09:34 +00:00
|
|
|
connection.settimeout(15)
|
2021-04-13 18:33:16 +00:00
|
|
|
with threadsLock:
|
|
|
|
threadId += 1
|
|
|
|
|
2021-04-13 23:09:34 +00:00
|
|
|
thread = inThread(threadId,connection,address)
|
|
|
|
inThreads[str(threadId)] = thread
|
2021-04-13 18:33:16 +00:00
|
|
|
thread.start()
|
|
|
|
|
2021-04-13 20:06:16 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|