mirror of
https://github.com/xszyou/Fay.git
synced 2026-03-12 17:51:28 +08:00
update config_util.py
“fay_url”配置项不存在时主动获取本机ip
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user