mirror of
				https://github.com/TeaOSLab/EdgeAdmin.git
				synced 2025-11-04 05:00:25 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			109 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			109 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package ipadmin
 | 
						|
 | 
						|
import (
 | 
						|
	"encoding/json"
 | 
						|
	"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
 | 
						|
	"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
 | 
						|
}) {
 | 
						|
	// 日志
 | 
						|
	defer this.CreateLog(oplogs.LevelInfo, "WAF策略 %d 设置禁止访问的国家和地区", params.FirewallPolicyId)
 | 
						|
 | 
						|
	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()
 | 
						|
}
 |