def select_miners_from_username(self, username): ''' 根据用户名查询这名用户下有哪些矿机数据 ''' sql = 'select * from miners where owner=?' a = self.cursor.execute(sql, (username, )) b = a.fetchall() miners_number = len(b) # 矿机数量 data = {} try: if b: for x in b: c = json.loads(x[2]) for z in c: c[z].append(x[0]) data[x[0]] = c return { 'status': 0, 'data': data, 'miners_number': miners_number } else: return {'status': -1} except Exception as e: logger('database - select miners from username', str(e)) return {'status': -1, 'errmsg': str(e)}
def start(message): ''' handler for user /start command ''' logger(message) BOT.send_message( message.chat.id, text=('Привет!\nЯ помогу тебе получить твоё расписание ' 'в формате,\nудобном для встраивания в любые календари,' '\nнапример, Google Calendar или macOS Calendar, и еще ' '\nкучу других.\nВ какой группе учишься?'))
def any_messages(message): ''' handler for any message containing text ''' logger(message) file_to_send = '' if not GC_REG_MATCHER.match(message.text): BOT.send_message(message.chat.id, text='Указан неверный формат номера группы.') return message.text = message.text.upper() BOT.send_message(message.chat.id, text='Пошел искать расписание для группы {}'.format( message.text)) if file_exist(message.text): file_to_send = open(SCHEDULE_FILE.format(ValueError, message.text), 'rb') else: try: bmstu_schedule.run(message.text, DT, VAULT_PATH) except SystemExit: BOT.send_message(message.chat.id, text=('Чёт я ничего не нашел для группы {}. ' 'Если проблема и правда во мне, то напиши ' '@lee_daniil или @gabolaev'.format( message.text))) return if file_exist(message.text): file_to_send = open(SCHEDULE_FILE.format(VAULT_PATH, message.text), 'rb') elif message.text[len(message.text) - 1].isnumeric(): BOT.send_message( message.chat.id, text=('Эээ, кажется, кто-то не уточнил тип своей ' 'группы (Б/М/А). Давай добавим соответствующую ' 'букву в конце и попробуем еще раз. ' 'Например {}Б'.format(message.text))) return if file_to_send: BOT.send_document(message.chat.id, file_to_send) BOT.send_message(message.chat.id, text='Тадам!') BOT.send_message( message.chat.id, text=('Если вдруг будут проблемы при импорте в ' 'календарь, можешь обращаться к @lee_daniil или @gabolaev')) else: BOT.send_message(message.chat.id, text='Указан неверный формат номера группы.')
def select_all_miner_info(self): ''' 查找所有矿机信息 ''' sql = 'select * from miners' try: a = self.cursor.execute(sql) b = a.fetchall() return {'status': 0, 'data': b} except Exception as e: logger('database - select all miner', str(e)) return {'status': -1, 'errmsg': str(e)}
def update_miner_status(self, miner_id, status): ''' 根据矿机Miner_id更新添加矿机状态数据 ''' sql = 'update miners set miner_data=? where miner_id=?' try: self.cursor.execute(sql, (status, miner_id)) self.conn.commit() self.close_db() except Exception as e: logger('database - update_miner_status', str(e)) return {'status': -1, 'errmsg': str(e)}
def miner(): miner_data = request.json owner = miner_data['data']['owner'] miner_id = miner_data['data']['miner_id'] now_miner_status = miner_data['data']['miner_data'] task_db = db() user_exist = task_db.check_user_miner(owner) # 确认是否有这个用户 if user_exist['status'] == 0: if user_exist['data']: # 有这个用户 try: miner_exist = task_db.check_minerid_exist( miner_id) # 检查是否有这个矿机id if miner_exist['data']: # 矿机已存在 更新矿机状态即可 before_status = task_db.select_status_from_miner_id( str(miner_id))['data'] for x in now_miner_status: before_status[x] = now_miner_status[x] result = task_db.update_miner_status( str(miner_id), json.dumps(before_status)) if result['status'] == 0: return json.dumps({ 'status': 0, 'data': 'update finished' }) else: return json.dumps({ 'status': -1, 'errmsg': 'db update miner status failed', 'db:': result }) else: # 矿机id不存在,新矿机,插入第一次矿机数据 result = task_db.insert_miner_data( str(miner_id), str(owner), json.dumps(now_miner_status)) if result['status'] == 0: return json.dumps({ 'status': 0, 'data': 'insert finished' }) else: return json.dumps({ 'status': -1, 'errmsg': 'db insert miner status failed', 'db:': result }) except Exception as e: logger('json miner data', str(e)) return json.dumps({'status': -1, 'errmsg': str(e)}) else: return json.dumps({'status': -1, 'errmsg': 'this user not exist'}) else: return json.dumps({'status': -1, 'errmsg': 'db error'})
def insert_miner_data(self, miner_id, owner, miner_data): ''' 当用户新添加一台矿机的时候,插入新的矿机及数据 ''' sql = 'insert into miners values(?,?,?)' try: self.cursor.execute(sql, (miner_id, owner, miner_data)) self.conn.commit() self.close_db() return {'status': 0} except Exception as e: logger('database - insert user', str(e)) return {'status': -1, 'errmsg': str(e)}
def select_status_from_miner_id(self, miner_id): ''' 根据Miner_id查找矿机状态数据 ''' sql = 'select miner_data from miners where miner_id=?' try: a = self.cursor.execute(sql, (miner_id, )) b = a.fetchall() c = b[0][0] return {'status': 0, 'data': json.loads(c)} except Exception as e: logger('database - select_miner_status_from_miner_id', str(e)) return {'status': -1, 'errmsg': str(e)}
def auth_admin_cookies(self, cookies): ''' 验证管理员cookies ''' try: sql = 'select * from admin where cookies=?' a = self.cursor.execute(sql, (cookies, )) b = a.fetchall() if b: return {'status': 0, 'data': True} else: return {'status': 0, 'data': False} except Exception as e: logger('database - auth_admin_cookies', str(e)) return {'status': -1, 'errmsg': str(e)}
def select_users_miners(self): ''' 统计用户数以及矿机数 ''' user_sql = 'select username from account' miner_sql = 'select miner_id from miners' try: a = self.cursor.execute(user_sql) b = len(a.fetchall()) c = self.cursor.execute(miner_sql) d = len(c.fetchall()) return {'status': 0, 'data': {'users': b, 'miners': d}} except Exception as e: logger('database - select user miner', str(e)) return {'status': -1, 'errmsg': str(e)}
def auth_username_password(self, username, password): ''' 验证用户登陆 ''' try: sql = 'select password from account where username=?' a = self.cursor.execute(sql, (username, )) b = a.fetchall()[0][0] if b == password: return {'status': 0, 'data': True} else: return {'status': 0, 'data': False} except Exception as e: logger('database - select password', str(e)) return {'status': -1, 'errmsg': str(e)}
def check_minerid_exist(self, miner_id): ''' 查询数据库中是否有这个矿机id,有则可以更新,没有则插入 ''' sql = 'select * from miners where miner_id=?' try: a = self.cursor.execute(sql, (miner_id, )) user_exist = a.fetchall() if user_exist: return {'status': 0, 'data': True} else: return {'status': 0, 'data': False} except Exception as e: logger('database - check_minerid_exist', str(e)) return {'status': -1, 'errmsg': str(e)}
def select_username_from_cookies(self, cookies): ''' 根据cookies,查找用户名,判别用户 ''' sql = 'select username from account where token=?' try: a = self.cursor.execute(sql, (cookies, )) b = a.fetchall()[0][0] if b: return {'status': 0, 'data': b} else: return {'status': -1} except Exception as e: logger('database - select username from cookies', str(e)) return {'status': -1, 'errmsg': str(e)}
def check_user_miner(self, username): ''' 客户端向服务端传输数据时的数据有用户名和矿机data,检查account表中是否有这个用户 ''' sql = 'select username from account where username=?' try: a = self.cursor.execute(sql, (username, )) user_exist = a.fetchall() if user_exist: return {'status': 0, 'data': True} else: return {'status': 0, 'data': False} except Exception as e: logger('database - check user miner', str(e)) return {'status': -1, 'errmsg': str(e)}
def __init__(self): ''' Constructor ''' path_base.__init__(self) self.logger = logger("path_a_star") self.logger.write_log("init search A*")
def insert_user(self, username, password, registertime, user_uuid, cookies, token): ''' 用户注册,插入新用户数据 ''' try: sql = 'insert into account values(?,?,?,?,?,?)' self.cursor.execute( sql, (username, password, registertime, user_uuid, cookies, token)) self.conn.commit() self.close_db() return {'status': 0} except Exception as e: logger('database - insert user', str(e)) return {'status': -1, 'errmsg': str(e)}
def __init__(self): ''' Constructor ''' path_base.__init__(self) self.logger = logger("path_dynamic_prog") self.value_grid = [] self.ego = simple_ego()
def __init__(self): ''' Constructor ''' self.list_tc = {} self.list_tc_init = {} self.logger = logger("test_planner") self.results = []
def __init__(self): ''' Constructor ''' self.logger = logger("simulator") self.grid = [] self.grid_size = [] self.static_obj = [] self.dynamic_obj = [] self.prev_pos_ego = {} self.path = []
def select_user_authtoken(self, username): ''' 根据用户名,查找用户的cookies和token ''' try: sql = 'select cookies,token from account where username=?' a = self.cursor.execute(sql, (username, )) b = a.fetchall() cookies = b[0][0] token = b[0][1] if cookies and token: return { 'status': 0, 'data': { 'cookies': cookies, 'token': token } } else: return {'status': 0, 'data': False} except Exception as e: logger('database - get authtoken', str(e)) return {'status': -1, 'errmsg': str(e)}
class Consumer(threading.Thread): def __init__(self, t_name, queue): threading.Thread.__init__(self, name=t_name) self.data = queue def run(self): reload(sys) sys.setdefaultencoding('utf-8') rss = news(log) while True: try: data = self.data.get() status = rss.start(self.getName(), data.strip()) if status: self.data.task_done() except Exception, e: log.write_log(traceback.print_exc(), 'error') if __name__ == "__main__": log = logger() # 加载日志类 queue = Queue() producer = Producer('Pro.', queue) producer.start() for i in range(10): consumer = Consumer('Con.' + str(i + 1), queue) consumer.start() producer.join() consumer.join()
def __init__(self): self.logger = logger("ego") self.position = {}
import logging from abc import ABC, ABCMeta, abstractmethod from typing import Iterable from calculator.converters import ToFloat, Converter from calculator.operations import Add, Subtract, Multiply, Divide from logger.logger import logger, obj_log from typing import TypeVar _log: logging.Logger = logger() CalcType: type = TypeVar('CalcType', int, float) class Calculator(ABC): """Represent abstraction for specific calculator object.""" __metaclass__: type = ABCMeta @abstractmethod def add(self) -> CalcType: pass @abstractmethod def subtract(self) -> CalcType: pass @abstractmethod def multiply(self) -> CalcType: pass @abstractmethod
''' Created on 09.03.2019 @author: Nicco ''' from testing.test_planner import test_planner from logger.logger import logger if __name__ == '__main__': """ here the testing routine is started """ tp = test_planner() log = logger("main") res = [] for i in range(0, 1): tp.get_available_testcases() results = tp.run() avg_dur = 0 count = 0 log.write_log("--------------------------------------------------------") for res in results: if res["name"] == "tc_path_plan_a_star": avg_dur = avg_dur + res["duration"] count += 1 log.write_log(res["name"] + ":\t result " + str(res["result"]) + "\t duration:" + str(res["duration"])[:6])
import torch from option import args from data import data from logger import logger from trainer import Trainer_CNN from trainer_rnn import Trainer_RNN from model import model torch.manual_seed(args.seed) checkpoint = logger.logger(args) dataloader = data(args) my_model = model(args, checkpoint) loader = dataloader.get_loader() if args.work_type == 'Character': t = Trainer_CNN(args, loader, my_model, checkpoint) else: t = Trainer_RNN(args, loader, my_model, checkpoint) while not t.termination(): t.train() t.test()
def __init__(self, name): ''' Constructor ''' self.name = name self.logger = logger(name)
from main import __working__directory__ import os import logger.logger as logger # Logger instance logger_instance = logger.logger() logger_instance.create_logger() class resources(): __url__blocked__template = 'url_blocked.html' __resources__directory__ = 'resources' path = os.path.join(__working__directory__, __resources__directory__) url_blocked_path = os.path.join(path, __url__blocked__template) def resources_dir(self): if (not os.path.isdir(self.path)): try: logger_instance.write_log(28, 1) os.mkdir(self.path) except Exception as e: logger_instance.write_log(128, 0, e) def is_blocked_template(self): self.resources_dir() print(self.url_blocked_path) if (not os.path.isfile(self.url_blocked_path)): try: logger_instance.write_log(29, 1) with open(self.url_blocked_path, 'w+') as f: f.writelines('''<html>
class MainConfig(): logger_instance = logger.logger() logger_instance.create_logger() # Join paths to getcwd working environment def is_file_exist(self, file_name, __working__directory__): '''Following function will check whther file exists in working directory Can handle list or Str ''' file_path = self.join_paths(file_name, __working__directory__) if (os.path.isfile(file_path)): return True return False def join_paths(self, path_to_join, config_dir): '''Function to join working directory path to file or list, Note that last element in the list is a file ['a','b','c.txt']''' path_after_join = config_dir try: if (isinstance(path_to_join, list)): for element in path_to_join: path_after_join = os.path.join(path_after_join, element) elif (isinstance(path_to_join, str)): path_after_join = os.path.join( path_after_join, path_to_join) # Will just join one line else: # Error please insert list or str pass except Exception as e: print(e) pass return path_after_join def create_configuration(self, __config__file__, __Version__, __config__path__, __working__directory__): '''The following function is aimed for creating a finger print Configuration file for initialized process that ran, this will be used in the long run for data science and identification through big networks''' if (not self.is_file_exist(__config__file__, __working__directory__)): # Configuration does not exist, creating config self.logger_instance.write_log(25, 1) config = configparser.ConfigParser() config.sections() # USER_CONFIG self.logger_instance.write_log(26, 1) config['DEFAULT'] = { 'ComputerName': gethostname(), # Host-Name # Socket IP 'Ipaddress': gethostbyname(gethostname()), 'Time': datetime.now().strftime( "%Y-%m-%d %H:%M:%S"), # Date + Clock without micros 'App_Version': __Version__, # Predefined version in globals 'Mac': ':'.join(findall('..', '%012x' % getnode())), 'Win_ver': platform(), 'CPU': processor() } # Returns in 48bits, we could also do hex(uuid.getnode()) # API_CONFIG self.logger_instance.write_log(27, 1) config['API'] = {} # API Key needs to be protected somehow with server etc.. config['API'][ 'api_key'] = '2a24732ab41b71b3a66db6e18595c189a37920067ce9e6f4095ebc5241062121' config['API']['api_url'] = 'https://www.virustotal.com/api/v3/' try: with open(__config__path__, 'w') as configfile: config.write(configfile) self.logger_instance.write_log(21, 1) # success code except Exception as e: self.logger_instance.write_log(121, 0, e) # data err code else: # Configuration exists, we will load the data via main self.logger_instance.write_log(22, 1) # success code pass def read_configuration(self, __working__directory__, __config__file__): '''This function will read api_url and api_key from our previously created configuration''' config = configparser.ConfigParser() config.sections() try: # Trying to read the configuration file self.logger_instance.write_log(23, 1) # Attempt config.read(os.path.join(__working__directory__, __config__file__)) self.logger_instance.write_log(24, 1) # Read api_url = config['API']['api_url'] # Set Vars api_key = config['API']['api_key'] self.logger_instance.write_log(30, 1) except Exception as e: self.logger_instance.write_log(122, 0, e) return (False, False) return api_url, api_key
class ProxyRequestHandler(BaseHTTPRequestHandler): logger_instance = logger.logger() logger_instance.create_logger() config_instance = main_config.MainConfig() api_url, api_key = config_instance.read_configuration( __working__directory__, __config__file__) vt_response_parser_instance = vt_response_parser.vt_response_parser() protocol_version = 'HTTP/1.1' # Var conn = get_connection() # Setting the SQL icon = systrayIcon() icon.start_icon_thread() def do_HEAD(self): self.do_GET(body=False) def _connect_to(self, netloc, soc): i = netloc.find(':') if i >= 0: host_port = netloc[:i], int(netloc[i + 1:]) else: host_port = netloc, 80 try: soc.connect(host_port) except socket.error as e: try: msg = e except: msg = e self.send_error(404, msg) return 0 return 1 def do_GET(self): '''Function handles Http requests and calls checkUrl in order to detect risk In case the checkurl returns there is no risk (True), and record doesnt exist in the db, will insert to the whitelist database table, if (False) Will load Error page Blocked URL and will insert to DB''' (scm, netloc, path, params, query, fragment) = urlparse(self.path, 'http') if scm != 'http' or fragment or not netloc: # Link Validity self.send_error(400, "bad url %s" % self.path) return url = scm + '://' + netloc # If function returns CHECK, we will check the link link_Status = isurlindb(self.conn, url) if (link_Status == 'CHECK'): if (self.checkUrl(url)): print("\n[*] Harmless url forwarding") self.socket_connection(netloc, path, params, query) inserturl(self.conn, 0, self.path, 0, 0, 0, scm) # Insert to DB whitelist insert_list_type(self.conn, url, 0, 'rdef_web_whitelist', scm) else: # Malicious, inserting to DB print("\n[!] Malicious url blocked") # Insert checked and malicious link to blacklist if self.load_blocked_page(url, alert=True): self.socket_connection(netloc, path, params, query) else: insert_list_type(self.conn, url, 0, 'rdef_web_blacklist', scm) elif (link_Status == 'WL'): # Whitelist, forwarding connection print("\n[*] Whitelisted url forwarding") self.socket_connection(netloc, path, params, query) else: # Blacklist, Loading error page print("\n[!] Blacklisted url blocked") if self.load_blocked_page(url): self.socket_connection(netloc, path, params, query) def load_blocked_page(self, url, alert=False): if alert: self.icon.icon_notify(f'Detected suspicious activity in {url}') if self.icon.defend_state(): self.send_error(403) self.close_connection = 1 return False else: return True else: if self.icon.defend_state(): self.send_error(403) self.close_connection = 1 else: return True def socket_connection(self, netloc, path, params, query): soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: if self._connect_to(netloc, soc): self.log_request() text = "%s %s %s\r\n" % (self.command, urlunparse( ('', '', path, params, query, '')), self.request_version) soc.send(text.encode()) self.headers['Connection'] = 'close' del self.headers['Proxy-Connection'] for key_val in self.headers.items(): text = "%s: %s\r\n" % key_val soc.send(text.encode()) soc.send("\r\n".encode()) self._read_write(soc) except Exception as e: self.logger_instance.write_log(172, 1, e) finally: soc.close() self.connection.close() def _read_write(self, soc, max_idling=50): try: iw = [self.connection, soc] ow = [] count = 0 while 1: count += 1 (ins, _, exs) = select.select(iw, ow, iw, 3) if exs: break if ins: for i in ins: if i is soc: out = self.connection else: out = soc data = None try: data = i.recv(8192) except Exception as e: self.logger_instance.write_log(170, 1, e) if data: out.send(data) count = 0 else: pass if count == max_idling: break except Exception as e: self.logger_instance.write_log(171, 1, e) def do_CONNECT_read_write(self, address): try: s = socket.create_connection(address, timeout=self.timeout) print("\n[*] Socket created") except Exception as e: self.send_error(502) print(e) return self.send_response(200, 'Connection Established') self.end_headers() conns = [self.connection, s] self.close_connection = 0 while not self.close_connection: rlist, wlist, xlist = select.select(conns, [], conns, self.timeout) if xlist or not rlist: break for r in rlist: other = conns[1] if r is conns[0] else conns[0] data = None try: data = r.recv(8192) except Exception as e: self.logger_instance.write_log(170, 1, e) if not data: self.close_connection = 1 break other.sendall(data) def do_POST(self, body=True): self.do_GET() def do_CONNECT(self): address = self.path.split(':', 1) url = '' if (address[0] == 'http'): address = [self.path.split('://')[1], 80] # print(address) url = 'http://' + address[0] protocol = 'http' else: address[1] = int(address[1]) or 443 url = address[0] url = "https://" + url protocol = 'https' link_Status = isurlindb(self.conn, url) if (link_Status == 'CHECK'): status = self.checkUrl(url) if (status): inserturl(self.conn, 0, url, 0, 0, 0, protocol) insert_list_type(self.conn, url, 0, 'rdef_web_whitelist', protocol) print("\n[*] Harmless url forwarding") self.do_CONNECT_read_write(address) else: # print("Malicious") print("\n[!] Malicious url blocked") # Insert checked and malicious link to blacklist insert_list_type(self.conn, url, 0, 'rdef_web_blacklist', protocol) if self.load_blocked_page(url, alert=True): self.do_CONNECT_read_write(address) elif (link_Status == 'WL'): print("\n[*] Whitelisted url forwarding") self.do_CONNECT_read_write(address) else: # TODO: Blacklisted url handling print("\n[!] Blacklisted url blocked") if self.load_blocked_page(url): self.do_CONNECT_read_write(address) def send_resp_headers(self, resp): try: respheaders = resp.headers for key in respheaders: if key not in [ 'Content-Encoding', 'Transfer-Encoding', 'content-encoding', 'transfer-encoding', 'content-length', 'Content-Length' ]: self.send_header(key, respheaders[key]) self.send_header('Content-Length', len(resp.content)) self.end_headers() except Exception as e: self.logger_instance.write_log(173, 1, e) def checkUrl(self, url): # Creating a web request to VirusTotal API url = base64.urlsafe_b64encode(url.encode()).decode().strip("=") vt_full_url = self.api_url + "urls/{}".format(url) parameters = {"x-apikey": self.api_key} response = requests.get(vt_full_url, headers=parameters) if (response.status_code == 200): # VT successfull request responseData = json.loads(response.text) harmless, malicious, suspicious, timeout, undetected = self.vt_response_parser_instance.last_analysis_stats( responseData) #malicious = 5 if (malicious > 0): # Writing to log that malicious site detected print( '[!] {} Agents found this url malicious'.format(malicious)) self.logger_instance.write_log(90, 2) return False else: return True else: print('[!] Bad VT request\n') return True