def __init__(self): self.bds_filter_enable = get_config('bds_filter_enable') == 'true' self.bds_filter_type = get_config('bds_filter_type') self.bds_filter_filters = get_config('bds_filter_filters') self.filter_sort_match = {} self.filter_sort_in = {} self.filter_ignore_match = [] self.filter_ignore_in = [] self.filter_sort_other = '' self.filters = yaml.safe_load(open( self.bds_filter_filters).read()).get(self.bds_filter_type, None) if self.filters is None: self.bds_filter_enable = False BdsLogger.put_log('filter', 'Unable to load Filters, disabled.') if self.bds_filter_enable: self.filter_sort_other = self.filters.get('sort', {}).get( 'other', 'result') self.filter_sort_match = self.filters.get('sort', {}).get('match', {}) self.filter_sort_in = self.filters.get('sort', {}).get('in', {}) self.filter_ignore_match = self.filters.get('ignore', {}).get('match', []) self.filter_ignore_in = self.filters.get('ignore', {}).get('in', [])
def cmd(ws: WebSocket): ws_id = self.wsCollector.add(ws) while not ws.closed: message = ws.receive() if message is not None: # noinspection PyBroadException msg = {'token': '', 'cmd': None} # noinspection PyBroadException try: msg = json.loads(message) except: msg['cmd'] = message if (msg.get('type', None) is not None) and (msg.get( 'msg', None) is not None): BdsLogger.put_log(msg['type'], msg['msg']) if self.tokenManager.checkToken(msg.get( 'token', '')) or self.wsCollector.check(ws_id): result = self.wsCollector.update(ws_id) if result == 1: ws.send( json.dumps(self.tokenManager.pass_msg, ensure_ascii=False)) self.cmd_in_via_ws(msg.get('cmd', None)) else: ws.send( json.dumps(self.tokenManager.error_msg, ensure_ascii=False))
def api_log_put(log_type: str, value: str): BdsLogger.put_log(log_type, value) self.bds.sent_to_all(log_type, value) return self.get_body(body_code=200, body_type='log_put', body_msg='OK', body_content='OK')
def on_stopped(self): self.ws_collector.sent_to_all('bds', 'stopping', status=self.if_alive()) BdsLogger.put_log('subprocess', 'stopped') print('>Server Stopped') print('>Bedrock Server stopped. Press Ctrl+C to exit.') print('Enter \'restart\' to restart Manager.') self.need_log = False
def save_log(self): # noinspection PyUnresolvedReferences for line in iter(self.bds.stdout.readline, b''): if self.need_log is False: return if self.bds is None: continue if not self.if_alive(): self.on_stopped() if line != '': line = line.replace('\n', '') log_type = self.log_filter.sort_log(line) if_ignore = self.log_filter.if_ignore(line) self.ws_collector.sent_to_all(log_type, line, ignore=if_ignore) BdsLogger.put_log(log_type, line, ignore=if_ignore)
def cmd_in(self, cmd: str, ignore=False): self.ws_collector.sent_to_all('cmd_in', cmd, status=self.if_alive(), ignore=ignore) BdsLogger.put_log('cmd_in', cmd, ignore=ignore) if cmd == 'restart': self.bds_restart() return in_time = datetime.now() _log = bds_log(time=in_time, log_type='bds', log='Null') if not self.if_alive(): return _log print('>>', cmd) self.bds.stdin.write(cmd + '\n') self.bds.stdin.flush()
def bds_restart(self): print('>restarting...') self.ws_collector.sent_to_all('bds', 'restart', status=self.if_alive()) BdsLogger.put_log('bds', 'restart') if self.if_alive(): self.bds.stdin.write('stop\n') self.bds.stdin.flush() while self.if_alive(): continue del self.bds self.bds = subprocess.Popen(self.script, shell=self.shell, stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True, encoding='utf-8') # Keep the `save_log` running self.need_log = True self.logger = threading.Thread(target=self.save_log) self.logger.start()
def api_log_type(log_type: str): _log = BdsLogger.get_log_all(log_type=log_type) if _log is None: return self.get_body(body_code=404, body_type=f'log_{log_type}', body_content=None, body_msg='No such type') else: return self.get_body(body_code=200, body_type=f'log_{log_type}', body_content=self.log2dict(_log), body_msg='OK')
def api_log_type_or_length(length, log_type=None): _log = BdsLogger.get_log_all(log_type=log_type) if log_type is None: log_type = 'all' if _log is None: return self.get_body(body_code=404, body_type=f'log_{log_type}', body_content=_log, body_msg='No such type') else: if len(_log) < length: return self.get_body(body_code=200, body_type=f'log_{log_type}', body_content=self.log2dict(_log), body_msg='OK') else: start = len(_log) - length stop = len(_log) return self.get_body( body_code=200, body_type=f'log_{log_type}', body_content=self.log2dict(_log)[start:stop], body_msg='OK')
def api_cmd_in(cmd: str): cmd_in_time = datetime.now() logs = [] ignore = request.args.get('ignore', None) == 'true' self.bds.cmd_in(cmd, ignore=ignore) _logs = [] line = request.args.get('line', None) wait_sec = int(request.args.get('timeout', 3)) if line is None: return self.get_body(body_code=400, body_type='cmd_log', body_msg='Error, missing "line"', body_content=None) else: line = int(line) while True: __logs = [] index = -1 _n = True while _n: ___logs = BdsLogger.get_log_all('result') if len(___logs) < -index: _n = False continue _log = ___logs[index] if _log.time >= cmd_in_time: __logs.append(_log) index -= 1 else: _n = False if len(__logs) >= line: _logs = __logs break if wait_sec == 0: continue if (datetime.now() - cmd_in_time).seconds >= wait_sec: _logs = __logs break if ignore: session = get_session() for v in _logs: session.delete(session.query(bds_log).get(v.time)) _l = f'The {len(_logs)} lines of log above has been removed from db.' BdsLogger.put_log('cmd_log', _l, ignore=True) for i in range(len(_logs)): logs.append({ 'time': _logs[-i - 1].time, 'log': _logs[-i - 1].log }) return self.get_body(body_code=200, body_type='cmd_log', body_content=logs, body_msg='OK')
def api_log_all(): _log = BdsLogger.get_log_all(log_type=None) return self.get_body(body_code=200, body_type='log_all', body_content=self.log2dict(_log), body_msg='OK')
def CtrlC(*args): print('>stopped<') BdsLogger.put_log('manager', 'stop_done') while True: sys.exit()
print('>Manager Debug') tokenManager = TokenManager(debug=debug) ws_collector = WebSocketCollector() prop_loader = PropertiesLoader(no_bds=debug_no_bds) bdsCore = BdsCore(no_bds=debug_no_bds, ws_collector=ws_collector) print('====================') print('>Manager Configs are:') printConfig() print('--------------------') print('>Bedrock Server Configs are:') print('\n'.join([f'{v}={prop_loader.prop[v]}' for v in prop_loader.prop])) print('====================') BdsLogger.put_log('manager', 'start') BdsLogger.put_log('manager', '%s:%s' % ( get_config('web_listening_address'), get_config('web_listening_port') )) print('>Listening at %s:%s' % ( get_config('web_listening_address'), get_config('web_listening_port') )) print('>Manager Token: %s' % tokenManager.token) app = Flask( __name__, static_url_path='/',