11from pydantic import BaseModel , Field
22from utils .request import post_slce_api
3- from tools import register_tool , Tool
3+ from tools import Tool , ABCTool , tools
44from urllib .parse import urlparse
5-
6- class CreateHttpApplication (BaseModel ):
5+ @ tools . register
6+ class CreateHttpApplication (BaseModel , ABCTool ):
77 domain : str = Field (default = "" ,description = "application domain, if empty, match all domain" )
88 port : int = Field (description = "application listen port, must between 1 and 65535" )
99 upstream : str = Field (description = "application proxy address, must be a valid url" )
1010
11-
12- async def create_http_application (arguments :dict ) -> str :
13- """
14- Create a new HTTP application.
15-
16- Args:
17- domain: application domain
18- port: application listen port
19- upstream: application proxy address
20- """
21-
22- port = arguments ["port" ]
23- upstream = arguments ["upstream" ]
24- domain = arguments ["domain" ]
25-
26- if port is None or port < 1 or port > 65535 :
27- return "invalid port"
28-
29- parsed_upstream = urlparse (upstream )
30- if parsed_upstream .scheme != "https" and parsed_upstream .scheme != "http" :
31- return "invalid upstream scheme"
32- if parsed_upstream .hostname == "" :
33- return "invalid upstream host"
34-
35- return await post_slce_api ("/api/open/site" ,{
36- "server_names" : [domain ],
37- "ports" : [ str (port ) ],
38- "upstreams" : [ upstream ],
39- "type" : 0 ,
40- "static_default" : 1 ,
41- "health_check" : True ,
42- "load_balance" : {
43- "balance_type" : 1
44- }
45- })
46-
47- register_tool (
48- Tool (
49- name = "create_http_application" ,
50- description = "在雷池 WAF 上创建一个站点应用" ,
51- inputSchema = CreateHttpApplication .model_json_schema ()
52- ),
53- create_http_application
54- )
11+ @classmethod
12+ async def run (self , arguments :dict ) -> str :
13+ port = arguments ["port" ]
14+ upstream = arguments ["upstream" ]
15+ domain = arguments ["domain" ]
16+
17+ if port is None or port < 1 or port > 65535 :
18+ return "invalid port"
19+
20+ parsed_upstream = urlparse (upstream )
21+ if parsed_upstream .scheme != "https" and parsed_upstream .scheme != "http" :
22+ return "invalid upstream scheme"
23+ if parsed_upstream .hostname == "" :
24+ return "invalid upstream host"
25+
26+ return await post_slce_api ("/api/open/site" ,{
27+ "server_names" : [domain ],
28+ "ports" : [ str (port ) ],
29+ "upstreams" : [ upstream ],
30+ "type" : 0 ,
31+ "static_default" : 1 ,
32+ "health_check" : True ,
33+ "load_balance" : {
34+ "balance_type" : 1
35+ }
36+ })
37+
38+ @classmethod
39+ def tool (self ) -> Tool :
40+ return Tool (
41+ name = "create_http_application" ,
42+ description = "在雷池 WAF 上创建一个站点应用" ,
43+ inputSchema = self .model_json_schema ()
44+ )
0 commit comments