Functional broadcast and watch passwords

This commit is contained in:
Fierelier 2021-04-14 03:12:13 +02:00
parent 2bf7d9950b
commit 19717d1094
2 changed files with 44 additions and 3 deletions

View File

@ -4,7 +4,7 @@ import sys
oldexcepthook = sys.excepthook oldexcepthook = sys.excepthook
def newexcepthook(type,value,traceback): def newexcepthook(type,value,traceback):
oldexcepthook(type,value,traceback) oldexcepthook(type,value,traceback)
input("Press ENTER to quit.") #input("Press ENTER to quit.")
sys.excepthook = newexcepthook sys.excepthook = newexcepthook
import os import os
@ -51,6 +51,7 @@ def main():
if sys.argv[1] == "watch": if sys.argv[1] == "watch":
while True: while True:
data = connection.recv(bufferSize) data = connection.recv(bufferSize)
if data == b"": return
sys.stdout.buffer.write(data) sys.stdout.buffer.write(data)
if sys.argv[1] == "broadcast": if sys.argv[1] == "broadcast":

View File

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