update config_util.py

“fay_url”配置项不存在时主动获取本机ip
This commit is contained in:
xszyou
2025-05-26 14:32:47 +08:00
parent 01538eb057
commit 9f4dd8df05
2 changed files with 31 additions and 0 deletions

View File

@@ -239,6 +239,15 @@ def load_config():
start_mode = system_config.get('key', 'start_mode', fallback=None)
fay_url = system_config.get('key', 'fay_url', fallback=None)
# 如果fay_url为空或None则动态获取本机IP地址
if not fay_url:
from utils.util import get_local_ip
local_ip = get_local_ip()
fay_url = f"http://{local_ip}:5000"
# 更新system_config中的值但不写入文件
if not system_config.has_section('key'):
system_config.add_section('key')
system_config.set('key', 'fay_url', fay_url)
# 读取用户配置
with codecs.open(config_json_path, encoding='utf-8') as f:

View File

@@ -3,6 +3,7 @@ import os
import sys
import random
import time
import socket
from core import wsa_server
from scheduler.thread_manager import MyThread
@@ -11,6 +12,27 @@ from utils import config_util
LOGS_FILE_URL = "logs/log-" + time.strftime("%Y%m%d%H%M%S") + ".log"
def get_local_ip():
"""
获取本机IP地址
返回:
str: 本机IP地址如果获取失败则返回127.0.0.1
"""
try:
# 创建一个临时socket连接用于获取本机IP
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# 连接任意可用地址(不需要真正建立连接)
s.connect(("8.8.8.8", 80))
# 获取本机IP地址
ip = s.getsockname()[0]
s.close()
return ip
except Exception as e:
log(1, f"获取本机IP地址失败: {str(e)}")
return "127.0.0.1"
def random_hex(length):
result = hex(random.randint(0, 16 ** length)).replace('0x', '').lower()
if len(result) < length: