diff --git a/.env.example b/.env.example index b8fffd45..0f500647 100644 --- a/.env.example +++ b/.env.example @@ -6,6 +6,8 @@ SPRING_PROFILES_ACTIVE=prod DEMO_MODE=false API_CORS=true +API_HOST=0.0.0.0 +# API_URL=http://127.0.0.1:9700/orion-visor/api API_IP_HEADERS=X-Forwarded-For,X-Real-IP API_EXPOSE_TOKEN=pmqeHOyZaumHm0Wt SECRET_KEY=uQeacXV8b3isvKLK diff --git a/docker-compose.yaml b/docker-compose.yaml index 74c07f87..53e3fc5d 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -50,6 +50,7 @@ services: SECRET_KEY: ${SECRET_KEY:-uQeacXV8b3isvKLK} API_EXPOSE_TOKEN: ${API_EXPOSE_TOKEN:-pmqeHOyZaumHm0Wt} API_IP_HEADERS: ${API_IP_HEADERS:-X-Forwarded-For,X-Real-IP} + API_HOST: ${API_HOST:-0.0.0.0} API_CORS: ${API_CORS:-true} DEMO_MODE: ${DEMO_MODE:-false} volumes: diff --git a/orion-visor-common/src/main/java/org/dromara/visor/common/constant/Const.java b/orion-visor-common/src/main/java/org/dromara/visor/common/constant/Const.java index e0b5ba56..633f86b6 100644 --- a/orion-visor-common/src/main/java/org/dromara/visor/common/constant/Const.java +++ b/orion-visor-common/src/main/java/org/dromara/visor/common/constant/Const.java @@ -55,4 +55,6 @@ public interface Const extends cn.orionsec.kit.lang.constant.Const, FieldConst, int BATCH_COUNT = 500; + String IP_0000 = "0.0.0.0"; + } diff --git a/orion-visor-framework/orion-visor-spring-boot-starter-web/src/main/java/org/dromara/visor/framework/web/configuration/OrionWebAutoConfiguration.java b/orion-visor-framework/orion-visor-spring-boot-starter-web/src/main/java/org/dromara/visor/framework/web/configuration/OrionWebAutoConfiguration.java index 6e89fe0e..581b348d 100644 --- a/orion-visor-framework/orion-visor-spring-boot-starter-web/src/main/java/org/dromara/visor/framework/web/configuration/OrionWebAutoConfiguration.java +++ b/orion-visor-framework/orion-visor-spring-boot-starter-web/src/main/java/org/dromara/visor/framework/web/configuration/OrionWebAutoConfiguration.java @@ -29,6 +29,7 @@ import org.dromara.visor.common.constant.AutoConfigureOrderConst; import org.dromara.visor.common.constant.FilterOrderConst; import org.dromara.visor.common.web.WebFilterCreator; import org.dromara.visor.framework.web.configuration.config.ExposeApiConfig; +import org.dromara.visor.framework.web.configuration.config.OrionApiConfig; import org.dromara.visor.framework.web.core.aspect.DemoDisableApiAspect; import org.dromara.visor.framework.web.core.aspect.ExposeApiAspect; import org.dromara.visor.framework.web.core.filter.TraceIdFilter; @@ -71,7 +72,7 @@ import java.util.List; @DependsOn({"executorContext"}) @AutoConfiguration @AutoConfigureOrder(AutoConfigureOrderConst.FRAMEWORK_WEB) -@EnableConfigurationProperties(ExposeApiConfig.class) +@EnableConfigurationProperties({ExposeApiConfig.class, OrionApiConfig.class}) public class OrionWebAutoConfiguration implements WebMvcConfigurer { @Value("${orion.prefix}") diff --git a/orion-visor-framework/orion-visor-spring-boot-starter-web/src/main/java/org/dromara/visor/framework/web/configuration/config/ExposeApiConfig.java b/orion-visor-framework/orion-visor-spring-boot-starter-web/src/main/java/org/dromara/visor/framework/web/configuration/config/ExposeApiConfig.java index 49e39feb..27d80d9e 100644 --- a/orion-visor-framework/orion-visor-spring-boot-starter-web/src/main/java/org/dromara/visor/framework/web/configuration/config/ExposeApiConfig.java +++ b/orion-visor-framework/orion-visor-spring-boot-starter-web/src/main/java/org/dromara/visor/framework/web/configuration/config/ExposeApiConfig.java @@ -36,6 +36,11 @@ import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties("orion.api.expose") public class ExposeApiConfig { + /** + * 对外服务地址 + */ + private String host; + /** * 对外服务请求头 */ diff --git a/orion-visor-framework/orion-visor-spring-boot-starter-web/src/main/java/org/dromara/visor/framework/web/configuration/config/OrionApiConfig.java b/orion-visor-framework/orion-visor-spring-boot-starter-web/src/main/java/org/dromara/visor/framework/web/configuration/config/OrionApiConfig.java new file mode 100644 index 00000000..bfbe94e1 --- /dev/null +++ b/orion-visor-framework/orion-visor-spring-boot-starter-web/src/main/java/org/dromara/visor/framework/web/configuration/config/OrionApiConfig.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2023 - present Dromara, All rights reserved. + * + * https://visor.dromara.org + * https://visor.dromara.org.cn + * https://visor.orionsec.cn + * + * Members: + * Jiahang Li - ljh1553488six@139.com - author + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.dromara.visor.framework.web.configuration.config; + +import cn.orionsec.kit.lang.utils.Strings; +import cn.orionsec.kit.lang.utils.net.IPs; +import lombok.Data; +import org.dromara.visor.common.constant.Const; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * api 配置属性 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2025/12/8 14:00 + */ +@Data +@ConfigurationProperties("orion.api") +public class OrionApiConfig { + + private static final String URL_TEMPLATE = "http://{}:{}{}"; + + /** + * 公共 api 前缀 + */ + private String prefix; + + /** + * 服务端主机地址 + */ + private String host; + + /** + * 服务端口 + */ + @Value("${server.port}") + private Integer port; + + /** + * 服务端 url + */ + private String url; + + public String getHost() { + if (Const.IP_0000.equalsIgnoreCase(host)) { + // 本机 + return IPs.IP; + } else { + return host; + } + } + + public String getUrl() { + if (!Strings.isBlank(url)) { + return url; + } + // 构建 + return Strings.format(URL_TEMPLATE, this.getHost(), port, prefix); + } + +} diff --git a/orion-visor-framework/orion-visor-spring-boot-starter-web/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/orion-visor-framework/orion-visor-spring-boot-starter-web/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 083938bf..82962d09 100644 --- a/orion-visor-framework/orion-visor-spring-boot-starter-web/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/orion-visor-framework/orion-visor-spring-boot-starter-web/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -33,6 +33,16 @@ "type": "java.lang.Boolean", "description": "是否开启 cors 过滤器." }, + { + "name": "orion.api.host", + "type": "java.lang.String", + "description": "服务端主机地址." + }, + { + "name": "orion.api.url", + "type": "java.lang.String", + "description": "服务端接口地址." + }, { "name": "orion.api.ip-headers", "type": "java.lang.String", diff --git a/orion-visor-launch/src/main/resources/application-prod.yaml b/orion-visor-launch/src/main/resources/application-prod.yaml index d9d0cfdf..6e2b95a4 100644 --- a/orion-visor-launch/src/main/resources/application-prod.yaml +++ b/orion-visor-launch/src/main/resources/application-prod.yaml @@ -74,6 +74,10 @@ orion: api: # 是否允许跨域 cors: ${API_CORS:true} + # 服务端主机地址 + host: ${API_HOST:0.0.0.0} + # 服务端接口地址 默认自动生成 + url: ${API_URL:} # 获取 IP 的请求头 ip-headers: ${API_IP_HEADERS:X-Forwarded-For,X-Real-IP} # 对外服务 diff --git a/orion-visor-launch/src/main/resources/application.yaml b/orion-visor-launch/src/main/resources/application.yaml index 52137bb7..5001f3e4 100644 --- a/orion-visor-launch/src/main/resources/application.yaml +++ b/orion-visor-launch/src/main/resources/application.yaml @@ -175,6 +175,10 @@ orion: prefix: ${orion.prefix}/api # 是否允许跨域 cors: true + # 服务端主机地址 + host: 0.0.0.0 + # 服务端接口地址 默认自动生成 + url: # 获取 IP 的请求头 ip-headers: X-Forwarded-For,X-Real-IP # 对外服务 diff --git a/orion-visor-modules/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/service/impl/HostAgentServiceImpl.java b/orion-visor-modules/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/service/impl/HostAgentServiceImpl.java index c77941f6..e281abbd 100644 --- a/orion-visor-modules/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/service/impl/HostAgentServiceImpl.java +++ b/orion-visor-modules/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/service/impl/HostAgentServiceImpl.java @@ -31,7 +31,6 @@ import cn.orionsec.kit.lang.utils.io.FileReaders; import cn.orionsec.kit.lang.utils.io.Files1; import cn.orionsec.kit.lang.utils.io.compress.CompressTypeEnum; import cn.orionsec.kit.lang.utils.io.compress.FileDecompressor; -import cn.orionsec.kit.lang.utils.net.IPs; import lombok.extern.slf4j.Slf4j; import org.dromara.visor.common.constant.Const; import org.dromara.visor.common.constant.ErrorMessage; @@ -41,6 +40,8 @@ import org.dromara.visor.common.utils.Assert; import org.dromara.visor.common.utils.PathUtils; import org.dromara.visor.framework.biz.operator.log.core.utils.OperatorLogs; import org.dromara.visor.framework.redis.core.utils.RedisStrings; +import org.dromara.visor.framework.web.configuration.config.ExposeApiConfig; +import org.dromara.visor.framework.web.configuration.config.OrionApiConfig; import org.dromara.visor.module.asset.convert.HostConvert; import org.dromara.visor.module.asset.dao.HostAgentLogDAO; import org.dromara.visor.module.asset.dao.HostDAO; @@ -53,7 +54,6 @@ import org.dromara.visor.module.asset.enums.*; import org.dromara.visor.module.asset.handler.agent.intstall.AgentInstaller; import org.dromara.visor.module.asset.handler.agent.model.AgentInstallParams; import org.dromara.visor.module.asset.service.HostAgentService; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -77,8 +77,11 @@ public class HostAgentServiceImpl implements HostAgentService { private String localVersion; - @Value("${orion.api.expose.token}") - private String exposeToken; + @Resource + private OrionApiConfig orionApiConfig; + + @Resource + private ExposeApiConfig exposeApiConfig; @Resource private HostDAO hostDAO; @@ -302,8 +305,8 @@ public class HostAgentServiceImpl implements HostAgentService { */ private Map getReplaceVars() { Map map = new HashMap<>(); - map.put("SERVER_HOST", IPs.IP); - map.put("SERVER_TOKEN", exposeToken); + map.put("SERVER_URL", orionApiConfig.getUrl()); + map.put("SERVER_TOKEN", exposeApiConfig.getToken()); return map; }