Files
SafeLine/mcp_server/tools/create_path_custom_rule.py

53 lines
1.5 KiB
Python
Raw Normal View History

2025-04-03 16:21:13 +08:00
from pydantic import BaseModel, Field
from utils.request import post_slce_api
from tools import Tool, ABCTool, tools
@tools.register
class CreatePathCustomRule(BaseModel, ABCTool):
path: str = Field(description="request path to block or allow")
2025-04-03 17:44:57 +08:00
action: int = Field(min=0, max=1,description="1: block, 0: allow")
2025-04-03 16:21:13 +08:00
@classmethod
async def run(self, arguments:dict) -> str:
2025-04-03 17:44:57 +08:00
try:
req = CreatePathCustomRule.model_validate(arguments)
except Exception as e:
return str(e)
2025-04-03 16:21:13 +08:00
name = ""
2025-04-03 17:44:57 +08:00
match req.action:
2025-04-03 16:21:13 +08:00
case 0:
name += "allow "
case 1:
name += "block "
case _:
return "invalid action"
2025-04-03 17:44:57 +08:00
if not req.path or req.path == "":
2025-04-03 16:21:13 +08:00
return "path is required"
2025-04-03 17:44:57 +08:00
name += f"path: {req.path}"
2025-04-03 16:21:13 +08:00
return await post_slce_api("/api/open/policy",{
"name": name,
"is_enabled": True,
"pattern": [
[
{
"k": "uri_no_query",
"op": "eq",
2025-04-03 17:44:57 +08:00
"v": [req.path],
2025-04-03 16:21:13 +08:00
"sub_k": ""
},
]
],
2025-04-03 17:44:57 +08:00
"action": req.action
2025-04-03 16:21:13 +08:00
})
@classmethod
def tool(self) -> Tool:
return Tool(
2025-04-07 07:41:42 +00:00
name="waf_create_path_custom_rule",
description="以 URL Path 为条件,在雷池 WAF 上创建一个黑/白名单",
2025-04-03 16:21:13 +08:00
inputSchema=self.model_json_schema()
)