def get_inbounds_traffic(reset=True): if __api_port < 0: logging.warning('v2ray api port is not configured') return None cmd = __get_v2ray_api_cmd('', 'StatsService', 'QueryStats', '', 'true' if reset else 'false') result, code = cmd_util.exec_cmd(cmd) if code != 0: logging.warning('v2ray api code %d' % code) return None inbounds = [] for match in __traffic_pattern.finditer(result): tag = match.group('tag') tag = codecs.getdecoder('unicode_escape')(tag)[0] tag = tag.encode('ISO8859-1').decode('utf-8') if tag == 'api': continue _type = match.group('type') value = match.group('value') if not value: value = 0 else: value = int(value) inbound = list_util.get(inbounds, 'tag', tag) if inbound: inbound[_type] = value else: inbounds.append({'tag': tag, _type: value}) return inbounds
def v2_status(): result, code = cmd_util.exec_cmd('systemctl is-active v2ray') if result.startswith('active'): code = 0 elif result.startswith('inactive'): code = 1 else: code = 2 __status['v2'] = {'code': code}
def get_v2ray_version(): global __version if __version != "": return __version result, code = cmd_util.exec_cmd(f"{__v2ray_cmd} -version") if code != 0: return "Unknown" else: try: __version = result.split(" ")[1] return __version except Exception as e: logging.warning(f"get {__v2_cmd_name} version failed: {e}") return "Error"
def get_v2ray_version(): global __v2ray_version if __v2ray_version != '': return __v2ray_version result, code = cmd_util.exec_cmd(f'{__v2ray_cmd} -version') if code != 0: return 'Unknown' else: try: __v2ray_version = result.split(' ')[1] return __v2ray_version except Exception as e: logging.warning(f'get v2ray version failed: {e}') return 'Error'
def get_traffic(isreset='true'): traffics = [] raw_traffics = [] moment = str(int(time.time())) results, code = cmd_util.exec_cmd(get_v2ray_api_cmd(reset = isreset)) if code != 0: return [] for match in _traffic_pattern.finditer(results): # inbound, user id_type = match.group('id_type') # email, api, ray tag = match.group('tag') # uplink, downlink traffice_type = match.group('traffice_type') # 流量 value = match.group('value') if not value: value = 0 else: value = int(value) raw_traffics.append({ 'id_type': id_type, 'tag': tag, 'traffice_type': traffice_type, 'value': value }) raw_traffics.sort(key = key_for_sort) raw_traffics = groupby(raw_traffics, key = key_for_sort) groups = [] for key, traffic in raw_traffics: groups.append(list(traffic)) for i in groups: traffics.append({ 'id_type': i[0]['id_type'], 'tag': i[0]['tag'], i[0]['traffice_type']: i[0]['value'], i[1]['traffice_type']: i[1]['value'] }) return traffics, moment
def v2_status(): result, code = cmd_util.exec_cmd('systemctl is-active v2ray') results = result.split('\n') has_result = False for result in results: if result.startswith('active'): code = 0 has_result = True break elif result.startswith('inactive'): code = 1 has_result = True break if not has_result: code = 2 __status['v2'] = {'code': code}
def f(): cmd_util.exec_cmd(config.get_v2_start_cmd())
def get_inbounds_traffic(reset=True): try: __api_address, __api_port = __get_api_address_port() if not __api_address or __api_address == "0.0.0.0": __api_address = "127.0.0.1" except Exception as e: logging.error( f"Failed to open {__v2_cmd_name} api, please reset all panel settings." ) logging.error(str(e)) sys.exit(-1) if __api_port < 0: logging.warning(f"{__v2_cmd_name} api port is not configured.") return None if __v2_cmd_name == "v2ray": cmd = __get_v2ray_api_cmd( __api_address, __api_port, "StatsService", "QueryStats", "user", "true" if reset else "false", ) else: cmd = __get_v2ray_api_cmd(__api_address, __api_port, "statsquery", "", "user", "-reset" if reset else "") result, code = cmd_util.exec_cmd(cmd) if code != 0: logging.warning(f"{__v2_cmd_name} api code %d" % code) return None inbounds = [] if __v2_cmd_name == "v2ray": __pattern_user_v2ray = re.compile( 'stat:\s*<\s*name:\s*"user>>>(?P<email>[^>]+)>>>traffic>>>(?P<type>uplink|downlink)"(\s*value:\s*(?P<value>\d+))?' ) for match in __pattern_user_v2ray.finditer(result): email = match.group("email") traffic_type = match.group("type") value = match.group("value") if not value: value = 0 else: value = int(value) inbound = list_util.get(inbounds, "email", email) if inbound: inbound[traffic_type] = value else: inbounds.append({"email": email, traffic_type: value}) else: __pattern_user_xray = re.compile( "user>>>(?P<email>[^>]+)>>>traffic>>>(?P<type>uplink|downlink)") for stat in json.loads(result).get("stat", [{}]): if stat: match = __pattern_user_xray.match(stat["name"]) if match: email, traffic_type = match.groups() value = stat.get("value", 0) inbound = list_util.get(inbounds, "email", email) if inbound: inbound[traffic_type] = value else: inbounds.append({"email": email, traffic_type: value}) return inbounds