wlzboy
2026-03-31 61c4c3f45e4257e2e7662f033e2719e62366c632
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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;
    }
}