Administrator
5 hours ago f1cf8741bad27ff99e644ad9cfa5458c78d79501
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package cc.mrbird.febs.firewall.controller;
 
import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.firewall.dto.CountryVo;
import cc.mrbird.febs.firewall.dto.SaveCountryRuleDto;
import cc.mrbird.febs.firewall.entity.FirewallCountryRule;
import cc.mrbird.febs.firewall.entity.FirewallSite;
import cc.mrbird.febs.firewall.mapper.FirewallSiteMapper;
import cc.mrbird.febs.firewall.service.IFirewallRuleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 防火墙管理控制器
 * <p>
 * 提供站点、国家白名单的完整 CRUD + Nginx配置生成 + reload 等管理接口
 *
 * @author auto-generated
 * @date 2026-07-31
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/admin/firewall")
public class AdminFirewallController extends BaseController {
 
    private final IFirewallRuleService firewallRuleService;
    private final FirewallSiteMapper firewallSiteMapper;
 
    // ==================== 站点数据表 CRUD ====================
 
    /**
     * 站点分页列表
     */
    @GetMapping("/siteList")
    public FebsResponse siteList(FirewallSite site, QueryRequest request) {
        Map<String, Object> data = getDataTable(firewallRuleService.siteList(site, request));
        return new FebsResponse().success().data(data);
    }
 
    /**
     * 保存/更新站点
     */
    @PostMapping("/site/save")
    @ControllerEndpoint(operation = "保存防火墙站点")
    public FebsResponse saveSite(@Validated FirewallSite site) {
        return firewallRuleService.saveSite(site);
    }
 
    /**
     * 删除站点
     */
    @GetMapping("/site/delete/{id}")
    @ControllerEndpoint(operation = "删除防火墙站点")
    public FebsResponse deleteSite(@PathVariable @NotNull Long id) {
        return firewallRuleService.deleteSite(id);
    }
 
    /**
     * 查询所有启用站点(下拉框用)
     */
    @GetMapping("/sites")
    public FebsResponse listSites() {
        List<FirewallSite> sites = firewallSiteMapper.selectList(
                new LambdaQueryWrapper<FirewallSite>()
                        .eq(FirewallSite::getStatus, 1)
        );
 
        List<Map<String, Object>> result = new java.util.ArrayList<>();
        for (FirewallSite site : sites) {
            Map<String, Object> item = new HashMap<>();
            item.put("id", site.getId());
            item.put("siteName", site.getSiteName());
            item.put("domain", site.getDomain());
            item.put("status", site.getStatus());
            result.add(item);
        }
        return new FebsResponse().success().data(result);
    }
 
    // ==================== 国家规则数据表 CRUD ====================
 
    /**
     * 国家规则分页列表
     */
    @GetMapping("/countryRuleList")
    public FebsResponse countryRuleList(FirewallCountryRule rule, QueryRequest request) {
        Map<String, Object> data = getDataTable(firewallRuleService.countryRuleList(rule, request));
        return new FebsResponse().success().data(data);
    }
 
    /**
     * 保存/更新单个国家规则(保存后自动生成Nginx配置)
     */
    @PostMapping("/countryRule/save")
    @ControllerEndpoint(operation = "保存防火墙国家规则")
    public FebsResponse saveCountryRuleSingle(@Validated FirewallCountryRule rule) {
        return firewallRuleService.saveCountryRuleSingle(rule);
    }
 
    /**
     * 删除单个国家规则(删除后自动生成Nginx配置)
     */
    @GetMapping("/countryRule/delete/{id}")
    @ControllerEndpoint(operation = "删除防火墙国家规则")
    public FebsResponse deleteCountryRule(@PathVariable @NotNull Long id) {
        return firewallRuleService.deleteCountryRule(id);
    }
 
    // ==================== 国家白名单(原有接口) ====================
 
    /**
     * 查询站点允许的国家列表
     */
    @GetMapping("/site/{siteId}/countries")
    public FebsResponse getCountries(@PathVariable @NotNull Long siteId) {
        List<CountryVo> countries = firewallRuleService.getCountries(siteId);
        return new FebsResponse().success().data(countries);
    }
 
    /**
     * 批量保存站点国家白名单规则(交互式checkbox页面用)
     */
    @PostMapping("/site/country/save")
    @ControllerEndpoint(operation = "批量保存防火墙国家白名单")
    public FebsResponse saveCountryRule(@RequestBody @Validated SaveCountryRuleDto dto) {
        firewallRuleService.saveCountryRule(dto.getSiteId(), dto.getCountries());
        return new FebsResponse().success().message("保存成功,Nginx 配置已更新");
    }
 
    // ==================== Nginx Reload ====================
 
    /**
     * 手动触发 Nginx 配置生成 & reload
     */
    @PostMapping("/site/{siteId}/reload")
    @ControllerEndpoint(operation = "手动reload Nginx配置")
    public FebsResponse triggerReload(@PathVariable @NotNull Long siteId) {
        firewallRuleService.triggerNginxReload(siteId);
        return new FebsResponse().success().message("Nginx reload 信号已发送");
    }
}