【修改】验证域名或者IP是否与服务器进行通讯
This commit is contained in:
@@ -25,12 +25,12 @@ public class ExamAuthorizeController {
|
|||||||
@Resource
|
@Resource
|
||||||
private AuthorizeService authorizeService;
|
private AuthorizeService authorizeService;
|
||||||
|
|
||||||
@GetMapping("/get-ip-status")
|
@GetMapping("/get-ip-status/{website}")
|
||||||
@Operation(summary = "使用租户域名,查看租户连接状态", description = "租户管理界面,使用租户域名,查看租户连接状态")
|
@Operation(summary = "使用租户域名,查看租户连接状态", description = "租户管理界面,使用租户域名,查看租户连接状态")
|
||||||
@Parameter(name = "name", description = "租户域名", required = true, example = "1024")
|
@Parameter(name = "website", description = "租户域名", required = true, example = "1024")
|
||||||
public CommonResult<String> getTenantIdByStatus(@RequestParam("ip") String ip) {
|
public CommonResult<String> getTenantIdByStatus(@PathVariable String website) {
|
||||||
// 使用传入的IP,进行ping,查看是否存在连接,并返回信号的强度
|
// 使用传入的IP,进行ping,查看是否存在连接,并返回信号的强度
|
||||||
return success(authorizeService.getTenantIdByStatus(ip));
|
return success(authorizeService.getTenantIdByStatus(website));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/get-mac-address")
|
@GetMapping("/get-mac-address")
|
||||||
|
@@ -3,6 +3,8 @@ package pc.exam.pp.module.exam.service.authorize;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import pc.exam.pp.module.exam.controller.admin.enums.authorize.ConnectionStrengthEnums;
|
import pc.exam.pp.module.exam.controller.admin.enums.authorize.ConnectionStrengthEnums;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -21,25 +23,43 @@ public class AuthorizeServiceImpl implements AuthorizeService {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String getTenantIdByStatus(String ip) {
|
public String getTenantIdByStatus(String ip) {
|
||||||
int timeout = 3000;
|
String[] schemes = {"https://", "http://"};
|
||||||
long startTime = System.currentTimeMillis();
|
int timeout = 3;
|
||||||
try (Socket socket = new Socket()) {
|
|
||||||
socket.connect(new InetSocketAddress(ip, 80), timeout);
|
|
||||||
long endTime = System.currentTimeMillis();
|
|
||||||
long duration = endTime - startTime;
|
|
||||||
|
|
||||||
if (duration < 100) {
|
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();
|
return ConnectionStrengthEnums.STRONG.getDescription();
|
||||||
} else if (duration < 500) {
|
} else if (millis < 500) {
|
||||||
return ConnectionStrengthEnums.WEAK.getDescription();
|
return ConnectionStrengthEnums.WEAK.getDescription();
|
||||||
} else {
|
} else {
|
||||||
return ConnectionStrengthEnums.NOT_CONNECTED.getDescription();
|
return ConnectionStrengthEnums.NOT_CONNECTED.getDescription();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
return ConnectionStrengthEnums.NOT_CONNECTED.getDescription();
|
} catch (Exception ignored) {
|
||||||
|
// 尝试下一个
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ConnectionStrengthEnums.NOT_CONNECTED.getDescription(); // 两种协议都失败
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 调用当前服务查看运行服务环境MAC地址
|
* 调用当前服务查看运行服务环境MAC地址
|
||||||
* @return MAC Address
|
* @return MAC Address
|
||||||
|
Reference in New Issue
Block a user