【修改】验证域名或者IP是否与服务器进行通讯
This commit is contained in:
@@ -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<String> getTenantIdByStatus(@RequestParam("ip") String ip) {
|
||||
@Parameter(name = "website", description = "租户域名", required = true, example = "1024")
|
||||
public CommonResult<String> getTenantIdByStatus(@PathVariable String website) {
|
||||
// 使用传入的IP,进行ping,查看是否存在连接,并返回信号的强度
|
||||
return success(authorizeService.getTenantIdByStatus(ip));
|
||||
return success(authorizeService.getTenantIdByStatus(website));
|
||||
}
|
||||
|
||||
@GetMapping("/get-mac-address")
|
||||
|
@@ -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(); // 两种协议都失败
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user