File size: 8,724 Bytes
3a5cf48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# coding: utf-8
# +-------------------------------------------------------------------
# | 宝塔Linux面板
# +-------------------------------------------------------------------
# | Copyright (c) 2015-2020 宝塔软件(http://www.bt.cn) All rights reserved.
# +-------------------------------------------------------------------
# | Author: baozi <baozi@bt.cn>
# | 消息通道HOOK模块
# +-------------------------------------------------------------------

import copy
import requests
from typing import Optional, Union
from urllib3.util import parse_url

from .util import write_push_log, get_test_msg
import json

# config = {
#     "name": "default",
#     "url": "https://www.bt.cn",
#     "query": {
#         "aaa": "111"
#     },
#     "header": {
#         "AAA": "BBBB",
#     },
#     "body_type": ["json", "form_data", "null"],
#     "custom_parameter": {
#         "rrr": "qqqq"
#     },
#     "method": ["GET", "POST", "PUT", "PATCH"],
#     "ssl_verify": [True, False]
# }
# #
# # 1.自动解析Query参数,拼接并展示给用户  # 可不做
# # 2.自定义Header头 # 必做
# # 3.Body中的内容是: type:str="首页磁盘告警", time:int=168955427, data:str="xxxxxx"  # ?
# # 4.自定义参数: key=value 添加在Body中  # 可不做
# # 5.请求类型自定义 # 必做
# # 以上内容需要让用户可测试--!


class WebHookMsg(object):
    DEFAULT_HEADERS = {
        "User-Agent": "BT-Panel",
    }

    def __init__(self, hook_data: dict):
        self.id = hook_data["id"]
        self.config = copy.deepcopy(hook_data["data"])

    def send_msg(self, msg: str, title:str, push_type:str) -> Optional[str]:
        the_url = parse_url(self.config['url'])

        ssl_verify = self.config.get("ssl_verify", None)
        if ssl_verify is None:
            ssl_verify = the_url.scheme == "https"
        else:
            ssl_verify = bool(int(ssl_verify))  # 转换为布尔值

        custom_parameter = self.config.get("custom_parameter", {})
        if not isinstance(custom_parameter, dict):
            custom_parameter = {}  # 如果 custom_parameter 不是字典,则设置为空字典

        real_data = self._build_real_data(msg, title, push_type, custom_parameter)

        headers = self.DEFAULT_HEADERS.copy()
        for k, v in self.config.get("headers", {}).items():
            if not isinstance(v, str):
                v = str(v)
            headers[k] = v

        data = None
        json_data = None
        if self.config["body_type"] == "json":
            json_data = real_data
        elif self.config["body_type"] == "form_data":
            data = real_data

        status = False
        error = None
        timeout = 10
        if data:
            for k, v in data.items():
                if isinstance(v, str):
                    continue
                else:
                    data[k]=json.dumps(v)   

        for i in range(3):
            try:
                if json_data is not None:
                    res = requests.request(
                        method=self.config["method"],
                        url=str(the_url),
                        json=json_data,
                        headers=headers,
                        timeout=timeout,
                        verify=ssl_verify,
                    )
                else:
                    res = requests.request(
                        method=self.config["method"],
                        url=str(the_url),
                        data=data,
                        headers=headers,
                        timeout=timeout,
                        verify=ssl_verify,
                    )

                if res.status_code == 200:
                    status = True
                    error = None
                    break
                else:
                    status = False
                    error = res.text or "请求错误,返回状态码为:{}".format(res.status_code)
            except (requests.exceptions.Timeout, requests.exceptions.ConnectionError):
                timeout += 5
                error = "请求超时,请检查网络连接"
                continue
            except requests.exceptions.RequestException as e:
                error = str(e)
                break

        write_push_log("Web Hook", status, title)
        return error if error else status

    @staticmethod
    def _build_real_data(msg: str, title:str, push_type:str, custom_parameter: dict):
        default_data = {
            "title": title,
            "msg": msg,
            "type": push_type,
        }
        _build_by_replace = False

        def _replace(tmp_data: Union[str, list, dict,]):
            nonlocal _build_by_replace
            if isinstance(tmp_data, str):
                if "$1" in tmp_data:
                    _build_by_replace = True
                    tmp_data = tmp_data.replace("$1", json.dumps(default_data, ensure_ascii=False))
                if "$msg" in tmp_data:
                    _build_by_replace = True
                    tmp_data = tmp_data.replace("$msg", msg)
                if "$title" in tmp_data:
                    _build_by_replace = True
                    tmp_data = tmp_data.replace("$title", title)
                if "$type" in tmp_data:
                    _build_by_replace = True
                    tmp_data = tmp_data.replace("$type", push_type)
                return tmp_data
            elif isinstance(tmp_data, list):
                new_data = []
                for i in tmp_data:
                    new_data.append(_replace(i))
                return new_data
            elif isinstance(tmp_data, dict):
                new_data = {}
                for k, v in tmp_data.items():
                    new_data[k] = _replace(v)
                return new_data
            else:
                return tmp_data

        real_data = _replace(custom_parameter)
        if _build_by_replace:
            return real_data
        else:
            custom_parameter["title"] = title
            custom_parameter["msg"] = msg
            custom_parameter["type"] = push_type
            return custom_parameter

    @classmethod
    def check_args(cls, args) -> Union[str, dict]:
        """配置hook"""
        try:
            title = args['title']
            url = args["url"]
            query = args.get("query", {})
            headers = args.get("headers", {})
            body_type = args.get("body_type", "json")
            custom_parameter = args.get("custom_parameter", {})
            method = args.get("method", "POST")
            ssl_verify = args.get("ssl_verify", None)  # null Ture
        except (ValueError, KeyError):
            return "参数错误"

        the_url = parse_url(url)
        if the_url.scheme is None or the_url.host is None:
            return"url解析错误,这可能不是一个合法的url"

        for i in (query, headers, custom_parameter):
            if not isinstance(i, dict):
                return "参数格式错误"

        if body_type not in ('json', 'form_data', 'null'):
            return "body_type必须为json,form_data或者null"

        if method not in ('GET', 'POST', 'PUT', 'PATCH'):
            return "发送方式选择错误"

        if ssl_verify not in (True, False, None):
            return "是否验证ssl选项错误"

        title = title.strip()
        if title == "":
            return"名称不能为空"

        data = {
            "title": title,
            "url": url,
            "query": query,
            "headers": headers,
            "body_type": body_type,
            "custom_parameter": custom_parameter,
            "method": method,
            "ssl_verify": ssl_verify,
            "status": True
        }

        test_obj = cls({"data": data, "id": None})
        test_msg = {
            "msg_list": ['>配置状态:成功\n\n']
        }

        test_task = get_test_msg("消息通道配置提醒")

        res = test_obj.send_msg(
            test_task.to_web_hook_msg(test_msg, test_task.the_push_public_data()),
            "消息通道配置提醒",
            "消息通道配置提醒"
        )
        if res is True:
            return data

        return res

    def test_send_msg(self) -> Optional[str]:
        test_msg = {
            "msg_list": ['>配置状态:<font color=#20a53a>成功</font>\n\n']
        }
        test_task = get_test_msg("消息通道配置提醒")
        res = self.send_msg(
            test_task.to_web_hook_msg(test_msg, test_task.the_push_public_data()),
            "消息通道配置提醒",
            "消息通道配置提醒"
        )
        if res is None:
            return None
        return res