实现防火墙配置

This commit is contained in:
刘祥超
2020-09-20 20:12:43 +08:00
parent 27b51edc05
commit f9da886402
7 changed files with 132 additions and 4 deletions

View File

@@ -34,6 +34,7 @@ type RPCClient struct {
httpPageClients []pb.HTTPPageServiceClient httpPageClients []pb.HTTPPageServiceClient
httpAccessLogPolicyClients []pb.HTTPAccessLogPolicyServiceClient httpAccessLogPolicyClients []pb.HTTPAccessLogPolicyServiceClient
httpCachePolicyClients []pb.HTTPCachePolicyServiceClient httpCachePolicyClients []pb.HTTPCachePolicyServiceClient
httpFirewallPolicyClients []pb.HTTPFirewallPolicyServiceClient
} }
func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) { func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
@@ -57,6 +58,7 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
httpPageClients := []pb.HTTPPageServiceClient{} httpPageClients := []pb.HTTPPageServiceClient{}
httpAccessLogPolicyClients := []pb.HTTPAccessLogPolicyServiceClient{} httpAccessLogPolicyClients := []pb.HTTPAccessLogPolicyServiceClient{}
httpCachePolicyClients := []pb.HTTPCachePolicyServiceClient{} httpCachePolicyClients := []pb.HTTPCachePolicyServiceClient{}
httpFirewallPolicyClients := []pb.HTTPFirewallPolicyServiceClient{}
conns := []*grpc.ClientConn{} conns := []*grpc.ClientConn{}
for _, endpoint := range apiConfig.RPC.Endpoints { for _, endpoint := range apiConfig.RPC.Endpoints {
@@ -88,6 +90,7 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
httpPageClients = append(httpPageClients, pb.NewHTTPPageServiceClient(conn)) httpPageClients = append(httpPageClients, pb.NewHTTPPageServiceClient(conn))
httpAccessLogPolicyClients = append(httpAccessLogPolicyClients, pb.NewHTTPAccessLogPolicyServiceClient(conn)) httpAccessLogPolicyClients = append(httpAccessLogPolicyClients, pb.NewHTTPAccessLogPolicyServiceClient(conn))
httpCachePolicyClients = append(httpCachePolicyClients, pb.NewHTTPCachePolicyServiceClient(conn)) httpCachePolicyClients = append(httpCachePolicyClients, pb.NewHTTPCachePolicyServiceClient(conn))
httpFirewallPolicyClients = append(httpFirewallPolicyClients, pb.NewHTTPFirewallPolicyServiceClient(conn))
} }
return &RPCClient{ return &RPCClient{
@@ -108,6 +111,7 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
httpPageClients: httpPageClients, httpPageClients: httpPageClients,
httpAccessLogPolicyClients: httpAccessLogPolicyClients, httpAccessLogPolicyClients: httpAccessLogPolicyClients,
httpCachePolicyClients: httpCachePolicyClients, httpCachePolicyClients: httpCachePolicyClients,
httpFirewallPolicyClients: httpFirewallPolicyClients,
}, nil }, nil
} }
@@ -223,6 +227,13 @@ func (this *RPCClient) HTTPCachePolicyRPC() pb.HTTPCachePolicyServiceClient {
return nil return nil
} }
func (this *RPCClient) HTTPFirewallPolicyRPC() pb.HTTPFirewallPolicyServiceClient {
if len(this.httpFirewallPolicyClients) > 0 {
return this.httpFirewallPolicyClients[rands.Int(0, len(this.httpFirewallPolicyClients)-1)]
}
return nil
}
func (this *RPCClient) Context(adminId int64) context.Context { func (this *RPCClient) Context(adminId int64) context.Context {
ctx := context.Background() ctx := context.Background()
m := maps.Map{ m := maps.Map{

View File

@@ -1,7 +1,12 @@
package waf package waf
import ( import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
) )
type IndexAction struct { type IndexAction struct {
@@ -16,7 +21,55 @@ func (this *IndexAction) Init() {
func (this *IndexAction) RunGet(params struct { func (this *IndexAction) RunGet(params struct {
ServerId int64 ServerId int64
}) { }) {
// TODO webConfigResp, err := this.RPC().ServerRPC().FindAndInitServerWebConfig(this.AdminContext(), &pb.FindAndInitServerWebRequest{ServerId: params.ServerId})
if err != nil {
this.ErrorPage(err)
return
}
webConfig := &serverconfigs.HTTPWebConfig{}
err = json.Unmarshal(webConfigResp.Config, webConfig)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["webId"] = webConfig.Id
this.Data["firewallConfig"] = webConfig.FirewallRef
// 当前已有策略
policiesResp, err := this.RPC().HTTPFirewallPolicyRPC().FindAllEnabledHTTPFirewallPolicies(this.AdminContext(), &pb.FindAllEnabledHTTPFirewallPoliciesRequest{})
if err != nil {
this.ErrorPage(err)
return
}
policyMaps := []maps.Map{}
for _, p := range policiesResp.FirewallPolicies {
policyMaps = append(policyMaps, maps.Map{
"id": p.Id,
"name": p.Name,
"isOn": p.IsOn,
})
}
this.Data["firewallPolicies"] = policyMaps
this.Show() this.Show()
} }
func (this *IndexAction) RunPost(params struct {
WebId int64
FirewallJSON []byte
Must *actions.Must
}) {
// TODO 检查配置
_, err := this.RPC().HTTPWebRPC().UpdateHTTPFirewall(this.AdminContext(), &pb.UpdateHTTPFirewallRequest{
WebId: params.WebId,
FirewallJSON: params.FirewallJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -12,7 +12,7 @@ func init() {
Helper(helpers.NewUserMustAuth()). Helper(helpers.NewUserMustAuth()).
Helper(serverutils.NewServerHelper()). Helper(serverutils.NewServerHelper()).
Prefix("/servers/server/settings/waf"). Prefix("/servers/server/settings/waf").
Get("", new(IndexAction)). GetPost("", new(IndexAction)).
EndAll() EndAll()
}) })
} }

View File

@@ -12,6 +12,11 @@ Vue.component("http-cache-config-box", {
cacheConfig: cacheConfig cacheConfig: cacheConfig
} }
}, },
methods: {
changePolicyId: function () {
this.cacheConfig.cachePolicyId = parseInt(this.cacheConfig.cachePolicyId)
}
},
template: `<div> template: `<div>
<input type="hidden" name="cacheJSON" :value="JSON.stringify(cacheConfig)"/> <input type="hidden" name="cacheJSON" :value="JSON.stringify(cacheConfig)"/>
<table class="ui table definition selectable"> <table class="ui table definition selectable">
@@ -32,7 +37,7 @@ Vue.component("http-cache-config-box", {
<td> <td>
<span class="disabled" v-if="vCachePolicies.length == 0">暂时没有可选的缓存策略</span> <span class="disabled" v-if="vCachePolicies.length == 0">暂时没有可选的缓存策略</span>
<div v-if="vCachePolicies.length > 0"> <div v-if="vCachePolicies.length > 0">
<select class="ui dropdown auto-width" v-model="cacheConfig.cachePolicyId"> <select class="ui dropdown auto-width" v-model="cacheConfig.cachePolicyId" @change="changePolicyId">
<option value="0">[不使用缓存策略]</option> <option value="0">[不使用缓存策略]</option>
<option v-for="policy in vCachePolicies" :value="policy.id">{{policy.name}}</option> <option v-for="policy in vCachePolicies" :value="policy.id">{{policy.name}}</option>
</select> </select>

View File

@@ -0,0 +1,52 @@
Vue.component("http-firewall-config-box", {
props: ["v-firewall-config", "v-firewall-policies"],
data: function () {
let firewall = this.vFirewallConfig
if (firewall == null) {
firewall = {
isOn: false,
firewallPolicyId: 0
}
}
return {
firewall: firewall
}
},
methods: {
changePolicyId: function () {
this.firewall.firewallPolicyId = parseInt(this.firewall.firewallPolicyId)
}
},
template: `<div>
<input type="hidden" name="firewallJSON" :value="JSON.stringify(firewall)"/>
<table class="ui table selectable definition">
<tbody>
<tr>
<td class="title">是否启用Web防火墙</td>
<td>
<div class="ui checkbox">
<input type="checkbox" v-model="firewall.isOn"/>
<label></label>
</div>
</td>
</tr>
</tbody>
<tbody v-show="firewall.isOn">
<tr>
<td>选择Web防火墙策略</td>
<td>
<span class="disabled" v-if="vFirewallPolicies.length == 0">暂时还没有防火墙策略</span>
<div v-if="vFirewallPolicies.length > 0">
<select class="ui dropdown auto-width" v-model="firewall.firewallPolicyId" @change="changePolicyId">
<option value="0">[请选择]</option>
<option v-for="policy in vFirewallPolicies" :value="policy.id">{{policy.name}}</option>
</select>
</div>
</td>
</tr>
</tbody>
</table>
<div class="margin"></div>
</div>`
})

View File

@@ -3,5 +3,9 @@
{$template "/left_menu"} {$template "/left_menu"}
<div class="right-box"> <div class="right-box">
<p class="ui message">此功能暂未开放,敬请期待。</p> <form class="ui form" data-tea-action="$" data-tea-success="success">
<input type="hidden" name="webId" :value="webId"/>
<http-firewall-config-box :v-firewall-config="firewallConfig" :v-firewall-policies="firewallPolicies"></http-firewall-config-box>
<submit-btn></submit-btn>
</form>
</div> </div>

View File

@@ -0,0 +1,3 @@
Tea.context(function () {
this.success = NotifyReloadSuccess("保存成功")
})