package com.ruoyi.system.controller;
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
import com.ruoyi.common.core.controller.BaseController;
|
import org.springframework.web.bind.annotation.*;
|
|
import java.net.InetAddress;
|
import java.net.UnknownHostException;
|
import java.util.*;
|
import java.net.Socket;
|
import java.io.IOException;
|
|
/**
|
* 网络诊断Controller
|
* 提供OCR服务连接诊断、DNS解析测试、网络连通性测试等功能
|
*/
|
@RestController
|
@RequestMapping("/system/diag")
|
public class NetworkDiagController extends BaseController {
|
|
/**
|
* 诊断OCR服务连接
|
* @return 诊断结果
|
*/
|
@GetMapping("/ocrConnection")
|
public AjaxResult diagOcrConnection() {
|
Map<String, Object> result = new HashMap<>();
|
|
String ocrEndpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
|
int port = 443; // HTTPS端口
|
|
try {
|
result.put("dns", testDns(ocrEndpoint));
|
result.put("connection", testConnection(ocrEndpoint, port));
|
result.put("network", getNetworkConfig());
|
|
return success(result);
|
|
} catch (Exception e) {
|
logger.error("网络诊断失败", e);
|
return error("网络诊断失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 测试DNS解析
|
* @param params 包含hostname的参数
|
* @return DNS解析结果
|
*/
|
@PostMapping("/testDns")
|
public AjaxResult testDns(@RequestBody Map<String, String> params) {
|
String hostname = params.get("hostname");
|
if (hostname == null || hostname.trim().isEmpty()) {
|
return error("主机名不能为空");
|
}
|
return success(testDns(hostname));
|
}
|
|
/**
|
* 测试网络连通性
|
* @param params 包含host和port的参数
|
* @return 连通性测试结果
|
*/
|
@PostMapping("/testConnectivity")
|
public AjaxResult testConnectivity(@RequestBody Map<String, Object> params) {
|
String host = (String) params.get("host");
|
Integer port = (Integer) params.get("port");
|
|
if (host == null || host.trim().isEmpty()) {
|
return error("主机不能为空");
|
}
|
if (port == null) {
|
return error("端口不能为空");
|
}
|
|
return success(testConnection(host, port));
|
}
|
|
/**
|
* 测试DNS解析
|
* @param hostname 主机名
|
* @return DNS解析结果
|
*/
|
private Map<String, Object> testDns(String hostname) {
|
Map<String, Object> dnsResult = new HashMap<>();
|
|
try {
|
InetAddress[] addresses = InetAddress.getAllByName(hostname);
|
dnsResult.put("success", true);
|
dnsResult.put("hostname", hostname);
|
dnsResult.put("ipCount", addresses.length);
|
|
String[] ips = new String[addresses.length];
|
for (int i = 0; i < addresses.length; i++) {
|
ips[i] = addresses[i].getHostAddress();
|
}
|
dnsResult.put("ips", ips);
|
|
} catch (UnknownHostException e) {
|
dnsResult.put("success", false);
|
dnsResult.put("error", "DNS解析失败: " + e.getMessage());
|
dnsResult.put("suggestion", "请检查DNS服务器配置,可以尝试使用公共DNS(如8.8.8.8)");
|
}
|
|
return dnsResult;
|
}
|
|
/**
|
* 测试网络连接
|
* @param hostname 主机名
|
* @param port 端口号
|
* @return 连接测试结果
|
*/
|
private Map<String, Object> testConnection(String hostname, int port) {
|
Map<String, Object> connResult = new HashMap<>();
|
long startTime = System.currentTimeMillis();
|
|
try (Socket socket = new Socket()) {
|
socket.connect(new java.net.InetSocketAddress(hostname, port), 10000); // 10秒超时
|
long endTime = System.currentTimeMillis();
|
long responseTime = endTime - startTime;
|
|
connResult.put("success", true);
|
connResult.put("hostname", hostname);
|
connResult.put("port", port);
|
connResult.put("responseTime", responseTime + "ms");
|
connResult.put("connected", true);
|
|
} catch (IOException e) {
|
long endTime = System.currentTimeMillis();
|
long responseTime = endTime - startTime;
|
|
connResult.put("success", false);
|
connResult.put("hostname", hostname);
|
connResult.put("port", port);
|
connResult.put("responseTime", responseTime + "ms");
|
connResult.put("error", "连接失败: " + e.getMessage());
|
connResult.put("suggestion", "请检查防火墙设置,确认端口" + port + "是否开放");
|
}
|
|
return connResult;
|
}
|
|
/**
|
* 获取网络配置信息
|
* @return 网络配置信息
|
*/
|
private Map<String, Object> getNetworkConfig() {
|
Map<String, Object> networkConfig = new HashMap<>();
|
try {
|
networkConfig.put("httpProxy", System.getProperty("http.proxyHost"));
|
networkConfig.put("httpsProxy", System.getProperty("https.proxyHost"));
|
networkConfig.put("localHostAddress", InetAddress.getLocalHost().getHostAddress());
|
networkConfig.put("localHostName", InetAddress.getLocalHost().getHostName());
|
} catch (Exception e) {
|
networkConfig.put("error", "获取网络配置失败: " + e.getMessage());
|
}
|
return networkConfig;
|
}
|
}
|