diff --git a/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/controller/admin/ExamAuthorizeController.java b/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/controller/admin/ExamAuthorizeController.java index 4f1b728f..4837cab4 100644 --- a/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/controller/admin/ExamAuthorizeController.java +++ b/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/controller/admin/ExamAuthorizeController.java @@ -25,12 +25,12 @@ public class ExamAuthorizeController { @Resource private AuthorizeService authorizeService; - @GetMapping("/get-ip-status") + @GetMapping("/get-ip-status/{website}") @Operation(summary = "使用租户域名,查看租户连接状态", description = "租户管理界面,使用租户域名,查看租户连接状态") - @Parameter(name = "name", description = "租户域名", required = true, example = "1024") - public CommonResult getTenantIdByStatus(@RequestParam("ip") String ip) { + @Parameter(name = "website", description = "租户域名", required = true, example = "1024") + public CommonResult getTenantIdByStatus(@PathVariable String website) { // 使用传入的IP,进行ping,查看是否存在连接,并返回信号的强度 - return success(authorizeService.getTenantIdByStatus(ip)); + return success(authorizeService.getTenantIdByStatus(website)); } @GetMapping("/get-mac-address") diff --git a/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/service/authorize/AuthorizeServiceImpl.java b/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/service/authorize/AuthorizeServiceImpl.java index face10c0..9967938a 100644 --- a/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/service/authorize/AuthorizeServiceImpl.java +++ b/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/service/authorize/AuthorizeServiceImpl.java @@ -3,6 +3,8 @@ package pc.exam.pp.module.exam.service.authorize; import org.springframework.stereotype.Service; import pc.exam.pp.module.exam.controller.admin.enums.authorize.ConnectionStrengthEnums; +import java.io.BufferedReader; +import java.io.InputStreamReader; import java.net.*; /** @@ -21,23 +23,41 @@ public class AuthorizeServiceImpl implements AuthorizeService { */ @Override public String getTenantIdByStatus(String ip) { - int timeout = 3000; - long startTime = System.currentTimeMillis(); - try (Socket socket = new Socket()) { - socket.connect(new InetSocketAddress(ip, 80), timeout); - long endTime = System.currentTimeMillis(); - long duration = endTime - startTime; + String[] schemes = {"https://", "http://"}; + int timeout = 3; - if (duration < 100) { - return ConnectionStrengthEnums.STRONG.getDescription(); - } else if (duration < 500) { - return ConnectionStrengthEnums.WEAK.getDescription(); - } else { - return ConnectionStrengthEnums.NOT_CONNECTED.getDescription(); + for (String scheme : schemes) { + try { + ProcessBuilder pb = new ProcessBuilder("curl", "-o", "/dev/null", "-s", + "-w", "%{time_total}", "--connect-timeout", String.valueOf(timeout), + scheme + ip); + Process process = pb.start(); + + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String timeString = reader.readLine(); + int exitCode = process.waitFor(); + + if (exitCode != 0 || timeString == null || timeString.trim().isEmpty()) { + continue; // 尝试下一个协议 + } + + double timeSeconds = Double.parseDouble(timeString.trim()); + long millis = (long) (timeSeconds * 1000); + + if (millis < 100) { + return ConnectionStrengthEnums.STRONG.getDescription(); + } else if (millis < 500) { + return ConnectionStrengthEnums.WEAK.getDescription(); + } else { + return ConnectionStrengthEnums.NOT_CONNECTED.getDescription(); + } + + } catch (Exception ignored) { + // 尝试下一个 } - } catch (Exception e) { - return ConnectionStrengthEnums.NOT_CONNECTED.getDescription(); } + + return ConnectionStrengthEnums.NOT_CONNECTED.getDescription(); // 两种协议都失败 } /**