【新增】新增使用租户域名,查看租户连接状态、查看运行服务器外网MAC Address
This commit is contained in:
@@ -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<String> 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<String> getTenantMacAddress() throws SocketException, UnknownHostException {
|
||||
// 使用传入的IP,进行ping,查看是否存在连接,并返回信号的强度
|
||||
return success(authorizeService.getTenantMacAddress());
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -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 {
|
||||
|
||||
}
|
@@ -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;
|
||||
|
||||
}
|
@@ -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;
|
||||
}
|
@@ -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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user