(self, status = False)
更改流量统计设置
更改流量统计设置
更改流量统计设置
def write_stats(self, status = False):
        '''
        更改流量统计设置
        '''
        conf_rules = self.config["routing"]["rules"]
        if status:
            stats_json = self.load_template('stats_settings.json')
            routing_rules = stats_json["routingRules"]
            del stats_json["routingRules"]

            has_rule = False
            for rule in conf_rules:
                if rule["outboundTag"] == "api":
                    has_rule = True
                    break
            if not has_rule:
                conf_rules.append(routing_rules)

            dokodemo_door = stats_json["dokodemoDoor"]
            del stats_json["dokodemoDoor"]
            #产生随机dokodemo_door的连接端口
            dokodemo_door["port"] = random_port(1000, 65535)

            has_door = False
            for inbound in self.config["inbounds"]:
                if inbound["protocol"] == "dokodemo-door" and inbound["tag"] == "api":
                    has_door = True 
                    break
            if not has_door:
                self.config["inbounds"].append(dokodemo_door)

            #加入流量统计三件套
            self.config.update(stats_json)

            # 为各个组节点打上tag, 以便流量统计
            for group in self.group_list:
                if type(group) == Mtproto:
                    continue
                self.config["inbounds"][group.index].update({"tag": group.tag})
        else:
            # 删除用于统计流量的tag标签
            for index, inbound in enumerate(self.config["inbounds"]):
                if inbound["protocol"] == "dokodemo-door" and inbound["tag"] == "api":
                    del self.config["inbounds"][index]
                elif "tag" in inbound:
                    del self.config["inbounds"][index]["tag"]

            if "stats" in self.config:
                del self.config["stats"]
            if "api" in self.config:
                del self.config["api"]
            if "policy" in self.config:
                del self.config["policy"]

            for index, rule in enumerate(conf_rules):
                if rule["outboundTag"] == "api":
                    del conf_rules[index]
        self.save()
def write_stats ( self , status = False ) : conf_rules = self . config [ "routing" ] [ "rules" ] if status : stats_json = self . load_template ( 'stats_settings.json' ) routing_rules = stats_json [ "routingRules" ] del stats_json [ "routingRules" ] has_rule = False for rule in conf_rules : if rule [ "outboundTag" ] == "api" : has_rule = True break if not has_rule : conf_rules . append ( routing_rules ) dokodemo_door = stats_json [ "dokodemoDoor" ] del stats_json [ "dokodemoDoor" ] #产生随机dokodemo_door的连接端口 dokodemo_door [ "port" ] = random_port ( 1000 , 65535 ) has_door = False for inbound in self . config [ "inbounds" ] : if inbound [ "protocol" ] == "dokodemo-door" and inbound [ "tag" ] == "api" : has_door = True break if not has_door : self . config [ "inbounds" ] . append ( dokodemo_door ) #加入流量统计三件套 self . config . update ( stats_json ) # 为各个组节点打上tag, 以便流量统计 for group in self . group_list : if type ( group ) == Mtproto : continue self . config [ "inbounds" ] [ group . index ] . update ( { "tag" : group . tag } ) else : # 删除用于统计流量的tag标签 for index , inbound in enumerate ( self . config [ "inbounds" ] ) : if inbound [ "protocol" ] == "dokodemo-door" and inbound [ "tag" ] == "api" : del self . config [ "inbounds" ] [ index ] elif "tag" in inbound : del self . config [ "inbounds" ] [ index ] [ "tag" ] if "stats" in self . config : del self . config [ "stats" ] if "api" in self . config : del self . config [ "api" ] if "policy" in self . config : del self . config [ "policy" ] for index , rule in enumerate ( conf_rules ) : if rule [ "outboundTag" ] == "api" : del conf_rules [ index ] self . save ( )