Skip to content

ServerStatus 客户端网站监控问题反馈报告 #278

@irol765

Description

@irol765

ServerStatus 客户端问题反馈报告

📋 基本信息

  • 脚本版本: 1.1.0 (20250902)
  • 反馈日期: 当前日期
  • 问题类型: 功能缺陷和兼容性问题

🚨 发现的主要问题

问题1: 编码兼容性问题

文件: 客户端脚本
位置: byte_str() 函数
问题描述: 只使用 UTF-8 编码,无法处理服务端返回的非 UTF-8 字符

问题代码:

def byte_str(object):
    if isinstance(object, str):
        return object.encode(encoding="utf-8")
    elif isinstance(object, bytes):
        return bytes.decode(object)  # 假设所有字节都是 UTF-8
    else:
        print(type(object))

影响: 当服务端返回 GBK、GB2312 或其他编码的字符时,客户端会解码失败,导致连接异常


问题2: 监控任务解析逻辑错误

文件: 客户端脚本
位置: 认证成功后的监控任务解析部分
问题描述: 监控任务解析依赖于错误的条件判断

问题代码:

if data.find("You are connecting via") < 0:
    data = byte_str(s.recv(1024))
    print(data)
    monitorServer.clear()
    for i in data.split('\n'):
        if "monitor" in i and "type" in i and "{" in i and "}" in i:
            # 解析监控任务...

问题分析:

  • 当服务端返回 "You are connecting via: IPv4" 时,条件为 False
  • 但实际上监控任务数据在同一响应中,导致监控任务无法被解析
  • 服务端实际返回格式:
  Authentication successful. Access granted.
  You are connecting via: IPv4
  [{"name":"Google","host":"https://www.google.com","interval":600,"type":"https","monitor":0}]
  [{"name":"Baidu","host":"https://www.baidu.com","interval":600,"type":"https","monitor":1}]

问题3: JSON 解析过于脆弱

文件: 客户端脚本
位置: 监控任务 JSON 解析
问题描述: JSON 解析依赖简单的字符串查找,不够健壮

问题代码:

jdata = json.loads(i[i.find("{"):i.find("}")+1])

问题:

  • 如果 JSON 中包含 } 字符会解析错误
  • 无法处理被方括号 [] 包裹的 JSON 数据
  • 没有处理 JSON 解析异常

问题4: 调试信息不足

问题描述: 缺乏详细的调试输出,难以定位问题

  • 无法看到服务端返回的完整数据
  • 无法看到 JSON 解析过程
  • 无法看到监控任务启动状态

🔧 修复方案

修复1: 增强编码兼容性

def byte_str(object):
 if isinstance(object, str):
 return object.encode(encoding="utf-8")
 elif isinstance(object, bytes):
 # 尝试多种编码
 for encoding in ['utf-8', 'gbk', 'gb2312', 'latin-1']:
 try:
 return object.decode(encoding)
 except UnicodeDecodeError:
 continue
 # 最后手段:忽略错误
 return object.decode('utf-8', errors='ignore')
 else:
 return str(object)

修复2: 改进监控任务解析逻辑

# 移除错误的条件判断,直接解析当前数据
monitorServer.clear()
server_monitor_count = 0

# 详细解析每一行
for line in data.split('\n'):
 line = line.strip()
 if not line:
 continue

 # 宽松的JSON检查
 if '{' in line and '}' in line:
 try:
 # 提取JSON部分
 start = line.find('{')
 end = line.rfind('}') + 1
 json_str = line[start:end]
 task_data = json.loads(json_str)

 # 验证必需字段
 if all(key in task_data for key in ['name', 'host', 'type']):
 name = task_data['name']
 monitorServer[name] = {
 "type": task_data.get("type"),
 "host": task_data.get("host"),
 "latency": 0
 }
 # 启动监控线程...
 server_monitor_count += 1

 except Exception as e:
 print(f"解析监控任务失败: {e}")

修复3: 健壮的 JSON 解析

def parse_monitor_task(line):
 """健壮的监控任务解析函数"""
 try:
 # 方法1: 直接解析
 return json.loads(line)
 except json.JSONDecodeError:
 try:
 # 方法2: 提取JSON对象
 start = line.find('{')
 end = line.rfind('}') + 1
 if start >= 0 and end > start:
 return json.loads(line[start:end])
 except:
 pass
 return None

修复4: 增强调试输出

print(f"完整认证响应: {data}")
print(f"解析服务端监控任务...")
print(f"服务端监控任务启动: {server_monitor_count} 个")

📝 完整的修复代码片段

# 修复后的监控任务解析部分
monitorServer.clear()
server_monitor_count = 0

print("解析服务端监控任务...")
for line in data.split('\n'):
 line = line.strip()
 if not line:
 continue

 # 宽松的JSON检查
 if '{' in line and '}' in line:
 try:
 # 处理可能的方括号包裹
 if line.startswith('[') and line.endswith(']'):
 line = line[1:-1] # 移除方括号

 # 提取JSON部分
 start = line.find('{')
 end = line.rfind('}') + 1
 json_str = line[start:end]
 task_data = json.loads(json_str)

 # 验证必需字段
 if all(key in task_data for key in ['name', 'host', 'type']):
 name = task_data['name']
 monitorServer[name] = {
 "type": task_data.get("type"),
 "host": task_data.get("host"),
 "latency": 0
 }

 # 启动监控线程
 t = threading.Thread(
 target=_monitor_thread,
 kwargs={
 'name': name,
 'host': task_data.get("host"),
 'interval': task_data.get("interval", 600),
 'type': task_data.get("type")
 }
 )
 t.daemon = True
 t.start()
 server_monitor_count += 1
 print(f"启动监控: {name}")

 except Exception as e:
 print(f"解析监控任务失败: {e}, 行: {line}")

print(f"服务端监控任务启动: {server_monitor_count} 个")

🎯 建议的修复优先级

  1. 高优先级: 修复监控任务解析逻辑
  2. 中优先级: 增强编码兼容性
  3. 低优先级: 改进调试输出

📋 测试验证

修复后应该能够:

  • ✅ 正确解析服务端下发的监控任务
  • ✅ 处理各种编码的服务端响应
  • ✅ 启动监控线程并正常上报数据
  • ✅ 在前端显示监控状态

💡 总结

这些问题影响了客户端的稳定性和兼容性,特别是在处理不同编码和服务端响应格式时。修复后将显著提升客户端的健壮性和用户体验。

建议在下一个版本中集成这些修复,并考虑增加更完善的错误处理和日志记录机制。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions