Amélioration d'un script pour le rendre plus performant
#2
Grâce à CrazyCat sur le channel irc #eggdrop , j'ai pu apporter pas mal d'améliorations :

tcl
proc ::checkpays::who:co {from keyword text} {
 
	if {![string match -nocase "*Client connecting*" $text]} {
		return;
	}
 
	set sslco 0
	set webirc 0
	set websocket 0
	set unknow 0
	set account 0
 
	regexp $::checkpays::regexpclientco $text - nick username ip ipdns vhost csa codepays asn asname reputation other

	# IP Except
	if {$ip eq "127.0.0.1" || [isbotnick $nick]} {
		return
	}
 
	set SESSION [md5 "${username}@${vhost}"]
 
	if {[string match -nocase "*tls-users*" $other]} {
		set sslco 1
	}
 
	if {[string match -nocase "*webirc-users*" $other]} {
		set webirc 1
	}
 
	if {[string match -nocase "*websocket-users*" $other]} {
		set websocket 1
	}
 
	if {[string match -nocase "*unknown-users*" $other]} {
		set unknow 1
	}
 
	if {[string match -nocase "*account:*" $csa]} {
		set account 1
	}

	# On met en mémoire les valeurs qu'on ne récupère pas dans le who
 
	set ::checkpays::DB($SESSION) [list $ip $ipdns $codepays $asn $asname $reputation $sslco $webirc $websocket $unknow $account]
 
	putlog "who => $::checkpays::DB($SESSION)"
 
	putnow "WHO $nick %unhr"
 
}
 
proc ::checkpays::who:gestion {from key text} {
 
   	set text [split [string trim $text]]
	lassign $text {} username vhost nick
	set SESSION [md5 "${username}@${vhost}"]
	set realname [join [lrange [split $text {:}] 1 end]]

	## On recupere les variables en mémoire
 
	lassign $::checkpays::DB($SESSION) ip ipdns codepays asn asname reputation sslco webirc websocket unknow account
 
	putlog "gestion => $::checkpays::DB($SESSION)"

	# Nick/Ip/Ident EXCEPTION
	if {[::toolsAmand::verify:b $::checkpays::fichier(ident) $username]} {
 
		putnow "PRIVMSG $::checkpays::backpaysbl :\002\0030,3EXCEPTION - IDENT\003\002 \002\[\002$codepays\002\]\002 - $nick ($username@$ip) (Réputation: $reputation) (Sslco: $sslco) (Webirc: $webirc) (Websocket: $websocket) (Asn: $asn) (Asname: $asname) - Realname: $realname"
		::checkpays::monitoring $nick $username $ip $vhost $codepays $reputation $sslco $webirc $websocket $realname $asn $asname
		::checkpays::CountIRC $ip $username $codepays $webirc $websocket
		unset ::checkpays::DB($SESSION)
		return
 
	} elseif {[::toolsAmand::verify:b $::checkpays::fichier(pseudo) $nick]} {
 
		putnow "PRIVMSG $::checkpays::backpaysbl :\002\0030,3EXCEPTION - PSEUDO\003\002 \002\[\002$codepays\002\]\002 - $nick ($username@$ip) (Réputation: $reputation) (Sslco: $sslco) (Webirc: $webirc) (Websocket: $websocket) (Asn: $asn) (Asname: $asname) - Realname: $realname"
		::checkpays::monitoring $nick $username $ip $vhost $codepays $reputation $sslco $webirc $websocket $realname $asn $asname
		::checkpays::CountIRC $ip $username $codepays $webirc $websocket
		unset ::checkpays::DB($SESSION)
		return
 
	} elseif {[::toolsAmand::verify:b $::checkpays::fichier(ip) $ip]} {
 
		putnow "PRIVMSG $::checkpays::backpaysbl :\002\0030,3EXCEPTION - IP\003\002 \002\[\002$codepays\002\]\002 - $nick ($username@$ip) (Réputation: $reputation) (Sslco: $sslco) (Webirc: $webirc) (Websocket: $websocket) (Asn: $asn) (Asname: $asname) - Realname: $realname"
		::checkpays::monitoring $nick $username $ip $vhost $codepays $reputation $sslco $webirc $websocket $realname $asn $asname
		::checkpays::CountIRC $ip $username $codepays $webirc $websocket
		unset ::checkpays::DB($SESSION)
		return
 
	}

	# On envoie à la proc de traitement
	::checkpays::traitement $nick $username $ip $ipdns $vhost $codepays $asn $asname $reputation $sslco $webirc $websocket $unknow $realname $account

	# Changement du realname en ASV
	if {[regexp $::checkpays::regexpagesex $realname] != 1} {
 
		putnow "CHGNAME $nick [expr 18 + int(rand()*72)] M"
		putnow "PRIVMSG $::checkpays::backpaysredirect :\002\00300,02REALNAME\003\002 - \002$nick\002 \[$codepays\] ($username@$ip) (Réputation: $reputation) (Ssl: $sslco) (Webirc: $webirc) (Websocket: $websocket) (Account: $account) (Asn: $asn) (Asname: $asname) vient d'être changé. Realname: $realname"
 
	} elseif {[regexp $::checkpays::regexpado $realname] == 1} {
 
		putnow "PRIVMSG OperServ :SVSJOIN $nick $::checkpays::salonado"
 
	}
 
}
 
proc ::checkpays::traitement {nick username ip ipdns vhost codepays asn asname reputation sslco webirc websocket unknow realname account} {
 
	set SESSION [md5 "${username}@${vhost}"]
	set ALLEXCEPTIONS [list {*}$::checkpays::PAYSINTERDIT {*}$::checkpays::PAYSINTERDITNWSI {*}$::checkpays::PAYSINTERDITNW {*}$::checkpays::PAYSIPAPI {*}$::checkpays::PAYSEXCEPTION]

	# Traitement
	if {[lsearch $::checkpays::PAYSINTERDIT $codepays] != -1} {
 
		putnow "GLINE *@$ip $::checkpays::timeBans $::checkpays::motifpaysbl"
		putnow "PRIVMSG $::checkpays::backpaysbl :\002\0030,4PAYS BLOQUÉ\003\002 \[\002$codepays\002\] - \002$nick\002 ($username@$ip) (Réputation: $reputation) (Ssl: $sslco) (Webirc: $webirc) (Websocket: $websocket) (Asn: $asn) (Asname: $asname) - Realname: $realname"
		unset ::checkpays::DB($SESSION)
		return
 
	} elseif {([lsearch $::checkpays::PAYSINTERDITNWSI $codepays] != -1) && ($webirc == 0) && ($websocket == 0) && ($reputation < 200)} {
 
		putnow "GLINE *@$ip $::checkpays::timeBans $::checkpays::motifpaysbl"
		putnow "PRIVMSG $::checkpays::backpaysbl :\002\0030,12PAYS BLOQUÉ - HORS WEB|RÉPUTATION\003\002 \[\002$codepays\002\] - \002$nick\002 ($username@$ip) (Réputation: $reputation) (Ssl: $sslco) (Webirc: $webirc) (Websocket: $websocket) (Asn: $asn) (Asname: $asname) - Realname: $realname"
		unset ::checkpays::DB($SESSION)
		return
 
	} elseif {([lsearch $::checkpays::PAYSINTERDITNW $codepays] != -1) && ($webirc == 0) && ($websocket == 0)} {
 
		putnow "GLINE *@$ip $::checkpays::timeBans $::checkpays::motifpaysbl"
		putnow "PRIVMSG $::checkpays::backpaysbl :\002\0030,6PAYS BLOQUÉ - HORS WEB\003\002 \[\002$codepays\002\] - \002$nick\002 ($username@$ip) (Réputation: $reputation) (Ssl: $sslco) (Webirc: $webirc) (Websocket: $websocket) (Asn: $asn) (Asname: $asname) - Realname: $realname"
		unset ::checkpays::DB($SESSION)
		return
 
	} elseif {($codepays eq "US") || ([string match "*.irccloud.com" $ip])} {
 
		putnow "GLINE $username@$ip $::checkpays::timeBans $::checkpays::motifpaysbl"
		putnow "PRIVMSG $::checkpays::backpaysbl :\002\0030,10IRCCloud|US\003\002 \[\002$codepays\002\] - \002$nick\002 ($username@$ip) (Réputation: $reputation) (Ssl: $sslco) (Webirc: $webirc) (Websocket: $websocket) (Asn: $asn) (Asname: $asname) - Realname: $realname"
		unset ::checkpays::DB($SESSION)
		return
 
	} else {
 
		# Si on entre pas dans une condition blacklisté on envoie à la proc du monitoring
		#::checkpays::monitoring $nick $username $ip $vhost $codepays $reputation $sslco $webirc $websocket $realname $asn $asname
		#::checkpays::CountIRC $ip $username $codepays $webirc $websocket
 
	}

	# Vérification si le codepays est hors liste interdit/protégé
	if {[lsearch $ALLEXCEPTIONS $codepays]==-1} {
		putnow "PRIVMSG $::checkpays::backpaysbl :\002\0030,14USER SUSPECT\003\002 \[\002$codepays\002\] - \002$nick\002 ($username@$ip) (Réputation: $reputation) (Ssl: $sslco) (Webirc: $webirc) (Websocket: $websocket) (Asn: $asn) (Asname: $asname) - Realname: $realname"
		::checkpays::infoAPI $nick $username $vhost $ip $ipdns $unknow $reputation $realname $webirc $websocket $account
		::checkpays::infoABUSEIPDB $nick $username $vhost $ipdns
	}

	# IP-API

	# Bloquer IPAPI Proxy/Hosting - IPABUSEIPDB
	if {[lsearch $::checkpays::PAYSIPAPI $codepays]!=-1} {
		::checkpays::infoAPI $nick $username $vhost $ip $ipdns $unknow $reputation $realname $webirc $websocket $account
	}

	# Suppression des valeurs en mémoire.
	# https://www.astro.princeton.edu/~rhl/Tcl-Tk_docs/tcl/unset.n.html
 
	unset ::checkpays::DB($SESSION)
 
}
 
proc ::checkpays::infoAPI {nick username vhost ip ipdns unknow reputation realname webirc websocket account} {
 
	set SESSION [md5 "${username}@${vhost}"]

	# Encoding pour corriger un problème de charset. Ex: Île-de-France
	set data [encoding convertfrom utf-8 [::toolsAmand::getipdatas $ipdns]]
 
	if {$data == 0} {
		unset ::checkpays::DB($SESSION)
		return
	}
 
    set country [dict get $data country]
    set regionName [dict get $data regionName]
	set region [dict get $data region]
    set city [dict get $data city]
    set codepays [dict get $data countryCode]
    set codepostal [dict get $data zip]
    set isp [dict get $data isp]
    set mobile [dict get $data mobile]
    set proxy [dict get $data proxy]
    set hosting [dict get $data hosting]
	set org [dict get $data org]
	set as [dict get $data as]
	set asname [dict get $data asname]
	set district [dict get $data district]

 
	# Bloquer IPAPI Proxy/Hosting
	if {([lsearch $::checkpays::PAYSIPAPI $codepays]!=-1) && ($proxy eq "true" || $hosting eq "true")} {
		putnow "GLINE *@$ip $::checkpays::timeBans $::checkpays::motifpaysbl \[IP-API\]"
		putnow "PRIVMSG $::checkpays::backpaysbl :\002\0030,13IP-API PROXY|HOSTING\003\002 \[\002$codepays\002\] - \002$nick\002 ($username@$ip) (Webirc: $webirc) (Websocket: $websocket) (Région: $regionName) (Ville: $city $codepostal) (Fournisseur: $isp) (Org: $org) (As: $as) (Asname: $asname) (Proxy: $proxy) (Mobile: $mobile) (Hébergeur: $hosting) (Unknow: $unknow) (Réputation: $reputation) - Realname: $realname"
		unset ::checkpays::DB($SESSION)
		return
	}
 
}
 
proc ::checkpays::infoABUSEIPDB {nick username vhost ipdns} {
 
	set SESSION [md5 "${username}@${vhost}"]
	set data [::toolsAmand::abuseipdb $ipdns]
 
	if {$data == 0} {
		unset ::checkpays::DB($SESSION)
		return
	}
 
	set abuseConfidenceScore [dict get $data data abuseConfidenceScore]
	set codepays [dict get $data data countryCode]
	set isTor [dict get $data data isTor]
	set totalReports [dict get $data data totalReports]
 
	putnow "PRIVMSG $::checkpays::backpaysbl :\002\0030,12ABUSEIPDB\003\002 \[$nick!$username@\002$ipdns\002\] \[\002$codepays\002\] - Score: $abuseConfidenceScore \002|\002 isTor: $isTor \002|\002 totalReports: $totalReports"
 
}



J'ai mis des unset de la variable $SESSION dans les conditions ou il y a un return , opter pour une liste sur le stockage des variables en session pour éviter de set chaque variable.
Répondre


Messages dans ce sujet
RE: Amélioration d'un script pour le rendre plus performant - par Amand - 27/09/2024, 19:29

Atteindre :


Utilisateur(s) parcourant ce sujet : 2 visiteur(s)
Tchat 100% gratuit -Discutez en toute liberté