Esempio n. 1
0
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
Esempio n. 2
0
def __get_api_address_port():
    template_config = json.loads(config.get_v2_template_config(),
                                 encoding='utf-8')
    inbounds = template_config['inbounds']
    api_inbound = list_util.get(inbounds, 'tag', 'api')
    return api_inbound['listen'], api_inbound['port']
Esempio n. 3
0
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
Esempio n. 4
0
def __get_api_address_port():
    template_config = json.loads(config.get_v2_template_config())
    inbounds = template_config["inbounds"]
    api_inbound = list_util.get(inbounds, "tag", "api")
    return api_inbound["listen"], api_inbound["port"]