diff --git a/src/main/java/com/cool/modules/agent/controller/app/AppAgentInfoController.java b/src/main/java/com/cool/modules/agent/controller/app/AppAgentInfoController.java index 35b491a..3f3c8da 100644 --- a/src/main/java/com/cool/modules/agent/controller/app/AppAgentInfoController.java +++ b/src/main/java/com/cool/modules/agent/controller/app/AppAgentInfoController.java @@ -3,24 +3,32 @@ package com.cool.modules.agent.controller.app; import cn.hutool.json.JSONObject; import com.cool.core.annotation.CoolRestController; import com.cool.core.request.R; +import com.cool.modules.agent.entity.AgentWhitelistEntity; import com.cool.modules.agent.service.AgentInfoService; +import com.cool.modules.agent.service.AgentWhitelistService; +import com.mybatisflex.core.query.QueryWrapper; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestAttribute; +import static com.cool.modules.agent.entity.table.AgentWhitelistEntityTableDef.AGENT_WHITELIST_ENTITY; + @RequiredArgsConstructor @Tag(name = "代理", description = "App端代理") -@CoolRestController +@CoolRestController(api = {"list"}) public class AppAgentInfoController { private final AgentInfoService agentInfoService; + private final AgentWhitelistService agentWhitelistService; @Operation(summary = "代理列表") @GetMapping("/list") public R list(@RequestAttribute JSONObject requestParams) { - return R.ok(agentInfoService.list(requestParams, null)); + return R.ok(agentInfoService.list(requestParams, QueryWrapper.create())); } @Operation(summary = "代理详情") @@ -29,4 +37,45 @@ public class AppAgentInfoController { Long id = requestParams.getLong("id"); return R.ok(agentInfoService.info(id)); } + + @Operation(summary = "获取当前IP") + @GetMapping("/currentIp") + public R currentIp(HttpServletRequest request) { + String ip = request.getHeader("X-Forwarded-For"); + if (ip == null || ip.isEmpty()) { + ip = request.getHeader("X-Real-IP"); + } + if (ip == null || ip.isEmpty()) { + ip = request.getRemoteAddr(); + } + if (ip != null && ip.contains(",")) { + ip = ip.split(",")[0].trim(); + } + JSONObject result = new JSONObject(); + result.set("ip", ip); + return R.ok(result); + } + + @Operation(summary = "添加白名单IP") + @PostMapping("/addWhitelist") + public R addWhitelist(@RequestAttribute JSONObject requestParams) { + Long agentInfoId = requestParams.getLong("agentInfoId"); + String ipAddress = requestParams.getStr("ipAddress"); + + AgentWhitelistEntity entity = new AgentWhitelistEntity(); + entity.setAgentInfoId(agentInfoId); + entity.setIpAddress(ipAddress); + agentWhitelistService.save(entity); + + return R.ok(entity); + } + + @Operation(summary = "获取白名单列表") + @GetMapping("/whitelist") + public R whitelist(@RequestAttribute JSONObject requestParams) { + Long agentInfoId = requestParams.getLong("agentInfoId"); + QueryWrapper qw = QueryWrapper.create() + .where(AGENT_WHITELIST_ENTITY.AGENT_INFO_ID.eq(agentInfoId)); + return R.ok(agentWhitelistService.list(requestParams, qw)); + } }