From e836fb6e9f37de05fb17e024d9fcb4aa29ab804c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=BB=E7=BB=B4=E7=82=B3?= Date: Fri, 18 Apr 2025 14:26:13 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=96=B0=E5=A2=9E=E3=80=91=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E4=BD=BF=E7=94=A8=E7=A7=9F=E6=88=B7=E5=9F=9F=E5=90=8D?= =?UTF-8?q?=EF=BC=8C=E6=9F=A5=E7=9C=8B=E7=A7=9F=E6=88=B7=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E3=80=81=E6=9F=A5=E7=9C=8B=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8=E5=A4=96=E7=BD=91MAC=20Address?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/ExamAuthorizeController.java | 46 ++++++++++++ .../exam/controller/admin/ExamController.java | 14 ---- .../authorize/ConnectionStrengthEnums.java | 16 ++++ .../service/authorize/AuthorizeService.java | 28 +++++++ .../authorize/AuthorizeServiceImpl.java | 74 +++++++++++++++++++ 5 files changed, 164 insertions(+), 14 deletions(-) create mode 100644 exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/controller/admin/ExamAuthorizeController.java delete mode 100644 exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/controller/admin/ExamController.java create mode 100644 exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/controller/admin/enums/authorize/ConnectionStrengthEnums.java create mode 100644 exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/service/authorize/AuthorizeService.java create mode 100644 exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/service/authorize/AuthorizeServiceImpl.java 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 new file mode 100644 index 00000000..4f1b728f --- /dev/null +++ b/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/controller/admin/ExamAuthorizeController.java @@ -0,0 +1,46 @@ +package pc.exam.pp.module.exam.controller.admin; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import jakarta.annotation.Resource; +import jakarta.annotation.security.PermitAll; +import org.springframework.web.bind.annotation.*; +import pc.exam.pp.framework.common.pojo.CommonResult; +import pc.exam.pp.module.exam.service.authorize.AuthorizeService; + +import java.net.SocketException; +import java.net.UnknownHostException; + +import static pc.exam.pp.framework.common.pojo.CommonResult.success; + +/** + * 管理后台 - 授权信息 + * + * @author pengchen + */ +@RestController +@RequestMapping("/exam/authorize") +public class ExamAuthorizeController { + + @Resource + private AuthorizeService authorizeService; + + @GetMapping("/get-ip-status") + @Operation(summary = "使用租户域名,查看租户连接状态", description = "租户管理界面,使用租户域名,查看租户连接状态") + @Parameter(name = "name", description = "租户域名", required = true, example = "1024") + public CommonResult getTenantIdByStatus(@RequestParam("ip") String ip) { + // 使用传入的IP,进行ping,查看是否存在连接,并返回信号的强度 + return success(authorizeService.getTenantIdByStatus(ip)); + } + + @GetMapping("/get-mac-address") + @PermitAll + @Operation(summary = "查看运行服务器外网MAC Address", description = "初次激活租户,查看租户电脑外网MAC Address") + @Parameter(description = "MAC Address", required = true, example = "1024") + public CommonResult getTenantMacAddress() throws SocketException, UnknownHostException { + // 使用传入的IP,进行ping,查看是否存在连接,并返回信号的强度 + return success(authorizeService.getTenantMacAddress()); + } + + +} diff --git a/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/controller/admin/ExamController.java b/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/controller/admin/ExamController.java deleted file mode 100644 index 04299bae..00000000 --- a/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/controller/admin/ExamController.java +++ /dev/null @@ -1,14 +0,0 @@ -package pc.exam.pp.module.exam.controller.admin; - -import org.springframework.web.bind.annotation.*; - -/** - * 管理后台 - 授权信息 - * - * @author pengchen - */ -@RestController -@RequestMapping("/exam/authorize") -public class ExamController { - -} diff --git a/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/controller/admin/enums/authorize/ConnectionStrengthEnums.java b/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/controller/admin/enums/authorize/ConnectionStrengthEnums.java new file mode 100644 index 00000000..6250073a --- /dev/null +++ b/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/controller/admin/enums/authorize/ConnectionStrengthEnums.java @@ -0,0 +1,16 @@ +package pc.exam.pp.module.exam.controller.admin.enums.authorize; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public enum ConnectionStrengthEnums { + + STRONG("强"), + WEAK("弱"), + NOT_CONNECTED("微弱或未连接"); + + private final String description; + +} diff --git a/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/service/authorize/AuthorizeService.java b/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/service/authorize/AuthorizeService.java new file mode 100644 index 00000000..9ece7543 --- /dev/null +++ b/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/service/authorize/AuthorizeService.java @@ -0,0 +1,28 @@ +package pc.exam.pp.module.exam.service.authorize; + + +import java.net.SocketException; +import java.net.UnknownHostException; + +/** + * 管理后台的授权权限 Service 接口 + * 提供租户的授权码,网络通信 + * + * @author r w b + */ +public interface AuthorizeService { + + /** + * 使用租户域名,查看租户连接状态 + * @param ip 域名或者IP + * @return 信号强度 + */ + public String getTenantIdByStatus(String ip); + + + /** + * 调用当前服务查看运行服务环境MAC地址 + * @return MAC Address + */ + public String getTenantMacAddress() throws SocketException, UnknownHostException; +} 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 new file mode 100644 index 00000000..face10c0 --- /dev/null +++ b/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/service/authorize/AuthorizeServiceImpl.java @@ -0,0 +1,74 @@ +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.net.*; + +/** + * 管理后台的授权权限 Service 接口实现类 + * 提供租户的授权码,网络通信 + * + * @author r w b + */ +@Service +public class AuthorizeServiceImpl implements AuthorizeService { + + /** + * 使用租户域名,查看租户连接状态 + * @param ip 域名或者IP + * @return 信号强度 + */ + @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; + + if (duration < 100) { + return ConnectionStrengthEnums.STRONG.getDescription(); + } else if (duration < 500) { + return ConnectionStrengthEnums.WEAK.getDescription(); + } else { + return ConnectionStrengthEnums.NOT_CONNECTED.getDescription(); + } + } catch (Exception e) { + return ConnectionStrengthEnums.NOT_CONNECTED.getDescription(); + } + } + + /** + * 调用当前服务查看运行服务环境MAC地址 + * @return MAC Address + */ + @Override + public String getTenantMacAddress() throws SocketException, UnknownHostException { + // 使用 UDP socket,不需要真的连接成功 + DatagramSocket ds = new DatagramSocket(); + ds.connect(InetAddress.getByName("www.baidu.com"), 80); // 百度DNS IP + + InetAddress localAddress = ds.getLocalAddress(); + System.out.println("本地出口IP: " + localAddress.getHostAddress()); + + // 获取这个 IP 所在的接口 + NetworkInterface network = NetworkInterface.getByInetAddress(localAddress); + if (network == null) { + return "找不到对应的网络接口"; + } + + System.out.println("接口名称: " + network.getName() + " (" + network.getDisplayName() + ")"); + + byte[] mac = network.getHardwareAddress(); + if (mac == null) { + return "无法获取 MAC 地址"; + } + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < mac.length; i++) { + sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "")); + } + return sb.toString(); + } +}