def __write_hosts(self, hosts, lines): try: hosts.writelines(lines) logg(f'hosts文件内容已更新!') return True except: logg(f'hosts文件更新失败!') return False
def wrapper(self, *args): try: action = 'w' if 'write' in func.__name__ else 'r' with open(self.open_path, action, encoding='utf-8') as hosts: return func(self, hosts, *args) except Exception as err: logg(f'处理时出错!函数:{func.__name__},文件打开方式:{action},错误:{err}') return False
def run_app(): app = Application(handlers, static_path=static_path, template_path=template_path, debug=False) app.listen(port) logg('KafuuService ver.{} NOW RUNNING...'.format(version)) _a.ShellExecute(0, 'open', 'http://127.0.0.1:{}'.format(port), '', '', 1) IOLoop.current().start()
def __init__(self): sys_root = system_root() self.sys_root = sys_root self.hosts_path = f'{sys_root}\\system32\\drivers\\etc\\hosts' self.open_path = self.hosts_path if isfile(self.hosts_path) != True: logg('请手动选择hosts文件') self.server_ip = self.refresh_ip('149.129.115.46') self.addresses = [ 'osu.ppy.sh', 'c.ppy.sh', 'c1.ppy.sh', 'c2.ppy.sh', 'c3.ppy.sh', 'c4.ppy.sh', 'c5.ppy.sh', 'c6.ppy.sh', 'ce.ppy.sh', 'a.ppy.sh' ]
def get(self, handle): func = getattr(Api, handle, None) re = {} if callable(func): temp = func() re['result'] = temp re['time'] = getTime(1) re['handle'] = handle logg(f'apiHandle:{handle} ({self.request.remote_ip}) done.') self.write(re) else: self.render(main_page, **render_data(msg=f'没有找到 “{handle}” 这个api哦~'))
def check_admin(): try: if ctypes.windll.shell32.IsUserAnAdmin() != True: text = "您好!欢迎使用osu!Kafuu服务系统~~\n程序需要管理员权限才能够正常运行,请您在随后的选项弹窗选择“是”。" title = "提示!" options = _c.MB_ICONASTERISK | _c.MB_SYSTEMMODAL message_box(text, title, options) run_with_admin() except Exception as err: text = f"出现以下错误:\n{err}\n\n请您截图报告开发者以解决问题,谢谢!" title = "致命错误!" options = _c.MB_SYSTEMMODAL | _c.MB_ICONHAND | _c.MB_ABORTRETRYIGNORE action = message_box(text, title, options) # stop if action == 3: sys.exit(0) # retry elif action == 4: run_with_admin() # pass else: logg(f'已忽略问题{err}继续运行')
def cert_status(self): logg('正在查询证书安装情况...') try: self.store.Open(self.OpenFlags.ReadOnly) certificates = self.store.Certificates.Find( self.X509FindType.FindBySubjectName, self.subject_name, True) self.store.Close() if len([i for i in certificates]) > 0: logg('证书已安装!') return True logg('证书未安装!') return False except Exception as err: logg(f'查询证书安装状态时失败:{err}') return False
def check_hosts(self, hosts): done = 0 lines = hosts.readlines() logg(f'检查hosts文件完整性...') for line in lines: host = line.replace('\n', '').split(' ' * 4) if host[0] == self.server_ip and host[1] in self.addresses: done += 1 if done == 0: logg(f'未找到hosts记录!') return -1 elif done < len(self.addresses): logg(f'hosts记录不完整,需要进行修复!') return 0 elif done >= len(self.addresses): logg(f'...ok!hosts记录完整') return 1
def remove_cert(self): logg('正在移除证书...') done = 0 try: self.store.Open(self.OpenFlags.ReadWrite) certificates = self.store.Certificates.Find( self.X509FindType.FindBySubjectName, self.subject_name, True) for cert in certificates: logg(f'找到证书:{cert}\n尝试删除...') self.store.Remove(cert) done += 1 self.store.Close() logg(f'处理完毕,共移除证书{done}张!') return True except Exception as err: logg(f'删除证书时出现错误:{err},处理完成数量:{done}') self.store.Close() return False
def kill_process_by_pid(pid): if pid != -1: try: logg(f'尝试杀死进程({pid})...') handle = _a.OpenProcess(1, False, int(pid)) _a.TerminateProcess(handle, -1) _a.CloseHandle(handle) logg('成功杀死进程!') return True except Exception as err: logg(f'杀死进程:执行方法1:{err} 将使用方法2执行!') return execute_cmd(f"taskkill -pid {pid} -f -t")
def backup_hosts(self, hosts): logg(f'备份hosts文件...') content = hosts.readlines() hosts.close() self.open_path = self.hosts_path + '.backup' result = self.__write_hosts(content) self.open_path = self.hosts_path if result == True: logg(f'备份hosts文件成功!') else: logg(f'备份hosts文件失败!') return result
def add_cert(self): logg('正在安装证书...') try: self.store.Open(self.OpenFlags.ReadWrite) cert = self.X509Certificate2(self.cert_file) logg(f'安装证书:{cert.Subject}') self.store.Add(cert) self.store.Close() return True except Exception as err: logg(f'证书安装失败:{err}') self.store.Close() return False
def __init__(self): logg('初始化证书管理器...') try: self.cert_file = base_path('cert\\cert.crt') self.subject_name = '*.ppy.sh' self.OpenFlags = _d.OpenFlags self.X509Certificate2 = _d.X509Certificate2 self.X509FindType = _d.X509FindType self.store = _d.X509Store(_d.StoreName.Root, _d.StoreLocation.CurrentUser) logg('证书管理器初始化完毕!') except Exception as err: logg(f'证书管理器初始化失败,错误:{err}')
def get_port_pid(port): logg(f'检查端口占用({port})...') res = execute_cmd(f'netstat -aon|findstr "{port}"').split('\n') result = [] for line in res: temp = [i for i in line.split(' ') if i != ''] if len(temp) > 4: result.append({ 'pid': temp[4], 'address': temp[1], 'state': temp[3] }) for process in result: if process['address'] == f'0.0.0.0:{port}': logg('发现端口被占用!') return process['pid'] logg('good,未发现端口占用!') return -1
def delete_hosts(self, hosts): done = 0 lines = hosts.readlines() for index, line in enumerate(lines): host = line.replace('\n', '').split(' ' * 4) if host[0] == self.server_ip and host[1] in self.addresses: lines[index] = '' done += 1 if done > 0: hosts.close() if self.__write_hosts(lines) == True: logg(f'已清除相关hosts记录共{done}条!') return True else: logg(f'清楚hosts失败!可能是没有管理员权限') return False else: logg(f'未找到相关hosts记录,无需删除!') return True
def add_hosts(self): check = self.check_hosts() if check != 1: if check == 0: self.backup_hosts() self.delete_hosts() try: logg(f'添加服务器到hosts中...') origin_hosts = self.get_hosts() make_hosts = [ '{}{}{}\n'.format(self.server_ip, ' ' * 4, i) for i in self.addresses ] return self.__write_hosts(origin_hosts + make_hosts) except Exception as err: logg(f'添加hosts时出错:{err}') return False else: logg(f'无需添加hosts!已经添加过了!') return True
def write_all(): logg('写出所有数据') return write_out(get_all())
def exit_all_service(): logg('KafuuService ALL STOPPED.') sys.exit(0) return False
def exit_api(): logg('KafuuService API STOPPED.') sys.exit(0) return False
def exit_api(): logg(f'apiHandle:退出api') return sy.exit_api()
def exit_all(): logg(f'apiHandle:停止并退出所有服务,并返回osu官服') h1 = delete_hosts() h2 = sy.exit_all_service() return {'delete_hosts': h1, 'exit_all': h2}
def get_hosts(self, hosts): logg(f'获取原hosts文件...') content = hosts.readlines() hosts.close() return content