Skip to content

Commit

Permalink
feat: support rule action in the frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
love98ooo committed Sep 13, 2024
1 parent e6e965b commit 082fbe5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 38 deletions.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func main() {
proxy.InitHttpClient()
object.InitSiteMap()
object.InitRuleMap()
object.InitActionMap()
run.InitAppMap()
run.InitSelfStart()
object.StartMonitorSitesLoop()
Expand Down
2 changes: 1 addition & 1 deletion rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ func CheckRules(ruleIds []string, r *http.Request) (actionObj *object.Action, re
return nil, "", err
}
if action == "" {
action = rule.Action
actionObj, err = object.GetActionByActionId(rule.Action)
if err != nil {
return nil, "", err
}
action = actionObj.Type
} else {
switch action {
case "Block":
Expand Down
65 changes: 28 additions & 37 deletions web/src/RuleEditPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
// limitations under the License.

import React from "react";
import {Button, Card, Col, Input, InputNumber, Row, Select} from "antd";
import {Button, Card, Col, Input, Row, Select} from "antd";
import * as Setting from "./Setting";
import * as RuleBackend from "./backend/RuleBackend";
import * as ActionBackend from "./backend/ActionBackend";
import i18next from "i18next";
import WafRuleTable from "./components/WafRuleTable";
import IpRuleTable from "./components/IpRuleTable";
Expand All @@ -33,11 +34,13 @@ class RuleEditPage extends React.Component {
owner: props.match.params.owner,
ruleName: props.match.params.ruleName,
rule: null,
actions: [],
};
}

UNSAFE_componentWillMount() {
this.getRule();
this.getActions();
}

getRule() {
Expand All @@ -48,6 +51,14 @@ class RuleEditPage extends React.Component {
});
}

getActions() {
ActionBackend.getActions(this.state.owner).then((res) => {
this.setState({
actions: res.data,
});
});
}

updateRuleField(key, value) {
const rule = Setting.deepCopy(this.state.rule);
rule[key] = value;
Expand Down Expand Up @@ -172,52 +183,32 @@ class RuleEditPage extends React.Component {
{i18next.t("general:Action")}:
</Col>
<Col span={22}>
<Select virtual={false} value={this.state.rule.action} defaultValue={"Block"} style={{width: "100%"}} onChange={value => {
<Select virtual={false} value={this.state.rule.action} defaultValue={"Block"} style={{width: "100%"}} onChange={(value, index) => {
this.setState({
action: this.state.actions[index],
});
this.updateRuleField("action", value);
}}>
{
[
{value: "Allow", text: i18next.t("rule:Allow")},
// {value: "redirect", text: "Redirect"},
{value: "Block", text: i18next.t("rule:Block")},
// {value: "drop", text: "Drop"},
{value: "Captcha", text: i18next.t("rule:Captcha")},
].map((item, index) => <Option key={index} value={item.value}>{item.text}</Option>)
this.state.actions.map((action, index) => <Option key={index} value={action.owner + "/" + action.name} label={action.type}></Option>)
}
</Select>
</Col>
</Row>
)
}
{
(this.state.rule.action === "Block" && this.state.rule.type !== "WAF") && (
<Row style={{marginTop: "20px"}}>
<Col span={2} style={{marginTop: "5px"}}>
{i18next.t("rule:Status Code")}:
</Col>
<Col span={22}>
<InputNumber value={this.state.rule.statusCode} defaultValue={403} disabled={true}
onChange={e => {
this.updateRuleField("statusCode", e.target.value);
}} />
</Col>
</Row>
)
}
{
(this.state.rule.action === "Block" || this.state.rule.type === "WAF") && (
<Row style={{marginTop: "20px"}}>
<Col span={2} style={{marginTop: "5px"}}>
{i18next.t("rule:Reason")}:
</Col>
<Col span={22}>
<Input value={this.state.rule.reason}
onChange={e => {
this.updateRuleField("reason", e.target.value);
}} />
</Col>
</Row>
)
<Row style={{marginTop: "20px"}}>
<Col span={2} style={{marginTop: "5px"}}>
{i18next.t("rule:Reason")}:
</Col>
<Col span={22}>
<Input value={this.state.rule.reason}
onChange={e => {
this.updateRuleField("reason", e.target.value);
}} />
</Col>
</Row>
}
</Card>
);
Expand Down

0 comments on commit 082fbe5

Please sign in to comment.