diff --git a/fstream-client.py b/fstream-client.py index 15ebe96..5736066 100644 --- a/fstream-client.py +++ b/fstream-client.py @@ -4,7 +4,7 @@ import sys oldexcepthook = sys.excepthook def newexcepthook(type,value,traceback): oldexcepthook(type,value,traceback) - input("Press ENTER to quit.") + #input("Press ENTER to quit.") sys.excepthook = newexcepthook import os @@ -51,6 +51,7 @@ def main(): if sys.argv[1] == "watch": while True: data = connection.recv(bufferSize) + if data == b"": return sys.stdout.buffer.write(data) if sys.argv[1] == "broadcast": diff --git a/fstream-server.py b/fstream-server.py index 19a727b..098d6ec 100644 --- a/fstream-server.py +++ b/fstream-server.py @@ -4,7 +4,7 @@ import sys oldexcepthook = sys.excepthook def newexcepthook(type,value,traceback): oldexcepthook(type,value,traceback) - input("Press ENTER to quit.") + #input("Press ENTER to quit.") sys.excepthook = newexcepthook import os @@ -22,11 +22,13 @@ import threading import queue import socket import subprocess +import configparser outThreads = {} inThreads = {} threadId = 0 threadsLock = threading.Lock() +fileLock = threading.Lock() serverAddr = ("127.0.0.1",12000) serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -76,6 +78,10 @@ class outThread(threading.Thread): def closeThread(self): with threadsLock: + try: + self.connection.close() + except Exception as e: + print("closing a connection failed: " +str(e)) del outThreads[str(self.threadId)] def run(self): @@ -97,6 +103,20 @@ class inThread(threading.Thread): def closeThread(self): with threadsLock: + 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 del inThreads[str(self.threadId)] def run(self): @@ -109,9 +129,24 @@ class inThread(threading.Thread): user = args[0] self.user = user - if len(args) > 1: self.password = args[1] + 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) if cmd == "watch": + if cmd in config and "pass" in config[cmd] and config[cmd]["pass"] != "" and config[cmd]["pass"] != password: + self.closeThread() + return + with threadsLock: thread = outThread(self.threadId,self.connection,self.address,self.user) outThreads[str(threadId)] = thread @@ -119,6 +154,10 @@ class inThread(threading.Thread): return if cmd == "broadcast": + if cmd in config and "pass" in config[cmd] and config[cmd]["pass"] != "" and config[cmd]["pass"] != password: + self.closeThread() + return + while True: data = self.connection.recv(bufferSize) with threadsLock: @@ -128,6 +167,7 @@ class inThread(threading.Thread): thread.queue.put(data) self.closeThread() + return except: self.closeThread() raise