minor network/socket changes

This commit is contained in:
Dpeta 2022-06-16 20:25:24 +02:00
parent 5c4d21640d
commit 1b479031fd
3 changed files with 31 additions and 29 deletions

12
irc.py
View file

@ -46,19 +46,16 @@ class PesterIRC(QtCore.QThread):
self.NickServ = services.NickServ() self.NickServ = services.NickServ()
self.ChanServ = services.ChanServ() self.ChanServ = services.ChanServ()
def IRCConnect(self): def IRCConnect(self):
with open(_datadir + "server.json", "r") as server_file:
read_file = server_file.read()
server_file.close()
server_obj = json.loads(read_file)
server = self.config.server() server = self.config.server()
port = self.config.port() port = self.config.port()
ssl = self.config.ssl()
self.cli = IRCClient(PesterHandler, self.cli = IRCClient(PesterHandler,
host=server, host=server,
port=int(port), port=port,
nick=self.mainwindow.profile().handle, nick=self.mainwindow.profile().handle,
real_name='pcc31', real_name='pcc31',
timeout=120, timeout=120,
ssl=server_obj['TLS']) ssl=ssl)
self.cli.command_handler.parent = self self.cli.command_handler.parent = self
self.cli.command_handler.mainwindow = self.mainwindow self.cli.command_handler.mainwindow = self.mainwindow
self.cli.connect() self.cli.connect()
@ -79,7 +76,7 @@ class PesterIRC(QtCore.QThread):
self.cli.close() self.cli.close()
self.stopIRC = "%s, %s" % (type(se), se) self.stopIRC = "%s, %s" % (type(se), se)
return return
except (OSError, IndexError) as se: except (OSError, IndexError, ValueError) as se:
self.stopIRC = "%s, %s" % (type(se), se) self.stopIRC = "%s, %s" % (type(se), se)
PchumLog.debug("socket error, exiting thread") PchumLog.debug("socket error, exiting thread")
return return
@ -543,6 +540,7 @@ class PesterHandler(DefaultCommandHandler):
#mychumhandle = self.mainwindow.profile().handle #mychumhandle = self.mainwindow.profile().handle
mymood = self.mainwindow.profile().mood.value() mymood = self.mainwindow.profile().mood.value()
if not self.mainwindow.config.lowBandwidth(): if not self.mainwindow.config.lowBandwidth():
time.sleep(0.413 + 0.097) # <--- somehow, this actually helps.
helpers.join(self.client, "#pesterchum") helpers.join(self.client, "#pesterchum")
# Moods via metadata # Moods via metadata
helpers.metadata(self.client, '*', 'sub', 'mood') helpers.metadata(self.client, '*', 'sub', 'mood')

View file

@ -152,8 +152,11 @@ class IRCClient:
# socket.timeout is deprecated in 3.10 # socket.timeout is deprecated in 3.10
PchumLog.warning("TimeoutError in on send, " + str(e)) PchumLog.warning("TimeoutError in on send, " + str(e))
raise socket.timeout raise socket.timeout
except (OSError, IndexError) as e: except (OSError,
IndexError,
ValueError) as e:
# Unknown error, might as well retry? # Unknown error, might as well retry?
# index/value can happen if the socket breaks.
PchumLog.warning("Unkown error on send, " + str(e)) PchumLog.warning("Unkown error on send, " + str(e))
if tries >= 9: if tries >= 9:
raise e raise e
@ -162,7 +165,7 @@ class IRCClient:
time.sleep(0.1) time.sleep(0.1)
PchumLog.debug("ready_to_write (len %s): " % str(len(ready_to_write)) + str(ready_to_write)) PchumLog.debug("ready_to_write (len %s): " % str(len(ready_to_write)) + str(ready_to_write))
except OSError as se: except Exception as se:
PchumLog.warning("socket.error %s" % str(se)) PchumLog.warning("socket.error %s" % str(se))
if not self.blocking and se.errno == 11: if not self.blocking and se.errno == 11:
pass pass
@ -176,8 +179,14 @@ class IRCClient:
if self.ssl == True: if self.ssl == True:
context = ssl.create_default_context() context = ssl.create_default_context()
# Checking relies on the system
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
bare_sock = socket.create_connection(("%s" % self.host, self.port)) bare_sock = socket.create_connection(("%s" % self.host, self.port))
self.socket = context.wrap_socket(bare_sock, server_hostname=self.host, do_handshake_on_connect=False) self.socket = context.wrap_socket(bare_sock,
server_hostname=self.host,
do_handshake_on_connect=False)
while True: while True:
try: try:
self.socket.do_handshake() self.socket.do_handshake()

View file

@ -369,9 +369,7 @@ with a backup from: <a href='%s'>%s</a></h3></html>" % (_datadir, self.filename,
read_file = server_file.read() read_file = server_file.read()
server_file.close() server_file.close()
server_obj = json.loads(read_file) server_obj = json.loads(read_file)
server = str(server_obj['server']) return server_obj['server']
#print("Server is: " + server)
return server
except: except:
try: try:
with open(_datadir + "server.json", "w") as server_file: with open(_datadir + "server.json", "w") as server_file:
@ -383,7 +381,6 @@ with a backup from: <a href='%s'>%s</a></h3></html>" % (_datadir, self.filename,
server_file.write(json.dumps(json_server_file, indent = 4) ) server_file.write(json.dumps(json_server_file, indent = 4) )
server_file.close() server_file.close()
server = "irc.pesterchum.xyz" server = "irc.pesterchum.xyz"
#print("Failed to read server, defaulting to irc.pesterchum.xyz")
except: except:
return self.config.get('server', "irc.pesterchum.xyz") return self.config.get('server', "irc.pesterchum.xyz")
def port(self): def port(self):
@ -394,24 +391,22 @@ with a backup from: <a href='%s'>%s</a></h3></html>" % (_datadir, self.filename,
read_file = server_file.read() read_file = server_file.read()
server_file.close() server_file.close()
server_obj = json.loads(read_file) server_obj = json.loads(read_file)
port = str(server_obj['port']) port = server_obj['port']
#print("Port is: " + port)
return port return port
except: except:
return self.config.get('port', '6697') return self.config.get('port', '6697')
## def TLS(self):
## if hasattr(self.parent, 'tlsOverride'): def ssl(self):
## return self.parent.tlsOverride #if hasattr(self.parent, 'tlsOverride'):
## try: # return self.parent.tlsOverride
## with open(_datadir + "server.json", "r") as server_file: try:
## read_file = server_file.read() with open(_datadir + "server.json", "r") as server_file:
## server_file.close() read_file = server_file.read()
## server_obj = json.loads(read_file) server_file.close()
## TLS = str(server_obj['TLS']) server_obj = json.loads(read_file)
## print("TLS-status is: " + TLS) return server_obj['TLS']
## return TLS except:
## except: return self.config.get('TLS', True)
## return self.config.get('TLS', True)
def soundOn(self): def soundOn(self):
if 'soundon' not in self.config: if 'soundon' not in self.config:
self.set('soundon', True) self.set('soundon', True)