Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 779f42c98c | |||
| 525620b0f8 | |||
| ebc8d687d0 |
@@ -8,7 +8,7 @@ import os
|
||||
import re
|
||||
import json
|
||||
import subprocess
|
||||
from flask import jsonify
|
||||
from flask import jsonify, request
|
||||
from utils import log_message
|
||||
from config import PROJECTS_FILE
|
||||
|
||||
@@ -71,13 +71,10 @@ def _get_listening_ports():
|
||||
port = int(port_match.group(1))
|
||||
|
||||
pid_match = re.search(r'pid=(\d+)', line)
|
||||
if not pid_match:
|
||||
continue
|
||||
pid = int(pid_match.group(1))
|
||||
pid = int(pid_match.group(1)) if pid_match else None
|
||||
|
||||
# 提取进程名
|
||||
proc_match = re.search(r'users:\(\(\"([^\"]+)\"', line)
|
||||
proc_name = proc_match.group(1) if proc_match else 'unknown'
|
||||
proc_name = proc_match.group(1) if proc_match else '未知进程'
|
||||
|
||||
services.append({
|
||||
'port': port,
|
||||
@@ -165,7 +162,7 @@ def register_discovery_routes(app):
|
||||
@app.route('/api/discovery/services')
|
||||
def api_discovery_services():
|
||||
"""扫描本机未注册的新服务"""
|
||||
# 1. 获取已知端口
|
||||
# 1. 收集已知端口
|
||||
known_ports = set()
|
||||
try:
|
||||
with open(PROJECTS_FILE, 'r', encoding='utf-8') as f:
|
||||
@@ -179,7 +176,16 @@ def register_discovery_routes(app):
|
||||
except:
|
||||
pass
|
||||
|
||||
# 2. 获取 Docker 容器端口
|
||||
# 2. 前端传入的额外排除端口(自定义服务等)
|
||||
exclude_str = request.args.get('exclude_ports', '')
|
||||
if exclude_str:
|
||||
try:
|
||||
for p in exclude_str.split(','):
|
||||
known_ports.add(int(p.strip()))
|
||||
except:
|
||||
pass
|
||||
|
||||
# 3. 获取 Docker 容器端口
|
||||
try:
|
||||
result = subprocess.run(
|
||||
['docker', 'ps', '--format', '{{.Ports}}'],
|
||||
@@ -210,10 +216,16 @@ def register_discovery_routes(app):
|
||||
continue
|
||||
seen_ports.add(port)
|
||||
|
||||
cmdline = _get_process_cmdline(pid)
|
||||
cwd = _get_process_cwd(pid)
|
||||
name = _guess_service_name(pid, svc['process'], cmdline)
|
||||
svc_type = _guess_service_type(cmdline)
|
||||
if pid:
|
||||
cmdline = _get_process_cmdline(pid)
|
||||
cwd = _get_process_cwd(pid)
|
||||
name = _guess_service_name(pid, svc['process'], cmdline)
|
||||
svc_type = _guess_service_type(cmdline)
|
||||
else:
|
||||
cmdline = ''
|
||||
cwd = ''
|
||||
name = svc['process']
|
||||
svc_type = 'unknown'
|
||||
|
||||
undiscovered.append({
|
||||
'port': port,
|
||||
|
||||
@@ -2238,6 +2238,7 @@
|
||||
const newService = {
|
||||
id: 'custom_' + Date.now(),
|
||||
name: name,
|
||||
port: port || '',
|
||||
url: mainUrl,
|
||||
admin_url: adminUrl || null,
|
||||
description: desc || '自定义外部服务',
|
||||
@@ -2886,7 +2887,12 @@
|
||||
list.innerHTML = '<div class="text-gray-400 text-xs text-center py-2"><i class="ri-loader-4-line animate-spin"></i> 扫描中...</div>';
|
||||
|
||||
try {
|
||||
const res = await fetch('/api/discovery/services');
|
||||
// 收集前端已注册的自定义服务端口,传给后端排除
|
||||
const customServices = getCustomServices();
|
||||
const customPorts = customServices.map(s => parseInt(s.port)).filter(p => !isNaN(p));
|
||||
const excludeParam = customPorts.length > 0 ? `?exclude_ports=${customPorts.join(',')}` : '';
|
||||
|
||||
const res = await fetch(`/api/discovery/services${excludeParam}`);
|
||||
const data = await res.json();
|
||||
const services = data.services || [];
|
||||
document.getElementById('discoveryCount').textContent = `发现 ${services.length} 个`;
|
||||
@@ -2899,7 +2905,7 @@
|
||||
<div class="flex items-center gap-3 flex-1 min-w-0">
|
||||
<span class="text-xs text-cyan-400 font-mono">:${s.port}</span>
|
||||
<span class="text-gray-300 text-sm truncate">${s.suggested_name}</span>
|
||||
<span class="text-gray-500 text-xs">PID ${s.pid}</span>
|
||||
<span class="text-gray-500 text-xs">PID ${s.pid != null ? s.pid : '受权限限制'}</span>
|
||||
<span class="text-gray-500 text-xs truncate hidden md:inline">${(s.cmdline || '').substring(0, 60)}</span>
|
||||
</div>
|
||||
<button onclick="quickAddService(${s.port}, '${s.suggested_name.replace(/'/g, "\\'")}', '${s.suggested_type}', '${(s.cwd || '').replace(/'/g, "\\'")}')" class="btn bg-green-600/50 hover:bg-green-600 px-2 py-1 rounded text-xs shrink-0 ml-2">
|
||||
|
||||
Reference in New Issue
Block a user