ChannelMonitor icon indicating copy to clipboard operation
ChannelMonitor copied to clipboard

bug : One-Hub渠道ID值被无意间增大

Open LoveU3th opened this issue 1 year ago • 9 comments

在使用过佬的服务后,发现One-Hub渠道ID值被无意间增大; 在One-Hub中渠道在删除后ID并未被释放,而是继续累加; 是不是调用刷新导致了什么变化? image

LoveU3th avatar Dec 17 '24 03:12 LoveU3th

刷新的原理是新增一个渠道,让One-Hub去调用同步用户分组可用性,然后把这个渠道删除。

ID在数据库中是自增字段,删除后再插入行时,新增的ID会接着累加

DullJZ avatar Dec 17 '24 03:12 DullJZ

我之前的方式是通过获取渠道详细内容,再利用保存接口PUT,这样可以保证只修改这个渠道的模型。 佬可以每个渠道先get数据,再刷新模型,最后调用API把修改后的模型和渠道数据一起PUT。 之前用python写的,可以参考参考,我不会go,实在不知道咋改。。。

# refresh_models.py
from calendar import c
from re import S
from sre_constants import SUCCESS
import requests
import os
import math
import logging

logging.basicConfig(level=logging.INFO)

headers = {
    'Accept': 'application/json, text/plain, */*',
    'Accept-Language': 'zh-CN,zh;q=0.9,ja;q=0.8',
    'Connection': 'keep-alive',
    'Referer': 'http://xxxxxxxx:xx/panel/channel',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
    "Authorization": "Bearer xxxxxxxxxxxxx"  #修改为令牌
}
total_channels = 0

def get_channel_list():
	#通过读取渠道页面获取渠道列表
    global total_channels
    page_num = 1
    params = {
        'page': page_num ,
        'size': '30',
        'order': '-id',
        'type': '0',
        'status': '0',
        'name': '',
        'group': '',
        'models': '',
        'key': '',
        'test_model': '',
        'other': '',
        'filter_tag': 'false',
        'tag': '',
    }
    #获取渠道列表
    response = requests.get('http://xxxxxxxx:xx/api/channel/', params=params, headers=headers, verify=False)
    json_data = response.json()
    # 1. 判断返回值是否正常
    if not json_data.get('success', False):
        logging.error(f"API请求失败,页码: {page_num}")

    # 2. 计算总页数
    size = json_data['data']['size']
    total_channels = json_data['data']['total_count']
    total_pages = math.ceil(total_channels / size)
    print(f"总页数: {total_pages}")

    # 3. 将data中的数据按照id保存成字典格式
    data_list = json_data['data']['data']
    data_dict = {item['id']: item for item in data_list}
    if total_pages == 1:
        return data_dict
    else:
        # 根据需要继续请求后续页的数据
        for page_num in range(2, total_pages + 1):
            params['page'] = page_num
            response = requests.get('http://xxxxxxxx:xx/api/channel/', params=params, headers=headers, verify=False)
            json_data = response.json()
            if not json_data.get('success', False):
                logging.error(f"API请求失败,页码: {page_num}")
            data_list = json_data['data']['data']
            data_dict.update({item['id']: item for item in data_list})
        return data_dict


def get_channel_data():
    data_dict = get_channel_list()
    # 获取每个渠道id的详细数据
    channel_data_dict = {}
    for item_id in data_dict:
        channel_response = requests.get(f'http://xxxxxxxx:xx/api/channel/{item_id}', headers=headers, verify=False)
        channel_json_data = channel_response.json()        
        if channel_json_data.get('success', False):
            channel_data = channel_json_data['data']
            channel_data_dict[item_id] = channel_data
        else:
            logging.error(f"获取ID为{item_id}的详细数据失败")
    
    return channel_data_dict


def provider_models_list():
    global total_channels
    SUCCESS_Refresh = 0
    # 更新每个渠道id的models列表
    channel_data_dict = get_channel_data()
    for item_id in channel_data_dict:
        item = channel_data_dict[item_id]
        json_data = {
        'id': item['id'],
        'type': item['type'],
        'key': item['key'],
        'status': item['status'],
        'name': item['name'],
        'weight': item['weight'],
        'created_time': item['created_time'],
        'test_time': item['test_time'],
        'response_time': item['response_time'],
        'base_url': item['base_url'],
        'other': item['other'],
        'balance': item['balance'],
        'balance_updated_time': item['balance_updated_time'],
        'models': item['models'],
        'group': item['group'],
        'tag':  item['tag'],
        'used_quota': item['used_quota'],
        'model_mapping': item['model_mapping'],
        'priority': item['priority'],
        'proxy': item['proxy'],
        'test_model': item['test_model'],
        'only_chat': item['only_chat'],
        'pre_cost': item['pre_cost'],
        'plugin': item['plugin'],
        'groups': [
            'default',
        ],
        'is_edit': True,
        }
        response = requests.post('http://xxxxxxxx:xx/api/channel/provider_models_list', headers=headers, json=json_data, verify=False)
        if response.json().get('success', False):
            channel_models_list = response.json()['data']
            if channel_models_list is None:
                logging.error(f"ID为{item_id},名字为{item['name']}的渠道模型刷新失败,失败原因为:模型列表为None")
                continue
            channel_models_string = ','.join(channel_models_list)
            json_data['models'] = channel_models_string
            # put更新渠道的models列表
            requests.put('http://xxxxxxxx:xx/api/channel/', headers=headers, json=json_data, verify=False)
            SUCCESS_Refresh += 1
            logging.info(f"ID为{item_id},名字为{item['name']}的渠道模型刷新成功")
        else:
            logging.error(f"ID为{item_id},名字为{item['name']}的渠道模型刷新失败,失败原因为:{response.json()['message']}")

    logging.info(f"总共{total_channels}个渠道,成功刷新{SUCCESS_Refresh}个渠道的模型列表")

if __name__ == '__main__':
    provider_models_list()

LoveU3th avatar Dec 18 '24 03:12 LoveU3th

懂了,就是调用OneHub自己的接口进行更新,我这段时间加个原生接口修改的功能

DullJZ avatar Dec 18 '24 06:12 DullJZ

#8 可以测试一下

DullJZ avatar Dec 18 '24 08:12 DullJZ

bug:字段model_headers不能为null,否则会前端校验不通过全部显示为空。 建议先获取每个渠道id的详细数据并保存所有字段,更新models,再重新PUT。确保数据为最新。

        "model_mapping": "{}",
        "model_headers": null,

image

LoveU3th avatar Dec 19 '24 07:12 LoveU3th

目前就是获取渠道的详细信息,然后只修改models项,其他全部原样PUT回去

现在我测试的OneHub没有出现显示为空的问题

image

DullJZ avatar Dec 20 '24 08:12 DullJZ

抱歉佬,这段时间太忙了,一直没反馈。 应该是我的onehub部分数据太旧了,相比最新版缺少字段内容。虽然一直升级到最新版但是字段没被更新。 我把所有数据重新搞了一下,测试字段没问题。 但是渠道编号依然增大了 用的是dulljz/channel-monitor:feature7版本镜像做的测试 看了一下feature7代码,好像feature7版本镜像没有正确打包新的PUT方法代码,还是旧的。log为刷新成功

LoveU3th avatar Jan 26 '25 07:01 LoveU3th

#8 同步了最新更改,同时最新代码的镜像重新push到了 dulljz/channel-monitor:feature7

目前在我的OneHub上测试通过,烦请佬再测试一遍,没有问题的话就可以合并分支了

DullJZ avatar Jan 26 '25 16:01 DullJZ

可以的佬,这个版本没问题,可以合并

LoveU3th avatar Jan 29 '25 02:01 LoveU3th