diff --git a/internal/rpc/rpc_client.go b/internal/rpc/rpc_client.go index eecf546e..eff4702b 100644 --- a/internal/rpc/rpc_client.go +++ b/internal/rpc/rpc_client.go @@ -184,6 +184,14 @@ func (this *RPCClient) FileChunkRPC() pb.FileChunkServiceClient { return pb.NewFileChunkServiceClient(this.pickConn()) } +func (this *RPCClient) RegionCountryRPC() pb.RegionCountryServiceClient { + return pb.NewRegionCountryServiceClient(this.pickConn()) +} + +func (this *RPCClient) RegionProvinceRPC() pb.RegionProvinceServiceClient { + return pb.NewRegionProvinceServiceClient(this.pickConn()) +} + // 构造Admin上下文 func (this *RPCClient) Context(adminId int64) context.Context { ctx := context.Background() diff --git a/internal/web/actions/default/servers/components/waf/init.go b/internal/web/actions/default/servers/components/waf/init.go index 88dd4fc5..04d03efb 100644 --- a/internal/web/actions/default/servers/components/waf/init.go +++ b/internal/web/actions/default/servers/components/waf/init.go @@ -2,6 +2,7 @@ package waf import ( "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/components/componentutils" + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/components/waf/ipadmin" "github.com/TeaOSLab/EdgeAdmin/internal/web/helpers" "github.com/iwind/TeaGo" ) @@ -26,7 +27,6 @@ func init() { GetPost("/import", new(ImportAction)). Post("/updateGroupOn", new(UpdateGroupOnAction)). Post("/deleteGroup", new(DeleteGroupAction)). - GetPost("/ipadmin", new(IpadminAction)). GetPost("/createGroupPopup", new(CreateGroupPopupAction)). Post("/sortGroups", new(SortGroupsAction)). GetPost("/updateGroupPopup", new(UpdateGroupPopupAction)). @@ -36,6 +36,12 @@ func init() { Post("/updateSetOn", new(UpdateSetOnAction)). Post("/deleteSet", new(DeleteSetAction)). GetPost("/updateSetPopup", new(UpdateSetPopupAction)). + + // IP管理 + GetPost("/ipadmin", new(ipadmin.IndexAction)). + GetPost("/ipadmin/provinces", new(ipadmin.ProvincesAction)). + Get("/ipadmin/lists", new(ipadmin.ListsAction)). + EndAll() }) } diff --git a/internal/web/actions/default/servers/components/waf/ipadmin.go b/internal/web/actions/default/servers/components/waf/ipadmin.go deleted file mode 100644 index 9a79e2b9..00000000 --- a/internal/web/actions/default/servers/components/waf/ipadmin.go +++ /dev/null @@ -1,15 +0,0 @@ -package waf - -import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" - -type IpadminAction struct { - actionutils.ParentAction -} - -func (this *IpadminAction) Init() { - this.Nav("", "", "ipadmin") -} - -func (this *IpadminAction) RunGet(params struct{}) { - this.Show() -} diff --git a/internal/web/actions/default/servers/components/waf/ipadmin/index.go b/internal/web/actions/default/servers/components/waf/ipadmin/index.go new file mode 100644 index 00000000..b7d7431f --- /dev/null +++ b/internal/web/actions/default/servers/components/waf/ipadmin/index.go @@ -0,0 +1,104 @@ +package ipadmin + +import ( + "encoding/json" + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeAdmin/internal/web/models" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs" + "github.com/iwind/TeaGo/actions" + "github.com/iwind/TeaGo/lists" + "github.com/iwind/TeaGo/maps" + "strings" +) + +type IndexAction struct { + actionutils.ParentAction +} + +func (this *IndexAction) Init() { + this.Nav("", "", "ipadmin") +} + +func (this *IndexAction) RunGet(params struct { + FirewallPolicyId int64 +}) { + this.Data["subMenuItem"] = "region" + + // 当前选中的地区 + policyConfig, err := models.SharedHTTPFirewallPolicyDAO.FindEnabledPolicyConfig(this.AdminContext(), params.FirewallPolicyId) + if err != nil { + this.ErrorPage(err) + return + } + if policyConfig == nil { + this.NotFound("firewallPolicy", params.FirewallPolicyId) + return + } + selectedCountryIds := []int64{} + if policyConfig.Inbound != nil && policyConfig.Inbound.Region != nil { + selectedCountryIds = policyConfig.Inbound.Region.DenyCountryIds + } + + countriesResp, err := this.RPC().RegionCountryRPC().FindAllEnabledRegionCountries(this.AdminContext(), &pb.FindAllEnabledRegionCountriesRequest{}) + if err != nil { + this.ErrorPage(err) + return + } + countryMaps := []maps.Map{} + for _, country := range countriesResp.Countries { + countryMaps = append(countryMaps, maps.Map{ + "id": country.Id, + "name": country.Name, + "letter": strings.ToUpper(string(country.Pinyin[0][0])), + "isChecked": lists.ContainsInt64(selectedCountryIds, country.Id), + }) + } + this.Data["countries"] = countryMaps + + this.Show() +} + +func (this *IndexAction) RunPost(params struct { + FirewallPolicyId int64 + CountryIds []int64 + + Must *actions.Must +}) { + policyConfig, err := models.SharedHTTPFirewallPolicyDAO.FindEnabledPolicyConfig(this.AdminContext(), params.FirewallPolicyId) + if err != nil { + this.ErrorPage(err) + return + } + if policyConfig == nil { + this.NotFound("firewallPolicy", params.FirewallPolicyId) + return + } + + if policyConfig.Inbound == nil { + policyConfig.Inbound = &firewallconfigs.HTTPFirewallInboundConfig{IsOn: true} + } + if policyConfig.Inbound.Region == nil { + policyConfig.Inbound.Region = &firewallconfigs.HTTPFirewallRegionConfig{ + IsOn: true, + } + } + policyConfig.Inbound.Region.DenyCountryIds = params.CountryIds + + inboundJSON, err := json.Marshal(policyConfig.Inbound) + if err != nil { + this.ErrorPage(err) + return + } + + _, err = this.RPC().HTTPFirewallPolicyRPC().UpdateHTTPFirewallInboundConfig(this.AdminContext(), &pb.UpdateHTTPFirewallInboundConfigRequest{ + FirewallPolicyId: params.FirewallPolicyId, + InboundJSON: inboundJSON, + }) + if err != nil { + this.ErrorPage(err) + return + } + + this.Success() +} diff --git a/internal/web/actions/default/servers/components/waf/ipadmin/lists.go b/internal/web/actions/default/servers/components/waf/ipadmin/lists.go new file mode 100644 index 00000000..a67633c3 --- /dev/null +++ b/internal/web/actions/default/servers/components/waf/ipadmin/lists.go @@ -0,0 +1,17 @@ +package ipadmin + +import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + +type ListsAction struct { + actionutils.ParentAction +} + +func (this *ListsAction) Init() { + this.Nav("", "", "ipadmin") +} + +func (this *ListsAction) RunGet(params struct{}) { + this.Data["subMenuItem"] = "list" + + this.Show() +} diff --git a/internal/web/actions/default/servers/components/waf/ipadmin/provinces.go b/internal/web/actions/default/servers/components/waf/ipadmin/provinces.go new file mode 100644 index 00000000..008398db --- /dev/null +++ b/internal/web/actions/default/servers/components/waf/ipadmin/provinces.go @@ -0,0 +1,16 @@ +package ipadmin + +import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + +type ProvincesAction struct { + actionutils.ParentAction +} + +func (this *ProvincesAction) Init() { + this.Nav("", "", "ipadmin") +} + +func (this *ProvincesAction) RunGet(params struct{}) { + this.Data["subMenuItem"] = "province" + this.Show() +} diff --git a/web/views/@default/servers/components/waf/ipadmin.html b/web/views/@default/servers/components/waf/ipadmin.html deleted file mode 100644 index 5bb825ea..00000000 --- a/web/views/@default/servers/components/waf/ipadmin.html +++ /dev/null @@ -1,8 +0,0 @@ -{$layout} -{$template "/left_menu"} - -