def main(): current_dir = os.getcwd() current_folder = current_dir.split('/')[-1] if current_folder == cfg.g_name_of_main_folder: print("Current folder: ", current_folder) print("Change current folder...") os.chdir(current_dir + generator_cfg.g_path_from_script_to_main) print("Current folder: ", os.getcwd) ## FAKE-UNIT TEST ## data = b'mo|1337-7331|deadbeaf' #data = b'mo|12|beefdea' #response = request_handler.processingRequest(data) #logger.debug(response) #exit(0) ## DELETE while True: try: logger.info("Server is started") server = EchoServer(cfg.HOST, cfg.PORT) asyncore.loop() except Exception as err: logger.error(err) time.sleep(cfg.g_error_sleep_sec)
def activate_user_by_id(id_db: int, hwid_db: str, expiration_date: datetime) -> None: conn = sqlite3.connect(cfg.g_user_db_path) cursor = conn.cursor() try: table_name = cfg.g_db_table_name column_id = cfg.g_db_column_id column_hwid = cfg.g_db_column_hwid column_expiration_date = cfg.g_db_column_expiration_date request = """ UPDATE %s SET %s = '%s', %s = '%s' WHERE %s = %d """ % (table_name, column_hwid, hwid_db, column_expiration_date, expiration_date, column_id, id_db) cursor.execute(request) except sqlite3.DatabaseError as err: symbol = cfg.separateSymbol user_metadata = symbol.join([str(id_db), hwid_db, expiration_date]) logger.error(user_metadata) logger.error(err) time.sleep(cfg.g_error_sleep_sec) else: conn.commit() conn.close()
def get_row_by_license_key(license_key: str) -> tuple: user_row = [] # if not exist: # make file "KIGK3122.sql" # conn = sqlite3.connect("KIGK3122.sql") conn = sqlite3.connect(cfg.g_user_db_path) cursor = conn.cursor() try: row = cfg.g_db_full_row column_name = cfg.g_db_license_key table_name = cfg.g_db_table_name request = "SELECT %s FROM %s WHERE %s = '%s'" % ( row, table_name, column_name, license_key) cursor.execute(request) user_row = cursor.fetchone() logger.debug(user_row) except sqlite3.DatabaseError as err: logger.error(license_key) logger.error(err) time.sleep(cfg.g_error_sleep_sec) conn.close() if user_row == None: user_row = [] return user_row
def processingRequest(data_bytes: str, ip="127.0.0.1") -> bytes: response = cfg.g_empty_response for attempts in range(cfg.g_max_attempts): try: """ Receive the encrypted data """ data = data_bytes.decode('ascii') if data[-1] != cfg.separateSymbol: data = decrypt_data(data) logger.info(data) data = data.lower() data_list = data.split(cfg.separateSymbol) # check for hacker if len(data_list) < 3: logger.warning([data_bytes, ip]) logger.warning(cfg.g_user_state_hacker) remember_hacker() break # Empty license key if not len(data_list[1]): return cfg.g_byte_empty_response request = data_list[0] license_key = data_list[1] hwid = data_list[2] version = data_list[3] if check_version(version): response = get_response_by_user_data(request, license_key, hwid, ip) else: logger.warning([data_bytes, ip]) logger.warning(cfg.g_user_state_outdated_version) response = cfg.g_user_state_outdated_version except Exception as e: args = e.args data = data_bytes.decode('ascii') user_metadata = data + cfg.separateSymbol + ip logger.error(user_metadata) logger.error(args) time.sleep(cfg.g_error_sleep_sec) else: break # debug #response = "Update your application!" # debug """ Send the encrypted data """ logger.debug("RESPONSE: " + response) response = encrypt_data(response) return bytes(response, encoding = 'ascii')
def handle_read(self): for attempt in range(cfg.g_max_attempts): try: data = self.recv(cfg.g_count_of_received_symbols) if len(data) > 1: logger.debug(data) response = request_handler.processingRequest(data) self.send(response + b'\0') except Exception as err: logger.error(err) time.sleep(cfg.g_error_sleep_sec) else: break