From 1b479031fdddf8bf5b15ec7b23cef5b203796b74 Mon Sep 17 00:00:00 2001 From: Dpeta Date: Thu, 16 Jun 2022 20:25:24 +0200 Subject: [PATCH] minor network/socket changes --- irc.py | 12 +++++------- oyoyo/client.py | 15 ++++++++++++--- profile.py | 33 ++++++++++++++------------------- 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/irc.py b/irc.py index 47d634b..a04f724 100644 --- a/irc.py +++ b/irc.py @@ -46,19 +46,16 @@ class PesterIRC(QtCore.QThread): self.NickServ = services.NickServ() self.ChanServ = services.ChanServ() 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() port = self.config.port() + ssl = self.config.ssl() self.cli = IRCClient(PesterHandler, host=server, - port=int(port), + port=port, nick=self.mainwindow.profile().handle, real_name='pcc31', timeout=120, - ssl=server_obj['TLS']) + ssl=ssl) self.cli.command_handler.parent = self self.cli.command_handler.mainwindow = self.mainwindow self.cli.connect() @@ -79,7 +76,7 @@ class PesterIRC(QtCore.QThread): self.cli.close() self.stopIRC = "%s, %s" % (type(se), se) return - except (OSError, IndexError) as se: + except (OSError, IndexError, ValueError) as se: self.stopIRC = "%s, %s" % (type(se), se) PchumLog.debug("socket error, exiting thread") return @@ -543,6 +540,7 @@ class PesterHandler(DefaultCommandHandler): #mychumhandle = self.mainwindow.profile().handle mymood = self.mainwindow.profile().mood.value() if not self.mainwindow.config.lowBandwidth(): + time.sleep(0.413 + 0.097) # <--- somehow, this actually helps. helpers.join(self.client, "#pesterchum") # Moods via metadata helpers.metadata(self.client, '*', 'sub', 'mood') diff --git a/oyoyo/client.py b/oyoyo/client.py index 2e383bc..0dfd925 100644 --- a/oyoyo/client.py +++ b/oyoyo/client.py @@ -152,8 +152,11 @@ class IRCClient: # socket.timeout is deprecated in 3.10 PchumLog.warning("TimeoutError in on send, " + str(e)) raise socket.timeout - except (OSError, IndexError) as e: + except (OSError, + IndexError, + ValueError) as e: # Unknown error, might as well retry? + # index/value can happen if the socket breaks. PchumLog.warning("Unkown error on send, " + str(e)) if tries >= 9: raise e @@ -162,7 +165,7 @@ class IRCClient: time.sleep(0.1) 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)) if not self.blocking and se.errno == 11: pass @@ -176,8 +179,14 @@ class IRCClient: if self.ssl == True: 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)) - 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: try: self.socket.do_handshake() diff --git a/profile.py b/profile.py index b16a1b3..fd660ec 100644 --- a/profile.py +++ b/profile.py @@ -369,9 +369,7 @@ with a backup from: %s" % (_datadir, self.filename, read_file = server_file.read() server_file.close() server_obj = json.loads(read_file) - server = str(server_obj['server']) - #print("Server is: " + server) - return server + return server_obj['server'] except: try: with open(_datadir + "server.json", "w") as server_file: @@ -383,7 +381,6 @@ with a backup from: %s" % (_datadir, self.filename, server_file.write(json.dumps(json_server_file, indent = 4) ) server_file.close() server = "irc.pesterchum.xyz" - #print("Failed to read server, defaulting to irc.pesterchum.xyz") except: return self.config.get('server', "irc.pesterchum.xyz") def port(self): @@ -394,24 +391,22 @@ with a backup from: %s" % (_datadir, self.filename, read_file = server_file.read() server_file.close() server_obj = json.loads(read_file) - port = str(server_obj['port']) - #print("Port is: " + port) + port = server_obj['port'] return port except: return self.config.get('port', '6697') -## def TLS(self): -## if hasattr(self.parent, 'tlsOverride'): -## return self.parent.tlsOverride -## try: -## with open(_datadir + "server.json", "r") as server_file: -## read_file = server_file.read() -## server_file.close() -## server_obj = json.loads(read_file) -## TLS = str(server_obj['TLS']) -## print("TLS-status is: " + TLS) -## return TLS -## except: -## return self.config.get('TLS', True) + + def ssl(self): + #if hasattr(self.parent, 'tlsOverride'): + # return self.parent.tlsOverride + try: + with open(_datadir + "server.json", "r") as server_file: + read_file = server_file.read() + server_file.close() + server_obj = json.loads(read_file) + return server_obj['TLS'] + except: + return self.config.get('TLS', True) def soundOn(self): if 'soundon' not in self.config: self.set('soundon', True)