This commit is contained in:
adator85
2024-03-24 01:01:18 +01:00
parent 7068d88168
commit 19b7f85ec7
4 changed files with 78 additions and 39 deletions

View File

@@ -1,4 +1,4 @@
import time, threading, os, random, socket, hashlib
import time, threading, os, random, socket, hashlib, ipaddress
from datetime import datetime
from sqlalchemy import create_engine, Engine, Connection, CursorResult
from sqlalchemy.sql import text
@@ -179,7 +179,7 @@ class Base:
"""Methode qui supprime les timers qui ont finis leurs job
"""
try:
self.__debug(f"=======> Checking for Timers to stop")
# self.__debug(f"=======> Checking for Timers to stop")
# print(f"{self.running_timers}")
for timer in self.running_timers:
if not timer.is_alive():
@@ -196,7 +196,7 @@ class Base:
"""Methode qui supprime les threads qui ont finis leurs job
"""
try:
self.__debug(f"=======> Checking for Threads to stop")
# self.__debug(f"=======> Checking for Threads to stop")
for thread in self.running_threads:
if thread.getName() != 'heartbeat':
if not thread.is_alive():
@@ -209,7 +209,7 @@ class Base:
def garbage_collector_sockets(self) -> None:
self.__debug(f"=======> Checking for Sockets to stop")
# self.__debug(f"=======> Checking for Sockets to stop")
for soc in self.running_sockets:
while soc.fileno() != -1:
self.__debug(soc.fileno())
@@ -358,6 +358,17 @@ class Base:
except TypeError:
return value
def is_valid_ip(self, ip_to_control:str) -> bool:
try:
if ip_to_control in self.Config.WHITELISTED_IP:
return False
ipaddress.ip_address(ip_to_control)
return True
except ValueError:
return False
def get_random(self, lenght:int) -> str:
"""
Retourn une chaîne aléatoire en fonction de la longueur spécifiée.

View File

@@ -18,6 +18,7 @@ class Config:
SERVEUR_PORT = 6666 # Port du link
SERVEUR_PASSWORD = 'your link password' # Mot de passe du link (Privilégiez argon2 sur Unrealircd)
SERVEUR_ID = '002' # SID (identification) du bot en tant que Services
SERVEUR_SSL = False # Activer / Desactiver la connexion SSL
SERVICE_NICKNAME = 'BotName' # Nick du bot sur IRC
SERVICE_REALNAME = 'BotRealname' # Realname du bot

View File

@@ -19,7 +19,8 @@ class Irc:
self.INIT = 1 # Variable d'intialisation | 1 -> indique si le programme est en cours d'initialisation
self.RESTART = 0 # Variable pour le redemarrage du bot | 0 -> indique que le programme n'es pas en cours de redemarrage
self.CHARSET = ['utf-8', 'iso-8859-1'] # Charset utiliser pour décoder/encoder les messages
self.CHARSET = ['utf-8', 'iso-8859-1'] # Charset utiliser pour décoder/encoder les messages
self.SSL_VERSION = None # Version SSL
self.Config = Config()
@@ -52,20 +53,35 @@ class Irc:
def __create_socket(self) -> None:
self.IrcSocket:socket.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connexion_information = (self.Config.SERVEUR_IP, self.Config.SERVEUR_PORT)
self.IrcSocket.connect(connexion_information)
try:
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM or socket.SOCK_NONBLOCK)
connexion_information = (self.Config.SERVEUR_IP, self.Config.SERVEUR_PORT)
# Créer un object ssl
# ssl_context = self.__ssl_context()
# ssl_connexion = ssl_context.wrap_socket(s, server_hostname=self.Config.SERVEUR_HOSTNAME)
# ssl_connexion.connect(connexion_information)
# self.IrcSocket = ssl_connexion
if self.Config.SERVEUR_SSL:
# Créer un object ssl
return None
ssl_context = self.__ssl_context()
ssl_connexion = ssl_context.wrap_socket(soc, server_hostname=self.Config.SERVEUR_HOSTNAME)
ssl_connexion.connect(connexion_information)
self.IrcSocket:ssl.SSLSocket = ssl_connexion
self.debug(f"Connexion en mode SSL : Version = {self.IrcSocket.version()}")
self.SSL_VERSION = self.IrcSocket.version()
else:
soc.connect(connexion_information)
self.IrcSocket:socket = soc
self.debug("Connexion en mode normal")
return None
except ssl.SSLEOFError as soe:
self.debug(f"SSLEOFError __create_socket: {soe} - {self.IrcSocket.fileno()}")
except ssl.SSLError as se:
self.debug(f"SSLError __create_socket: {se} - {self.IrcSocket.fileno()}")
except OSError as oe:
self.debug(f"OSError __create_socket: {oe} - {self.IrcSocket.fileno()}")
def __ssl_context(self) -> ssl.SSLContext:
ctx = ssl.create_default_context()
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
@@ -114,9 +130,6 @@ class Irc:
break
self.send_response(data)
if self.IrcSocket.fileno() == -1:
print('restarting the socket')
self.RESTART = 1
except ssl.SSLEOFError as soe:
self.debug(f"SSLEOFError __connect_to_irc: {soe} - {data}")
@@ -124,8 +137,8 @@ class Irc:
self.debug(f"SSLError __connect_to_irc: {se} - {data}")
except OSError as oe:
self.debug(f"SSLError __connect_to_irc: {oe} - {data}")
except Exception as e:
self.debug(f"Exception __connect_to_irc: {e} - {data}")
# except Exception as e:
# self.debug(f"Exception __connect_to_irc: {e} - {data}")
self.IrcSocket.shutdown(socket.SHUT_RDWR)
self.IrcSocket.close()
@@ -137,8 +150,8 @@ class Irc:
self.debug(f'Value Error : {ve}')
except ssl.SSLEOFError as soe:
self.debug(f"OS Error __connect_to_irc: {soe}")
except Exception as e:
self.debug(f"Exception: {e}")
# except Exception as e:
# self.debug(f"Exception: {e}")
def __link(self, writer:socket.socket) -> None:
"""Créer le link et envoyer les informations nécessaires pour la
@@ -187,7 +200,9 @@ class Irc:
string (Str): contient la commande à envoyer au serveur.
"""
try:
self.IrcSocket.send(f"{send_message}\r\n".encode(self.CHARSET[0]))
with self.Base.lock:
# print(f">{str(send_message)}")
self.IrcSocket.send(f"{send_message}\r\n".encode(self.CHARSET[0]))
except UnicodeDecodeError:
self.debug('Write Decode impossible try iso-8859-1')
@@ -698,6 +713,8 @@ class Irc:
print(f"# SERVICE CONNECTE ")
print(f"# SERVEUR : {self.Config.SERVEUR_IP} ")
print(f"# PORT : {self.Config.SERVEUR_PORT} ")
print(f"# SSL : {self.Config.SERVEUR_SSL} ")
print(f"# SSL VER : {self.SSL_VERSION} ")
print(f"# NICKNAME : {self.Config.SERVICE_NICKNAME} ")
print(f"# CHANNEL : {self.Config.SERVICE_CHANLOG} ")
print(f"# VERSION : {self.Config.DEFENDER_VERSION} ")