Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support IP and UA rules #41

Closed
wants to merge 21 commits into from

Conversation

love98ooo
Copy link
Contributor

Allow or block requests by IP or UA

web/src/RuleListPage.js Outdated Show resolved Hide resolved
web/src/RuleEditPage.js Outdated Show resolved Hide resolved
web/src/RuleEditPage.js Outdated Show resolved Hide resolved
web/src/RuleEditPage.js Outdated Show resolved Hide resolved
web/src/RuleEditPage.js Outdated Show resolved Hide resolved
object/util.go Outdated Show resolved Hide resolved
object/rule.go Outdated Show resolved Hide resolved
object/rule.go Show resolved Hide resolved
web/src/components/IPRuleTable.js Show resolved Hide resolved
web/src/components/UARuleTable.js Outdated Show resolved Hide resolved

func GetWAFRulesByIds(ids []string) string {
// Get rules by id (owner/name)
owners, names := util.GetOwnersAndNamesFromIds(ids)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't mess with SQL. It's fragile.

Get all rules and do filtering in Go code

return true, nil
}
case "User-Agent":
isMatch := checkUARule(rule.Expressions, r.UserAgent())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All isMatch needs to be isMatched

for _, rule := range rules {
switch rule.Type {
case "IP":
isMatch := checkIPRule(rule.Expressions, clientIp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkIpRule


func createWAF() coraza.WAF {
func createWAF(site *object.Site) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createWaf

}

func UpdateWAF() {
waf = createWAF()
func UpdateWAFs() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wafs

</Col>
</Row>
)
}
<Row style={{marginTop: "20px"}}>
{/* <Row style={{marginTop: "20px"}}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented code

@@ -150,24 +169,25 @@ class RuleEditPage extends React.Component {
</Col>
</Row>
)
}
} */}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't add commented code, just remove it

@@ -49,6 +49,7 @@ type Site struct {
OtherDomains []string `xorm:"varchar(500)" json:"otherDomains"`
NeedRedirect bool `json:"needRedirect"`
EnableWaf bool `json:"enableWaf"`
WafRuleIds []string `xorm:"varchar(500)" json:"wafRuleIds"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WafRuleIds -> Rules

@@ -116,6 +117,87 @@ func redirectToHost(w http.ResponseWriter, r *http.Request, host string) {
http.Redirect(w, r, targetUrl, http.StatusMovedPermanently)
}

func checkRules(wafRuleIds []string, r *http.Request, clientIp string) (bool, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clientIp is already in request I think?

return true, nil
}

func checkUARule(expressions []*object.Expression, userAgent string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct UaRule {

checkRule(expressions []*object.Expression, req http.Request) (string, string, error)

}

(string, string, error) is (action, reason, error)
action can be "Allow", "Block", etc.
reason is the formatted string

@love98ooo love98ooo closed this Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants