diff --git a/main/manager-api/src/main/java/xiaozhi/common/config/RestTemplateConfig.java b/main/manager-api/src/main/java/xiaozhi/common/config/RestTemplateConfig.java new file mode 100644 index 000000000..ecfaa3597 --- /dev/null +++ b/main/manager-api/src/main/java/xiaozhi/common/config/RestTemplateConfig.java @@ -0,0 +1,17 @@ +package xiaozhi.common.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +/** + * RestTemplate配置 + */ +@Configuration +public class RestTemplateConfig { + + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } +} \ No newline at end of file diff --git a/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java b/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java index 47d45e7ed..1edea3fd9 100644 --- a/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java +++ b/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java @@ -89,6 +89,11 @@ public interface Constant { */ String SERVER_WEBSOCKET = "server.websocket"; + /** + * ota地址 + */ + String SERVER_OTA = "server.ota"; + /** * 是否允许用户注册 */ diff --git a/main/manager-api/src/main/java/xiaozhi/modules/device/service/impl/DeviceServiceImpl.java b/main/manager-api/src/main/java/xiaozhi/modules/device/service/impl/DeviceServiceImpl.java index 2a4517338..86afa7c06 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/device/service/impl/DeviceServiceImpl.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/device/service/impl/DeviceServiceImpl.java @@ -132,7 +132,7 @@ public class DeviceServiceImpl extends BaseServiceImpl // 从系统参数获取WebSocket URL,如果未配置则使用默认值 String wsUrl = sysParamsService.getValue(Constant.SERVER_WEBSOCKET, true); if (StringUtils.isBlank(wsUrl) || wsUrl.equals("null")) { - log.error("WebSocket URL is not configured"); + log.error("WebSocket地址未配置,请登录智控台,在参数管理找到【server.websocket】配置"); wsUrl = "ws://xiaozhi.server.com:8000/xiaozhi/v1/"; websocket.setUrl(wsUrl); } else { @@ -141,7 +141,7 @@ public class DeviceServiceImpl extends BaseServiceImpl // 随机选择一个WebSocket URL websocket.setUrl(wsUrls[RandomUtil.randomInt(0, wsUrls.length)]); } else { - log.error("WebSocket URL list is empty"); + log.error("WebSocket地址未配置,请登录智控台,在参数管理找到【server.websocket】配置"); websocket.setUrl("ws://xiaozhi.server.com:8000/xiaozhi/v1/"); } } @@ -311,14 +311,19 @@ public class DeviceServiceImpl extends BaseServiceImpl String downloadUrl = null; if (ota != null) { - // 获取当前请求的URL - HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) - .getRequest(); - String requestUrl = request.getRequestURL().toString(); + + String otaUrl = sysParamsService.getValue(Constant.SERVER_OTA, true); + if (StringUtils.isBlank(otaUrl) || otaUrl.equals("null")) { + log.error("OTA地址未配置,请登录智控台,在参数管理找到【server.ota】配置"); + // 尝试从请求中获取 + HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) + .getRequest(); + otaUrl = request.getRequestURL().toString(); + } // 将URL中的/ota/替换为/otaMag/download/ String uuid = UUID.randomUUID().toString(); redisUtils.set(RedisKeys.getOtaIdKey(uuid), ota.getId()); - downloadUrl = requestUrl.replace("/ota/", "/otaMag/download/") + uuid; + downloadUrl = otaUrl.replace("/ota/", "/otaMag/download/") + uuid; } firmware.setVersion(ota == null ? null : ota.getVersion()); diff --git a/main/manager-api/src/main/java/xiaozhi/modules/sys/controller/SysParamsController.java b/main/manager-api/src/main/java/xiaozhi/modules/sys/controller/SysParamsController.java index 157eb94eb..9b80c76c8 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/sys/controller/SysParamsController.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/sys/controller/SysParamsController.java @@ -4,6 +4,8 @@ import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.authz.annotation.RequiresPermissions; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -12,6 +14,7 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; @@ -47,6 +50,7 @@ import xiaozhi.modules.sys.utils.WebSocketValidator; public class SysParamsController { private final SysParamsService sysParamsService; private final ConfigService configService; + private final RestTemplate restTemplate; @GetMapping("page") @Operation(summary = "分页") @@ -97,6 +101,9 @@ public class SysParamsController { // 验证WebSocket地址列表 validateWebSocketUrls(dto.getParamCode(), dto.getParamValue()); + // 验证OTA地址 + validateOtaUrl(dto.getParamCode(), dto.getParamValue()); + sysParamsService.update(dto); configService.getConfig(false); return new Result(); @@ -118,6 +125,11 @@ public class SysParamsController { } for (String url : wsUrls) { if (StringUtils.isNotBlank(url)) { + // 检查是否包含localhost或127.0.0.1 + if (url.contains("localhost") || url.contains("127.0.0.1")) { + throw new RenException("WebSocket地址不能使用localhost或127.0.0.1"); + } + // 验证WebSocket地址格式 if (!WebSocketValidator.validateUrlFormat(url)) { throw new RenException("WebSocket地址格式不正确: " + url); @@ -131,6 +143,46 @@ public class SysParamsController { } } + /** + * 验证OTA地址 + */ + private void validateOtaUrl(String paramCode, String url) { + if (!paramCode.equals(Constant.SERVER_OTA)) { + return; + } + if (StringUtils.isBlank(url) || url.equals("null")) { + throw new RenException("OTA地址不能为空"); + } + + // 检查是否包含localhost或127.0.0.1 + if (url.contains("localhost") || url.contains("127.0.0.1")) { + throw new RenException("OTA地址不能使用localhost或127.0.0.1"); + } + + // 验证URL格式 + if (!url.toLowerCase().startsWith("http")) { + throw new RenException("OTA地址必须以http或https开头"); + } + if (!url.endsWith("/ota/")) { + throw new RenException("OTA地址必须以/ota/结尾"); + } + + try { + // 发送GET请求 + ResponseEntity response = restTemplate.getForEntity(url, String.class); + if (response.getStatusCode() != HttpStatus.OK) { + throw new RenException("OTA接口访问失败,状态码:" + response.getStatusCode()); + } + // 检查响应内容是否包含OTA相关信息 + String body = response.getBody(); + if (body == null || !body.contains("OTA")) { + throw new RenException("OTA接口返回内容格式不正确,可能不是一个真实的OTA接口"); + } + } catch (Exception e) { + throw new RenException("OTA接口验证失败:" + e.getMessage()); + } + } + @PostMapping("/delete") @Operation(summary = "删除") @LogOperation("删除")