Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- #Script d'analyse de log chat JKA
- import time
- import datetime
- import ConfigParser
- import socket
- import hashlib
- import threading
- import MySQLdb as mdb
- import os, sys
- from sys import exit
- ####
- def __unicode__(self):
- return unicode(self.name)
- def strReplace(str, array):
- for search, replace in array:
- str = str.replace(search, replace)
- return str
- ###
- #LECTURE LOCAL LOG
- class thread_send(threading.Thread):
- def __init__(self, nom=''):
- threading.Thread.__init__(self)
- self.nom = nom
- self.Terminated = False
- def run(self):
- new_back = "none"
- lastmessage = ""
- new = ""
- pass_starup = 0
- autodebogue = int(0)
- global lastmessage
- global con
- global autodebogue
- while True:
- try:
- with con:
- sql_request = "SELECT " + MYSQL_BD_Col_username + "," + MYSQL_BD_Col_Message + " FROM " + MYSQL_TB_NAME + " ORDER BY " + MYSQL_BD_Col_id + " DESC LIMIT 1"
- cur.execute(sql_request)
- data = cur.fetchall()
- for row in data :
- new = row[1]
- except:
- print "An error occurred while selecting the message in the database"
- autodebogue = autodebogue + int(1)
- print u"Auto-debug [",autodebogue,"/4]"
- if (autodebogue == 4):
- print "Automatic debugging!"
- workingdir = os.getcwd()
- os.startfile(workingdir + "\JKA_BDD_client.exe")
- os._exit(1)
- if (pass_starup == 0):
- new_back = new
- pass_starup = 1
- if ((new != new_back) and (new != "") and (new != lastmessage)):
- PEERS_COUNT = int(0)
- while PEERS_COUNT != len(LIST_SERVER.split(',')):
- try:
- PEER = LIST_SERVER.split(',')[PEERS_COUNT]
- PORT_PEER = PEER.split(':')[1]
- IP_PEER = PEER.split(':')[0]
- sock.sendto(USERNAME + "|-|" + hashlib.sha256(PASSWORD).hexdigest() + "|-|" + TAG + "|-|" + str(row[0]) + "|-|" + str(row[1]), (IP_PEER, int(PORT_PEER)))
- except:
- print "An error has occurred while the sending"
- PEERS_COUNT = PEERS_COUNT + 1
- new_back = new
- time.sleep(0.5)
- def stop(self):
- self.Terminated = True
- #RECEPTION
- class thread_recept(threading.Thread):
- def __init__(self, nom=''):
- threading.Thread.__init__(self)
- self.nom = nom
- self.Terminated = False
- def run(self):
- autodebogue = int(0)
- global lastmessage
- global con
- global autodebogue
- while True:
- data, addr = sock_recu.recvfrom(1024)
- if addr[0] in AUTORISE_IPS_LIST:
- try:
- DATE = int(time.time())
- SERVERNAME_recv = data.split('|-|')[0]
- PASSWORD_recv = data.split('|-|')[1]
- TAG_recv = data.split('|-|')[2]
- NAME_recv = data.split('|-|')[3]
- MESSAGE_recv = data.split('|-|')[4]
- MESSAGE_recv = strReplace(MESSAGE_recv, replace_color_flag)
- error = False
- except:
- print "An error has occurred while processing!"
- error = True
- if ((hashlib.sha256(PASSWORD).hexdigest() == PASSWORD_recv) and (error == False)):
- lastmessage = MESSAGE_recv
- print u"Received data[",SERVERNAME_recv,"]",NAME_recv,":",MESSAGE_recv
- if (MYSQL_BD_Col_time_format == "UNIX"):
- date = int(time.time())
- else:
- date = int(datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))
- try:
- with con:
- sql_request = "INSERT INTO " + MYSQL_TB_NAME + " (" + MYSQL_BD_Col_username + "," + MYSQL_BD_Col_Message + "," + MYSQL_BD_Col_time + ") VALUES ('[" + mdb.escape_string(SERVERNAME_recv) + "]" + mdb.escape_string(NAME_recv) + "','" + mdb.escape_string(MESSAGE_recv) + "','" + str(date) + "')"
- cur.execute(sql_request)
- except:
- print u"An error occurred while adding the message in the database"
- autodebogue = autodebogue + int(1)
- print u"Auto-debug [",autodebogue,"/4]"
- if (autodebogue == 4):
- print "Automatic debugging!"
- workingdir = os.getcwd()
- os.startfile(workingdir + "\JKA_BDD_client.exe")
- os._exit(1)
- else:
- print u"Unauthorized IP!"
- def stop(self):
- self.Terminated = True
- print "------------------------"
- print "-- Programme de Tlams"
- print "-- Version 1.0"
- print "-- Date: 28/09/13"
- print "-- Langage: Python V2.7"
- print u"-- Email: tlams[at]free.fr"
- print u"-- Fonction: Script de chat inter-serveur JKA / BDD MOD"
- print "------------------------"
- print ""
- time.sleep(1)
- #LECTURE DU FICHIER DE CONFIGURATION
- print "Opening configurations file...",
- try:
- config = ConfigParser.RawConfigParser()
- config.read('config.ini')
- print "[OK]"
- except:
- print "[FAILED]"
- print u"Opening the configuration file (config.ini) failed!"
- print "File not available."
- print "Closing..."
- time.sleep(5)
- exit(0)
- time.sleep(1)
- #SCRIPT INFO
- TAG = "BDD_JKA"
- #TRAITEMENT DU FICHIER DE CONFIG
- print u"Reading the configuration file..."
- try:
- MYSQL_BD_Col_Message = config.get('MYSQL', 'MYSQL_BD_Col_Message')
- print u"[Info-config]MYSQL colonne message:",MYSQL_BD_Col_Message
- MYSQL_BD_Col_username = config.get('MYSQL', 'MYSQL_BD_Col_username')
- print u"[Info-config]MYSQL colonne user:",MYSQL_BD_Col_username
- MYSQL_BD_Col_id = config.get('MYSQL', 'MYSQL_BD_Col_id')
- print u"[Info-config]MYSQL colonne ID:",MYSQL_BD_Col_id
- MYSQL_BD_Col_time = config.get('MYSQL', 'MYSQL_BD_Col_time')
- print u"[Info-config]MYSQL colonne time:",MYSQL_BD_Col_time
- MYSQL_BD_Col_time_format = config.get('MYSQL', 'MYSQL_BD_Col_time_format')
- print u"[Info-config]MYSQL colonne time format:",MYSQL_BD_Col_time_format
- MYSQL_TB_NAME = config.get('MYSQL', 'MYSQL_TB_NAME')
- print u"[Info-config]MYSQL table:",MYSQL_TB_NAME
- MYSQL_BD_NAME = config.get('MYSQL', 'MYSQL_BD_NAME')
- print u"[Info-config]MYSQL database:",MYSQL_BD_NAME
- MYSQL_PASSWORD = config.get('MYSQL', 'MYSQL_PASSWORD')
- MYSQL_USER = config.get('MYSQL', 'MYSQL_USER')
- print u"[Info-config]MYSQL user:",MYSQL_USER
- MYSQL_IP = config.get('MYSQL', 'MYSQL_IP')
- print u"[Info-config]MYSQL serveur IP:",MYSQL_IP
- LOCAL_IP = config.get('LOCAL', 'IP')
- print u"[Info-config]Script IP:",LOCAL_IP
- LOCAL_PORT = config.get('LOCAL', 'PORT')
- print u"[Info-config]Script PORT:",LOCAL_PORT
- USERNAME = config.get('LOCAL', 'Username')
- print u"[Info-config]Serveur nom:",USERNAME
- PASSWORD = config.get('LOCAL', 'Password')
- LIST_SERVER = config.get('PEERS', 'List_servers')
- print u"[Info-config]Liste PEERS:",LIST_SERVER
- AUTORISE_IPS = config.get('PEERS', 'IP_Autorise')
- print u"[Info-config]IP autorisés:",AUTORISE_IPS
- except:
- print "[FAILED]"
- print "File is corrupt or the data are invalid !"
- print "Closing..."
- time.sleep(5)
- exit(0)
- print ""
- time.sleep(1)
- # CREATION DU TABLEAU IP AUTORISES
- AUTORISE_IPS_LIST = []
- nb_ips = int(0)
- while nb_ips != len(AUTORISE_IPS.split(',')):
- AUTORISE_IPS_LIST.append(AUTORISE_IPS.split(',')[nb_ips])
- nb_ips = nb_ips + 1
- #PREPARATION SOCKET UDP
- sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- sock_recu = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- #CONNEXION MYSQL
- try:
- global con
- print u"Database connection...",
- con = mdb.connect(MYSQL_IP, MYSQL_USER, MYSQL_PASSWORD, MYSQL_BD_NAME)
- print "[OK]"
- except:
- print "Connection Error !"
- print "Closing..."
- time.sleep(5)
- exit(0)
- cur = con.cursor()
- #CREATION DU SOCK LOCAL
- print "Opening a local sock...",
- try:
- sock_recu.bind((LOCAL_IP, int(LOCAL_PORT)))
- print "[OK]"
- except:
- print "[FAILED]"
- print u"A socket is already open on this port!"
- print "Closing..."
- time.sleep(5)
- exit(0)
- #FLAG LIST
- replace_color_flag = [('^0', ''), ('^1', ''), ('^2', ''), ('^3', ''), ('^4', ''), ('^5', ''), ('^6', ''), ('^7', ''), ('^8', '')]
- #START THREAD
- a = thread_send('envoi_donne')
- a.start()
- b = thread_recept('recept_donne')
- b.start()
- print u"Loading completed !"
- print ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement